I am trying to find a way to get the duration of an WAV audio file loaded in SDL library.
I am using the latest official build of SDL for Windows, 64-bit, version 2.26.5.
SDL provides following data about the loaded file after a call to LoadWAV
function.
SDL_AudioSpec* SDL_LoadWAV(const char* file,
SDL_AudioSpec* spec,
Uint8** audio_buf,
Uint32* audio_len)
Here we have SDL_AudioSpec
which I describe below, and audio_len
which is simply the file size in bytes.
typedef struct SDL_AudioSpec
{
int freq;
SDL_AudioFormat format;
Uint8 channels;
Uint8 silence;
Uint16 samples;
Uint16 padding;
Uint32 size;
SDL_AudioCallback callback;
void *userdata;
} SDL_AudioSpec;
The file which I load was taken from the system, it is located in C:\Windows\Media
folder, so this file must be 100% correct.
After I load my WAV file, I see only two fields of SDL_AudioSpec
filled in. These fields are: freq
and format
. All other fields are zeroed. File size in bytes is filled into another function argument, but it does not mean anything to me while it is set in bytes instead of time units like second of millisecond.
I have searched in documentation of SDL, but I do not see other ways to get the file length in seconds, or simply, file duration in seconds.
Is it really possible to get WAV file duration using only SDL library without any other third-party tools ?
UPDATE
I found the reason for zeroed values. I was using a 16-bit int size instead of 32-bit int. Forgive me, MS-DOS, the world has changed.