Calculating audio buffer

Hi there, I am a little confused by the various elements of
SDL_AudioSpec structure. Afeter I decode an audio frame (aac, g711
etc) I know how many channels, sample rate, of the raw audio
bitstream, so I use these parameters to create an SDL audio renderer,

SDL_AudioSpec wanted, ret;

    wanted.freq = sampleRate;
    wanted.format = AUDIO_S16LSB; //AUDIO_U16LSB;
    wanted.silence = 0;
    wanted.channels = numChannels;
    wanted.samples = SAMPLESIZE;
    wanted.callback = fill_audio_cb;
    wanted.userdata = pSDLContext;


    SDL_Init(SDL_INIT_AUDIO |  SDL_INIT_NOPARACHUTE);


    SDL_OpenAudio(&wanted, &ret);

   What should I do if I ret.freq or ret.channels is not what I

passed? What does ret.samples or ret.size
represent? I create a circular buffer at this point and while
fill_audio_cb asks for certain size data, I notice
what I get from the source is not the same sometimes. How do I
know how much data fill_audio_cb
requires ahead of time, to make sure it will not be starving or
overrun with more data it needs? Is there
a formula to calculate the raw data size based on sample rate,
format, number of channel?

Thanks,

Eric

Eric Glaser wrote:

What should I do if I ret.freq or ret.channels is not what I passed?

Normally when you’re generating audio you do some calculations on those numbers to figure out what to put in the buffer. If you’re just e.g. playing a simple wave file, this might mean adding a few hundred lines of C to resample the audio and fill out channels you’re not using.

I don’t know about SDL 1.2, but in SDL 1.3, if you ask, the audio driver will do the conversion for you – it’s inefficient, but simple. On the other hand, if you’re using SDL 1.2, there are a few bolt-on audio libraries that let you do higher-level audio programming so you don’t have to deal with the streaming yourself.

Eric Glaser wrote:

What does ret.samples or ret.size represent? I create a circular buffer at this point and while fill_audio_cb asks for certain size data, I notice what I get from the source is not the same sometimes. How do I know how much data fill_audio_cb requires ahead of time, to make sure it will not be starving or overrun with more data it needs? Is there a formula to calculate the raw data size based on sample rate, format, number of channel?

ret.size is the number of bytes in the buffer passed into fill_audio_cb. ret.samples is a misnomer – it’s actually the number of frames in that same buffer. So, if you’re double-buffering in your circular buffer, you need 2 * ret.size bytes.

ret.size = ret.samples * ret.channels * sizeof (SampleType) where SampleType is the type used to represent a sample: uint8_t, int16_t, int32_t, or float usually.

Sample rate (freq) doesn’t figure into the formula, the buffer is the same size but fill_audio_cb will just get called more often for a higher sample rate. (Usually samples is larger for higher sample rates, but the relationship is not direct. Generally it’s more related to the OS/hardware.)

I am using SDL 1.3 , with network audio data. Thanks for clarifying this to
me!On Mon, Sep 27, 2010 at 6:48 PM, jlunderville wrote:

Eric Glaser wrote:

What should I do if I ret.freq or ret.channels is not what I passed?

Normally when you’re generating audio you do some calculations on those
numbers to figure out what to put in the buffer. If you’re just e.g. playing
a simple wave file, this might mean adding a few hundred lines of C to
resample the audio and fill out channels you’re not using.

I don’t know about SDL 1.2, but in SDL 1.3, if you ask, the audio driver
will do the conversion for you – it’s inefficient, but simple. On the other
hand, if you’re using SDL 1.2, there are a few bolt-on audio libraries that
let you do higher-level audio programming so you don’t have to deal with the
streaming yourself.

Eric Glaser wrote:

What does ret.samples or ret.size represent? I create a circular buffer at
this point and while fill_audio_cb asks for certain size data, I notice what
I get from the source is not the same sometimes. How do I know how much data
fill_audio_cb requires ahead of time, to make sure it will not be starving
or overrun with more data it needs? Is there a formula to calculate the raw
data size based on sample rate, format, number of channel?

ret.size is the number of bytes in the buffer passed into fill_audio_cb.
ret.samples is a misnomer – it’s actually the number of frames in that same
buffer. So, if you’re double-buffering in your circular buffer, you need 2 *
ret.size bytes.

ret.size = ret.samples * ret.channels * sizeof (SampleType) where
SampleType is the type used to represent a sample: uint8_t, int16_t,
int32_t, or float usually.

Sample rate (freq) doesn’t figure into the formula, the buffer is the same
size but fill_audio_cb will just get called more often for a higher sample
rate. (Usually samples is larger for higher sample rates, but the
relationship is not direct. Generally it’s more related to the OS/hardware.)


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org