[SDL] Total time of a sample

Nikos Chantziaras realnc at arcor.de
Thu Apr 29 08:22:06 PDT 2010


Thanks, Jonathan.  That code sure came in handy!


On 04/27/2010 11:19 PM, Jonathan Dearborn wrote:
> I was just looking at Ryan's old code for this:
>
> 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) / 8));
>
> /* sample points / channels == sample frames */
> frames = (points / chans);
>
> /* (sample frames * 1000) / frequency == play length in ms */
> return ((frames * 1000) / freq);
>
> The same can't be done with the Mix_Music struct since it's so opaque
> and format-specific.  I think this calls for SDL_sound...  If anyone who
> knows about this is listening, how mature is SDL_sound's mixer?
>
> Jonny D
>
>
> On Tue, Apr 27, 2010 at 10:50 AM, Christoph Nelles
> <evilazrael at evilazrael.de <mailto:evilazrael at evilazrael.de>> wrote:
>
>     Hi,
>
>     Try using Mix_QuerySpec to get the output frequency, sampleformat
>     and output channel count. Use this to calculate the output data rate
>     frequency * sample resolution * output channel count. For 16bit
>     stereo at 44100 sampling frequency (CD-Quality) you get
>     1411200bit/sec = 176400byte/sec. Use Mix_Chunk->alen for getting the
>     size of the sample and divide it by the output data rate. This
>     should get you the exact or rough sample duration, depending on
>     whether SDL_Mixer pads frames or so.
>
>     Nikos Chantziaras schrieb:
>
>         Any way I can get the total time (probably in milliseconds) of a
>         sample (Mix_Chunk) with SDL_Mixer?




More information about the SDL mailing list