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 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;
}
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<std::recursive_mutex> 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;
}