From a93d9ac71c6978f5c0097cc713d50514e97dccf5 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 10 Mar 2021 00:14:34 +0300 Subject: [PATCH] engine: platform: simplify sound code Remove SNDDMA_GetSoundtime, because it was mostly copypasted from one platform code to another. SNDDMA_GetDMAPos gets removed too, as we have dma.samplepos for that. --- engine/client/s_main.c | 41 +++++++++++++++++++- engine/platform/android/snd_opensles.c | 46 +---------------------- engine/platform/linux/s_alsa.c | 52 -------------------------- engine/platform/platform.h | 1 - engine/platform/sdl/s_sdl.c | 50 ------------------------- engine/platform/stub/s_stub.c | 51 ------------------------- 6 files changed, 41 insertions(+), 200 deletions(-) diff --git a/engine/client/s_main.c b/engine/client/s_main.c index aaed0bb6..a0a83048 100644 --- a/engine/client/s_main.c +++ b/engine/client/s_main.c @@ -1450,6 +1450,45 @@ void S_StopAllSounds( qboolean ambient ) memset( &soundfade, 0, sizeof( soundfade )); } +/* +============== +S_GetSoundtime + +update global soundtime + +(was part of platform code) +=============== +*/ +static int S_GetSoundtime( void ) +{ + static int buffers, oldsamplepos; + int samplepos, fullsamples; + + fullsamples = dma.samples / 2; + + // it is possible to miscount buffers + // if it has wrapped twice between + // calls to S_Update. Oh well. + samplepos = dma.samplepos; + + if( samplepos < oldsamplepos ) + { + buffers++; // buffer wrapped + + if( paintedtime > 0x40000000 ) + { + // time to chop things off to avoid 32 bit limits + buffers = 0; + paintedtime = fullsamples; + S_StopAllSounds( true ); + } + } + + oldsamplepos = samplepos; + + return ( buffers * fullsamples + samplepos / 2 ); +} + //============================================================================= void S_UpdateChannels( void ) { @@ -1461,7 +1500,7 @@ void S_UpdateChannels( void ) if( !dma.buffer ) return; // updates DMA time - soundtime = SNDDMA_GetSoundtime(); + soundtime = S_GetSoundtime(); // soundtime - total samples that have been played out to hardware at dmaspeed // paintedtime - total samples that have been mixed at speed diff --git a/engine/platform/android/snd_opensles.c b/engine/platform/android/snd_opensles.c index 6d732f25..b8649aa9 100644 --- a/engine/platform/android/snd_opensles.c +++ b/engine/platform/android/snd_opensles.c @@ -35,7 +35,6 @@ static SLPlayItf snddma_android_play; static pthread_mutex_t snddma_android_mutex = PTHREAD_MUTEX_INITIALIZER; -static int snddma_android_pos; static int snddma_android_size; static const SLInterfaceID *pSL_IID_ENGINE; @@ -78,7 +77,7 @@ static void SNDDMA_Android_Callback( SLBufferQueueItf bq, void *context ) (*bq)->Enqueue( bq, buffer2, snddma_android_size ); memcpy( buffer2, dma.buffer, snddma_android_size ); memset( dma.buffer, 0, snddma_android_size ); - snddma_android_pos += dma.samples; + dma.samplepos += dma.samples; pthread_mutex_unlock( &snddma_android_mutex ); } @@ -215,11 +214,6 @@ qboolean SNDDMA_Init( void ) return true; } -int SNDDMA_GetDMAPos( void ) -{ - return snddma_android_pos; -} - void SNDDMA_Shutdown( void ) { Msg( "Closing OpenSL ES audio device...\n" ); @@ -261,42 +255,4 @@ void SNDDMA_BeginPainting( void ) { pthread_mutex_lock( &snddma_android_mutex ); } - - -/* -============== -SNDDMA_GetSoundtime - -update global soundtime -=============== -*/ -int SNDDMA_GetSoundtime( void ) -{ - static int buffers, oldsamplepos; - int samplepos, fullsamples; - - fullsamples = dma.samples / 2; - - // it is possible to miscount buffers - // if it has wrapped twice between - // calls to S_Update. Oh well. - samplepos = SNDDMA_GetDMAPos(); - - if( samplepos < oldsamplepos ) - { - buffers++; // buffer wrapped - - if( paintedtime > 0x40000000 ) - { - // time to chop things off to avoid 32 bit limits - buffers = 0; - paintedtime = fullsamples; - S_StopAllSounds( true ); - } - } - - oldsamplepos = samplepos; - - return (buffers * fullsamples + samplepos / 2); -} #endif diff --git a/engine/platform/linux/s_alsa.c b/engine/platform/linux/s_alsa.c index f3b5262c..4cddee95 100644 --- a/engine/platform/linux/s_alsa.c +++ b/engine/platform/linux/s_alsa.c @@ -198,58 +198,6 @@ qboolean SNDDMA_Init( void ) return true; } -/* -============== -SNDDMA_GetDMAPos - -return the current sample position (in mono samples read) -inside the recirculating dma buffer, so the mixing code will know -how many sample are required to fill it up. -=============== -*/ -int SNDDMA_GetDMAPos( void ) -{ - return dma.samplepos; -} - -/* -============== -SNDDMA_GetSoundtime - -update global soundtime -=============== -*/ -int SNDDMA_GetSoundtime( void ) -{ - static int buffers, oldsamplepos; - int samplepos, fullsamples; - - fullsamples = dma.samples / 2; - - // it is possible to miscount buffers - // if it has wrapped twice between - // calls to S_Update. Oh well. - samplepos = SNDDMA_GetDMAPos( ); - - if( samplepos < oldsamplepos ) - { - buffers++; // buffer wrapped - - if( paintedtime > 0x40000000 ) - { - // time to chop things off to avoid 32 bit limits - buffers = 0; - paintedtime = fullsamples; - S_StopAllSounds( true ); - } - } - - oldsamplepos = samplepos; - - return ( buffers * fullsamples + samplepos / 2 ); -} - - /* ============== SNDDMA_Shutdown diff --git a/engine/platform/platform.h b/engine/platform/platform.h index a8d903ea..c25ffe29 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -144,7 +144,6 @@ void IN_EvdevFrame ( void ); */ // initializes cycling through a DMA buffer and returns information on it qboolean SNDDMA_Init( void ); -int SNDDMA_GetSoundtime( void ); void SNDDMA_Shutdown( void ); void SNDDMA_BeginPainting( void ); void SNDDMA_Submit( void ); diff --git a/engine/platform/sdl/s_sdl.c b/engine/platform/sdl/s_sdl.c index 6de35fcd..1df8d4f5 100644 --- a/engine/platform/sdl/s_sdl.c +++ b/engine/platform/sdl/s_sdl.c @@ -145,56 +145,6 @@ fail: return false; } -/* -============== -SNDDMA_GetDMAPos - -return the current sample position (in mono samples read) -inside the recirculating dma buffer, so the mixing code will know -how many sample are required to fill it up. -=============== -*/ -int SNDDMA_GetDMAPos( void ) -{ - return dma.samplepos; -} - -/* -============== -SNDDMA_GetSoundtime - -update global soundtime -=============== -*/ -int SNDDMA_GetSoundtime( void ) -{ - static int buffers, oldsamplepos; - int samplepos, fullsamples; - - fullsamples = dma.samples / 2; - - // it is possible to miscount buffers - // if it has wrapped twice between - // calls to S_Update. Oh well. - samplepos = SNDDMA_GetDMAPos( ); - - if( samplepos < oldsamplepos ) - { - buffers++; // buffer wrapped - - if( paintedtime > 0x40000000 ) - { - // time to chop things off to avoid 32 bit limits - buffers = 0; - paintedtime = fullsamples; - S_StopAllSounds( true ); - } - } - - oldsamplepos = samplepos; - - return ( buffers * fullsamples + samplepos / 2 ); -} /* ============== diff --git a/engine/platform/stub/s_stub.c b/engine/platform/stub/s_stub.c index f99c2b29..509ae9b7 100644 --- a/engine/platform/stub/s_stub.c +++ b/engine/platform/stub/s_stub.c @@ -51,57 +51,6 @@ qboolean SNDDMA_Init( void ) return false; } -/* -============== -SNDDMA_GetDMAPos - -return the current sample position (in mono samples read) -inside the recirculating dma buffer, so the mixing code will know -how many sample are required to fill it up. -=============== -*/ -int SNDDMA_GetDMAPos( void ) -{ - return dma.samplepos; -} - -/* -============== -SNDDMA_GetSoundtime - -update global soundtime -=============== -*/ -int SNDDMA_GetSoundtime( void ) -{ - static int buffers, oldsamplepos; - int samplepos, fullsamples; - - fullsamples = dma.samples / 2; - - // it is possible to miscount buffers - // if it has wrapped twice between - // calls to S_Update. Oh well. - samplepos = SNDDMA_GetDMAPos(); - - if( samplepos < oldsamplepos ) - { - buffers++; // buffer wrapped - - if( paintedtime > 0x40000000 ) - { - // time to chop things off to avoid 32 bit limits - buffers = 0; - paintedtime = fullsamples; - S_StopAllSounds( true ); - } - } - - oldsamplepos = samplepos; - - return (buffers * fullsamples + samplepos / 2); -} - /* ============== SNDDMA_BeginPainting