Homebrew Playing MP3s

  • Thread starter Thread starter Deleted User
  • Start date Start date
  • Views Views 2,253
  • Replies Replies 8
D

Deleted User

Guest
Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.
 
Last edited by ,
Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.
 
Last edited by ,
Can't spot anything obviously wrong with your code at a quick glance...
I suspect you've got some sort of pointer corruption somewhere overwriting the memory that the MP3 has been loaded into causing your issues...

Anyhow here's a copy of my working C++ code (Music class) from TetWiis that I've been developing.


This isn't by any means the prettiest code I've done
laugh.gif
but it does work, other than there appears to be a slight timing issue at the end of MP3s where it loops too early.
I've just got around this by adding some silent seconds at the end of the file
wacko.gif


Music.h
CODE/**
*
* Tetwiis
* (C)2009 http://www.pembo.co.uk
*
**/

//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include


#ifndef MUSIC_HPP
#define MUSIC_HPP
class Music
{
ÂÂÂÂpublic:
ÂÂÂÂÂÂÂÂMusic();
ÂÂÂÂÂÂÂÂ~Music();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂFILE *titleBGM;
ÂÂÂÂÂÂÂÂlong titlelSize;
ÂÂÂÂÂÂÂÂchar * titleBuffer;
ÂÂÂÂÂÂÂÂsize_t titleResult;
ÂÂÂÂÂÂÂÂbool titleMP3isready;
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂFILE *ingame1BGM;
ÂÂÂÂÂÂÂÂlong ingame1lSize;
ÂÂÂÂÂÂÂÂchar * ingame1Buffer;
ÂÂÂÂÂÂÂÂsize_t ingame1Result;
ÂÂÂÂÂÂÂÂbool ingame1MP3isready;ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂFILE *gameOverBGM;
ÂÂÂÂÂÂÂÂlong gameOverlSize;
ÂÂÂÂÂÂÂÂchar * gameOverBuffer;
ÂÂÂÂÂÂÂÂsize_t gameOverResult;
ÂÂÂÂÂÂÂÂbool gameOverMP3isready;ÂÂÂÂ

ÂÂÂÂÂÂÂÂ

ÂÂÂÂÂÂÂÂvoid loadMusicIntoBuffer();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid initTitleMusic();
ÂÂÂÂÂÂÂÂvoid playTitleMusic();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid initIngame1Music();
ÂÂÂÂÂÂÂÂvoid playIngame1Music();

ÂÂÂÂÂÂÂÂvoid initGameOverMusic();
ÂÂÂÂÂÂÂÂvoid playGameOverMusic();ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid stop();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid mute();
ÂÂÂÂÂÂÂÂvoid unmute();
ÂÂÂÂÂÂÂÂ
ÂÂÂÂprivate:
ÂÂÂÂ
ÂÂÂÂÂÂÂÂvoid initialiseFat();
ÂÂÂÂÂÂÂÂbool can_open_root_fs();

};
#endif

Music.cpp
/**
CODE *
* Tetwiis
* (C)2009 http://www.pembo.co.uk
*
**/


//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
# include "Music.h"
# include

//------------------------------------------------------------------------------
// Externals
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Globals
//------------------------------------------------------------------------------

//_______________________________________________________________________________
/**
* Constructor
*/
Music::Music()
{
ÂÂÂÂinitialiseFat();
ÂÂÂÂASND_Init();
ÂÂÂÂMP3Player_Init();
ÂÂÂÂ
ÂÂÂÂtitleMP3isready = false;
ÂÂÂÂingame1MP3isready = false;
ÂÂÂÂloadMusicIntoBuffer();
ÂÂÂÂ
}


//_______________________________________________________________________________
/**
* destructor
*/
Music::~Music()
{
}

void Music::loadMusicIntoBuffer()
{
ÂÂÂÂMP3Player_Stop();
ÂÂÂÂ//if(titleBuffer)free(titleBuffer);
ÂÂÂÂtitleBGM = fopen("sd:/apps/tetwiis/music/title.mp3", "rb");
ÂÂÂÂif(titleBGM)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//Obtain file size
ÂÂÂÂÂÂÂÂfseek (titleBGM , 0 , SEEK_END);
ÂÂÂÂÂÂÂÂtitlelSize = ftell(titleBGM);
ÂÂÂÂÂÂÂÂrewind (titleBGM);
ÂÂÂÂÂÂÂÂ//Allocate memory to contain the whole file
ÂÂÂÂÂÂÂÂtitleBuffer = (char*) malloc (sizeof(char)*titlelSize);
ÂÂÂÂÂÂÂÂ//Copy the file into the buffer
ÂÂÂÂÂÂÂÂtitleResult = fread (titleBuffer,1,titlelSize,titleBGM);
ÂÂÂÂÂÂÂÂfclose(titleBGM);
ÂÂÂÂ}
ÂÂÂÂtitleMP3isready = true;
ÂÂÂÂ
ÂÂÂÂingame1BGM = fopen("sd:/apps/tetwiis/music/ingame1.mp3", "rb");
ÂÂÂÂif(ingame1BGM)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//Obtain file size
ÂÂÂÂÂÂÂÂfseek (ingame1BGM , 0 , SEEK_END);
ÂÂÂÂÂÂÂÂingame1lSize = ftell(ingame1BGM);
ÂÂÂÂÂÂÂÂrewind (ingame1BGM);
ÂÂÂÂÂÂÂÂ//Allocate memory to contain the whole file
ÂÂÂÂÂÂÂÂingame1Buffer = (char*) malloc (sizeof(char)*ingame1lSize);
ÂÂÂÂÂÂÂÂ//Copy the file into the buffer
ÂÂÂÂÂÂÂÂingame1Result = fread (ingame1Buffer,1,ingame1lSize,ingame1BGM);
ÂÂÂÂÂÂÂÂfclose(ingame1BGM);
ÂÂÂÂ}
ÂÂÂÂingame1MP3isready = true;
ÂÂÂÂ
ÂÂÂÂgameOverBGM = fopen("sd:/apps/tetwiis/music/gameover.mp3", "rb");
ÂÂÂÂif(gameOverBGM)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//Obtain file size
ÂÂÂÂÂÂÂÂfseek (gameOverBGM , 0 , SEEK_END);
ÂÂÂÂÂÂÂÂgameOverlSize = ftell(gameOverBGM);
ÂÂÂÂÂÂÂÂrewind (gameOverBGM);
ÂÂÂÂÂÂÂÂ//Allocate memory to contain the whole file
ÂÂÂÂÂÂÂÂgameOverBuffer = (char*) malloc (sizeof(char)*gameOverlSize);
ÂÂÂÂÂÂÂÂ//Copy the file into the buffer
ÂÂÂÂÂÂÂÂgameOverResult = fread (gameOverBuffer,1,gameOverlSize,gameOverBGM);
ÂÂÂÂÂÂÂÂfclose(gameOverBGM);
ÂÂÂÂ}
ÂÂÂÂgameOverMP3isready = true;ÂÂÂÂ

}

void Music::initTitleMusic()
{
ÂÂÂÂMP3Player_PlayBuffer(titleBuffer,titlelSize,NULL);
}

void Music::initIngame1Music()
{
ÂÂÂÂMP3Player_PlayBuffer(ingame1Buffer,ingame1lSize,NULL);
}

void Music::initGameOverMusic()
{
ÂÂÂÂMP3Player_PlayBuffer(gameOverBuffer,gameOverlSize,NULL);
}

void Music::playTitleMusic()
{
ÂÂÂÂif( (!MP3Player_IsPlaying() && titleMP3isready))
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂMP3Player_PlayBuffer(titleBuffer, titlelSize, NULL);
ÂÂÂÂ}

}

void Music::playIngame1Music()
{
ÂÂÂÂif( (!MP3Player_IsPlaying() && ingame1MP3isready))
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂMP3Player_PlayBuffer(ingame1Buffer, ingame1lSize, NULL);
ÂÂÂÂ}
}

void Music::playGameOverMusic()
{
ÂÂÂÂif(MP3Player_IsPlaying() && (gameOverMP3isready))
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂMP3Player_PlayBuffer(gameOverBuffer, gameOverlSize, NULL);
ÂÂÂÂ}

}

void Music::stop()
{
ÂÂÂÂMP3Player_Stop();
}

//_______________________________________________________________________________
/*
* Can open the SD Card
*/
bool Music::can_open_root_fs()
{
ÂÂÂÂDIR_ITER *root = diropen("SD:/");
ÂÂÂÂif (root)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂdirclose(root);
ÂÂÂÂÂÂÂÂreturn true;
ÂÂÂÂ}
ÂÂÂÂreturn false;
}

//_______________________________________________________________________________
/*
* Initialises FAT
*/
void Music::initialiseFat()
{
ÂÂÂÂ//printf(" Initialising FAT ......... ");
ÂÂÂÂif (!fatInitDefault())
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//drawError(15,11,11,50,err1_initFatErr);
ÂÂÂÂÂÂÂÂ//errorApplication("Unable to initialise FAT subsystem, exiting.\n");
ÂÂÂÂ}
ÂÂÂÂ
ÂÂÂÂif (!can_open_root_fs())
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂ//errorApplication("Unable to open SD card, exiting.\n");
ÂÂÂÂÂÂÂÂ//drawError(15,11,11,50,err2_rootFS);
ÂÂÂÂ}
ÂÂÂÂ//printf("Done\n");

}

void Music::mute()
{
ÂÂÂÂMP3Player_Volume(0);
}

void Music::unmute()
{
ÂÂÂÂMP3Player_Volume(254);
}


And here is a video of it in action playing the music...
 
Your error is, you call MP3Player_PlayBuffer but this call is a non blocking call (async), means it starts playing the music in it's own thread and once playing started returns to your calling function...

Let's see you main function

Code:
int main()
{
ÂÂÂÂPlayMP3("data/music/RPB-WoPiMedley.mp3");
}

The last statement by PlayMP3 is the MP3Player_PlayBuffer which starts playing music, returns and your program exits... The weird sound is because you didn't properly stopped the audio...

ps: You should wait some external event (wiimote button press or something), sleep, or do some processing... Finally you should call MP3Player_IsPlaying, if this function returns true then you may go ahead and stop the music manually...

like this

Code:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 
if (MP3Player_IsPlaying())ÂÂ{
ÂÂÂÂMP3Player_Stop();
}
 
Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.
 
Last edited by ,
diego_pmc said:
WiiCrazy said:
You should wait some external event (wiimote button press or something), sleep, or do some processing... Finally you should call MP3Player_IsPlaying, if this function returns true then you may go ahead and stop the music manually...

like this

Code:
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 
if (MP3Player_IsPlaying())ÂÂ{
ÂÂÂÂMP3Player_Stop();
}
But I want the music file to play throughout the whole time the game is running. When should I call MP3Player_Stop()? In main(), after PlayMP3(), or in PlayMP3(), after MP3Player_PlayBuffer()?

Well my comments were for the pasted code in your original post, if you are already doing processing then there is no reason to call stop directly... you need to do it only when user decides to exit and the music is still playing.

As pembo pointed out possibly you are messing the memory up with invalid pointers and such... First you should find out at what point this happens and examine the code... One good permanent way of it is outputting debug information as your code proceeds... debug output in certain places can really help speed the development...
For an example : http://code.google.com/p/crazyintro/source...urce/launcher.c (this one using syslogd client, on the pc side you run a server listening
messages from wii)

Find the details here : http://forum.wiibrew.org/read.php?11,7232,7860
 
Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.
 
Last edited by ,
Lorem Ipsum is slechts een proeftekst uit het drukkerij- en zetterijwezen. Lorem Ipsum is de standaard proeftekst in deze bedrijfstak sinds de 16e eeuw, toen een onbekende drukker een zethaak met letters nam en ze door elkaar husselde om een font-catalogus te maken.
 
Last edited by ,

Site & Scene News

Popular threads in this forum