2010-11-13 23:34:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* SettingsPrompts
|
|
|
|
* USB Loader GX 2009
|
|
|
|
*
|
|
|
|
* Backgroundmusic
|
|
|
|
***************************************************************************/
|
2011-02-02 19:30:15 +01:00
|
|
|
#include <sys/dirent.h>
|
2011-01-08 13:35:41 +01:00
|
|
|
#include "themes/CTheme.h"
|
2010-11-13 23:34:53 +01:00
|
|
|
#include "gui_bgm.h"
|
|
|
|
#include "menu.h"
|
|
|
|
|
|
|
|
GuiBGM::GuiBGM(const u8 *s, int l, int v) :
|
2012-05-06 12:59:58 +02:00
|
|
|
GuiSound(s, l, v, 0)
|
2010-11-13 23:34:53 +01:00
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
currentPath = NULL;
|
|
|
|
currentPlaying = 0;
|
|
|
|
voice = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiBGM::~GuiBGM()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (currentPath) delete[] currentPath;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
ClearList();
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::Load(const char *path)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (strcmp(path, "") == 0)
|
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GuiSound::Load(path))
|
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParsePath(path);
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::LoadStandard()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
ClearList();
|
|
|
|
if (currentPath)
|
|
|
|
{
|
|
|
|
delete[] currentPath;
|
|
|
|
currentPath = NULL;
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
strcpy(Settings.ogg_path, "");
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2012-05-06 12:59:58 +02:00
|
|
|
bool ret = GuiSound::Load(Resources::GetFile("bg_music.ogg"), Resources::GetFileSize("bg_music.ogg"));
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (ret) Play();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return ret;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::ParsePath(const char * folderpath)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
ClearList();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (currentPath) delete[] currentPath;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
currentPath = new char[strlen(folderpath) + 1];
|
2012-05-06 12:59:58 +02:00
|
|
|
strcpy(currentPath, folderpath);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * isdirpath = strrchr(folderpath, '.');
|
|
|
|
if (isdirpath)
|
|
|
|
{
|
|
|
|
char * pathptr = strrchr(currentPath, '/');
|
|
|
|
if (pathptr)
|
|
|
|
{
|
|
|
|
pathptr++;
|
|
|
|
pathptr[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * LoadedFilename = strrchr(folderpath, '/') + 1;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char filename[1024];
|
2011-02-02 19:30:15 +01:00
|
|
|
struct dirent * dirent = NULL;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
DIR * dir = opendir(currentPath);
|
|
|
|
if (dir == NULL)
|
|
|
|
{
|
|
|
|
LoadStandard();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
u32 counter = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
while ((dirent = readdir(dir)) != 0)
|
|
|
|
{
|
|
|
|
snprintf(filename, sizeof(filename), dirent->d_name);
|
2011-02-02 19:30:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * fileext = strrchr(filename, '.');
|
|
|
|
if (!fileext)
|
|
|
|
continue;
|
2011-02-02 19:30:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (strcasecmp(fileext, ".mp3") == 0 || strcasecmp(fileext, ".ogg") == 0 ||
|
|
|
|
strcasecmp(fileext, ".wav") == 0 || strcasecmp(fileext, ".aif") == 0)
|
|
|
|
{
|
|
|
|
AddEntrie(filename);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (strcmp(LoadedFilename, filename) == 0) currentPlaying = counter;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
counter++;
|
|
|
|
}
|
2011-02-02 19:30:15 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
closedir(dir);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s", folderpath);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiBGM::AddEntrie(const char * filename)
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!filename) return;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
char * NewEntrie = new char[strlen(filename) + 1];
|
2012-05-06 12:59:58 +02:00
|
|
|
strcpy(NewEntrie, filename);
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
PlayList.push_back(NewEntrie);
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiBGM::ClearList()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
for (u32 i = 0; i < PlayList.size(); i++)
|
|
|
|
{
|
|
|
|
if (PlayList.at(i) != NULL)
|
|
|
|
{
|
|
|
|
delete[] PlayList.at(i);
|
|
|
|
PlayList.at(i) = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayList.clear();
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::PlayNext()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!currentPath) return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
currentPlaying++;
|
|
|
|
if (currentPlaying >= (int) PlayList.size()) currentPlaying = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s%s", currentPath, PlayList.at(currentPlaying));
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!GuiSound::Load(Settings.ogg_path)) return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Play();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::PlayPrevious()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!currentPath) return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
currentPlaying--;
|
|
|
|
if (currentPlaying < 0) currentPlaying = PlayList.size() - 1;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s%s", currentPath, PlayList.at(currentPlaying));
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!GuiSound::Load(Settings.ogg_path)) return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Play();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiBGM::PlayRandom()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!currentPath) return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
srand(time(NULL));
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
currentPlaying = rand() % PlayList.size();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
//just in case
|
|
|
|
if (currentPlaying < 0)
|
|
|
|
currentPlaying = PlayList.size() - 1;
|
|
|
|
else if (currentPlaying >= (int) PlayList.size()) currentPlaying = 0;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
snprintf(Settings.ogg_path, sizeof(Settings.ogg_path), "%s%s", currentPath, PlayList.at(currentPlaying));
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!GuiSound::Load(Settings.ogg_path)) return false;
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
Play();
|
2010-11-13 23:34:53 +01:00
|
|
|
|
2011-07-26 00:28:22 +02:00
|
|
|
return true;
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiBGM::UpdateState()
|
|
|
|
{
|
2011-07-26 00:28:22 +02:00
|
|
|
if (!IsPlaying())
|
|
|
|
{
|
|
|
|
if (loop == DIR_LOOP)
|
|
|
|
{
|
|
|
|
PlayNext();
|
|
|
|
}
|
|
|
|
else if (loop == RANDOM_BGM)
|
|
|
|
{
|
|
|
|
PlayRandom();
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 23:34:53 +01:00
|
|
|
}
|