From 210b294246c8f35e51387e139aee8e68a837c731 Mon Sep 17 00:00:00 2001 From: SRSaunders <82544213+SRSaunders@users.noreply.github.com> Date: Thu, 13 Feb 2025 00:19:10 -0500 Subject: [PATCH] Fix Cinematic Audio OpenAL error handling and Classic Doom OpenAL API mistakes --- doomclassic/doom/i_sound_openal.cpp | 18 ++++++++++----- neo/sound/OpenAL/AL_CinematicAudio.cpp | 32 +++++++++++++++----------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/doomclassic/doom/i_sound_openal.cpp b/doomclassic/doom/i_sound_openal.cpp index 91b87dc9..86ee7963 100644 --- a/doomclassic/doom/i_sound_openal.cpp +++ b/doomclassic/doom/i_sound_openal.cpp @@ -613,6 +613,9 @@ void I_ShutdownSound( void ) } I_StopSound( sound->id, 0 ); + + // SRS - Detach buffers before freeing memory to allow reallocation in I_InitSound() + alSourcei( sound->alSourceVoice, AL_BUFFER, 0 ); } // Free allocated sound memory @@ -683,7 +686,7 @@ void I_ShutdownSoundHardware() continue; } - if( sound->alSourceVoice ) + if( alIsSource( sound->alSourceVoice ) ) { alSourceStop( sound->alSourceVoice ); alSourcei( sound->alSourceVoice, AL_BUFFER, 0 ); @@ -694,7 +697,10 @@ void I_ShutdownSoundHardware() // Delete OpenAL buffers for all sounds for( int i = 0; i < NUMSFX; i++ ) { - alDeleteBuffers( 1, &alBuffers[i] ); + if( alIsBuffer( alBuffers[i] ) ) + { + alDeleteBuffers( 1, &alBuffers[i] ); + } } } @@ -710,7 +716,7 @@ void I_InitSoundChannel( int channel, int numOutputChannels_ ) alGenSources( ( ALuint )1, &soundchannel->alSourceVoice ); alSource3f( soundchannel->alSourceVoice, AL_VELOCITY, 0.f, 0.f, 0.f ); - alSourcef( soundchannel->alSourceVoice, AL_LOOPING, AL_FALSE ); + alSourcei( soundchannel->alSourceVoice, AL_LOOPING, AL_FALSE ); alSourcef( soundchannel->alSourceVoice, AL_MAX_DISTANCE, SFX_MAX_DISTANCE ); alSourcef( soundchannel->alSourceVoice, AL_REFERENCE_DISTANCE, SFX_REFERENCE_DISTANCE ); alSourcef( soundchannel->alSourceVoice, AL_ROLLOFF_FACTOR, SFX_ROLLOFF_FACTOR ); @@ -819,7 +825,7 @@ void I_InitMusic( void ) alGenSources( ( ALuint )1, &alMusicSourceVoice ); alSourcef( alMusicSourceVoice, AL_PITCH, 1.f ); - alSourcef( alMusicSourceVoice, AL_LOOPING, AL_TRUE ); + alSourcei( alMusicSourceVoice, AL_LOOPING, AL_TRUE ); alGenBuffers( ( ALuint )1, &alMusicBuffer ); @@ -836,14 +842,14 @@ void I_ShutdownMusic( void ) { if( Music_initialized ) { - if( alMusicSourceVoice ) + if( alIsSource( alMusicSourceVoice ) ) { I_StopSong( 0 ); alSourcei( alMusicSourceVoice, AL_BUFFER, 0 ); alDeleteSources( 1, &alMusicSourceVoice ); } - if( alMusicBuffer ) + if( alIsBuffer( alMusicBuffer ) ) { alDeleteBuffers( 1, &alMusicBuffer ); } diff --git a/neo/sound/OpenAL/AL_CinematicAudio.cpp b/neo/sound/OpenAL/AL_CinematicAudio.cpp index 04adf508..0e766eeb 100644 --- a/neo/sound/OpenAL/AL_CinematicAudio.cpp +++ b/neo/sound/OpenAL/AL_CinematicAudio.cpp @@ -153,11 +153,11 @@ void CinematicAudio_OpenAL::PlayAudio( uint8_t* data, int size ) #elif defined(USE_BINKDEC) Mem_Free( tempdata ); #endif + CheckALErrors(); alSourceQueueBuffers( alMusicSourceVoicecin, 1, &bufid ); - ALenum error = alGetError(); - if( error != AL_NO_ERROR ) + if( CheckALErrors() != AL_NO_ERROR ) { - common->Warning( "OpenAL Cinematic: %s\n", alGetString( error ) ); + common->Warning( "CinematicAudio_OpenAL::PlayAudio: error queueing OpenAL hardware buffers" ); return; } } @@ -183,11 +183,11 @@ void CinematicAudio_OpenAL::PlayAudio( uint8_t* data, int size ) // SRS - Initiate playback trigger once we have MIN_BUFFERS filled: limit startup latency if( offset == MIN_BUFFERS ) { + CheckALErrors(); alSourceQueueBuffers( alMusicSourceVoicecin, MIN_BUFFERS, &alMusicBuffercin[0] ); - ALenum error = alGetError(); - if( error != AL_NO_ERROR ) + if( CheckALErrors() != AL_NO_ERROR ) { - common->Warning( "OpenAL Cinematic: %s\n", alGetString( error ) ); + common->Warning( "CinematicAudio_OpenAL::PlayAudio: error queueing OpenAL hardware buffers" ); return; } // SRS - Prepare additional free buffers to handle variable packet rate codecs (e.g. webm vorbis) @@ -209,11 +209,11 @@ void CinematicAudio_OpenAL::PlayAudio( uint8_t* data, int size ) { return; } + CheckALErrors(); alSourcePlay( alMusicSourceVoicecin ); - ALenum error = alGetError(); - if( error != AL_NO_ERROR ) + if( CheckALErrors() != AL_NO_ERROR ) { - common->Warning( "OpenAL Cinematic: %s\n", alGetString( error ) ); + common->Warning( "CinematicAudio_OpenAL::PlayAudio: error playing OpenAL streaming source" ); return; } } @@ -259,6 +259,8 @@ void CinematicAudio_OpenAL::ShutdownAudio() { alSourceStop( alMusicSourceVoicecin ); alSourcei( alMusicSourceVoicecin, AL_BUFFER, 0 ); + + CheckALErrors(); alDeleteSources( 1, &alMusicSourceVoicecin ); if( CheckALErrors() == AL_NO_ERROR ) { @@ -266,12 +268,16 @@ void CinematicAudio_OpenAL::ShutdownAudio() } } - alDeleteBuffers( NUM_BUFFERS, &alMusicBuffercin[0] ); - if( CheckALErrors() == AL_NO_ERROR ) + for( int i = 0; i < NUM_BUFFERS; i++ ) { - for( int i = 0; i < NUM_BUFFERS; i++ ) + if( alIsBuffer( alMusicBuffercin[i] ) ) { - alMusicBuffercin[ i ] = 0; + CheckALErrors(); + alDeleteBuffers( 1, &alMusicBuffercin[i] ); + if( CheckALErrors() == AL_NO_ERROR ) + { + alMusicBuffercin[i] = 0; + } } }