Added smooth transition in music - issue #205
parent
0ff7e55b33
commit
e9addb5a5e
|
@ -1072,7 +1072,7 @@ void CRobotMain::ChangePhase(Phase phase)
|
||||||
if (m_phase == PHASE_SIMUL) // ends a simulation?
|
if (m_phase == PHASE_SIMUL) // ends a simulation?
|
||||||
{
|
{
|
||||||
SaveAllScript();
|
SaveAllScript();
|
||||||
m_sound->StopMusic();
|
m_sound->StopMusic(0.0f);
|
||||||
m_camera->SetControllingObject(0);
|
m_camera->SetControllingObject(0);
|
||||||
|
|
||||||
/* TODO: #if _SCHOOL
|
/* TODO: #if _SCHOOL
|
||||||
|
@ -1226,7 +1226,7 @@ void CRobotMain::ChangePhase(Phase phase)
|
||||||
m_infoFilename[SATCOM_HUSTON][0] != 0)
|
m_infoFilename[SATCOM_HUSTON][0] != 0)
|
||||||
StartDisplayInfo(SATCOM_HUSTON, false); // shows the instructions
|
StartDisplayInfo(SATCOM_HUSTON, false); // shows the instructions
|
||||||
|
|
||||||
m_sound->StopMusic();
|
m_sound->StopMusic(0.0f);
|
||||||
if (!m_base || loading) StartMusic();
|
if (!m_base || loading) StartMusic();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6954,7 +6954,6 @@ void CRobotMain::UpdateAudio(bool frame)
|
||||||
nb <= m_audioChange[t].max)
|
nb <= m_audioChange[t].max)
|
||||||
{
|
{
|
||||||
CLogger::GetInstancePointer()->Info("Changing music to \"%s\"\n", m_audioChange[t].music);
|
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_sound->PlayMusic(std::string(m_audioChange[t].music), m_audioChange[t].repeat);
|
||||||
m_audioChange[t].changed = true;
|
m_audioChange[t].changed = true;
|
||||||
}
|
}
|
||||||
|
@ -7510,8 +7509,7 @@ void CRobotMain::StartMusic()
|
||||||
CLogger::GetInstancePointer()->Debug("Starting music...\n");
|
CLogger::GetInstancePointer()->Debug("Starting music...\n");
|
||||||
if (m_audioTrack != "")
|
if (m_audioTrack != "")
|
||||||
{
|
{
|
||||||
m_sound->StopMusic();
|
m_sound->PlayMusic(m_audioTrack, m_audioRepeat, 0.0f);
|
||||||
m_sound->PlayMusic(m_audioTrack, m_audioRepeat);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,11 @@ void ALSound::CleanUp()
|
||||||
{
|
{
|
||||||
delete m_currentMusic;
|
delete m_currentMusic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto item : m_oldMusic)
|
||||||
|
{
|
||||||
|
delete item.music;
|
||||||
|
}
|
||||||
|
|
||||||
for (auto item : m_sounds)
|
for (auto item : m_sounds)
|
||||||
{
|
{
|
||||||
|
@ -101,7 +106,6 @@ bool ALSound::Create()
|
||||||
alListenerf(AL_GAIN, m_audioVolume);
|
alListenerf(AL_GAIN, m_audioVolume);
|
||||||
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
|
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
|
||||||
|
|
||||||
m_currentMusic = new Channel();
|
|
||||||
GetLogger()->Info("Done.\n");
|
GetLogger()->Info("Done.\n");
|
||||||
m_enabled = true;
|
m_enabled = true;
|
||||||
return true;
|
return true;
|
||||||
|
@ -469,13 +473,15 @@ bool ALSound::MuteAll(bool bMute)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bMute)
|
if (m_currentMusic) {
|
||||||
{
|
if (bMute)
|
||||||
m_currentMusic->SetVolume(0.0f);
|
{
|
||||||
}
|
m_currentMusic->SetVolume(0.0f);
|
||||||
else
|
}
|
||||||
{
|
else
|
||||||
m_currentMusic->SetVolume(m_musicVolume);
|
{
|
||||||
|
m_currentMusic->SetVolume(m_musicVolume);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
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);
|
alListenerfv(AL_ORIENTATION, orientation);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ALSound::PlayMusic(int rank, bool bRepeat)
|
bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime)
|
||||||
{
|
{
|
||||||
std::stringstream filename;
|
std::stringstream filename;
|
||||||
filename << "music" << std::setfill('0') << std::setw(3) << rank << ".ogg";
|
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)
|
if (!m_enabled)
|
||||||
{
|
{
|
||||||
|
@ -573,6 +604,8 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat)
|
||||||
|
|
||||||
std::stringstream file;
|
std::stringstream file;
|
||||||
file << m_soundPath << "/" << filename;
|
file << m_soundPath << "/" << filename;
|
||||||
|
|
||||||
|
Buffer *buffer;
|
||||||
|
|
||||||
// check if we have music in cache
|
// check if we have music in cache
|
||||||
if (m_music.find(filename) == m_music.end())
|
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());
|
GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Buffer *buffer = new Buffer();
|
|
||||||
|
buffer = new Buffer();
|
||||||
buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
|
buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
|
||||||
m_currentMusic->SetBuffer(buffer);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
GetLogger()->Debug("Music loaded from cache\n");
|
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->SetVolume(m_musicVolume);
|
||||||
m_currentMusic->SetLoop(bRepeat);
|
m_currentMusic->SetLoop(bRepeat);
|
||||||
m_currentMusic->Play();
|
m_currentMusic->Play();
|
||||||
|
@ -613,14 +656,20 @@ bool ALSound::RestartMusic()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ALSound::StopMusic()
|
void ALSound::StopMusic(float fadeTime)
|
||||||
{
|
{
|
||||||
if (!m_enabled || !m_currentMusic)
|
if (!m_enabled || !m_currentMusic)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SuspendMusic();
|
OldMusic old;
|
||||||
|
old.music = m_currentMusic;
|
||||||
|
old.fadeTime = fadeTime;
|
||||||
|
old.currentTime = 0.0f;
|
||||||
|
m_oldMusic.push_back(old);
|
||||||
|
|
||||||
|
m_currentMusic = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,10 +31,17 @@
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
#include <al.h>
|
#include <al.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct OldMusic {
|
||||||
|
Channel* music;
|
||||||
|
float fadeTime;
|
||||||
|
float currentTime;
|
||||||
|
};
|
||||||
|
|
||||||
class ALSound : public CSoundInterface
|
class ALSound : public CSoundInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -65,11 +72,11 @@ public:
|
||||||
bool StopAll();
|
bool StopAll();
|
||||||
bool MuteAll(bool bMute);
|
bool MuteAll(bool bMute);
|
||||||
|
|
||||||
bool PlayMusic(int rank, bool bRepeat);
|
bool PlayMusic(int rank, bool bRepeat, float fadeTime=5.0f);
|
||||||
bool PlayMusic(const std::string &filename, bool bRepeat);
|
bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=5.0f);
|
||||||
bool RestartMusic();
|
bool RestartMusic();
|
||||||
void SuspendMusic();
|
void SuspendMusic();
|
||||||
void StopMusic();
|
void StopMusic(float fadeTime=5.0f);
|
||||||
bool IsPlayingMusic();
|
bool IsPlayingMusic();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -86,6 +93,7 @@ private:
|
||||||
std::map<std::string, Buffer*> m_music;
|
std::map<std::string, Buffer*> m_music;
|
||||||
std::map<int, Channel*> m_channels;
|
std::map<int, Channel*> m_channels;
|
||||||
Channel *m_currentMusic;
|
Channel *m_currentMusic;
|
||||||
|
std::list<OldMusic> m_oldMusic;
|
||||||
Math::Vector m_eye;
|
Math::Vector m_eye;
|
||||||
Math::Vector m_lookat;
|
Math::Vector m_lookat;
|
||||||
};
|
};
|
||||||
|
|
|
@ -147,12 +147,12 @@ bool CSoundInterface::MuteAll(bool bMute)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CSoundInterface::PlayMusic(int rank, bool bRepeat)
|
bool CSoundInterface::PlayMusic(int rank, bool bRepeat, float fadeTime)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CSoundInterface::PlayMusic(const std::string &filename, bool bRepeat)
|
bool CSoundInterface::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -166,7 +166,7 @@ void CSoundInterface::SuspendMusic()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSoundInterface::StopMusic()
|
void CSoundInterface::StopMusic(float fadeTime)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -284,16 +284,18 @@ public:
|
||||||
/** Start playing music
|
/** Start playing music
|
||||||
* \param rank - track number
|
* \param rank - track number
|
||||||
* \param bRepeat - repeat playing
|
* \param bRepeat - repeat playing
|
||||||
|
* \param fadeTime - time of transition between music
|
||||||
* \return return true on success
|
* \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
|
/** Start playing music
|
||||||
* \param filename - name of file to play
|
* \param filename - name of file to play
|
||||||
* \param bRepeat - repeat playing
|
* \param bRepeat - repeat playing
|
||||||
|
* \param fadeTime - time of transition between music
|
||||||
* \return return true on success
|
* \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
|
/** Restart music
|
||||||
* @return return true on success
|
* @return return true on success
|
||||||
|
@ -308,7 +310,7 @@ public:
|
||||||
/** Stop playing music
|
/** Stop playing music
|
||||||
* \return return true on success
|
* \return return true on success
|
||||||
*/
|
*/
|
||||||
virtual void StopMusic();
|
virtual void StopMusic(float fadeTime=5.0f);
|
||||||
|
|
||||||
/** Check if music if playing
|
/** Check if music if playing
|
||||||
* \return return true if music is playing
|
* \return return true if music is playing
|
||||||
|
|
Loading…
Reference in New Issue