![]() |
![]() |



Author: philk
Date Submitted: 2004-06-24 06:57:44
/*
==========================
s_GetWaveLength
Returns the length of the sound in milliseconds
==========================
*/
int snd_GetLength(const tchar_t* filename)
{
int size;
char sSampleBuffer[16*1024];
int pcmlength = 0;
int bits, channels, freq;
int handle;
handle = s_OpenWave(filename);
if (0 == handle)
{
return 0;
}
do
{
size = s_ReadWave(handle, sSampleBuffer, sizeof(sSampleBuffer), &bits, &channels, &freq);
pcmlength += size;
}while (size);
// close the file again
s_CloseWave(handle);
return pcmlength / (bits / 8) / channels / freq;
}I just thought I might share some small piece of code I recently added to the sound.c file. It calculates the duration of any sound in seconds.
This is not optimized though, since it was mainly written for calculating the length of Ogg musics. And due to the nature of the Ogg/Vorbis file format one cannot just read the length of the uncompressed pcm data from the header. So one optimization would be for WAVE files to just read the RIFF header and do some small calcs for the ADPCM files. A good place would be sound.c and the function
int s_UncompressedSampleSize(streamingwave_t* wavedata)
[Recent Contributions] [Recent Source Code]