2010-02-15 00:22:52 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* SettingsPrompts
|
|
|
|
* USB Loader GX 2009
|
|
|
|
*
|
|
|
|
* Backgroundmusic
|
|
|
|
***************************************************************************/
|
|
|
|
#include <sys/dir.h>
|
|
|
|
#include "gui_bgm.h"
|
|
|
|
#include "menu.h"
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
GuiBGM::GuiBGM( const u8 *s, int l, int v )
|
|
|
|
: GuiSound( s, l, v )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
loop = 0;
|
|
|
|
loopMode = ONCE;
|
|
|
|
currentPath = NULL;
|
|
|
|
currentPlaying = 0;
|
|
|
|
|
|
|
|
//shouldn't be needed but
|
|
|
|
//fixes some kind of weird bug in ogg system
|
2010-09-19 01:16:05 +02:00
|
|
|
GuiSound::Load( s, l, v );
|
2010-02-15 00:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiBGM::~GuiBGM()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currentPath )
|
2010-02-15 00:22:52 +01:00
|
|
|
delete [] currentPath;
|
|
|
|
|
|
|
|
ClearList();
|
|
|
|
};
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void GuiBGM::SetLoop( bool l )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void GuiBGM::SetLoop( int l )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
loop = false;
|
|
|
|
loopMode = ONCE;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( l == LOOP )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
loop = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
loopMode = l;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool GuiBGM::Load( const char *path )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !path )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( strcmp( path, "" ) == 0 )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !GuiSound::Load( path ) )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return ParsePath( path );
|
2010-02-15 00:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::LoadStandard()
|
|
|
|
{
|
|
|
|
ClearList();
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currentPath )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
delete [] currentPath;
|
|
|
|
currentPath = NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
strcpy( Settings.ogg_path, "" );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool ret = GuiSound::Load( bg_music_ogg, bg_music_ogg_size, true );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ret )
|
2010-02-15 00:22:52 +01:00
|
|
|
Play();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool GuiBGM::ParsePath( const char * folderpath )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
ClearList();
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currentPath )
|
2010-02-15 00:22:52 +01:00
|
|
|
delete [] currentPath;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
currentPath = new char[strlen( folderpath )+1];
|
|
|
|
sprintf( currentPath, "%s", folderpath );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
char * isdirpath = strrchr( folderpath, '.' );
|
|
|
|
if ( isdirpath )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
char * pathptr = strrchr( currentPath, '/' );
|
|
|
|
if ( pathptr )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
pathptr++;
|
|
|
|
pathptr[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
char * LoadedFilename = strrchr( folderpath, '/' ) + 1;
|
2010-02-15 00:22:52 +01:00
|
|
|
|
|
|
|
char filename[1024];
|
|
|
|
struct stat st;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
DIR_ITER * dir = diropen( currentPath );
|
|
|
|
if ( dir == NULL )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
u32 counter = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( dirnext( dir, filename, &st ) == 0 )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
char * fileext = strrchr( filename, '.' );
|
|
|
|
if ( fileext )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( strcasecmp( fileext, ".mp3" ) == 0 || strcasecmp( fileext, ".ogg" ) == 0
|
|
|
|
|| strcasecmp( fileext, ".wav" ) == 0 )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
AddEntrie( filename );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( strcmp( LoadedFilename, filename ) == 0 )
|
2010-02-15 00:22:52 +01:00
|
|
|
currentPlaying = counter;
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
dirclose( dir );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
snprintf( Settings.ogg_path, sizeof( Settings.ogg_path ), "%s", folderpath );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void GuiBGM::AddEntrie( const char * filename )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !filename )
|
2010-02-15 00:22:52 +01:00
|
|
|
return;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
char * NewEntrie = new char[strlen( filename )+1];
|
|
|
|
sprintf( NewEntrie, "%s", filename );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
PlayList.push_back( NewEntrie );
|
2010-02-15 00:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiBGM::ClearList()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
for ( u32 i = 0; i < PlayList.size(); i++ )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( PlayList.at( i ) != NULL )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
delete [] PlayList.at( i );
|
|
|
|
PlayList.at( i ) = NULL;
|
2010-02-15 00:22:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::PlayNext()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !currentPath )
|
2010-02-15 00:22:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
currentPlaying++;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currentPlaying >= ( int ) PlayList.size() )
|
2010-02-15 00:22:52 +01:00
|
|
|
currentPlaying = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
snprintf( Settings.ogg_path, sizeof( Settings.ogg_path ), "%s%s", currentPath, PlayList.at( currentPlaying ) );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !GuiSound::Load( Settings.ogg_path ) )
|
2010-02-15 00:22:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Play();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::PlayPrevious()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !currentPath )
|
2010-02-15 00:22:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
currentPlaying--;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currentPlaying < 0 )
|
|
|
|
currentPlaying = PlayList.size() - 1;
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
snprintf( Settings.ogg_path, sizeof( Settings.ogg_path ), "%s%s", currentPath, PlayList.at( currentPlaying ) );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !GuiSound::Load( Settings.ogg_path ) )
|
2010-02-15 00:22:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Play();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::PlayRandom()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !currentPath )
|
2010-02-15 00:22:52 +01:00
|
|
|
return false;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
srand ( time( NULL ) );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
|
|
|
currentPlaying = rand() % PlayList.size();
|
|
|
|
|
|
|
|
//just in case
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( currentPlaying < 0 )
|
|
|
|
currentPlaying = PlayList.size() - 1;
|
|
|
|
else if ( currentPlaying >= ( int ) PlayList.size() )
|
2010-02-15 00:22:52 +01:00
|
|
|
currentPlaying = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
snprintf( Settings.ogg_path, sizeof( Settings.ogg_path ), "%s%s", currentPath, PlayList.at( currentPlaying ) );
|
2010-02-15 00:22:52 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !GuiSound::Load( Settings.ogg_path ) )
|
2010-02-15 00:22:52 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Play();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiBGM::UpdateState()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !IsPlaying() )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( loopMode == DIR_LOOP )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
PlayNext();
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
else if ( loopMode == RANDOM_BGM )
|
2010-02-15 00:22:52 +01:00
|
|
|
{
|
|
|
|
PlayRandom();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|