Why is the SDL_MixAudio volume 128?

And why should SDL_AudioSpec.samples be a power of 2? I did 1000 and everything worked…

Generally that’s what hardware wants, but SDL can manage this, so it might be quietly buffering data in between what the app is providing and what the hardware is consuming. Slightly less efficient is better than doesn’t work at all, in this case.

Because I know … If it actually causes slowdown, it’s probably better to display the appropriate error …

If you’re using SDL_OpenAudioDevice, have you checked that your desired value of samples is the value you obtained?

Looking at the code in SDL_audio.c it looks like it will at least sometimes change the value to a power of two if necessary.

/* For backends that require a power-of-two value for spec.samples, take the
 * value we got from 'desired' and round up to the nearest value
 */
if (!current_audio.impl.SupportsNonPow2Samples && device->spec.samples > 0) {
    device->spec.samples = SDL_powerof2(device->spec.samples);
}

How to check ? len in callback is samples*4, even if samples=500. It may not matter to me… but such rounding can cause undesirable effects…

SDL_OpenAudioDevice() takes two SDL_AudioSpec pointers; one for what you want, and in the other it tells you what you got. That’s how you can check.

I open it with SDL_OpenAudio. I give 260, len=1040 and no errors come out… If it rounded to 256, it should be overflowing.

It rounds up, not down.

Also note the last argument “allowed_changes”.

allowed_changes can have the following flags OR’d together:

  • SDL_AUDIO_ALLOW_FREQUENCY_CHANGE
  • SDL_AUDIO_ALLOW_FORMAT_CHANGE
  • SDL_AUDIO_ALLOW_CHANNELS_CHANGE
  • SDL_AUDIO_ALLOW_SAMPLES_CHANGE
  • SDL_AUDIO_ALLOW_ANY_CHANGE

These flags specify how SDL should behave when a device cannot offer a specific feature. If the application requests a feature that the hardware doesn’t offer, SDL will always try to get the closest equivalent.

So if you pass SDL_AUDIO_ALLOW_ANY_CHANGE then it might give you a different format than you requested,

but if you pass 0 it will always give you the same format that you requested but this means it might have to convert your data behind the scenes to something that the hardware supports which will obviously be slower.

I don’t change anything… If it rounds up, I put 1040 bytes instead of 2048, there should be audio distortion.