-fixed the background music issue FINALLY the correct way, found

the core of the problem :D
-made background music code easier and smaller, cleaned it up
This commit is contained in:
fix94.1 2012-08-15 22:33:54 +00:00
parent 6963b0c637
commit 5cef9d88e6
10 changed files with 102 additions and 168 deletions

View File

@ -140,12 +140,12 @@ CMenu::CMenu(CVideo &vid) :
m_initialCoverStatusComplete = false;
m_reload = false;
m_gamesound_changed = false;
m_video_playing = false;
m_base_font_size = 0;
m_current_view = COVERFLOW_USB;
m_Emulator_boot = false;
m_banner = new BannerWindow;
m_music = new MusicPlayer; //Voice 0
m_gameSound = new GuiSound; //Voice 1
m_gameSound.SetVoice(1);
}
void CMenu::init(void)
@ -437,7 +437,7 @@ void CMenu::init(void)
}
m_btnMgr.init(m_vid);
m_music->Init(m_cfg, m_musicDir, sfmt("%s/music", m_themeDataDir.c_str()));
m_music.Init(m_cfg, m_musicDir, sfmt("%s/music", m_themeDataDir.c_str()));
_buildMenus();
@ -502,7 +502,7 @@ void CMenu::cleanup(bool hb)
m_plugin.Cleanup();
_stopSounds();
delete m_music;
m_music.cleanup();
m_cameraSound.release();
ClearGameSoundThreadStack();
SoundHandler::DestroyInstance();
@ -1814,17 +1814,17 @@ void CMenu::_mainLoopCommon(bool withCF, bool blockReboot, bool adjusting)
Sys_Test();
}
if(withCF && m_gameSelected && m_gamesound_changed && (m_gameSoundHdr == NULL) && !m_gameSound->IsPlaying() && m_music->GetVolume() == 0)
if(withCF && m_gameSelected && m_gamesound_changed && (m_gameSoundHdr == NULL) && !m_gameSound.IsPlaying() && m_music.GetVolume() == 0)
{
CheckGameSoundThread();
m_gameSound->Play(m_bnrSndVol);
m_gameSound.Play(m_bnrSndVol);
m_gamesound_changed = false;
}
else if(!m_gameSelected)
m_gameSound->Stop();
m_gameSound.Stop();
m_music->Tick(m_video_playing || (m_gameSelected &&
m_gameSound->IsLoaded()) || m_gameSound->IsPlaying());
m_music.Tick(m_video_playing || (m_gameSelected &&
m_gameSound.IsLoaded()) || m_gameSound.IsPlaying());
//Take Screenshot
if(gc_btnsPressed & PAD_TRIGGER_Z)
@ -2243,21 +2243,21 @@ void CMenu::_stopSounds(void)
// Fade out sounds
int fade_rate = m_cfg.getInt("GENERAL", "music_fade_rate", 8);
if(!m_music->IsStopped())
if(!m_music.IsStopped())
{
while(m_music->GetVolume() > 0 || m_gameSound->GetVolume() > 0)
while(m_music.GetVolume() > 0 || m_gameSound.GetVolume() > 0)
{
m_music->Tick(true);
if(m_gameSound->GetVolume() > 0)
m_gameSound->SetVolume(m_gameSound->GetVolume() < fade_rate ? 0 : m_gameSound->GetVolume() - fade_rate);
m_music.Tick(true);
if(m_gameSound.GetVolume() > 0)
m_gameSound.SetVolume(m_gameSound.GetVolume() < fade_rate ? 0 : m_gameSound.GetVolume() - fade_rate);
VIDEO_WaitVSync();
}
}
m_btnMgr.stopSounds();
m_cf.stopSound();
m_music->Stop();
m_gameSound->Stop();
m_music.Stop();
m_gameSound.Stop();
}
bool CMenu::_loadFile(SmartBuf &buffer, u32 &size, const char *path, const char *file)

View File

@ -678,7 +678,7 @@ private:
volatile float m_fileProgress;
volatile bool m_thrdMessageAdded;
volatile bool m_gameSelected;
GuiSound *m_gameSound;
GuiSound m_gameSound;
SmartGuiSound m_cameraSound;
dir_discHdr *m_gameSoundHdr;
lwp_t m_gameSoundThread;

View File

@ -101,14 +101,14 @@ int CMenu::_configSnd(void)
int musicVol = min(m_cfg.getInt("GENERAL", "sound_volume_music", 255) + step, 255);
m_cfg.setInt("GENERAL", "sound_volume_music", musicVol);
_showConfigSnd();
m_music->SetVolume(m_music->GetVolume(), musicVol);
m_music.SetVolume(musicVol);
}
else if (m_btnMgr.selected(m_configSndBtnMusicVolM))
{
int musicVol = max(m_cfg.getInt("GENERAL", "sound_volume_music", 255) - step, 0);
m_cfg.setInt("GENERAL", "sound_volume_music", musicVol);
_showConfigSnd();
m_music->SetVolume(m_music->GetVolume(), musicVol);
m_music.SetVolume(musicVol);
}
else if (m_btnMgr.selected(m_configSndBtnCFVolP))
{

View File

@ -419,13 +419,13 @@ void CMenu::_game(bool launch)
m_banner->DeleteBanner();
_CategorySettings(true);
_showGame();
if (!m_gameSound->IsPlaying())
if(!m_gameSound.IsPlaying())
startGameSound = -6;
continue;
}
if(BTN_HOME_PRESSED || BTN_B_PRESSED)
{
m_gameSound->FreeMemory();
m_gameSound.FreeMemory();
CheckGameSoundThread();
ClearGameSoundThreadStack();
m_banner->DeleteBanner();
@ -438,7 +438,7 @@ void CMenu::_game(bool launch)
m_gameSelected = true;
_gameinfo();
_showGame();
if (!m_gameSound->IsPlaying())
if(!m_gameSound.IsPlaying())
startGameSound = -6;
}
else if(BTN_MINUS_PRESSED)
@ -467,7 +467,7 @@ void CMenu::_game(bool launch)
}
movie.Stop();
_showGame();
m_music->Play();
m_music.Play();
m_video_playing = false;
//m_gameSound->play(m_bnrSndVol);
}
@ -492,7 +492,7 @@ void CMenu::_game(bool launch)
_hideGame();
if(_wbfsOp(CMenu::WO_REMOVE_GAME))
{
m_gameSound->FreeMemory();
m_gameSound.FreeMemory();
CheckGameSoundThread();
ClearGameSoundThreadStack();
m_banner->DeleteBanner();
@ -507,7 +507,7 @@ void CMenu::_game(bool launch)
m_gcfg1.setBool("ADULTONLY", id, !m_gcfg1.getBool("ADULTONLY", id, false));
else if(m_btnMgr.selected(m_gameBtnBack) || m_btnMgr.selected(m_gameBtnBackFull))
{
m_gameSound->FreeMemory();
m_gameSound.FreeMemory();
CheckGameSoundThread();
ClearGameSoundThreadStack();
m_banner->DeleteBanner();
@ -529,7 +529,7 @@ void CMenu::_game(bool launch)
m_banner->ToogleGameSettings();
_showGame();
if(!m_gameSound->IsPlaying())
if(!m_gameSound.IsPlaying())
startGameSound = -6;
}
else if(launch || m_btnMgr.selected(m_gameBtnPlay) || m_btnMgr.selected(m_gameBtnPlayFull) || (!WPadIR_Valid(0) && !WPadIR_Valid(1) && !WPadIR_Valid(2) && !WPadIR_Valid(3) && m_btnMgr.selected((u16)-1)))
@ -633,7 +633,7 @@ void CMenu::_game(bool launch)
}
if(startGameSound == -10)
{
m_gameSound->Stop();
m_gameSound.Stop();
m_gameSelected = false;
m_fa.unload();
m_banner->DeleteBanner(true);
@ -1539,7 +1539,7 @@ void CMenu::_gameSoundThread(CMenu *m)
if(m->m_cf.getHdr()->type == TYPE_PLUGIN)
{
m_banner->DeleteBanner();
m->m_gameSound->Load(m->m_plugin.GetBannerSound(m->m_cf.getHdr()->settings[0]), m->m_plugin.GetBannerSoundSize(), false);
m->m_gameSound.Load(m->m_plugin.GetBannerSound(m->m_cf.getHdr()->settings[0]), m->m_plugin.GetBannerSoundSize(), false);
m->m_gamesound_changed = true;
m->m_gameSoundHdr = NULL;
return;
@ -1568,7 +1568,7 @@ void CMenu::_gameSoundThread(CMenu *m)
cached_bnr_file = (u8*)malloc(cached_bnr_size);
if(cached_bnr_file == NULL)
{
m->m_gameSound->FreeMemory();
m->m_gameSound.FreeMemory();
m_banner->DeleteBanner();
m->m_gameSoundHdr = NULL;
return;
@ -1592,7 +1592,7 @@ void CMenu::_gameSoundThread(CMenu *m)
u8 *opening_bnr = disc.GetGameCubeBanner();
if(opening_bnr != NULL)
m_banner->CreateGCBanner(opening_bnr, &m->m_vid, m_wbf1_font.get(), m_wbf2_font.get(), m->m_cf.getHdr()->title);
m->m_gameSound->Load(gc_ogg, gc_ogg_size, false);
m->m_gameSound.Load(gc_ogg, gc_ogg_size, false);
m->m_gamesound_changed = true;
m->m_gameSoundHdr = NULL;
disc.clear();
@ -1608,7 +1608,7 @@ void CMenu::_gameSoundThread(CMenu *m)
custom_bnr_file = (u8*)malloc(custom_bnr_size);
if(custom_bnr_file == NULL)
{
m->m_gameSound->FreeMemory();
m->m_gameSound.FreeMemory();
m_banner->DeleteBanner();
m->m_gameSoundHdr = NULL;
return;
@ -1631,7 +1631,7 @@ void CMenu::_gameSoundThread(CMenu *m)
}
else
{
m->m_gameSound->FreeMemory();
m->m_gameSound.FreeMemory();
m_banner->DeleteBanner();
m->m_gameSoundHdr = NULL;
delete banner;
@ -1650,9 +1650,9 @@ void CMenu::_gameSoundThread(CMenu *m)
{
u32 newSize = 0;
u8 *newSound = DecompressCopy(soundBin, sndSize, &newSize);
if(newSound == NULL || newSize == 0 || !m->m_gameSound->Load(newSound, newSize))
if(newSound == NULL || newSize == 0 || !m->m_gameSound.Load(newSound, newSize))
{
m->m_gameSound->FreeMemory();
m->m_gameSound.FreeMemory();
m_banner->DeleteBanner();
m->m_gameSoundHdr = NULL;
return;
@ -1660,13 +1660,13 @@ void CMenu::_gameSoundThread(CMenu *m)
free(soundBin);
}
else
m->m_gameSound->Load(soundBin, sndSize);
m->m_gameSound.Load(soundBin, sndSize);
if(m->m_gameSound->IsLoaded())
if(m->m_gameSound.IsLoaded())
m->m_gamesound_changed = true;
else
{
m->m_gameSound->FreeMemory();
m->m_gameSound.FreeMemory();
m_banner->DeleteBanner();
}
}
@ -1674,7 +1674,7 @@ void CMenu::_gameSoundThread(CMenu *m)
{
gprintf("WARNING: No sound found in banner!\n");
m->m_gamesound_changed = true;
m->m_gameSound->FreeMemory();
m->m_gameSound.FreeMemory();
}
m->m_gameSoundHdr = NULL;
}

View File

@ -272,7 +272,7 @@ int CMenu::main(void)
_initAsyncNetwork();
SetupInput(true);
m_music->Play();
m_music.Play();
GameTDB m_gametdb;
m_gametdb.OpenFile(fmt("%s/wiitdb.xml", m_settingsDir.c_str()));
@ -538,14 +538,14 @@ int CMenu::main(void)
else if(BTN_MINUS_PRESSED)
{
if(b_lr_mode)
m_music->Previous();
m_music.Previous();
else
m_cf.pageUp();
}
else if(BTN_PLUS_PRESSED)
{
if(b_lr_mode)
m_music->Next();
m_music.Next();
else
m_cf.pageDown();
}
@ -588,7 +588,7 @@ int CMenu::main(void)
if(b_lr_mode)
m_cf.pageUp();
else
m_music->Previous();
m_music.Previous();
}
else if(BTN_RIGHT_PRESSED)
{
@ -596,7 +596,7 @@ int CMenu::main(void)
if(b_lr_mode)
m_cf.pageDown();
else
m_music->Next();
m_music.Next();
}
else if(BTN_PLUS_PRESSED && !m_locked)
{

View File

@ -433,7 +433,7 @@ bool CMenu::_wbfsOp(CMenu::WBFS_OP op)
m_thrdMessageAdded = false;
m_cf.stopCoverLoader();
_stopSounds();
m_music->cleanup();
m_music.cleanup();
SoundHandler::DestroyInstance();
soundDeinit();
Nand::Instance()->Disable_Emu();

View File

@ -373,6 +373,11 @@ void GuiSound::Rewind()
decoder->Rewind();
}
void GuiSound::SetVoice(s8 v)
{
this->voice = v;
}
void soundInit(void)
{
ASND_Init();

View File

@ -81,6 +81,8 @@ public:
void SetVolume(int v);
//!\param l Loop (true to loop)
void SetLoop(u8 l);
//!Needed for music etc
void SetVoice(s8 v);
private:
//!Initializes the GuiSound object by setting the default values
void Init();

View File

@ -2,76 +2,50 @@
using namespace std;
MusicPlayer *m_music;
MusicPlayer::MusicPlayer()
{
m_music = NULL;
}
MusicPlayer m_music;
void MusicPlayer::cleanup()
{
if (m_music != NULL)
{
m_music->Stop();
delete m_music;
}
MusicFile.FreeMemory();
}
void MusicPlayer::Init(Config &cfg, string musicDir, string themeMusicDir)
{
m_manual_stop = true;
m_stopped = true;
m_fade_rate = cfg.getInt("GENERAL", "music_fade_rate", 8);
m_music_volume = cfg.getInt("GENERAL", "sound_volume_music", 255);
SetVolume(0); // Fades in with tick()
SetVolume(0);
MusicFile.SetVoice(0);
MusicDirectory dir = (MusicDirectory) cfg.getInt("GENERAL", "music_directories", NORMAL_MUSIC | THEME_MUSIC);
MusicDirectory dir = (MusicDirectory)cfg.getInt("GENERAL", "music_directories", NORMAL_MUSIC | THEME_MUSIC);
m_music_files.Init(cfg.getString("GENERAL", "dir_list_cache"), std::string(), std::string(), std::string(), false);
if (dir & THEME_MUSIC)
if(dir & THEME_MUSIC)
m_music_files.Load(themeMusicDir, ".ogg|.mp3", "EN", cfg); //|.mod|.xm|.s3m|.wav|.aiff");
if (dir & NORMAL_MUSIC)
if(dir & NORMAL_MUSIC)
m_music_files.Load(musicDir, ".ogg|.mp3", "EN", cfg); //|.mod|.xm|.s3m|.wav|.aiff");
if (cfg.getBool("GENERAL", "randomize_music", true) && m_music_files.size() > 0)
if(cfg.getBool("GENERAL", "randomize_music", true) && m_music_files.size() > 0)
{
srand(unsigned(time(NULL)));
random_shuffle(m_music_files.begin(), m_music_files.end());
}
m_current_music = m_music_files.begin();
}
MusicPlayer::~MusicPlayer()
void MusicPlayer::SetVolume(u8 volume)
{
if (m_music != NULL)
{
m_music->Stop();
delete m_music;
}
}
void MusicPlayer::SetVolume(int volume)
{
m_music_current_volume = volume > m_music_volume ? m_music_volume : volume;
if (m_music != NULL)
m_music->SetVolume(m_music_current_volume);
}
void MusicPlayer::SetVolume(int volume, int max_volume)
{
m_music_volume = max_volume;
SetVolume(volume);
m_music_current_volume = volume;
MusicFile.SetVolume(m_music_current_volume);
}
void MusicPlayer::Previous()
{
if (m_music_files.empty()) return;
if (m_current_music == m_music_files.begin())
if(m_music_files.empty())
return;
if(m_current_music == m_music_files.begin())
m_current_music = m_music_files.end();
m_current_music--;
@ -81,7 +55,8 @@ void MusicPlayer::Previous()
void MusicPlayer::Next()
{
if (m_music_files.empty()) return;
if(m_music_files.empty())
return;
m_current_music++;
if (m_current_music == m_music_files.end())
@ -90,75 +65,40 @@ void MusicPlayer::Next()
LoadCurrentFile();
}
void MusicPlayer::Pause()
{
if (m_music != NULL)
m_music->Pause();
m_paused = true;
}
void MusicPlayer::Play()
{
m_manual_stop = m_paused = false; // Next tick will start the music
if (m_music != NULL)
m_music->SetVolume(m_music_current_volume);
SetVolume(m_music_current_volume);
MusicFile.Play();
m_stopped = false;
}
void MusicPlayer::Stop()
{
m_manual_stop = true;
if (m_music != NULL)
{
m_music->Pause();
m_music->Stop();
delete m_music;
m_music = NULL;
}
MusicFile.Pause();
MusicFile.Stop();
m_stopped = true;
}
void MusicPlayer::Tick(bool attenuate)
{
if (m_music_files.empty()) return;
if (m_music_current_volume == 0 && attenuate) return;
if (m_music != NULL)
if(m_music_files.empty())
return;
if(!attenuate && m_music_current_volume < m_music_volume)
{
if (!attenuate && m_music_current_volume < m_music_volume)
{
int volume = m_music_current_volume + m_fade_rate > m_music_volume ?
m_music_volume : m_music_current_volume + m_fade_rate;
SetVolume(volume);
}
else if (attenuate && m_music_current_volume > 0)
{
int volume = m_music_current_volume - m_fade_rate < 0 ?
0 : m_music_current_volume - m_fade_rate;
SetVolume(volume);
}
SetVolume(m_music_current_volume + m_fade_rate > m_music_volume ? m_music_volume
: m_music_current_volume + m_fade_rate);
if(!MusicFile.IsPlaying())
Next();
}
else if(attenuate && m_music_current_volume > 0)
{
SetVolume(m_music_current_volume - m_fade_rate < 0 ? 0
: m_music_current_volume - m_fade_rate);
}
if (!attenuate && !m_manual_stop && (m_music == NULL || m_stopped || !m_music->IsPlaying()))
Next();
}
void MusicPlayer::LoadCurrentFile()
{
if (m_music_files.empty()) return;
if (m_music != NULL)
m_music->Stop();
if (m_music == NULL)
m_music = new GuiSound((*m_current_music).c_str(), ASND_MUSIC_VOICE);
else
m_music->Load((*m_current_music).c_str());
if (m_music != NULL && !m_manual_stop)
{
m_music->SetVolume(m_music_current_volume);
m_music->Play();
m_stopped = false;
}
MusicFile.Load((*m_current_music).c_str());
Play();
}

View File

@ -12,50 +12,37 @@ enum MusicDirectory
THEME_MUSIC = 2
};
#define ASND_MUSIC_VOICE 0
class MusicPlayer
{
public:
MusicPlayer();
~MusicPlayer();
void cleanup();
void Init(Config &cfg, std::string musicDir, std::string themeMusicDir);
void Tick(bool attenuate);
void SetVolume(int volume);
void SetVolume(int volume, int max_volume);
int GetVolume() { return m_music != NULL ? m_music_current_volume : 0; };
int GetMaxVolume() { return m_music_volume; };
void SetVolume(u8 volume);
u8 GetVolume() { return m_music_current_volume; };
u8 GetMaxVolume() { return m_music_volume; };
void Previous();
void Next();
void Pause();
void Play();
void Stop();
bool IsStopped() { return m_stopped; };
private:
protected:
void LoadCurrentFile();
u8 m_music_volume;
u8 m_music_current_volume;
u8 m_fade_rate;
bool m_stopped;
GuiSound MusicFile;
CachedList<std::string> m_music_files;
vector<std::string>::iterator m_current_music;
int m_fade_rate;
int m_music_volume;
int m_music_current_volume;
bool m_manual_stop;
bool m_paused;
bool m_stopped;
bool m_playbackFinished;
GuiSound *m_music;
u32 *m_songCount;
};
extern MusicPlayer *m_music;
extern MusicPlayer m_music;
#endif