diff --git a/src/sound/Sound.cpp b/src/sound/Sound.cpp index 97a09897ed..832e708004 100644 --- a/src/sound/Sound.cpp +++ b/src/sound/Sound.cpp @@ -702,12 +702,23 @@ namespace Sound { } } - void Event::PlayMusic(const char *fx, const float volume_left, const float volume_right, Op op) + void Event::PlayMusic(const char *fx, float volume, float fadeDelta, bool repeat, Event* fadeOut) { + // The FadeOut, Stop, PlayMusicSample & VolumeAnimate calls perform + // five mutex lock operations. These could be reduced to a single + // lock/unlock if this were re-written. + + if (fadeOut) { + fadeOut->FadeOut(fadeDelta); + } Stop(); Sample* sample = GetSample(fx); if (sample) { - eid = PlayMusicSample(sample, volume_left, volume_right, op); + float start = fadeDelta ? 0.0f : volume; + eid = PlayMusicSample(sample, start, start, repeat ? Sound::OP_REPEAT : 0); + if (fadeDelta) { + VolumeAnimate(volume, volume, fadeDelta, fadeDelta); + } } } diff --git a/src/sound/Sound.h b/src/sound/Sound.h index f56506f787..8c45cff7fe 100644 --- a/src/sound/Sound.h +++ b/src/sound/Sound.h @@ -25,7 +25,7 @@ namespace Sound { eid(0) {} void Play(const char *fx, const float volume_left, const float volume_right, Op op); void Play(const char *fx) { Play(fx, 1.0f, 1.0f, 0); } - void PlayMusic(const char *fx, const float volume_left, const float volume_right, Op op); + void PlayMusic(const char *fx, float volume, float fadeDelta, bool repeat, Event* fadeOut = nullptr); bool Stop(); bool IsPlaying() const; bool SetOp(Op op); diff --git a/src/sound/SoundMusic.cpp b/src/sound/SoundMusic.cpp index 099a786f39..6414b0565a 100644 --- a/src/sound/SoundMusic.cpp +++ b/src/sound/SoundMusic.cpp @@ -39,20 +39,18 @@ namespace Sound { void MusicPlayer::Play(const std::string &name, const bool repeat /* = false */, const float fadeDelta /* = 1.f */) { if (!m_enabled) return; - Sound::Op op = 0; - if (repeat) - op |= Sound::OP_REPEAT; + + Event *current, *next; if (m_eventOnePlaying) { - m_eventOne.FadeOut(fadeDelta); - m_eventTwo.PlayMusic(name.c_str(), 0.f, 0.f, op); - m_eventTwo.VolumeAnimate(m_volume, m_volume, fadeDelta, fadeDelta); m_eventOnePlaying = false; + current = &m_eventOne; + next = &m_eventTwo; } else { - m_eventTwo.FadeOut(fadeDelta); - m_eventOne.PlayMusic(name.c_str(), 0.f, 0.f, op); - m_eventOne.VolumeAnimate(m_volume, m_volume, fadeDelta, fadeDelta); m_eventOnePlaying = true; + current = &m_eventTwo; + next = &m_eventOne; } + next->PlayMusic(name.c_str(), m_volume, repeat, fadeDelta, current); m_playing = true; m_currentSongName = name; }