Welcome! Log In Create A New Profile

Advanced

mp3 keeps crashing app

Posted by Sinman 
mp3 keeps crashing app
January 12, 2009 02:23AM
First let me say thanks to everyone sharing code and helping others on this wiki and board. I've been able to "learn" C and put together a relatively simple game in three days because of your generosity.

I'm trying to play an mp3 on continuous loop as background music for my game. I'm using libfat to read the mp3 off of an SD card if available and playing it with MP3Player. For some reason, with about 1 second left before the loop should repeat itself the first time around, the app freezes requiring a hard shutdown of the Wii.

I was using a 192kbps VBR joint stereo mp3 (824k) at first but thinking that might be the issue switched to a 128kbps mono mp3 (380k) with the same results. My code compiles fine with no errors or warnings in Xcode and the game plays fine when the SD with the mp3 isn't present. I've had no problems running other homebrew off this same card. Are there any obvious problems with the code below?

This code appears in the "main" function outside of the "while" loop:

	// Audio
	if (fatInitDefault()) {
		
		MP3Player_Init();
		
		FILE *fp;
		if ((fp = fopen("/apps/horrorvacui/data/susan-low.mp3", "r"))) {
		
			MP3Player_PlayFile(fp, mp3_reader, NULL);
		}
	}

And the "mp3_reader" function:
s32 mp3_reader(void *fp, void *dat, s32 size) {
	
	return fread(dat, 1, size, (FILE *) fp);
}

Re: mp3 keeps crashing app
January 12, 2009 02:43AM
Also, there's a lot of samples of playing external files from an SD card but I can't find any examples (using MP3Player, asndlib or otherwise) that illustrate playing embedded audio (I just figured out .s files and .INCBIN). So, if anyone wants to share...
Re: mp3 keeps crashing app
January 12, 2009 03:06AM
MP3Player_PlayBuffer(mp31data,mp31length,NULL);

where
mp31data = sizeof(buffer with the mp3)
mp31length = the buffer with the mpĀ“3 you made in the .s file

they are included in the project with

extern u8 mp31data[];
extern u32 mp31length;

if youre unsure about the .s file
   .rodata
   .globl mp31data
   .globl mp31length
   .balign 32

   mp31length:	.long  mp31end - mp31data
   mp31data:
   .incbin "../source/penis.mp3"
   mp31end:



yes the newlines are important
Re: mp3 keeps crashing app
January 12, 2009 03:48AM
Thanks, that worked a treat.

Turns out the app wasn't crashing until I tried to return to the HBC. Because I wasn't calling MP3Player_Stop(); before exit(0);

So the mp3 was stopping abruptly and not looping. Using your example, adding the following to the "while" loop keeps the music going:

	// stopped? start again
	if (!MP3Player_IsPlaying()) {
		
		MP3Player_PlayBuffer(susan_mp3, susan_mp3_size, NULL);
	}

but doesn't explain why the sample is being truncated. Oh well, guess I can just fudge it until it sounds right. Thanks again.
Re: mp3 keeps crashing app
January 12, 2009 03:53AM
no problem, glad i could help :)
Re: mp3 keeps crashing app
January 12, 2009 07:54AM
Follow up question about the .s file. What are the paths relative to? The .s file? The makefile or maybe main.c? I've tried all three and the only way that works for me is absolute paths from the root of the drive. Is there something in the makefile I'm missing?
Re: mp3 keeps crashing app
January 16, 2009 06:06AM
The .s file I think.
Re: mp3 keeps crashing app
January 16, 2009 03:03PM
I figured out what the problem was. My environment (Xcode) is treating all paths relative to the root of the project as if the root of the project was located at the root of the drive. I also figured out that if I just added all my assets (images and audio) to a data directory and pointed my Makefile at it, bin2o (after identifying all the extensions I'm using) would replace the need for both running raw2c (which I was doing for each image) and creating .s files (which I was doing for each audio sample).
Sorry, only registered users may post in this forum.

Click here to login