Skip to content

Account for output latency and recover from underruns in OpenAL - #784

Open
rcombs wants to merge 13 commits into
TypesettingTools:masterfrom
rcombs:codex/openal-improvements
Open

rcombs wants to merge 13 commits into
TypesettingTools:masterfrom
rcombs:codex/openal-improvements

Conversation

@rcombs

@rcombs rcombs commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

The OpenAL playback cursor could run ahead of the sound on high-latency outputs such as Bluetooth. It could also keep advancing through an underrun while the source stayed stopped.

This series uses the OpenAL source offset and accounts for output delay. It reads the source latency extension when available and uses a Core Audio estimate with Apple's OpenAL framework. Playback resumes after an underrun, drains through the requested endpoint, and refreshes queued buffers when the selection changes. The series also cleans up source teardown and buffer sizing.

Validation: macOS build, Meson test suite, and native Apple OpenAL regression tests with injected output latency, queue starvation, and selection endpoint changes. Still needs testing on Linux with OpenAL Soft.

🤖 Codex

On Apple's OpenAL, deleting the source already stops playback, so
the old teardown did stop the sound. The subsequent alSourceStop and
alSourcei calls could no longer change anything. They set an OpenAL
error after the current context had been cleared, which Stop ignored.
The next InitContext cleared the error before creating buffers.

Stop and detach explicitly while both the source and context are
valid, and remove the invalid calls after teardown.
Play creates the context before starting the timer. If a pending
timer event is delivered after Stop, InitContext in Notify can open
a fresh context and allocate a source and buffers while playback is
idle. Those resources are then kept until the next cleanup.

Check whether playback is active before accessing its context.
Stop used to release the audio resources before cancelling the
timer that drives them. The callback now ignores idle playback,
but the timer can still schedule unnecessary work during teardown.

Cancel the timer first, including when playback is already marked
inactive. TeardownContext handles the case where no context remains.
A delayed UI timer can let OpenAL consume every queued buffer. OpenAL
then stops the source, and adding buffers alone does not restart it.
Preview can go silent for the rest of the playback session.

Restart a stopped source after the refill so playback can resume.
Neither caller can request zero today. Play primes the full queue,
and Notify calls FillBuffers only after reclaiming a positive number
of processed buffers. This is a defensive correction.

If a caller passes zero, for example by forwarding the processed
count before any buffers are ready, the old lower bound still enters
the loop. With a full queue it tries to refill a buffer still in use
and advances the queue indices and free count even if OpenAL rejects
the operation.

Cap the request at the free count and let the loop skip zero.
Calculating the byte count first can leave a partial frame even for
16-bit mono. At 44110 Hz the old calculation gives 5513 bytes per
buffer. The player can only fill 5512 bytes with complete samples,
but submits all 5513. OpenAL implementations that require whole PCM
frames can reject the upload, leaving the queue without that audio.

Round down in frames so the buffer size matches the audio it holds.
The old cursor extrapolated from the last UI refill, so a late timer
could make it jump when buffers were reclaimed. It also advanced
while the mixer was stalled.

Add the source offset to the number of frames already unqueued.
When an underrun resets the source offset, use the processed buffer
count to recover it. Keep the last position when playback stops.
The source offset describes the mixer, which can run ahead of the
speakers. Query AL_SOFT_source_latency to get the offset and the
delay measured for that offset in the same call. After an underrun,
allow for the audio that can still drain from the output pipeline.
Apple's OpenAL framework lacks AL_SOFT_source_latency. Its source
offset can lead the sound by the device's output delay, particularly
with Bluetooth headphones. Use Core Audio's default output device
properties to estimate that delay in seconds.

Cache the hardware property queries for 100 ms and read the source
offset on every position query. Refresh the estimate during playback
to pick up changes to the output route.
The extra round of buffers used to approximate when playback could
stop. Its length was unrelated to device latency, so it could leave
the source running after the audio ended or cut off the tail on a
slow output.

Keep queuing silence until the latency-adjusted position reaches the
end of the range. Check every 20 ms to keep the extra wait short.
The remaining range is an int64_t. Passing it to mid<ALsizei>
narrows it before applying the buffer limit. A range longer than
INT32_MAX frames can therefore be read as a negative or short count,
causing silence to be queued in place of the requested audio. This
first occurs at 2^31 remaining frames, about 12 h 25 min at 48 kHz
or 6 h 13 min at 96 kHz.

Clamp the count in 64 bits, then store the buffer-sized result.
A shorter selection can leave audio in the queue beyond the new
endpoint. An extension can leave silence in the queue where newly
selected audio should play.

Advance cur_frame by the full buffer size so it tracks the end of
the queue, including zero padding. When an endpoint edit touches
already queued frames, refill from the current audible position.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant