Added smooth transition in music - issue #205

dev-ui
krzys-h 2013-12-28 12:30:46 +01:00
parent 0ff7e55b33
commit e9addb5a5e
5 changed files with 87 additions and 30 deletions

View File

@ -1072,7 +1072,7 @@ void CRobotMain::ChangePhase(Phase phase)
if (m_phase == PHASE_SIMUL) // ends a simulation?
{
SaveAllScript();
m_sound->StopMusic();
m_sound->StopMusic(0.0f);
m_camera->SetControllingObject(0);
/* TODO: #if _SCHOOL
@ -1226,7 +1226,7 @@ void CRobotMain::ChangePhase(Phase phase)
m_infoFilename[SATCOM_HUSTON][0] != 0)
StartDisplayInfo(SATCOM_HUSTON, false); // shows the instructions
m_sound->StopMusic();
m_sound->StopMusic(0.0f);
if (!m_base || loading) StartMusic();
}
@ -6954,7 +6954,6 @@ void CRobotMain::UpdateAudio(bool frame)
nb <= m_audioChange[t].max)
{
CLogger::GetInstancePointer()->Info("Changing music to \"%s\"\n", m_audioChange[t].music);
m_sound->StopMusic();
m_sound->PlayMusic(std::string(m_audioChange[t].music), m_audioChange[t].repeat);
m_audioChange[t].changed = true;
}
@ -7510,8 +7509,7 @@ void CRobotMain::StartMusic()
CLogger::GetInstancePointer()->Debug("Starting music...\n");
if (m_audioTrack != "")
{
m_sound->StopMusic();
m_sound->PlayMusic(m_audioTrack, m_audioRepeat);
m_sound->PlayMusic(m_audioTrack, m_audioRepeat, 0.0f);
}
}

View File

@ -57,6 +57,11 @@ void ALSound::CleanUp()
{
delete m_currentMusic;
}
for (auto item : m_oldMusic)
{
delete item.music;
}
for (auto item : m_sounds)
{
@ -101,7 +106,6 @@ bool ALSound::Create()
alListenerf(AL_GAIN, m_audioVolume);
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
m_currentMusic = new Channel();
GetLogger()->Info("Done.\n");
m_enabled = true;
return true;
@ -469,13 +473,15 @@ bool ALSound::MuteAll(bool bMute)
}
}
if (bMute)
{
m_currentMusic->SetVolume(0.0f);
}
else
{
m_currentMusic->SetVolume(m_musicVolume);
if (m_currentMusic) {
if (bMute)
{
m_currentMusic->SetVolume(0.0f);
}
else
{
m_currentMusic->SetVolume(m_musicVolume);
}
}
return true;
}
@ -542,6 +548,22 @@ void ALSound::FrameMove(float delta)
}
}
}
std::list<OldMusic> toRemove;
for (auto& it : m_oldMusic)
{
if (it.currentTime >= it.fadeTime) {
delete it.music;
toRemove.push_back(it);
} else {
it.currentTime += delta;
it.music->SetVolume(((it.fadeTime-it.currentTime) / it.fadeTime) * m_musicVolume);
}
}
for (auto it : toRemove)
m_oldMusic.remove(it);
}
@ -557,14 +579,23 @@ void ALSound::SetListener(const Math::Vector &eye, const Math::Vector &lookat)
alListenerfv(AL_ORIENTATION, orientation);
}
bool ALSound::PlayMusic(int rank, bool bRepeat)
bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime)
{
std::stringstream filename;
filename << "music" << std::setfill('0') << std::setw(3) << rank << ".ogg";
return PlayMusic(filename.str(), bRepeat);
return PlayMusic(filename.str(), bRepeat, fadeTime);
}
bool ALSound::PlayMusic(const std::string &filename, bool bRepeat)
bool operator<(const OldMusic & l, const OldMusic & r)
{
return l.currentTime < r.currentTime;
}
bool operator==(const OldMusic & l, const OldMusic & r)
{
return l.currentTime == r.currentTime;
}
bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime)
{
if (!m_enabled)
{
@ -573,6 +604,8 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat)
std::stringstream file;
file << m_soundPath << "/" << filename;
Buffer *buffer;
// check if we have music in cache
if (m_music.find(filename) == m_music.end())
@ -583,16 +616,26 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat)
GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str());
return false;
}
Buffer *buffer = new Buffer();
buffer = new Buffer();
buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
m_currentMusic->SetBuffer(buffer);
}
else
{
GetLogger()->Debug("Music loaded from cache\n");
m_currentMusic->SetBuffer(m_music[filename]);
buffer = m_music[filename];
}
if (m_currentMusic) {
OldMusic old;
old.music = m_currentMusic;
old.fadeTime = fadeTime;
old.currentTime = 0.0f;
m_oldMusic.push_back(old);
}
m_currentMusic = new Channel();
m_currentMusic->SetBuffer(buffer);
m_currentMusic->SetVolume(m_musicVolume);
m_currentMusic->SetLoop(bRepeat);
m_currentMusic->Play();
@ -613,14 +656,20 @@ bool ALSound::RestartMusic()
return true;
}
void ALSound::StopMusic()
void ALSound::StopMusic(float fadeTime)
{
if (!m_enabled || !m_currentMusic)
{
return;
}
SuspendMusic();
OldMusic old;
old.music = m_currentMusic;
old.fadeTime = fadeTime;
old.currentTime = 0.0f;
m_oldMusic.push_back(old);
m_currentMusic = nullptr;
}

View File

@ -31,10 +31,17 @@
#include <map>
#include <string>
#include <list>
#include <al.h>
struct OldMusic {
Channel* music;
float fadeTime;
float currentTime;
};
class ALSound : public CSoundInterface
{
public:
@ -65,11 +72,11 @@ public:
bool StopAll();
bool MuteAll(bool bMute);
bool PlayMusic(int rank, bool bRepeat);
bool PlayMusic(const std::string &filename, bool bRepeat);
bool PlayMusic(int rank, bool bRepeat, float fadeTime=5.0f);
bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=5.0f);
bool RestartMusic();
void SuspendMusic();
void StopMusic();
void StopMusic(float fadeTime=5.0f);
bool IsPlayingMusic();
private:
@ -86,6 +93,7 @@ private:
std::map<std::string, Buffer*> m_music;
std::map<int, Channel*> m_channels;
Channel *m_currentMusic;
std::list<OldMusic> m_oldMusic;
Math::Vector m_eye;
Math::Vector m_lookat;
};

View File

@ -147,12 +147,12 @@ bool CSoundInterface::MuteAll(bool bMute)
return true;
}
bool CSoundInterface::PlayMusic(int rank, bool bRepeat)
bool CSoundInterface::PlayMusic(int rank, bool bRepeat, float fadeTime)
{
return true;
}
bool CSoundInterface::PlayMusic(const std::string &filename, bool bRepeat)
bool CSoundInterface::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime)
{
return true;
}
@ -166,7 +166,7 @@ void CSoundInterface::SuspendMusic()
{
}
void CSoundInterface::StopMusic()
void CSoundInterface::StopMusic(float fadeTime)
{
}

View File

@ -284,16 +284,18 @@ public:
/** Start playing music
* \param rank - track number
* \param bRepeat - repeat playing
* \param fadeTime - time of transition between music
* \return return true on success
*/
virtual bool PlayMusic(int rank, bool bRepeat);
virtual bool PlayMusic(int rank, bool bRepeat, float fadeTime=5.0f);
/** Start playing music
* \param filename - name of file to play
* \param bRepeat - repeat playing
* \param fadeTime - time of transition between music
* \return return true on success
*/
virtual bool PlayMusic(const std::string &filename, bool bRepeat);
virtual bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=5.0f);
/** Restart music
* @return return true on success
@ -308,7 +310,7 @@ public:
/** Stop playing music
* \return return true on success
*/
virtual void StopMusic();
virtual void StopMusic(float fadeTime=5.0f);
/** Check if music if playing
* \return return true if music is playing