Skip to content

Commit

Permalink
Add sound playback result enum
Browse files Browse the repository at this point in the history
  • Loading branch information
pskelton committed Mar 19, 2024
1 parent 88e63f0 commit b0df623
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
44 changes: 29 additions & 15 deletions src/Media/Audio/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void AudioPlayer::playSound(SoundId eSoundID, SoundPlaybackMode mode, Pid pid) {

PAudioSample sample = CreateAudioSample();

bool result = true;
SoundPlaybackResult result = SOUND_PLAYBACK_INVALID;
sample->SetVolume(uMasterVolume);

if (mode == SOUND_MODE_UI) {
Expand Down Expand Up @@ -316,22 +316,36 @@ void AudioPlayer::playSound(SoundId eSoundID, SoundPlaybackMode mode, Pid pid) {
}
}

if (!result) {
if (si->sName.empty()) {
logger->warning("AudioPlayer: failed to play audio {} with name '{}'", std::to_underlying(eSoundID), si->sName);
} else {
logger->warning("AudioPlayer: failed to play audio {}", std::to_underlying(eSoundID));
}
} else {
// Only log sounds that actually play
if (result == SOUND_PLAYBACK_FAILED || result == SOUND_PLAYBACK_SUCCEEDED) {
// Only log sounds that actually play or tried to play
if (engine->callObserver)
engine->callObserver->notify(CALL_PLAY_SOUND, eSoundID);
}

if (si->sName.empty()) {
logger->trace("AudioPlayer: playing sound {}", std::to_underlying(eSoundID));
} else {
logger->trace("AudioPlayer: playing sound {} with name '{}'", std::to_underlying(eSoundID), si->sName);
}
switch (result) {
case SoundPlaybackResult::SOUND_PLAYBACK_FAILED:
if (si->sName.empty()) {
logger->warning("AudioPlayer: failed to play audio {} with name '{}'", std::to_underlying(eSoundID), si->sName);
} else {
logger->warning("AudioPlayer: failed to play audio {}", std::to_underlying(eSoundID));
}
break;
case SoundPlaybackResult::SOUND_PLAYBACK_SKIPPED:
if (si->sName.empty()) {
logger->trace("AudioPlayer: skipped playing sound {}", std::to_underlying(eSoundID));
} else {
logger->trace("AudioPlayer: skipped playing sound {} with name '{}'", std::to_underlying(eSoundID), si->sName);
}
break;
case SoundPlaybackResult::SOUND_PLAYBACK_SUCCEEDED:
if (si->sName.empty()) {
logger->trace("AudioPlayer: playing sound {}", std::to_underlying(eSoundID));
} else {
logger->trace("AudioPlayer: playing sound {} with name '{}'", std::to_underlying(eSoundID), si->sName);
}
break;
default:
break;
}
}

Expand Down Expand Up @@ -426,7 +440,7 @@ float AudioPlayer::getSoundLength(SoundId eSoundID) {
// then force the sample to load/play to save codec info
PAudioSample sample = CreateAudioSample();
sample->SetVolume(0);
bool result = _regularSoundPool.playNew(sample, si->dataSource);
_regularSoundPool.playNew(sample, si->dataSource);
}

return si->dataSource->GetDuration();
Expand Down
22 changes: 11 additions & 11 deletions src/Media/Audio/AudioSamplePool.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
#include "AudioSamplePool.h"

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

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

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

void AudioSamplePool::pause() {
Expand Down
6 changes: 3 additions & 3 deletions src/Media/Audio/AudioSamplePool.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ 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);
SoundPlaybackResult playNew(PAudioSample sample, PAudioDataSource source, bool positional = false);
SoundPlaybackResult playUniqueSoundId(PAudioSample sample, PAudioDataSource source, SoundId id, bool positional = false);
SoundPlaybackResult playUniquePid(PAudioSample sample, PAudioDataSource source, Pid pid, bool positional = false);
void pause();
void resume();
void stop();
Expand Down
8 changes: 8 additions & 0 deletions src/Media/Audio/SoundEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,11 @@ enum class SoundPlaybackMode {
SOUND_MODE_HOUSE_SPEECH,
};
using enum SoundPlaybackMode;

enum class SoundPlaybackResult {
SOUND_PLAYBACK_INVALID,
SOUND_PLAYBACK_FAILED,
SOUND_PLAYBACK_SKIPPED,
SOUND_PLAYBACK_SUCCEEDED
};
using enum SoundPlaybackResult;
2 changes: 1 addition & 1 deletion test/Bin/GameTest/GameTests_1500.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ GAME_TEST(Issues, Issue1515) {
EXPECT_TRUE(soundsTape.flattened().contains(SOUND_RechargeItem)); // dispel magic
}

GAME_TEST(Issues, Issue1515_1524) {
GAME_TEST(Issues, Issue1524) {
// More enemy spells without sound
auto soundsTape = tapes.sounds();
test.playTraceFromTestData("issue_1524.mm7", "issue_1524.json");
Expand Down

0 comments on commit b0df623

Please sign in to comment.