Skip to content

Commit

Permalink
Change Sound::Event::PlayMusic to handle crossfading.
Browse files Browse the repository at this point in the history
Consolidating all the stop/play/fade operations will allow the number of
mutex locks to be reduced in the future.
  • Loading branch information
WickedSmoke committed Nov 17, 2024
1 parent d2f19ec commit 99990ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/sound/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/sound/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 7 additions & 9 deletions src/sound/SoundMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 99990ad

Please sign in to comment.