2012-01-21 21:57:41 +01:00
|
|
|
#include "musicplayer.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
MusicPlayer m_music;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-05 16:27:05 +02:00
|
|
|
void MusicPlayer::cleanup()
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
MusicFile.FreeMemory();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::Init(Config &cfg, string musicDir, string themeMusicDir)
|
|
|
|
{
|
|
|
|
m_stopped = true;
|
|
|
|
m_fade_rate = cfg.getInt("GENERAL", "music_fade_rate", 8);
|
|
|
|
m_music_volume = cfg.getInt("GENERAL", "sound_volume_music", 255);
|
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
SetVolume(0);
|
|
|
|
MusicFile.SetVoice(0);
|
|
|
|
|
|
|
|
MusicDirectory dir = (MusicDirectory)cfg.getInt("GENERAL", "music_directories", NORMAL_MUSIC | THEME_MUSIC);
|
2012-04-05 13:00:05 +02:00
|
|
|
m_music_files.Init(cfg.getString("GENERAL", "dir_list_cache"), std::string(), std::string(), std::string(), false);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
if(dir & THEME_MUSIC)
|
2012-04-28 11:57:29 +02:00
|
|
|
m_music_files.Load(themeMusicDir, ".ogg|.mp3", "EN", cfg); //|.mod|.xm|.s3m|.wav|.aiff");
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
if(dir & NORMAL_MUSIC)
|
2012-04-28 11:57:29 +02:00
|
|
|
m_music_files.Load(musicDir, ".ogg|.mp3", "EN", cfg); //|.mod|.xm|.s3m|.wav|.aiff");
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
if(cfg.getBool("GENERAL", "randomize_music", true) && m_music_files.size() > 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
srand(unsigned(time(NULL)));
|
|
|
|
random_shuffle(m_music_files.begin(), m_music_files.end());
|
|
|
|
}
|
|
|
|
m_current_music = m_music_files.begin();
|
|
|
|
}
|
|
|
|
|
2012-08-16 00:33:54 +02:00
|
|
|
void MusicPlayer::SetVolume(u8 volume)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
m_music_current_volume = volume;
|
|
|
|
MusicFile.SetVolume(m_music_current_volume);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::Previous()
|
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
if(m_music_files.empty())
|
|
|
|
return;
|
|
|
|
if(m_current_music == m_music_files.begin())
|
2012-01-21 21:57:41 +01:00
|
|
|
m_current_music = m_music_files.end();
|
|
|
|
|
|
|
|
m_current_music--;
|
|
|
|
|
|
|
|
LoadCurrentFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::Next()
|
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
if(m_music_files.empty())
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
m_current_music++;
|
|
|
|
if (m_current_music == m_music_files.end())
|
|
|
|
m_current_music = m_music_files.begin();
|
|
|
|
|
|
|
|
LoadCurrentFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::Play()
|
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
SetVolume(m_music_current_volume);
|
|
|
|
MusicFile.Play();
|
|
|
|
m_stopped = false;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-08-16 00:33:54 +02:00
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
void MusicPlayer::Stop()
|
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
MusicFile.Pause();
|
|
|
|
MusicFile.Stop();
|
2012-01-21 21:57:41 +01:00
|
|
|
m_stopped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::Tick(bool attenuate)
|
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
if(m_music_files.empty())
|
|
|
|
return;
|
|
|
|
if(!attenuate && m_music_current_volume < m_music_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)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
SetVolume(m_music_current_volume - m_fade_rate < 0 ? 0
|
|
|
|
: m_music_current_volume - m_fade_rate);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicPlayer::LoadCurrentFile()
|
|
|
|
{
|
2012-08-16 00:33:54 +02:00
|
|
|
MusicFile.Load((*m_current_music).c_str());
|
|
|
|
Play();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|