Browse Source

client: make sound cvars statically allocated, to avoid access to invalid pointers in the future

pull/2/head
Alibek Omarov 4 years ago
parent
commit
3d9a4560d5
  1. 62
      engine/client/s_main.c
  2. 4
      engine/client/s_mix.c
  3. 4
      engine/client/s_stream.c
  4. 13
      engine/client/sound.h
  5. 5
      engine/platform/android/snd_opensles.c
  6. 4
      engine/platform/sdl/events.c
  7. 2
      engine/platform/sdl/s_sdl.c

62
engine/client/s_main.c

@ -35,17 +35,17 @@ int total_channels;
int soundtime; // sample PAIRS int soundtime; // sample PAIRS
int paintedtime; // sample PAIRS int paintedtime; // sample PAIRS
convar_t *s_volume; static CVAR_DEFINE( s_volume, "volume", "0.7", FCVAR_ARCHIVE, "sound volume" );
convar_t *s_musicvolume; CVAR_DEFINE( s_musicvolume, "MP3Volume", "1.0", FCVAR_ARCHIVE, "background music volume" );
convar_t *s_show; static CVAR_DEFINE( s_mixahead, "_snd_mixahead", "0.12", 0, "how much sound to mix ahead of time" );
convar_t *s_mixahead; static CVAR_DEFINE_AUTO( s_show, "0", FCVAR_ARCHIVE, "show playing sounds" );
convar_t *s_lerping; CVAR_DEFINE_AUTO( s_lerping, "0", FCVAR_ARCHIVE, "apply interpolation to sound output" );
convar_t *s_ambient_level; static CVAR_DEFINE( s_ambient_level, "ambient_level", "0.3", FCVAR_ARCHIVE, "volume of environment noises (water and wind)" );
convar_t *s_ambient_fade; static CVAR_DEFINE( s_ambient_fade, "ambient_fade", "1000", FCVAR_ARCHIVE, "rate of volume fading when client is moving" );
convar_t *s_combine_sounds; static CVAR_DEFINE_AUTO( s_combine_sounds, "0", FCVAR_ARCHIVE, "combine channels with same sounds" );
convar_t *snd_mute_losefocus; CVAR_DEFINE_AUTO( snd_mute_losefocus, "1", FCVAR_ARCHIVE, "silence the audio when game window loses focus" );
convar_t *s_test; // cvar for testing new effects CVAR_DEFINE_AUTO( s_test, "0", 0, "engine developer cvar for quick testing new features" );
convar_t *s_samplecount; 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 = bound( 0.0f, soundfade.percent / 100.0f, 1.0f );
scale = 1.0f - scale; 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 ); 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++ ) for( ambient_channel = 0; ambient_channel < NUM_AMBIENTS; ambient_channel++ )
channels[ambient_channel].sfx = NULL; channels[ambient_channel].sfx = NULL;
@ -965,18 +965,18 @@ void S_UpdateAmbientSounds( void )
continue; 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; if( vol < 0 ) vol = 0;
// don't adjust volume too fast // don't adjust volume too fast
if( chan->master_vol < vol ) 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; if( chan->master_vol > vol ) chan->master_vol = vol;
} }
else if( 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; 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; ch->s_rawend = soundtime;
// position is changed, synchronization is lost etc // 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->sound_info.loopStart = AVI_TimeToSoundPosition( Avi, synctime * 1000 );
ch->oldtime = synctime; // keep actual time 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 // soundtime - total samples that have been played out to hardware at dmaspeed
// paintedtime - total samples that have been mixed at speed // paintedtime - total samples that have been mixed at speed
// endtime - target for samples in mixahead buffer 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; samps = dma.samples >> 1;
if((int)(endtime - soundtime) > samps ) 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 // try to combine static sounds with a previous channel of the same
// sound effect so we don't mix five torches every frame // sound effect so we don't mix five torches every frame
// g-cont: perfomance option, probably kill stereo effect in most cases // 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 // see if it can just use the last one
if( combine && combine->sfx == ch->sfx ) if( combine && combine->sfx == ch->sfx )
@ -1602,7 +1602,7 @@ void SND_UpdateSound( void )
S_SpatializeRawChannels(); S_SpatializeRawChannels();
// debugging output // debugging output
if( CVAR_TO_BOOL( s_show )) if( s_show.value != 0.0f )
{ {
info.color[0] = 1.0f; info.color[0] = 1.0f;
info.color[1] = 0.6f; info.color[1] = 0.6f;
@ -1830,17 +1830,17 @@ qboolean S_Init( void )
return false; return false;
} }
s_volume = Cvar_Get( "volume", "0.7", FCVAR_ARCHIVE, "sound volume" ); Cvar_RegisterVariable( &s_volume );
s_musicvolume = Cvar_Get( "MP3Volume", "1.0", FCVAR_ARCHIVE, "background music volume" ); Cvar_RegisterVariable( &s_musicvolume );
s_mixahead = Cvar_Get( "_snd_mixahead", "0.12", 0, "how much sound to mix ahead of time" ); Cvar_RegisterVariable( &s_mixahead );
s_show = Cvar_Get( "s_show", "0", FCVAR_ARCHIVE, "show playing sounds" ); Cvar_RegisterVariable( &s_show );
s_lerping = Cvar_Get( "s_lerping", "0", FCVAR_ARCHIVE, "apply interpolation to sound output" ); Cvar_RegisterVariable( &s_lerping );
s_ambient_level = Cvar_Get( "ambient_level", "0.3", FCVAR_ARCHIVE, "volume of environment noises (water and wind)" ); Cvar_RegisterVariable( &s_ambient_level );
s_ambient_fade = Cvar_Get( "ambient_fade", "1000", FCVAR_ARCHIVE, "rate of volume fading when client is moving" ); Cvar_RegisterVariable( &s_ambient_fade );
s_combine_sounds = Cvar_Get( "s_combine_channels", "0", FCVAR_ARCHIVE, "combine channels with same sounds" ); Cvar_RegisterVariable( &s_combine_sounds );
snd_mute_losefocus = Cvar_Get( "snd_mute_losefocus", "1", FCVAR_ARCHIVE, "silence the audio when game window loses focus" ); Cvar_RegisterVariable( &snd_mute_losefocus );
s_test = Cvar_Get( "s_test", "0", 0, "engine developer cvar for quick testing new features" ); Cvar_RegisterVariable( &s_test );
s_samplecount = Cvar_Get( "s_samplecount", "0", FCVAR_ARCHIVE, "sample count (0 for default value)" ); Cvar_RegisterVariable( &s_samplecount );
Cmd_AddCommand( "play", S_Play_f, "playing a specified sound file" ); 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 Cmd_AddCommand( "play2", S_Play2_f, "playing a group of specified sound files" ); // nehahra stuff

4
engine/client/s_mix.c

@ -991,7 +991,7 @@ void MIX_UpsampleAllPaintbuffers( int end, int count )
// upsample all 11khz buffers by 2x // upsample all 11khz buffers by 2x
// only upsample roombuffer if dsp fx are on KDB: perf // only upsample roombuffer if dsp fx are on KDB: perf
MIX_SetCurrentPaintbuffer( IROOMBUFFER ); // operates on MixUpSample 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 22khz sounds:
MIX_MixChannelsToPaintbuffer( end, SOUND_22k, SOUND_22k ); MIX_MixChannelsToPaintbuffer( end, SOUND_22k, SOUND_22k );
@ -999,7 +999,7 @@ void MIX_UpsampleAllPaintbuffers( int end, int count )
// upsample all 22khz buffers by 2x // upsample all 22khz buffers by 2x
// only upsample roombuffer if dsp fx are on KDB: perf // only upsample roombuffer if dsp fx are on KDB: perf
MIX_SetCurrentPaintbuffer( IROOMBUFFER ); 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 all 44khz sounds to all active paintbuffers
MIX_MixChannelsToPaintbuffer( end, SOUND_44k, SOUND_DMA_SPEED ); MIX_MixChannelsToPaintbuffer( end, SOUND_44k, SOUND_DMA_SPEED );

4
engine/client/s_stream.c

@ -63,7 +63,7 @@ float S_GetMusicVolume( void )
scale = 1.0f - scale; scale = 1.0f - scale;
} }
return s_musicvolume->value * scale; return s_musicvolume.value * scale;
} }
/* /*
@ -183,7 +183,7 @@ void S_StreamBackgroundTrack( void )
return; return;
// don't bother playing anything if musicvolume is 0 // 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; return;
if( !cl.background ) if( !cl.background )

13
engine/client/sound.h

@ -217,15 +217,12 @@ extern listener_t s_listener;
extern int idsp_room; extern int idsp_room;
extern dma_t dma; extern dma_t dma;
extern convar_t *s_volume; extern convar_t s_musicvolume;
extern convar_t *s_musicvolume; extern convar_t s_lerping;
extern convar_t *s_show;
extern convar_t *s_mixahead;
extern convar_t *s_lerping;
extern convar_t *dsp_off; extern convar_t *dsp_off;
extern convar_t *s_test; // cvar to testify new effects extern convar_t s_test; // cvar to testify new effects
extern convar_t *s_samplecount; extern convar_t s_samplecount;
extern convar_t *snd_mute_losefocus; extern convar_t snd_mute_losefocus;
void S_InitScaletable( void ); void S_InitScaletable( void );
wavdata_t *S_LoadSound( sfx_t *sfx ); wavdata_t *S_LoadSound( sfx_t *sfx );

5
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 "pthread.h"
#include "sound.h" #include "sound.h"
extern convar_t *s_primary;
extern dma_t dma; extern dma_t dma;
static SLObjectItf snddma_android_engine = NULL; static SLObjectItf snddma_android_engine = NULL;
@ -64,8 +63,6 @@ void SNDDMA_Activate( qboolean active )
} }
else else
{ {
//if( s_globalfocus->integer )
//return;
(*snddma_android_play)->SetPlayState( snddma_android_play, SL_PLAYSTATE_STOPPED ); (*snddma_android_play)->SetPlayState( snddma_android_play, SL_PLAYSTATE_STOPPED );
(*snddma_android_bufferQueue)->Clear( snddma_android_bufferQueue ); (*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 ); result = (*snddma_android_bufferQueue)->RegisterCallback( snddma_android_bufferQueue, SNDDMA_Android_Callback, NULL );
if( result != SL_RESULT_SUCCESS ) return "bufferQueue->RegisterCallback"; if( result != SL_RESULT_SUCCESS ) return "bufferQueue->RegisterCallback";
samples = s_samplecount->value; samples = s_samplecount.value;
if( !samples ) if( !samples )
samples = 4096; samples = 4096;

4
engine/platform/sdl/events.c

@ -341,7 +341,7 @@ static void SDLash_ActiveEvent( int gain )
{ {
host.status = HOST_FRAME; host.status = HOST_FRAME;
IN_ActivateMouse(true); IN_ActivateMouse(true);
if( snd_mute_losefocus->value ) if( snd_mute_losefocus.value )
{ {
SNDDMA_Activate( true ); SNDDMA_Activate( true );
} }
@ -361,7 +361,7 @@ static void SDLash_ActiveEvent( int gain )
#endif #endif
host.status = HOST_NOFOCUS; host.status = HOST_NOFOCUS;
IN_DeactivateMouse(); IN_DeactivateMouse();
if( snd_mute_losefocus->value ) if( snd_mute_losefocus.value )
{ {
SNDDMA_Activate( false ); SNDDMA_Activate( false );
} }

2
engine/platform/sdl/s_sdl.c

@ -126,7 +126,7 @@ qboolean SNDDMA_Init( void )
dma.format.speed = obtained.freq; dma.format.speed = obtained.freq;
dma.format.channels = obtained.channels; dma.format.channels = obtained.channels;
dma.format.width = 2; dma.format.width = 2;
samplecount = s_samplecount->value; samplecount = s_samplecount.value;
if( !samplecount ) if( !samplecount )
samplecount = 0x8000; samplecount = 0x8000;
dma.samples = samplecount * obtained.channels; dma.samples = samplecount * obtained.channels;

Loading…
Cancel
Save