Repository navigation
Description
Problem
My apps draw the sound they play: a music player with a spectrum, and a game whose animations follow the music. To show what is being heard, an app needs to know how long its sound takes to reach the listener after a player hands it over. oto has no way to tell it.
With the phone's speaker the delay is small, so a guess works. With Bluetooth earbuds it is about a quarter of a second on my Android phone, and the animations run visibly ahead of the sound.
Together with Claude we measured it on Android, from the AAudio stream's timestamp, which counts the earbuds' own delay:
| oto's fifo | The stream | Total | |
|---|---|---|---|
| Sony WF-1000XM4 earbuds (LDAC) | 40–80 ms | 186–207 ms | 240–280 ms |
| Phone speaker | 12–14 ms | 13–15 ms | about 27 ms |
Reproduction
- Play music through oto on an Android phone, and draw something that follows it, such as a level meter.
- With Bluetooth earbuds connected, the drawing runs about 250 ms ahead of the sound.
- On the speaker, it lines up.
Expected behavior
An app can ask oto how late its sound is heard, and draw in step with it on any output.
Possible Solution
#307 adds Context.OutputLatency() (time.Duration, bool), reported on Android. With it, both my apps stay in step on the earbuds and on the speaker.
Other platforms return false for now. We can implement and test it on:
- Windows (WASAPI): from
IAudioClockagainst the frames written. We'll check with Bluetooth earbuds whether it counts their delay. - Linux: from PulseAudio's latency query, and
snd_pcm_delayon ALSA. - Web: from
AudioContext.outputLatencyandbaseLatency.
We have no Mac or iPhone (yet), so macOS and iOS we could write but not test.
How would you like it: all of these in #307, #307 as the API with Android and the others following one PR each, or something else? And should macOS and iOS return false until someone with the hardware takes them, or would you rather have an untested implementation to check?
Environment
- oto v3.5.1, and
mainat a473742 - Android 16, Pixel 8 Pro
- Sony WF-1000XM4 earbuds (LDAC)
- go1.27.1
#307 is merged (4d4c9d1), so here is where this stands.
The API ended up different from the one proposed above. Instead of Context.OutputLatency, there is a player method:
// UnplayedSize returns the byte size of the data read from the source that the audio hardware has not played yet:
// the buffered data, and the data sent to the audio hardware but not played yet, also after Pause and after the end
// of the source. While the platform does not report how long the audio hardware takes to play, UnplayedSize returns
// the same as BufferedSize. After Seek or Reset, the data sent before it is not counted.
func (p *Player) UnplayedSize() intThe part of the source being played now is the byt…
Activity
hajimehoshi commentedon Oct 8, 2026
Is this a proposal for a new API?
marrasen commentedon Oct 8, 2026
Yes, a way to read the real latency added by accessories such as true wireless buds. We have tested this on android, and I can get my apps to sync perfectly to audio even when the wireless buds adds a quarter of a second to the latency.
So it's a proposal to add this to the API and implement it for all platforms, the problem is that I don't have access to any apple hardware (yet), so the question for you was if it's ok to provide implementation for Android, Windows and Linux for now, or if we should wait until we have access to apple hardware as well, which we plan on getting some time in the future.
hajimehoshi commentedon Oct 8, 2026
I see. Let me have time to think about this. Thank you for the proposal!
oto: add Player.UnplayedSize (#307)
hajimehoshi commentedon Nov 8, 2026
#307 is merged (4d4c9d1), so here is where this stands.
The API ended up different from the one proposed above. Instead of
Context.OutputLatency, there is a player method:The part of the source being played now is the bytes read from it less
UnplayedSize. This stays right afterPlay, afterPauseand at the end of the source, where a fixed latency would be off. A separate latency API is left out for now, and we can come back to it if a use comes up thatUnplayedSizedoes not cover.Platforms
Only Android reports so far: from the stream's timestamps on AAudio, and from the stream's buffer size on OpenSL ES. Everywhere else,
UnplayedSizereturns the same asBufferedSize.To report, a driver calls
Mux.SetPlayed(frame, at)at least once a second while playing: frameframeof the mix, counting from the first frame read withReadFloat32s, is played at timeat.Mux.ClearPlayedclears the report, as when the stream is paused or replaced. Each driver has a TODO naming what it could use:IAudioClock::GetPositionreports.IAudioClockis not bound yet, and whether its position counts Bluetooth earbuds' delay needs checking.waveOutGetPositionreports. It is not bound yet.jfreymuth/pulse'sPlaybackStreamhas no latency query, butClient.RawRequestwithproto.GetPlaybackLatencyfor the stream'sStreamIndexreturns the sink's latency and the stream's read and write indices.snd_pcm_delay, plus what the driver has read and not yet written. It is not bound yet.AudioQueueGetCurrentTime, plus the output device's latency, which on iOSAVAudioSession'soutputLatencygives.AudioQueueGetCurrentTimeis not bound yet.AudioContext.getOutputTimestampgives the context time of the frame played now, and the frames queued in the worklet or the script processor are played after it.Consoles would need the console proxy to expose it, and are not planned here.
One PR per platform would be easiest to review. @marrasen, you offered to take Windows, Linux and Web. Would you still like to? I can take macOS and iOS.
This comment was drafted with Claude (Claude Code).
[-]Report the true latency of the sound, so apps can keep visuals in sync[/-][+]oto: count the audio hardware's delay in Player.UnplayedSize on platforms other than Android[/+][-]oto: count the audio hardware's delay in Player.UnplayedSize on platforms other than Android[/-][+]oto: count the audio hardware's delay in Player.UnplayedSize[/+]marrasen commentedon Nov 8, 2026
Yes, gladly! Thanks for laying out each platform. I'll take them one PR each, starting with Web, then Linux (PulseAudio and ALSA), then Windows (WASAPI, and WinMM if you'd like it too). Windows will take a little longer, as I'll check it on real machines with Bluetooth earbuds. Thank you for taking macOS and iOS.
Reply written by Claude (Anthropic), an AI agent, and posted at Marcus's request.
oto: drop a PulseAudio report measured before Suspend
oto: drop browser reports that arrive while suspended
oto: drop worklet reports from before a Suspend after Resume
oto: drop browser reports by the output timestamp's age
oto: say where the worklet gets its report generation
oto: report when frames are played on browsers (#332)
oto: report played frames on PulseAudio and ALSA (#333)
oto: a WASAPI starvation check for ebitengine#311, not for merging
oto: report played frames on macOS and iOS