Welcome! Log In Create A New Profile

Advanced

MP3Player_PlayFile() - Parameter Definitions

Posted by Arikado 
MP3Player_PlayFile() - Parameter Definitions
December 30, 2008 09:28PM
Would someone please explain the purpose of each parameter in the function:

s32 MP3Player_PlayFile(void *cb_data,s32 (*reader)(void *,void *,s32),void (*filterfunc)(struct mad_stream *,struct mad_frame *));

I am trying to play sound1.mp3 in /apps/Wiibreaker/data/sound1.mp3

I appreciate any and all help. Thanks to all in advance.



Edited 1 time(s). Last edit at 12/30/2008 09:29PM by Arikado.
Re: MP3Player_PlayFile() - Parameter Definitions
December 30, 2008 09:49PM
s32 MP3Player_PlayFile(void *cb_data,s32 (*reader)(void *,void *,s32),void (*filterfunc)(struct mad_stream *,struct mad_frame *));

that is 3 parameters, no?
the first one is the mp3 data
the second one, might be the size, length
and the last one i think can be NULL.

this is just me guessing, ive only used the MP3Player_PlayBuffer before, and with that the last function looked similar and worked with NULL.
the first two were as i guessed, but i have no decent clue to the PlayFile, realy...
Re: MP3Player_PlayFile() - Parameter Definitions
December 30, 2008 10:27PM
Yeah theres only three. Second one is definitely the size/length.

Would "/apps/Wiibreaker/data/sound1.mp3" fit into the first parameter, then?

Would it be possible for me to use fopen() (and a pointer) to retrieve the size of my .mp3 file?



Edited 1 time(s). Last edit at 12/30/2008 10:55PM by Arikado.
Re: MP3Player_PlayFile() - Parameter Definitions
January 01, 2009 12:20AM
This is an example that uses MP3Player_PlayFile. I found it on the forum @ entuwii.net. The website is not in english. If you want to complie this example you will need to make a few changes so that it will use asndlib included with libogc 1.7.0, but I hope it helps.

/* ASNDLIB Example2: Sound effects and play a MP3 file

NOTE: libmad is released under GPL v2. if you release one executable, remember you to change the license to the GPL.
	  Without libmad, you can use it as you want.

      If you prefer a MP3 lib under LGPL, looks my application 'Wiireader' to obtain a MP3/OGG player under LGP V2.1 terms
	  If you prefer only a Oggplayer without GPL or LGPL license, looks the old sndlib and change the includes "snd.h" by "asnd.h"
	
Copyright (c) 2008 Hermes 
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are 
permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this list of 
  conditions and the following disclaimer. 
- Redistributions in binary form must reproduce the above copyright notice, this list 
  of conditions and the following disclaimer in the documentation and/or other 
  materials provided with the distribution. 
- The names of the contributors may not be used to endorse or promote products derived 
  from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include "unistd.h"


#include "asnd.h"

// MOD Player
#include "mp3player.h"

// audio resources
#include "audio_raw.h"  // sound effects in RAW PCM 8 bits (signed)
#include "audio2_raw.h"
#include "organ8_smp.h"

GXRModeObj *rmode;		// Graphics Mode Object
u32 *xfb = NULL;		// Framebuffer  


int return_reset=2;

int exit_by_reset=0;

void reset_call() {exit_by_reset=return_reset;}
void power_call() {exit_by_reset=3;}


void fun_exit()
{

	ASND_End();

	fatUnmount(PI_INTERNAL_SD);

	VIDEO_WaitVSync();
	VIDEO_WaitVSync();

	if(exit_by_reset==2)
		SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
	if(exit_by_reset==3)
		SYS_ResetSystem(SYS_POWEROFF_STANDBY, 0, 0);
}

s32  my_reader(void *fp,void *dat, s32 size)
{
	return fread(dat, 1, size, fp);
}


int main(int argc, char **argv) 
{
FILE *fp;
int counter;
int voice1;

	return_reset=1;

	if(argc<1) return_reset=2;

	VIDEO_Init();                                        //Inicialización del Vídeo.
    
	rmode = VIDEO_GetPreferredMode(NULL);                //mediante esta función rmode recibe el valor de tu modo de vídeo.
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));  //inicialización del buffer.
	console_init(xfb,20,20,rmode->fbWidth,rmode->        //inicialización de la consola.
	xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);  
	VIDEO_Configure(rmode);                              //configuración del vídeo.
	VIDEO_SetNextFramebuffer(xfb);                       //Configura donde guardar el siguiente buffer .
	VIDEO_SetBlack(FALSE);                               //Hace visible el display  .                     
	VIDEO_Flush();
	VIDEO_WaitVSync();                                   
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

	SYS_SetResetCallback(reset_call);
	SYS_SetPowerCallback(power_call); 


	ASND_Init(); // Initialize the Accelerated Sound Lib (ASND)

	fatInit(8, false);
    fatSetDefaultInterface(PI_INTERNAL_SD);

	atexit(fun_exit);
	
	printf("\33[2J\n\n\n \33[42m Example2 of the ASNDLIB \33[40m \n\n");
	printf("Press RESET to exit\n\n");

	ASND_Pause(0); // Global pause=0 to start (note initially it is paused!!)

// loading a mod file from the SD
	
	fp=fopen("fat:/test.mp3","r");
	if(fp==NULL)
		{
		printf("can't open fat:/test.mp3\n\n");
		
		voice1=SND_GetFirstUnusedVoice();
		
		// sound for fail open
		ASND_SetVoice(voice1, VOICE_MONO_8BIT, 8000, 20, audio2_raw, size_audio2_raw, 255, 255, NULL);
		
		while(ASND_StatusVoice(voice1)==SND_WORKING)
			{
			if(exit_by_reset) break;
			VIDEO_WaitVSync();
			}
		
		}
	

	printf("Test an Infinite Voice\n");

	voice1=SND_GetFirstUnusedVoice();
	ASND_SetInfiniteVoice(voice1, VOICE_MONO_8BIT, 8000, 0, organ8_smp, size_organ8_smp, 255, 255); 

	// Infinite voices only can stop using ASND_PauseVoice() or ASND_StopVoice()
	// Normal voices stops with NULL callback, other case it wait to a ASND_AddVoice() to continue
	// remember you to use aligned voices and padded to 32 bytes to avoid strange cache problems!!!

	counter=0;

	while(1)
		{
		// pitch control 'alarm'
		if(counter>=120 && counter<300)
			{
			if(counter==120) printf("    Pitch control test...\n");
			if((counter % 60)<60)
				{
				ASND_ChangePitchVoice(voice1, 16000+(counter % 30)*800);
				}
			}
		// pitch control and left to right

		if(counter>=300 && counter<480)
			{
			int left,right;
			
			if(counter==300) printf("    Volume control test...\n");
			
			left=390-counter;if(left<0) left=0;
			right=counter-390;if(right<0) right=0;

			ASND_ChangePitchVoice(voice1, 8000+(480-counter)*400);
			ASND_ChangeVolumeVoice(voice1, 255*left/90,  255*right/90);
			}
		// pitch control and right to left

		if(counter>=480 && counter<660)
			{
			int left,right;
			
			right=570-counter;if(right<0) right=0;
			left=counter-570;if(left<0) left=0;

			ASND_ChangePitchVoice(voice1, 8000+(counter-480)*200);
			ASND_ChangeVolumeVoice(voice1, 255*left/90,  255*right/90);
			}
		
		// infinite voice paused
		if(counter==780)
			{
            printf("    Pause the Voice...\n");
			ASND_PauseVoice(voice1,1);
			}
		
		// programa infinite multiples voices

		if(counter>=870 && counter<990)
			{
			if(counter==870)printf("    Arggghhhh!\n");
			ASND_ChangeVolumeVoice(voice1, 255,  255);
			ASND_PauseVoice(voice1,0);
			if((counter % 10)==0) ASND_SetInfiniteVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000+100*(counter-870), 0, organ8_smp, size_organ8_smp, 255, 255);
			}

		if(counter>=990)
			{
			int n;
			// stops the crazy sounds 
			if(counter==990) 
				{
				for(n=0;n<16;n++) ASND_StopVoice(n);
				}
			
			// Multiple Delayed voices example
			if(counter==1020) 
				{
				printf("\nMultiple Delayed Voices (16 voices)\n");

			    ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 0, audio2_raw, size_audio2_raw, 255, 255, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 1000, audio2_raw, size_audio2_raw, 255, 0, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 1200, audio2_raw, size_audio2_raw, 0, 255, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 1800, audio2_raw, size_audio2_raw, 255, 0, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 2800, audio2_raw, size_audio2_raw, 25, 255, NULL);
				
				// BANG BANG !
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 3800, audio_raw, size_audio_raw, 255, 255, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 3820, audio_raw, size_audio_raw, 255, 255, NULL); // reverb

				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4300, audio_raw, size_audio_raw, 255, 255, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4320, audio_raw, size_audio_raw, 255, 255, NULL); // reverb


				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4420, audio_raw, size_audio_raw, 255, 64, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4520, audio_raw, size_audio_raw, 255, 64, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4620, audio_raw, size_audio_raw, 255, 64, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4720, audio_raw, size_audio_raw, 255, 64, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 4820, audio_raw, size_audio_raw, 255, 64, NULL);

				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 5300, audio_raw, size_audio_raw, 64, 255, NULL);
				ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 5600, audio_raw, size_audio_raw, 64, 255, NULL); 

				}

			// wait to the multiple delayed voices to exit

			if(counter>1020) 
				{
				int n;
				for(n=0;n<16;n++) if(ASND_StatusVoice(n)==SND_WORKING) break;
				if(n==16) break;
				}

			}

		if(exit_by_reset) break;
		VIDEO_WaitVSync();
		counter++;
		}

    printf("\nExit\n");
	if(fp)
		{
		
		MP3Player_Init();

		MP3Player_Volume(127); // Medium volume (you can change also using SND_ChangeVolumeVoice(0, vol_l,vol_r) )
		MP3Player_PlayFile(fp, my_reader, NULL);


		ASND_Pause(0); // Global pause=0 to start
		
		counter=0;

		printf("\33[2J\n\n\n \33[42m Example2 of the ASNDLIB\33[40m \n\n");
		printf("Press RESET to exit\n\n");
		printf("Playing the MP3 file (voice 0) and some effects\n\n");

		while(1)
			{
			unsigned time;

			
			time=ASND_GetTimerVoice(0)/1000;
			printf("\33[9;0HVoice 0 time: %2.2u:%2.2u   ", time/60, time % 60);
			
			if(MP3Player_IsPlaying())
				{
				// bang bang 
				if((counter % 600)==150)
					{
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 0, audio_raw, size_audio_raw, 255, 64, NULL);
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 100, audio_raw, size_audio_raw, 255, 64, NULL);
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 200, audio_raw, size_audio_raw, 255, 64, NULL);
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 300, audio_raw, size_audio_raw, 255, 64, NULL);
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 400, audio_raw, size_audio_raw, 255, 64, NULL);
					}
				
				// ^^
				if((counter % 600)==450)
					{
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 0, audio2_raw, size_audio2_raw, 255, 255, NULL);
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 1000, audio2_raw, size_audio2_raw, 255, 0, NULL);
					ASND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 1200, audio2_raw, size_audio2_raw, 0, 255, NULL);
					}
				}
			

			counter++;
			if(exit_by_reset) break;
			VIDEO_WaitVSync();
			}

			MP3Player_Stop();
			fclose(fp);
		}
	else
		{
		while(1)
			{
			if(exit_by_reset) break;
			VIDEO_WaitVSync();
			}
		}

exit(0);

}


Re: MP3Player_PlayFile() - Parameter Definitions
January 02, 2009 12:58AM
Linus, could you please edit your post replacing "<" with "(" so we can see the includes?

MP3Player_Volume();

I cant find that function anywhere in libogc. And neither can my compiler.



Edited 2 time(s). Last edit at 01/02/2009 01:09AM by Arikado.
Re: MP3Player_PlayFile() - Parameter Definitions
January 03, 2009 12:00AM
I finally got mp3 sound working!

Here is how I did everything and the results of it.

The first thing you need to do to add mp3 sound is add -lmad to LIBS: in your makefile. Also add -lfat if you plan on loading mp3 sound from an SD card.

In your code simply add #include (mp3player.h>//"( " replaced "<" due to forum software. Also add #include (fat.h> if you'll be using the front SD loader.

In the beginning of your code when you perform initialization routines. Add the line MP3Player_Init(); to initialze mp3 player code. Also add fatInitDefault(); if you'll be using the front SD loader.

This is all basic stuff. But here's where things get tricky.

To call s32 MP3Player_PlayFile(void *cb_data,s32 (*reader)(void *,void *,s32),void (*filterfunc)(struct mad_stream *,struct mad_frame *)); you first need a pointer of the type FILE. So somwhere in scope of where you'll be calling the function declare FILE *filepointer;

The second parameter takes the statistics of the file you want to play. To create the statistics add this function into your code:

s32  my_reader(void *fp,void *dat, s32 size){
	
	return fread(dat, 1, size, fp);
}

I'm still not sure what the third parameter does, but just pass it as NULL.

So you'll be using the playfile function as such:

MP3Player_PlayFile(filepointer, my_reader, NULL);

NOTE: Do not forget to declare where your mp3 file is located. Example:

filepointer=fopen("fat:/apps/Wiibreaker/data/sound1.mp3","r");

Now this is all fine and dandy and the way code should work. However, if you compile it, you'll get errors. This is because the last parameter of fread() conflicts with the first parameter of the reader you pass into MP3Player_Playfile(). And unless you rewrite libogc, you're not going to change that. My solution to the problem was a sloppy and frowned upon method of coding.

I globally declared another FILE pointer. In my code I assigned which mp3 file my second FILE pointer would open. And then I changed the reader function to this:

s32  my_reader(void *fp,void *dat, s32 size){
	
	return fread(dat, 1, size, filepointer2);
}

So basically, my code just ignores the first parameter of my_reader and instead passes a global FILE pointer into fread(). Its sloppy, but it worked.

So that I had sound working in my program, there was one problem left. The sound wouldnt stop playing. It just kept looping itself. To stop a sound from playing call MP3Player_Stop(); In the end, I put a counter variable in my main loop to determine when to call MP3Player_Stop(). Another problem (one that I have yet to resolve) is that when MP3Player_Stop() is called is freezes my program for half a second. Still another problem (I have yet to resolve) is that I cannot play more than one sound at a time.

Conclusion: If you can, use sndlib instead.

EDIT: I'll be releasing WiiBreaker 0.3 very, very soon. The source code uses the method I described above. So for a complete example email/PM me/Find me on the irc and ask me for the source after the app is released.

EDIT2: While this post only demostrated loading sound from an SD card, it is also possible to load sound from a buffer in a presumably similiar fashion.



Edited 7 time(s). Last edit at 01/03/2009 12:11AM by Arikado.
Re: MP3Player_PlayFile() - Parameter Definitions
January 03, 2009 12:07AM
Just typecast: fread(dat, 1, size, (FILE *) fp)
Re: MP3Player_PlayFile() - Parameter Definitions
January 03, 2009 12:11AM
Quote
comex
Just typecast: fread(dat, 1, size, (FILE *) fp)
*facepalm* I'm unable to see if this works at the moment but I'll let everyone know if it does (It most definitely will).
Re: MP3Player_PlayFile() - Parameter Definitions
January 06, 2009 05:33PM
Hi, I'm working with this but I cannot make it sound OK, I get a buzz sound but something I get 1sec of the mp3 I'm playing.

This is my code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


s32  my_reader(void *fp,void *dat, s32 size){
	
	return fread(dat, 1, size, (FILE *) fp);

}

int main() {
  	fatInitDefault(); // i/o
	
	MP3Player_Init();
  //some stuff
  if (pressed & WPAD_BUTTON_A) {
			FILE *fp_actual=fopen("sd:/APPS/LASTFM/DATA/PROMISES.MP3","r");
			MP3Player_PlayFile(fp_actual, my_reader, NULL);

       }
}

any idea of what could be the problem? maybe I need to initialize something more?

Regards!
Sorry, only registered users may post in this forum.

Click here to login