Avoid additional audio buffer copy if frequency or samples are different from what we requested (#26488)

We pass a requested frequence and sample rate to SDL_OpenAudioDevice(), which will then return
an object that contains what we actually got. Our code properly handles this. However, unless
we explicit tell SDL that we can handle a different frequency or samples, SDL may try to emulate
what we requested, which incurs another copy in our already quite inefficient audio pipeline.

It’s unlikely to be triggered currently, since we still output as 22,050 Hz with 2048 samples
(though theoretically possible if a device cannot go lower than 44,100 Hz), but will become more
pressing once we going higher than that.

Approach discussed with @frozensnowy .
This commit is contained in:
Michael Steenbeek authored and GitHub committed 2026-05-17 17:58:06 +02:00
1 parent caed40c4fc
commit 1aa1d9cdf3
1 file changed
+1 -1
+1 -1
View File
@@ -38,7 +38,7 @@ void AudioMixer::Init(const char* device)
want.userdata = this;
SDL_AudioSpec have;
_deviceId = SDL_OpenAudioDevice(device, 0, &want, &have, 0);
_deviceId = SDL_OpenAudioDevice(device, 0, &want, &have, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE);
_outputFormat.format = have.format;
_outputFormat.channels = have.channels;
_outputFormat.freq = have.freq;