How to get (milli)seconds from al_get_sample_length()?

How do I get seconds or milliseconds from al_get_sample_length()? My ~1s sample return ~6600. I guess the value needs to be divided...or?

A5 examples or demos does not cover this.


My guess would be to obtain the sample frequency (al_get_sample_instance_frequency) and the number of channels (al_get_channel_count). The frequency is the number of samples per second per channel, so you should be able to find the length in milliseconds with:

length = al_get_sample_length(sample) / al_get_channel_count(sample) / al_get_sample_instance_frequency(sample);

Thomas Fjellstrom

That might also need to take into account stereo samples, you would then need to divide by two.

Trent Gamblin

That'll give you the length in seconds, multiply by 1000 to get milliseconds. The channel count is the stereo part, it'll be 2 for a stereo sample.


al_get_channel_count() or does not take a ALLEGRO_SOUND as parameter. Did you mean that function or al_get_sample_channels()?

al_get_sample_length(sound) = 5641
al_get_sample_channels(sound) = 16
al_get_sample_frequency(sound) = 11025

length / channels / frequency = 0,032

Actual sound length is according to Window Movie Maker 0,5s.

length / frequency = 0,51

Oscar Giner
TeaRDoWN said:

al_get_sample_channels(sound) = 16

The value returned by al_get_sample_channels is not the number of channels directly, but one of these constants:



Yes, but I just tried to figure out which function raynebc wanted me to use when he wrote al_get_channel_count(sample).


Off the top of my head, I don't know which function you should use, but whichever one directly returns the number of channels instead of a macro defined value. I did forget to mention dividing by 1000.

Thread #608628. Printed from