From 74a9a9e530eada91b0ecd92acfc1a1a9bd2639ac Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Fri, 7 Oct 2022 15:17:07 +0300 Subject: [PATCH] Add audio engine initialization check Fixes various bugs when movie system attemps to create audio buffer with sound system being disabled via fallout2.cfg. --- src/audio_engine.cc | 63 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/src/audio_engine.cc b/src/audio_engine.cc index 462f7fe..494a216 100644 --- a/src/audio_engine.cc +++ b/src/audio_engine.cc @@ -34,6 +34,11 @@ static SDL_AudioSpec gAudioEngineSpec; static SDL_AudioDeviceID gAudioEngineDeviceId = -1; static AudioEngineSoundBuffer gAudioEngineSoundBuffers[AUDIO_ENGINE_SOUND_BUFFERS]; +static bool audioEngineIsInitialized() +{ + return gAudioEngineDeviceId != -1; +} + static bool soundBufferIsValid(int soundBufferIndex) { return soundBufferIndex >= 0 && soundBufferIndex < AUDIO_ENGINE_SOUND_BUFFERS; @@ -113,30 +118,36 @@ bool audioEngineInit() void audioEngineExit() { - if (gAudioEngineDeviceId != -1) { + if (audioEngineIsInitialized()) { SDL_CloseAudioDevice(gAudioEngineDeviceId); gAudioEngineDeviceId = -1; } - SDL_QuitSubSystem(SDL_INIT_AUDIO); + if (SDL_WasInit(SDL_INIT_AUDIO)) { + SDL_QuitSubSystem(SDL_INIT_AUDIO); + } } void audioEnginePause() { - if (gAudioEngineDeviceId != -1) { + if (audioEngineIsInitialized()) { SDL_PauseAudioDevice(gAudioEngineDeviceId, 1); } } void audioEngineResume() { - if (gAudioEngineDeviceId != -1) { + if (audioEngineIsInitialized()) { SDL_PauseAudioDevice(gAudioEngineDeviceId, 0); } } int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int channels, int rate) { + if (!audioEngineIsInitialized()) { + return -1; + } + for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) { AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]); std::lock_guard lock(soundBuffer->mutex); @@ -162,6 +173,10 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann bool audioEngineSoundBufferRelease(int soundBufferIndex) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -186,6 +201,10 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex) bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -204,6 +223,10 @@ bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume) bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -222,6 +245,10 @@ bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr) bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -241,6 +268,10 @@ bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan) bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -263,6 +294,10 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags) bool audioEngineSoundBufferStop(int soundBufferIndex) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -281,6 +316,10 @@ bool audioEngineSoundBufferStop(int soundBufferIndex) bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int* readPosPtr, unsigned int* writePosPtr) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -312,6 +351,10 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int pos) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -330,6 +373,10 @@ bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, unsigned int writeBytes, void** audioPtr1, unsigned int* audioBytes1, void** audioPtr2, unsigned int* audioBytes2, unsigned int flags) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -387,6 +434,10 @@ bool audioEngineSoundBufferLock(int soundBufferIndex, unsigned int writePos, uns bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigned int audioBytes1, void* audioPtr2, unsigned int audioBytes2) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; } @@ -405,6 +456,10 @@ bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigne bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusPtr) { + if (!audioEngineIsInitialized()) { + return false; + } + if (!soundBufferIsValid(soundBufferIndex)) { return false; }