Homebrew Playing MP3s

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

Deleted User

Guest
OP
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 ,
D

Deleted User

Guest
OP
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 ,

pembo

Well-Known Member
Member
Joined
Jun 1, 2009
Messages
105
Trophies
0
XP
121
Country
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...
 

WiiCrazy

Be water my friend!
Member
Joined
May 8, 2008
Messages
2,395
Trophies
0
Location
Istanbul
Website
www.tepetaklak.com
XP
387
Country
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();
}
 
D

Deleted User

Guest
OP
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 ,

WiiCrazy

Be water my friend!
Member
Joined
May 8, 2008
Messages
2,395
Trophies
0
Location
Istanbul
Website
www.tepetaklak.com
XP
387
Country
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
 
D

Deleted User

Guest
OP
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 ,
D

Deleted User

Guest
OP
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 ,

Similar threads

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BigOnYa @ BigOnYa:
    I don't know bout sets, I downloaded all the roms for mine separately.
  • Psionic Roshambo @ Psionic Roshambo:
    My 1500ish games is all hand picked and scraped it's taken me months but zero trash well except MAME.... Tried deleting clones and broke the originals uugghh
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    I legit have been complaining about MAME for over 20 years... Hate the way they handle roms
  • BigOnYa @ BigOnYa:
    Yea is a pain sometimes
  • Psionic Roshambo @ Psionic Roshambo:
    Now that they decided to add Tiger handheld and fruit machines.... Newer sets are worthless
  • Psionic Roshambo @ Psionic Roshambo:
    Because who doesn't want to emulate Halo LCD by Tiger when the machine needed to emulate it... Could just play Halo....
  • K3Nv2 @ K3Nv2:
    Batocera has some tiger games
  • Psionic Roshambo @ Psionic Roshambo:
    I don't have them on mine Ken lol
  • K3Nv2 @ K3Nv2:
    Waste of the 128kb they take
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    Tiger handhelds are good for one thing.... Occupying space in a landfill
  • K3Nv2 @ K3Nv2:
    They make good burning plastic
  • BigOnYa @ BigOnYa:
    Makes me wonder if the Pi5 can play PS2? If there's even a core for it yet.
  • K3Nv2 @ K3Nv2:
    Pi5 should be able to do ps2
  • Psionic Roshambo @ Psionic Roshambo:
    Try Dragon Quest VIII it ran perfectly on a core 2 Duo I had
  • Psionic Roshambo @ Psionic Roshambo:
    Easiest game to run I found
  • K3Nv2 @ K3Nv2:
    Ps2 emulation is cake compared to 3 years ago
  • Psionic Roshambo @ Psionic Roshambo:
    Hardest is probably Gran Turismo 4
  • Psionic Roshambo @ Psionic Roshambo:
    It's much better now yes but Gran Turismo 4 is still the hardest one to emulate that I have in my collection
  • Psionic Roshambo @ Psionic Roshambo:
    Runs perfectly fine but it's as if I can feel it always on the boarder line of dropping a frame lol
  • BigOnYa @ BigOnYa:
    I ordered a spin ball couple days ago to add to my arcade cabinet, will be nice for games like golden tee, or bowling
  • Psionic Roshambo @ Psionic Roshambo:
    I always wanted a controller for like Ikari Warriors, Time Soldiers, Heavy Barrel, Forgotten Worlds games like those
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    Not even sure what to call that controller
  • Xdqwerty @ Xdqwerty:
    an online friend I've known since 2021 left me :( bc my attitude is "cutty"
  • Psionic Roshambo @ Psionic Roshambo:
    Sylvester Stallone should have played Kyle Reese lol Arnold still as the Terminator
  • BakerMan @ BakerMan:
    TF DOES "CUTTY" EVEN MEAN?
    BakerMan @ BakerMan: TF DOES "CUTTY" EVEN MEAN?