From 433e7de686486a61e9349322d56eaa14b8dc92fb Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 5 May 2024 06:15:01 +0300 Subject: [PATCH] engine: clean up unused soundlib flags, set SOUND_LOOPED flag on looped sounds --- engine/client/s_load.c | 9 ++++++--- engine/client/s_main.c | 10 +++++----- engine/client/s_mix.c | 2 +- engine/client/s_utils.c | 2 +- engine/common/common.h | 29 ++++++++++------------------- engine/common/soundlib/snd_main.c | 2 +- engine/common/soundlib/snd_mp3.c | 5 +++-- engine/common/soundlib/snd_utils.c | 2 +- engine/common/soundlib/snd_wav.c | 3 ++- engine/common/soundlib/soundlib.h | 2 +- 10 files changed, 31 insertions(+), 35 deletions(-) diff --git a/engine/client/s_load.c b/engine/client/s_load.c index 17f1dd4e..8ac95971 100644 --- a/engine/client/s_load.c +++ b/engine/client/s_load.c @@ -52,8 +52,11 @@ void S_SoundList_f( void ) { totalSize += sc->size; - if( sc->loopStart >= 0 ) Con_Printf( "L" ); - else Con_Printf( " " ); + if( FBitSet( sc->flags, SOUND_LOOPED )) + Con_Printf( "L" ); + else + Con_Printf( " " ); + if( sfx->name[0] == '*' || !Q_strncmp( sfx->name, DEFAULT_SOUNDPATH, sizeof( DEFAULT_SOUNDPATH ) - 1 )) Con_Printf( " (%2db) %s : %s\n", sc->width * 8, Q_memprint( sc->size ), sfx->name ); else Con_Printf( " (%2db) %s : " DEFAULT_SOUNDPATH "%s\n", sc->width * 8, Q_memprint( sc->size ), sfx->name ); @@ -110,7 +113,7 @@ static wavdata_t *S_CreateDefaultSound( void ) sc->width = 2; sc->channels = 1; - sc->loopStart = -1; + sc->loopStart = 0; sc->rate = SOUND_DMA_SPEED; sc->samples = SOUND_DMA_SPEED; sc->size = sc->samples * sc->width * sc->channels; diff --git a/engine/client/s_main.c b/engine/client/s_main.c index 28191b84..4a9b3d48 100644 --- a/engine/client/s_main.c +++ b/engine/client/s_main.c @@ -328,7 +328,7 @@ channel_t *SND_PickDynamicChannel( int entnum, int channel, sfx_t *sfx, qboolean // don't restart looping sounds for the same entity wavdata_t *sc = channels[first_to_die].sfx->cache; - if( sc && sc->loopStart != -1 ) + if( sc && FBitSet( sc->flags, SOUND_LOOPED )) { channel_t *ch = &channels[first_to_die]; @@ -503,7 +503,7 @@ static void SND_Spatialize( channel_t *ch ) pSource = ch->sfx->cache; - if( ch->use_loop && pSource && pSource->loopStart != -1 ) + if( ch->use_loop && pSource && FBitSet( pSource->flags, SOUND_LOOPED )) looping = true; if( !ch->staticsound ) @@ -641,7 +641,7 @@ void S_StartSound( const vec3_t pos, int ent, int chan, sound_t handle, float fv if( !target_chan->leftvol && !target_chan->rightvol ) { // looping sounds don't use this optimization because they should stick around until they're killed. - if( !sfx->cache || sfx->cache->loopStart == -1 ) + if( !sfx->cache || !FBitSet( sfx->cache->flags, SOUND_LOOPED )) { // if this is a streaming sound, play the whole thing. if( chan != CHAN_STREAM ) @@ -893,7 +893,7 @@ int S_GetCurrentStaticSounds( soundlist_t *pout, int size ) VectorCopy( channels[i].origin, pout->origin ); pout->volume = (float)channels[i].master_vol / 255.0f; pout->attenuation = channels[i].dist_mult * SND_CLIP_DISTANCE; - pout->looping = ( channels[i].use_loop && channels[i].sfx->cache->loopStart != -1 ); + pout->looping = ( channels[i].use_loop && FBitSet( channels[i].sfx->cache->flags, SOUND_LOOPED )); pout->pitch = channels[i].basePitch; pout->channel = channels[i].entchannel; pout->wordIndex = channels[i].wordIndex; @@ -928,7 +928,7 @@ int S_GetCurrentDynamicSounds( soundlist_t *pout, int size ) if( !channels[i].sfx || !channels[i].sfx->name[0] || !Q_stricmp( channels[i].sfx->name, "*default" )) continue; // don't serialize default sounds - looped = ( channels[i].use_loop && channels[i].sfx->cache->loopStart != -1 ); + looped = ( channels[i].use_loop && FBitSet( channels[i].sfx->cache->flags, SOUND_LOOPED )); if( channels[i].entchannel == CHAN_STATIC && looped && !Host_IsQuakeCompatible()) continue; // never serialize static looped sounds. It will be restoring in game code diff --git a/engine/client/s_mix.c b/engine/client/s_mix.c index 281c4f22..c6bd6f9c 100644 --- a/engine/client/s_mix.c +++ b/engine/client/s_mix.c @@ -576,7 +576,7 @@ static void MIX_MixChannelsToPaintbuffer( int endtime, int rate, int outputRate bZeroVolume = true; } - if( !pSource || ( bZeroVolume && pSource->loopStart == -1 )) + if( !pSource || ( bZeroVolume && !FBitSet( pSource->flags, SOUND_LOOPED ))) { if( !pSource ) { diff --git a/engine/client/s_utils.c b/engine/client/s_utils.c index fc5a4bb2..0f2a8d3e 100644 --- a/engine/client/s_utils.c +++ b/engine/client/s_utils.c @@ -27,7 +27,7 @@ int S_ConvertLoopedPosition( wavdata_t *pSource, int samplePosition, qboolean us // convert to a position within the loop // At the end of the loop, we return a short buffer, and subsequent call // will loop back and get the rest of the buffer - if( pSource->loopStart >= 0 && samplePosition >= pSource->samples && use_loop ) + if( FBitSet( pSource->flags, SOUND_LOOPED ) && samplePosition >= pSource->samples && use_loop ) { // size of loop int loopSize = pSource->samples - pSource->loopStart; diff --git a/engine/common/common.h b/engine/common/common.h index 8cb701f0..8defc2e9 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -479,14 +479,6 @@ typedef enum WF_TOTALCOUNT, // must be last } sndformat_t; -// soundlib global settings -typedef enum -{ - SL_USE_LERPING = BIT(0), // lerping sounds during resample - SL_KEEP_8BIT = BIT(1), // don't expand 8bit sounds automatically up to 16 bit - SL_ALLOW_OVERWRITE = BIT(2), // allow to overwrite stored sounds -} slFlags_t; - // wavdata output flags typedef enum { @@ -495,21 +487,20 @@ typedef enum SOUND_STREAM = BIT( 1 ), // this is a streaminfo, not a real sound // Sound_Process manipulation flags - SOUND_RESAMPLE = BIT(12), // resample sound to specified rate - SOUND_CONVERT16BIT = BIT(13), // change sound resolution from 8 bit to 16 + SOUND_RESAMPLE = BIT( 12 ), // resample sound to specified rate } sndFlags_t; typedef struct { - word rate; // num samples per second (e.g. 11025 - 11 khz) - byte width; // resolution - bum bits divided by 8 (8 bit is 1, 16 bit is 2) - byte channels; // num channels (1 - mono, 2 - stereo) - int loopStart; // offset at this point sound will be looping while playing more than only once - int samples; // total samplecount in wav - uint type; // compression type - uint flags; // misc sound flags - byte *buffer; // sound buffer - size_t size; // for bounds checking + word rate; // num samples per second (e.g. 11025 - 11 khz) + byte width; // resolution - bum bits divided by 8 (8 bit is 1, 16 bit is 2) + byte channels; // num channels (1 - mono, 2 - stereo) + uint loopStart; // offset at this point sound will be looping while playing more than only once + uint samples; // total samplecount in wav + uint type; // compression type + uint flags; // misc sound flags + byte *buffer; // sound buffer + size_t size; // for bounds checking } wavdata_t; // diff --git a/engine/common/soundlib/snd_main.c b/engine/common/soundlib/snd_main.c index 15c2ad3c..20b6796c 100644 --- a/engine/common/soundlib/snd_main.c +++ b/engine/common/soundlib/snd_main.c @@ -216,7 +216,7 @@ wavdata_t *FS_StreamInfo( stream_t *stream ) if( !stream ) return NULL; // fill structure - info.loopStart = -1; + info.loopStart = 0; info.rate = stream->rate; info.width = stream->width; info.channels = stream->channels; diff --git a/engine/common/soundlib/snd_mp3.c b/engine/common/soundlib/snd_mp3.c index 685d7993..b7ab1380 100644 --- a/engine/common/soundlib/snd_mp3.c +++ b/engine/common/soundlib/snd_mp3.c @@ -74,7 +74,10 @@ static uint32_t Sound_ParseSynchInteger( uint32_t v ) static void Sound_HandleCustomID3Comment( const char *key, const char *value ) { if( !Q_strcmp( key, "LOOP_START" ) || !Q_strcmp( key, "LOOPSTART" )) + { sound.loopstart = Q_atoi( value ); + SetBits( sound.flags, SOUND_LOOPED ); + } // unknown comment is not an error } @@ -233,7 +236,6 @@ qboolean Sound_LoadMPG( const char *name, const byte *buffer, fs_offset_t filesi sound.channels = sc.channels; sound.rate = sc.rate; sound.width = 2; // always 16-bit PCM - sound.loopstart = -1; sound.size = ( sound.channels * sound.rate * sound.width ) * ( sc.playtime / 1000 ); // in bytes padsize = sound.size % FRAME_SIZE; pos += FRAME_SIZE; // evaluate pos @@ -241,7 +243,6 @@ qboolean Sound_LoadMPG( const char *name, const byte *buffer, fs_offset_t filesi if( !Sound_ParseID3Tag( buffer, filesize )) { Con_DPrintf( S_WARN "Sound_LoadMPG: (%s) failed to extract LOOP_START tag\n", name ); - sound.loopstart = -1; } if( !sound.size ) diff --git a/engine/common/soundlib/snd_utils.c b/engine/common/soundlib/snd_utils.c index 08edff28..655ed10a 100644 --- a/engine/common/soundlib/snd_utils.c +++ b/engine/common/soundlib/snd_utils.c @@ -154,7 +154,7 @@ static qboolean Sound_ResampleInternal( wavdata_t *sc, int inrate, int inwidth, sound.tempbuffer = (byte *)Mem_Realloc( host.soundpool, sound.tempbuffer, sc->size ); sc->samples = outcount; - if( sc->loopStart != -1 ) + if( FBitSet( sc->flags, SOUND_LOOPED )) sc->loopStart = sc->loopStart / stepscale; if( inrate == outrate ) diff --git a/engine/common/soundlib/snd_wav.c b/engine/common/soundlib/snd_wav.c index b7837d16..9119dfa3 100644 --- a/engine/common/soundlib/snd_wav.c +++ b/engine/common/soundlib/snd_wav.c @@ -245,6 +245,7 @@ qboolean Sound_LoadWAV( const char *name, const byte *buffer, fs_offset_t filesi { iff_dataPtr += 32; sound.loopstart = GetLittleLong(); + SetBits( sound.flags, SOUND_LOOPED ); FindNextChunk( name, "LIST" ); // if the next chunk is a LIST chunk, look for a cue length marker if( iff_dataPtr ) @@ -259,7 +260,7 @@ qboolean Sound_LoadWAV( const char *name, const byte *buffer, fs_offset_t filesi } else { - sound.loopstart = -1; + sound.loopstart = 0; sound.samples = 0; } diff --git a/engine/common/soundlib/soundlib.h b/engine/common/soundlib/soundlib.h index 9ce2af46..a40b4de7 100644 --- a/engine/common/soundlib/soundlib.h +++ b/engine/common/soundlib/soundlib.h @@ -50,7 +50,7 @@ typedef struct sndlib_s int rate; // num samples per second (e.g. 11025 - 11 khz) int width; // resolution - bum bits divided by 8 (8 bit is 1, 16 bit is 2) int channels; // num channels (1 - mono, 2 - stereo) - int loopstart; // start looping from + uint loopstart; // start looping from uint samples; // total samplecount in sound uint flags; // additional sound flags size_t size; // sound unpacked size (for bounds checking)