Skip to content

Commit

Permalink
Moved out AudioSamplePool
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Nov 9, 2023
1 parent cb3a74c commit 31c774a
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 147 deletions.
128 changes: 10 additions & 118 deletions src/Media/Audio/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ constexpr Pid FAKE_HOUSE_SPEECH_PID = Pid::fromPacked(-6);

extern OpenALSoundProvider *provider;

// TODO(captainurist): doesn't belong here
#pragma pack(push, 1)
struct SoundHeader_mm7 {
char pSoundName[40];
uint32_t uFileOffset;
uint32_t uCompressedSize;
uint32_t uDecompressedSize;
};
#pragma pack(pop)

AudioPlayer::~AudioPlayer() = default;

void AudioPlayer::MusicPlayTrack(MusicID eTrack) {
Expand Down Expand Up @@ -395,124 +405,6 @@ bool AudioPlayer::isWalkingSoundPlays() {
return false;
}

bool AudioSamplePool::playNew(PAudioSample sample, PAudioDataSource source, bool positional) {
update();
if (!sample->Open(source)) {
return false;
}
sample->Play(_looping, positional);
_samplePool.push_back(AudioSamplePoolEntry(sample, SOUND_Invalid, Pid()));
return true;
}

bool AudioSamplePool::playUniqueSoundId(PAudioSample sample, PAudioDataSource source, SoundID id, bool positional) {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
if (entry.id == id) {
return true;
}
}
if (!sample->Open(source)) {
return false;
}
sample->Play(_looping, positional);
_samplePool.push_back(AudioSamplePoolEntry(sample, id, Pid()));
return true;
}

bool AudioSamplePool::playUniquePid(PAudioSample sample, PAudioDataSource source, Pid pid, bool positional) {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
if (entry.pid == pid) {
return true;
}
}
if (!sample->Open(source)) {
return false;
}
sample->Play(_looping, positional);
_samplePool.push_back(AudioSamplePoolEntry(sample, SOUND_Invalid, pid));
return true;
}

void AudioSamplePool::pause() {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->Pause();
}
}

void AudioSamplePool::resume() {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->Resume();
}
}

void AudioSamplePool::stop() {
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->Stop();
}
_samplePool.clear();
}

void AudioSamplePool::stopSoundId(SoundID soundId) {
assert(soundId != SOUND_Invalid);

auto it = _samplePool.begin();
while (it != _samplePool.end()) {
if ((*it).id == soundId) {
(*it).samplePtr->Stop();
it = _samplePool.erase(it);
} else {
it++;
}
}
}

void AudioSamplePool::stopPid(Pid pid) {
assert(pid != Pid());

auto it = _samplePool.begin();
while (it != _samplePool.end()) {
if ((*it).pid == pid) {
(*it).samplePtr->Stop();
it = _samplePool.erase(it);
} else {
it++;
}
}
}

void AudioSamplePool::update() {
auto it = _samplePool.begin();
std::erase_if(_samplePool, [](const AudioSamplePoolEntry& entry) { return entry.samplePtr->IsStopped(); });
}

void AudioSamplePool::setVolume(float value) {
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->SetVolume(value);
}
}

bool AudioSamplePool::hasPlaying() {
for (AudioSamplePoolEntry &entry : _samplePool) {
if (!entry.samplePtr->IsStopped()) {
return true;
}
}
return false;
}

#pragma pack(push, 1)
struct SoundHeader_mm7 {
char pSoundName[40];
uint32_t uFileOffset;
uint32_t uCompressedSize;
uint32_t uDecompressedSize;
};
#pragma pack(pop)

void AudioPlayer::LoadAudioSnd() {
static_assert(sizeof(SoundHeader_mm7) == 52, "Wrong type size");

Expand Down
30 changes: 1 addition & 29 deletions src/Media/Audio/AudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,7 @@
#include "Utility/Streams/FileInputStream.h"

#include "SoundEnums.h"

struct AudioSamplePoolEntry {
AudioSamplePoolEntry(PAudioSample samplePtr, SoundID id, Pid pid) : samplePtr(samplePtr), id(id), pid(pid) {}

PAudioSample samplePtr;
SoundID id;
Pid pid;
};

class AudioSamplePool {
public:
explicit AudioSamplePool(bool looping):_looping(looping) {}

bool playNew(PAudioSample sample, PAudioDataSource source, bool positional = false);
bool playUniqueSoundId(PAudioSample sample, PAudioDataSource source, SoundID id, bool positional = false);
bool playUniquePid(PAudioSample sample, PAudioDataSource source, Pid pid, bool positional = false);
void pause();
void resume();
void stop();
void stopSoundId(SoundID soundId);
void stopPid(Pid pid);
void update();
void setVolume(float value);
bool hasPlaying();
private:
std::list<AudioSamplePoolEntry> _samplePool;
bool _looping;
};

#include "AudioSamplePool.h"

class AudioPlayer {
protected:
Expand Down
110 changes: 110 additions & 0 deletions src/Media/Audio/AudioSamplePool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "AudioSamplePool.h"

bool AudioSamplePool::playNew(PAudioSample sample, PAudioDataSource source, bool positional) {
update();
if (!sample->Open(source)) {
return false;
}
sample->Play(_looping, positional);
_samplePool.push_back(AudioSamplePoolEntry(sample, SOUND_Invalid, Pid()));
return true;
}

bool AudioSamplePool::playUniqueSoundId(PAudioSample sample, PAudioDataSource source, SoundID id, bool positional) {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
if (entry.id == id) {
return true;
}
}
if (!sample->Open(source)) {
return false;
}
sample->Play(_looping, positional);
_samplePool.push_back(AudioSamplePoolEntry(sample, id, Pid()));
return true;
}

bool AudioSamplePool::playUniquePid(PAudioSample sample, PAudioDataSource source, Pid pid, bool positional) {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
if (entry.pid == pid) {
return true;
}
}
if (!sample->Open(source)) {
return false;
}
sample->Play(_looping, positional);
_samplePool.push_back(AudioSamplePoolEntry(sample, SOUND_Invalid, pid));
return true;
}

void AudioSamplePool::pause() {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->Pause();
}
}

void AudioSamplePool::resume() {
update();
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->Resume();
}
}

void AudioSamplePool::stop() {
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->Stop();
}
_samplePool.clear();
}

void AudioSamplePool::stopSoundId(SoundID soundId) {
assert(soundId != SOUND_Invalid);

auto it = _samplePool.begin();
while (it != _samplePool.end()) {
if ((*it).id == soundId) {
(*it).samplePtr->Stop();
it = _samplePool.erase(it);
} else {
it++;
}
}
}

void AudioSamplePool::stopPid(Pid pid) {
assert(pid != Pid());

auto it = _samplePool.begin();
while (it != _samplePool.end()) {
if ((*it).pid == pid) {
(*it).samplePtr->Stop();
it = _samplePool.erase(it);
} else {
it++;
}
}
}

void AudioSamplePool::update() {
auto it = _samplePool.begin();
std::erase_if(_samplePool, [](const AudioSamplePoolEntry& entry) { return entry.samplePtr->IsStopped(); });
}

void AudioSamplePool::setVolume(float value) {
for (AudioSamplePoolEntry &entry : _samplePool) {
entry.samplePtr->SetVolume(value);
}
}

bool AudioSamplePool::hasPlaying() {
for (AudioSamplePoolEntry &entry : _samplePool) {
if (!entry.samplePtr->IsStopped()) {
return true;
}
}
return false;
}
37 changes: 37 additions & 0 deletions src/Media/Audio/AudioSamplePool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <list>

#include "Engine/Pid.h"

#include "Media/Media.h"

#include "SoundEnums.h"

struct AudioSamplePoolEntry {
AudioSamplePoolEntry(PAudioSample samplePtr, SoundID id, Pid pid) : samplePtr(samplePtr), id(id), pid(pid) {}

PAudioSample samplePtr;
SoundID id;
Pid pid;
};

class AudioSamplePool {
public:
explicit AudioSamplePool(bool looping):_looping(looping) {}

bool playNew(PAudioSample sample, PAudioDataSource source, bool positional = false);
bool playUniqueSoundId(PAudioSample sample, PAudioDataSource source, SoundID id, bool positional = false);
bool playUniquePid(PAudioSample sample, PAudioDataSource source, Pid pid, bool positional = false);
void pause();
void resume();
void stop();
void stopSoundId(SoundID soundId);
void stopPid(Pid pid);
void update();
void setVolume(float value);
bool hasPlaying();
private:
std::list<AudioSamplePoolEntry> _samplePool;
bool _looping;
};
2 changes: 2 additions & 0 deletions src/Media/Audio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ cmake_minimum_required(VERSION 3.24 FATAL_ERROR)

set(MEDIA_AUDIO_SOURCES
AudioPlayer.cpp
AudioSamplePool.cpp
OpenALSoundProvider.cpp
SoundList.cpp)

set(MEDIA_AUDIO_HEADERS
AudioPlayer.h
AudioSamplePool.h
OpenALSoundProvider.h
SoundEnums.h
SoundInfo.h
Expand Down

0 comments on commit 31c774a

Please sign in to comment.