Skip to content

Commit

Permalink
Sound: Make eventid private. Remove MusicEvent.
Browse files Browse the repository at this point in the history
MusicEvent differs from Event by only one line and is replaced by an
Event::PlayMusic method.
  • Loading branch information
WickedSmoke committed Nov 22, 2024
1 parent 206df18 commit 33044de
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
38 changes: 30 additions & 8 deletions src/sound/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ namespace Sound {
(*volRightOut) = Clamp((*volRightOut), 0.0f, 1.0f);
}

eventid BodyMakeNoise(const Body *b, const char *sfx, float vol)
void BodyMakeNoise(const Body *b, const char *sfx, float vol)
{
float vl, vr;
CalculateStereo(b, vol, &vl, &vr);
return Sound::PlaySfx(sfx, vl, vr, 0);
Sound::PlaySfx(sfx, vl, vr, 0);
}

struct Sample {
Expand All @@ -189,6 +189,8 @@ namespace Sound {
bool isMusic;
};

typedef uint32_t eventid;

struct SoundEvent {
const Sample *sample;
OggVorbis_File *oggv; // if sample->buf = 0 then stream this
Expand Down Expand Up @@ -255,7 +257,7 @@ namespace Sound {
* Volume should be 0-65535
*/
static uint32_t identifier = 1;
eventid PlaySfx(const char *fx, const float volume_left, const float volume_right, const Op op)
static eventid PlaySfxSample(Sample *sample, const float volume_left, const float volume_right, const Op op)
{
SDL_LockAudioDevice(m_audioDevice);
unsigned int idx;
Expand All @@ -276,7 +278,7 @@ namespace Sound {
}
DestroyEvent(&wavstream[idx]);
}
wavstream[idx].sample = GetSample(fx);
wavstream[idx].sample = sample;
wavstream[idx].oggv = 0;
wavstream[idx].buf_pos = 0;
wavstream[idx].volume[0] = volume_left * GetSfxVolume();
Expand All @@ -290,17 +292,25 @@ namespace Sound {
return identifier++;
}

//unlike PlaySfx, we want uninterrupted play and do not care about age
void PlaySfx(const char *fx, const float volume_left, const float volume_right, const Op op)
{
Sample* sample = GetSample(fx);
if (sample) {
PlaySfxSample(sample, volume_left, volume_right, op);
}
}

//unlike PlaySfxSample, we want uninterrupted play and do not care about age
//alternate between two streams for crossfade
static int nextMusicStream = 0;
eventid PlayMusic(const char *fx, const float volume_left, const float volume_right, const Op op)
static eventid PlayMusicSample(Sample *sample, const float volume_left, const float volume_right, const Op op)
{
const int idx = nextMusicStream;
nextMusicStream ^= 1;
SDL_LockAudioDevice(m_audioDevice);
if (wavstream[idx].sample)
DestroyEvent(&wavstream[idx]);
wavstream[idx].sample = GetSample(fx);
wavstream[idx].sample = sample;
wavstream[idx].oggv = nullptr;
wavstream[idx].buf_pos = 0;
wavstream[idx].volume[0] = volume_left;
Expand Down Expand Up @@ -700,7 +710,19 @@ namespace Sound {
void Event::Play(const char *fx, float volume_left, float volume_right, Op op)
{
Stop();
eid = PlaySfx(fx, volume_left, volume_right, op);
Sample* sample = GetSample(fx);
if (sample) {
eid = PlaySfxSample(sample, volume_left, volume_right, op);
}
}

void Event::PlayMusic(const char *fx, const float volume_left, const float volume_right, Op op)
{
Stop();
Sample* sample = GetSample(fx);
if (sample) {
eid = PlayMusicSample(sample, volume_left, volume_right, op);
}
}

bool Event::Stop()
Expand Down
16 changes: 6 additions & 10 deletions src/sound/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ namespace Sound {
public:
Event() :
eid(0) {}
Event(uint32_t id) :
eid(id) {}
virtual void Play(const char *fx, const float volume_left, const float volume_right, Op op);
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);
bool Stop();
bool IsPlaying() const;
uint32_t EventId() { return eid; }
bool SetOp(Op op);
bool VolumeAnimate(const float targetVol1, const float targetVol2, const float dv_dt1, const float dv_dt2);
bool VolumeAnimate(const float targetVols[2], const float dv_dt[2])
Expand All @@ -43,10 +41,9 @@ namespace Sound {
return SetVolume(vol, vol);
}

protected:
private:
uint32_t eid;
};
typedef uint32_t eventid;

bool Init(bool automaticallyOpenDevice = true);
bool InitDevice(std::string &name);
Expand All @@ -59,11 +56,10 @@ namespace Sound {
void DestroyAllEvents();
void DestroyAllEventsExceptMusic();
void Pause(int on);
eventid PlaySfx(const char *fx, const float volume_left, const float volume_right, const Op op);
eventid PlayMusic(const char *fx, const float volume_left, const float volume_right, const Op op);
inline static eventid PlaySfx(const char *fx) { return PlaySfx(fx, 1.0f, 1.0f, 0); }
void PlaySfx(const char *fx, const float volume_left, const float volume_right, const Op op);
inline static void PlaySfx(const char *fx) { PlaySfx(fx, 1.0f, 1.0f, 0); }
void CalculateStereo(const Body *b, float vol, float *volLeftOut, float *volRightOut);
eventid BodyMakeNoise(const Body *b, const char *fx, float vol);
void BodyMakeNoise(const Body *b, const char *fx, float vol);
void SetMasterVolume(const float vol);
float GetMasterVolume();
void SetSfxVolume(const float vol);
Expand Down
18 changes: 2 additions & 16 deletions src/sound/SoundMusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@

namespace Sound {

MusicEvent::MusicEvent() :
Event() {}

MusicEvent::MusicEvent(uint32_t id) :
Event(id) {}

MusicEvent::~MusicEvent() {}

void MusicEvent::Play(const char *fx, const float volume_left, const float volume_right, Op op)
{
Stop();
eid = PlayMusic(fx, volume_left, volume_right, op);
}

MusicPlayer::MusicPlayer() :
m_volume(0.8f),
m_playing(false),
Expand Down Expand Up @@ -59,13 +45,13 @@ namespace Sound {
if (m_eventOnePlaying) {
m_eventOne.VolumeAnimate(0.f, 0.f, fadeDelta, fadeDelta);
m_eventOne.SetOp(Sound::OP_STOP_AT_TARGET_VOLUME);
m_eventTwo.Play(name.c_str(), 0.f, 0.f, op);
m_eventTwo.PlayMusic(name.c_str(), 0.f, 0.f, op);
m_eventTwo.VolumeAnimate(m_volume, m_volume, fadeDelta, fadeDelta);
m_eventOnePlaying = false;
} else {
m_eventTwo.VolumeAnimate(0.f, 0.f, fadeDelta, fadeDelta);
m_eventTwo.SetOp(Sound::OP_STOP_AT_TARGET_VOLUME);
m_eventOne.Play(name.c_str(), 0.f, 0.f, op);
m_eventOne.PlayMusic(name.c_str(), 0.f, 0.f, op);
m_eventOne.VolumeAnimate(m_volume, m_volume, fadeDelta, fadeDelta);
m_eventOnePlaying = true;
}
Expand Down
12 changes: 2 additions & 10 deletions src/sound/SoundMusic.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
#include <vector>

namespace Sound {
class MusicEvent : public Event {
public:
MusicEvent();
MusicEvent(uint32_t id);
~MusicEvent();
virtual void Play(const char *fx, const float volume_left, const float volume_right, Op op);
};

class MusicPlayer {
public:
MusicPlayer();
Expand All @@ -39,8 +31,8 @@ namespace Sound {
private:
float m_volume;
//two streams for crossfade
MusicEvent m_eventOne;
MusicEvent m_eventTwo;
Event m_eventOne;
Event m_eventTwo;
bool m_playing;
bool m_eventOnePlaying;
std::string m_currentSongName;
Expand Down

0 comments on commit 33044de

Please sign in to comment.