engine: soundlib: make Sound_GetApproxWavePlayLen behave like in GoldSrc

This commit is contained in:
Alibek Omarov 2021-03-07 02:49:40 +03:00
parent 3e7dd30fd7
commit fe2c4af8c2

View File

@ -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;
}
/*