forked from OpenEnroth/OpenEnroth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb3a74c
commit 31c774a
Showing
5 changed files
with
160 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters