The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
(Time) Length of SDL_mixer chunks?
Ryan C. Gordon
Guest

Quote:
is it possible to get the time length of a loaded chunk in SDL_mixer? In
seconds, ticks, or the like?

/* untested code follows... */
Uint32 getChunkTimeMilliseconds(Mix_Chunk *chunk)
{
Uint32 points = 0;
Uint32 frames = 0;
int freq = 0;
Uint16 fmt = 0;
int chans = 0;
/* Chunks are converted to audio device format... */
if (!Mix_QuerySpec(&freq, &fmt, &chans))
return 0; /* never called Mix_OpenAudio()?! */

/* bytes / samplesize == sample points */
points = (chunk->alen / ((fmt & 0xFF) / Cool);

/* sample points / channels == sample frames */
frames = (points / chans);

/* (sample frames * 1000) / frequency == play length in ms */
return (frames * 1000) / freq);
}

--ryan.