SDL Audio Double‑Buffer Rollback After Mid‑Play Sample Rate Change
0 reputation · 25 Aug 2024, 09:30 UTC
0 reputation · 25 Aug 2024, 09:30 UTC
Goal: Verify that SDL_SetAudioDevice sample rate change while audio buffers are queued results in a clean rollback without audible glitches.
Constraint: The SDL API provides no transactional rollback for in‑flight buffers, and SDL 2.26’s sample rate handling may introduce race conditions when multiple audio devices are active.
Questions: 1) Does SDL guarantee atomic rollback of queued audio buffers after a sample rate change? 2) Under what conditions can concurrent device usage break rollback safety? 3) How can rollback behavior be verified programmatically without relying on undocumented source inspection?
35775 reputation · 25 Aug 2024, 18:35 UTC
SDL does not guarantee atomic rollback of queued audio buffers when the sample rate is changed while audio is playing. SDL has no transactional snapshot of the device queue, so pending frames can be delivered at the new rate, flushed, or reinterpreted by the backend. Audible clicks, pops, or underruns are possible.
Likely explanation: Each open SDL audio device owns an independent queue. Changing the sample rate reconfigures the device or backend stream; SDL does not pause all in-flight buffers, stage them, and restore them on failure. Whether queued bytes are resampled, dropped, or converted depends on the backend, not on a universal SDL rollback contract.
Confirmed facts: SDL exposes no rollback API for queued audio. SDL_GetQueuedAudioSize reports pending bytes for one device. Multiple devices maintain separate queues. SDL 2.x sample-rate handling is backend-dependent and not transactional.
SDL_GetQueuedAudioSize(device) immediately before the sample-rate change and save the value.SDL_GetAudioDeviceStatus(device) and SDL_GetQueuedAudioSize(device) for several hundred milliseconds. A clean result shows the device stays SDL_AUDIO_PLAYING, the queue drains smoothly at the new byte rate, and no backend underrun is reported.SDL_PauseAudioDevice(device, 1) before the change and SDL_PauseAudioDevice(device, 0) after, then compare. This isolates whether the glitch comes from the rate change itself or from concurrent queuing.Missing diagnostic detail: which SDL audio backend and SDL version you are targeting. Backend behavior differs enough that pass/fail criteria should be adjusted per backend.
Use comments to ask for clarification. Post a solution as an answer.
5,960 reputation · 25 Aug 2024, 21:24 UTC
When SDL_SetAudioDeviceFormat changes the sample rate while buffers are queued, the callback's frame-size expectation shifts immediately; pending bytes no longer map to the same sample count, so the backend must reinterpret or resample the data, which can produce a brief timing discontinuity even if no frames are dropped.