From 3d9a4560d5c359f95bfd85cbe611bfc1318c3df5 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Tue, 9 Mar 2021 23:59:29 +0300 Subject: [PATCH] client: make sound cvars statically allocated, to avoid access to invalid pointers in the future --- engine/client/s_main.c | 62 +++++++++++++------------- engine/client/s_mix.c | 4 +- engine/client/s_stream.c | 4 +- engine/client/sound.h | 13 +++--- engine/platform/android/snd_opensles.c | 5 +-- engine/platform/sdl/events.c | 4 +- engine/platform/sdl/s_sdl.c | 2 +- 7 files changed, 44 insertions(+), 50 deletions(-) diff --git a/engine/client/s_main.c b/engine/client/s_main.c index c6246832..aaed0bb6 100644 --- a/engine/client/s_main.c +++ b/engine/client/s_main.c @@ -35,17 +35,17 @@ int total_channels; int soundtime; // sample PAIRS int paintedtime; // sample PAIRS -convar_t *s_volume; -convar_t *s_musicvolume; -convar_t *s_show; -convar_t *s_mixahead; -convar_t *s_lerping; -convar_t *s_ambient_level; -convar_t *s_ambient_fade; -convar_t *s_combine_sounds; -convar_t *snd_mute_losefocus; -convar_t *s_test; // cvar for testing new effects -convar_t *s_samplecount; +static CVAR_DEFINE( s_volume, "volume", "0.7", FCVAR_ARCHIVE, "sound volume" ); +CVAR_DEFINE( s_musicvolume, "MP3Volume", "1.0", FCVAR_ARCHIVE, "background music volume" ); +static CVAR_DEFINE( s_mixahead, "_snd_mixahead", "0.12", 0, "how much sound to mix ahead of time" ); +static CVAR_DEFINE_AUTO( s_show, "0", FCVAR_ARCHIVE, "show playing sounds" ); +CVAR_DEFINE_AUTO( s_lerping, "0", FCVAR_ARCHIVE, "apply interpolation to sound output" ); +static CVAR_DEFINE( s_ambient_level, "ambient_level", "0.3", FCVAR_ARCHIVE, "volume of environment noises (water and wind)" ); +static CVAR_DEFINE( s_ambient_fade, "ambient_fade", "1000", FCVAR_ARCHIVE, "rate of volume fading when client is moving" ); +static CVAR_DEFINE_AUTO( s_combine_sounds, "0", FCVAR_ARCHIVE, "combine channels with same sounds" ); +CVAR_DEFINE_AUTO( snd_mute_losefocus, "1", FCVAR_ARCHIVE, "silence the audio when game window loses focus" ); +CVAR_DEFINE_AUTO( s_test, "0", 0, "engine developer cvar for quick testing new features" ); +CVAR_DEFINE_AUTO( s_samplecount, "0", FCVAR_ARCHIVE, "sample count (0 for default value)" ); /* ============================================================================= @@ -68,7 +68,7 @@ float S_GetMasterVolume( void ) scale = bound( 0.0f, soundfade.percent / 100.0f, 1.0f ); scale = 1.0f - scale; } - return s_volume->value * scale; + return s_volume.value * scale; } /* @@ -945,7 +945,7 @@ void S_UpdateAmbientSounds( void ) leaf = Mod_PointInLeaf( s_listener.origin, cl.worldmodel->nodes ); - if( !leaf || !s_ambient_level->value ) + if( !leaf || !s_ambient_level.value ) { for( ambient_channel = 0; ambient_channel < NUM_AMBIENTS; ambient_channel++ ) channels[ambient_channel].sfx = NULL; @@ -965,18 +965,18 @@ void S_UpdateAmbientSounds( void ) continue; } - vol = s_ambient_level->value * leaf->ambient_sound_level[ambient_channel]; + vol = s_ambient_level.value * leaf->ambient_sound_level[ambient_channel]; if( vol < 0 ) vol = 0; // don't adjust volume too fast if( chan->master_vol < vol ) { - chan->master_vol += s_listener.frametime * s_ambient_fade->value; + chan->master_vol += s_listener.frametime * s_ambient_fade.value; if( chan->master_vol > vol ) chan->master_vol = vol; } else if( chan->master_vol > vol ) { - chan->master_vol -= s_listener.frametime * s_ambient_fade->value; + chan->master_vol -= s_listener.frametime * s_ambient_fade.value; if( chan->master_vol < vol ) chan->master_vol = vol; } @@ -1192,7 +1192,7 @@ void S_StreamAviSamples( void *Avi, int entnum, float fvol, float attn, float sy ch->s_rawend = soundtime; // position is changed, synchronization is lost etc - if( fabs( ch->oldtime - synctime ) > s_mixahead->value ) + if( fabs( ch->oldtime - synctime ) > s_mixahead.value ) ch->sound_info.loopStart = AVI_TimeToSoundPosition( Avi, synctime * 1000 ); ch->oldtime = synctime; // keep actual time @@ -1466,7 +1466,7 @@ void S_UpdateChannels( void ) // soundtime - total samples that have been played out to hardware at dmaspeed // paintedtime - total samples that have been mixed at speed // endtime - target for samples in mixahead buffer at speed - endtime = soundtime + s_mixahead->value * SOUND_DMA_SPEED; + endtime = soundtime + s_mixahead.value * SOUND_DMA_SPEED; samps = dma.samples >> 1; if((int)(endtime - soundtime) > samps ) @@ -1562,7 +1562,7 @@ void SND_UpdateSound( void ) // try to combine static sounds with a previous channel of the same // sound effect so we don't mix five torches every frame // g-cont: perfomance option, probably kill stereo effect in most cases - if( i >= MAX_DYNAMIC_CHANNELS && s_combine_sounds->value ) + if( i >= MAX_DYNAMIC_CHANNELS && s_combine_sounds.value ) { // see if it can just use the last one if( combine && combine->sfx == ch->sfx ) @@ -1602,7 +1602,7 @@ void SND_UpdateSound( void ) S_SpatializeRawChannels(); // debugging output - if( CVAR_TO_BOOL( s_show )) + if( s_show.value != 0.0f ) { info.color[0] = 1.0f; info.color[1] = 0.6f; @@ -1830,17 +1830,17 @@ qboolean S_Init( void ) return false; } - s_volume = Cvar_Get( "volume", "0.7", FCVAR_ARCHIVE, "sound volume" ); - s_musicvolume = Cvar_Get( "MP3Volume", "1.0", FCVAR_ARCHIVE, "background music volume" ); - s_mixahead = Cvar_Get( "_snd_mixahead", "0.12", 0, "how much sound to mix ahead of time" ); - s_show = Cvar_Get( "s_show", "0", FCVAR_ARCHIVE, "show playing sounds" ); - s_lerping = Cvar_Get( "s_lerping", "0", FCVAR_ARCHIVE, "apply interpolation to sound output" ); - s_ambient_level = Cvar_Get( "ambient_level", "0.3", FCVAR_ARCHIVE, "volume of environment noises (water and wind)" ); - s_ambient_fade = Cvar_Get( "ambient_fade", "1000", FCVAR_ARCHIVE, "rate of volume fading when client is moving" ); - s_combine_sounds = Cvar_Get( "s_combine_channels", "0", FCVAR_ARCHIVE, "combine channels with same sounds" ); - snd_mute_losefocus = Cvar_Get( "snd_mute_losefocus", "1", FCVAR_ARCHIVE, "silence the audio when game window loses focus" ); - s_test = Cvar_Get( "s_test", "0", 0, "engine developer cvar for quick testing new features" ); - s_samplecount = Cvar_Get( "s_samplecount", "0", FCVAR_ARCHIVE, "sample count (0 for default value)" ); + Cvar_RegisterVariable( &s_volume ); + Cvar_RegisterVariable( &s_musicvolume ); + Cvar_RegisterVariable( &s_mixahead ); + Cvar_RegisterVariable( &s_show ); + Cvar_RegisterVariable( &s_lerping ); + Cvar_RegisterVariable( &s_ambient_level ); + Cvar_RegisterVariable( &s_ambient_fade ); + Cvar_RegisterVariable( &s_combine_sounds ); + Cvar_RegisterVariable( &snd_mute_losefocus ); + Cvar_RegisterVariable( &s_test ); + Cvar_RegisterVariable( &s_samplecount ); Cmd_AddCommand( "play", S_Play_f, "playing a specified sound file" ); Cmd_AddCommand( "play2", S_Play2_f, "playing a group of specified sound files" ); // nehahra stuff diff --git a/engine/client/s_mix.c b/engine/client/s_mix.c index 87aa09d7..78bef1db 100644 --- a/engine/client/s_mix.c +++ b/engine/client/s_mix.c @@ -991,7 +991,7 @@ void MIX_UpsampleAllPaintbuffers( int end, int count ) // upsample all 11khz buffers by 2x // only upsample roombuffer if dsp fx are on KDB: perf MIX_SetCurrentPaintbuffer( IROOMBUFFER ); // operates on MixUpSample - S_MixUpsample( count / ( SOUND_DMA_SPEED / SOUND_11k ), s_lerping->value ); + S_MixUpsample( count / ( SOUND_DMA_SPEED / SOUND_11k ), s_lerping.value ); // mix 22khz sounds: MIX_MixChannelsToPaintbuffer( end, SOUND_22k, SOUND_22k ); @@ -999,7 +999,7 @@ void MIX_UpsampleAllPaintbuffers( int end, int count ) // upsample all 22khz buffers by 2x // only upsample roombuffer if dsp fx are on KDB: perf MIX_SetCurrentPaintbuffer( IROOMBUFFER ); - S_MixUpsample( count / ( SOUND_DMA_SPEED / SOUND_22k ), s_lerping->value ); + S_MixUpsample( count / ( SOUND_DMA_SPEED / SOUND_22k ), s_lerping.value ); // mix all 44khz sounds to all active paintbuffers MIX_MixChannelsToPaintbuffer( end, SOUND_44k, SOUND_DMA_SPEED ); diff --git a/engine/client/s_stream.c b/engine/client/s_stream.c index 7debfcca..10ed064a 100644 --- a/engine/client/s_stream.c +++ b/engine/client/s_stream.c @@ -63,7 +63,7 @@ float S_GetMusicVolume( void ) scale = 1.0f - scale; } - return s_musicvolume->value * scale; + return s_musicvolume.value * scale; } /* @@ -183,7 +183,7 @@ void S_StreamBackgroundTrack( void ) return; // don't bother playing anything if musicvolume is 0 - if( !s_musicvolume->value || s_listener.paused || s_listener.stream_paused ) + if( !s_musicvolume.value || s_listener.paused || s_listener.stream_paused ) return; if( !cl.background ) diff --git a/engine/client/sound.h b/engine/client/sound.h index 9ae1232b..791820db 100644 --- a/engine/client/sound.h +++ b/engine/client/sound.h @@ -217,15 +217,12 @@ extern listener_t s_listener; extern int idsp_room; extern dma_t dma; -extern convar_t *s_volume; -extern convar_t *s_musicvolume; -extern convar_t *s_show; -extern convar_t *s_mixahead; -extern convar_t *s_lerping; +extern convar_t s_musicvolume; +extern convar_t s_lerping; extern convar_t *dsp_off; -extern convar_t *s_test; // cvar to testify new effects -extern convar_t *s_samplecount; -extern convar_t *snd_mute_losefocus; +extern convar_t s_test; // cvar to testify new effects +extern convar_t s_samplecount; +extern convar_t snd_mute_losefocus; void S_InitScaletable( void ); wavdata_t *S_LoadSound( sfx_t *sfx ); diff --git a/engine/platform/android/snd_opensles.c b/engine/platform/android/snd_opensles.c index 09f3821d..6d732f25 100644 --- a/engine/platform/android/snd_opensles.c +++ b/engine/platform/android/snd_opensles.c @@ -25,7 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "pthread.h" #include "sound.h" -extern convar_t *s_primary; extern dma_t dma; static SLObjectItf snddma_android_engine = NULL; @@ -64,8 +63,6 @@ void SNDDMA_Activate( qboolean active ) } else { - //if( s_globalfocus->integer ) - //return; (*snddma_android_play)->SetPlayState( snddma_android_play, SL_PLAYSTATE_STOPPED ); (*snddma_android_bufferQueue)->Clear( snddma_android_bufferQueue ); } @@ -175,7 +172,7 @@ static const char *SNDDMA_Android_Init( void ) result = (*snddma_android_bufferQueue)->RegisterCallback( snddma_android_bufferQueue, SNDDMA_Android_Callback, NULL ); if( result != SL_RESULT_SUCCESS ) return "bufferQueue->RegisterCallback"; - samples = s_samplecount->value; + samples = s_samplecount.value; if( !samples ) samples = 4096; diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c index afcee026..5bcc4203 100644 --- a/engine/platform/sdl/events.c +++ b/engine/platform/sdl/events.c @@ -341,7 +341,7 @@ static void SDLash_ActiveEvent( int gain ) { host.status = HOST_FRAME; IN_ActivateMouse(true); - if( snd_mute_losefocus->value ) + if( snd_mute_losefocus.value ) { SNDDMA_Activate( true ); } @@ -361,7 +361,7 @@ static void SDLash_ActiveEvent( int gain ) #endif host.status = HOST_NOFOCUS; IN_DeactivateMouse(); - if( snd_mute_losefocus->value ) + if( snd_mute_losefocus.value ) { SNDDMA_Activate( false ); } diff --git a/engine/platform/sdl/s_sdl.c b/engine/platform/sdl/s_sdl.c index 6a260489..14782091 100644 --- a/engine/platform/sdl/s_sdl.c +++ b/engine/platform/sdl/s_sdl.c @@ -126,7 +126,7 @@ qboolean SNDDMA_Init( void ) dma.format.speed = obtained.freq; dma.format.channels = obtained.channels; dma.format.width = 2; - samplecount = s_samplecount->value; + samplecount = s_samplecount.value; if( !samplecount ) samplecount = 0x8000; dma.samples = samplecount * obtained.channels;