From fe2c4af8c2ec6bebe4b474ef6387747c4508d9f7 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 7 Mar 2021 02:49:40 +0300 Subject: [PATCH] engine: soundlib: make Sound_GetApproxWavePlayLen behave like in GoldSrc --- engine/common/soundlib/snd_utils.c | 32 ++++++++++-------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/engine/common/soundlib/snd_utils.c b/engine/common/soundlib/snd_utils.c index 5c9d53b6..e6ffd977 100644 --- a/engine/common/soundlib/snd_utils.c +++ b/engine/common/soundlib/snd_utils.c @@ -95,14 +95,14 @@ byte *Sound_Copy( size_t size ) uint GAME_EXPORT Sound_GetApproxWavePlayLen( const char *filepath ) { - file_t *f; - wavehdr_t wav; - size_t filesize; - float seconds; - uint samples; + file_t *f; + wavehdr_t wav; + size_t filesize; + uint msecs; f = FS_Open( filepath, "rb", false ); - if( !f ) return 0; + if( !f ) + return 0; if( FS_Read( f, &wav, sizeof( wav )) != sizeof( wav )) { @@ -111,7 +111,7 @@ uint GAME_EXPORT Sound_GetApproxWavePlayLen( const char *filepath ) } filesize = FS_FileLength( f ); - filesize -= ( sizeof( wavehdr_t ) + sizeof( chunkhdr_t )); + filesize -= 128; // magic number from GoldSrc, seems to be header size FS_Close( f ); @@ -119,21 +119,11 @@ uint GAME_EXPORT Sound_GetApproxWavePlayLen( const char *filepath ) if( wav.riff_id != RIFFHEADER || wav.wave_id != WAVEHEADER || wav.fmt_id != FORMHEADER ) return 0; - if( wav.wFormatTag != 1 ) - return 0; + if( wav.nAvgBytesPerSec >= 1000 ) + msecs = (uint)((float)filesize / ((float)wav.nAvgBytesPerSec / 1000.0f)); + else msecs = (uint)(((float)filesize / (float)wav.nAvgBytesPerSec) * 1000.0f); - if( wav.nChannels != 1 && wav.nChannels != 2 ) - return 0; - - if( wav.nBitsPerSample != 8 && wav.nBitsPerSample != 16 ) - return 0; - - // calc samplecount - seconds = (float)filesize / wav.nAvgBytesPerSec / wav.nChannels; - samples = (uint)(( wav.nSamplesPerSec * wav.nChannels ) * seconds ); - - // g-cont. this function returns samplecount or time in milliseconds ??? - return (uint)(seconds * 1000); + return msecs; } /*