Add audio engine initialization check

Fixes various bugs when movie system attemps to create audio buffer with
sound system being disabled via fallout2.cfg.
This commit is contained in:
Alexander Batalov 2022-10-07 15:17:07 +03:00
parent 223d214f57
commit 74a9a9e530
1 changed files with 59 additions and 4 deletions

View File

@ -34,6 +34,11 @@ static SDL_AudioSpec gAudioEngineSpec;
static SDL_AudioDeviceID gAudioEngineDeviceId = -1; static SDL_AudioDeviceID gAudioEngineDeviceId = -1;
static AudioEngineSoundBuffer gAudioEngineSoundBuffers[AUDIO_ENGINE_SOUND_BUFFERS]; static AudioEngineSoundBuffer gAudioEngineSoundBuffers[AUDIO_ENGINE_SOUND_BUFFERS];
static bool audioEngineIsInitialized()
{
return gAudioEngineDeviceId != -1;
}
static bool soundBufferIsValid(int soundBufferIndex) static bool soundBufferIsValid(int soundBufferIndex)
{ {
return soundBufferIndex >= 0 && soundBufferIndex < AUDIO_ENGINE_SOUND_BUFFERS; return soundBufferIndex >= 0 && soundBufferIndex < AUDIO_ENGINE_SOUND_BUFFERS;
@ -113,30 +118,36 @@ bool audioEngineInit()
void audioEngineExit() void audioEngineExit()
{ {
if (gAudioEngineDeviceId != -1) { if (audioEngineIsInitialized()) {
SDL_CloseAudioDevice(gAudioEngineDeviceId); SDL_CloseAudioDevice(gAudioEngineDeviceId);
gAudioEngineDeviceId = -1; gAudioEngineDeviceId = -1;
} }
if (SDL_WasInit(SDL_INIT_AUDIO)) {
SDL_QuitSubSystem(SDL_INIT_AUDIO); SDL_QuitSubSystem(SDL_INIT_AUDIO);
} }
}
void audioEnginePause() void audioEnginePause()
{ {
if (gAudioEngineDeviceId != -1) { if (audioEngineIsInitialized()) {
SDL_PauseAudioDevice(gAudioEngineDeviceId, 1); SDL_PauseAudioDevice(gAudioEngineDeviceId, 1);
} }
} }
void audioEngineResume() void audioEngineResume()
{ {
if (gAudioEngineDeviceId != -1) { if (audioEngineIsInitialized()) {
SDL_PauseAudioDevice(gAudioEngineDeviceId, 0); SDL_PauseAudioDevice(gAudioEngineDeviceId, 0);
} }
} }
int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int channels, int rate) 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++) { for (int index = 0; index < AUDIO_ENGINE_SOUND_BUFFERS; index++) {
AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]); AudioEngineSoundBuffer* soundBuffer = &(gAudioEngineSoundBuffers[index]);
std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex); std::lock_guard<std::recursive_mutex> lock(soundBuffer->mutex);
@ -162,6 +173,10 @@ int audioEngineCreateSoundBuffer(unsigned int size, int bitsPerSample, int chann
bool audioEngineSoundBufferRelease(int soundBufferIndex) bool audioEngineSoundBufferRelease(int soundBufferIndex)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -186,6 +201,10 @@ bool audioEngineSoundBufferRelease(int soundBufferIndex)
bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume) bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -204,6 +223,10 @@ bool audioEngineSoundBufferSetVolume(int soundBufferIndex, int volume)
bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr) bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -222,6 +245,10 @@ bool audioEngineSoundBufferGetVolume(int soundBufferIndex, int* volumePtr)
bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan) bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -241,6 +268,10 @@ bool audioEngineSoundBufferSetPan(int soundBufferIndex, int pan)
bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags) bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -263,6 +294,10 @@ bool audioEngineSoundBufferPlay(int soundBufferIndex, unsigned int flags)
bool audioEngineSoundBufferStop(int soundBufferIndex) bool audioEngineSoundBufferStop(int soundBufferIndex)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -281,6 +316,10 @@ bool audioEngineSoundBufferStop(int soundBufferIndex)
bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int* readPosPtr, unsigned int* writePosPtr) bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int* readPosPtr, unsigned int* writePosPtr)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -312,6 +351,10 @@ bool audioEngineSoundBufferGetCurrentPosition(int soundBufferIndex, unsigned int
bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int pos) bool audioEngineSoundBufferSetCurrentPosition(int soundBufferIndex, unsigned int pos)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; 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) 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)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; 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) bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigned int audioBytes1, void* audioPtr2, unsigned int audioBytes2)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }
@ -405,6 +456,10 @@ bool audioEngineSoundBufferUnlock(int soundBufferIndex, void* audioPtr1, unsigne
bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusPtr) bool audioEngineSoundBufferGetStatus(int soundBufferIndex, unsigned int* statusPtr)
{ {
if (!audioEngineIsInitialized()) {
return false;
}
if (!soundBufferIsValid(soundBufferIndex)) { if (!soundBufferIsValid(soundBufferIndex)) {
return false; return false;
} }