Skip to content

oto: count the audio hardware's delay in Player.UnplayedSize #311

Open

Description

@marrasen

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

  1. Play music through oto on an Android phone, and draw something that follows it, such as a level meter.
  2. With Bluetooth earbuds connected, the drawing runs about 250 ms ahead of the sound.
  3. 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 IAudioClock against the frames written. We'll check with Bluetooth earbuds whether it counts their delay.
  • Linux: from PulseAudio's latency query, and snd_pcm_delay on ALSA.
  • Web: from AudioContext.outputLatency and baseLatency.

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 main at a473742
  • Android 16, Pixel 8 Pro
  • Sony WF-1000XM4 earbuds (LDAC)
  • go1.27.1
Pinned by hajimehoshi

Activity

  1. hajimehoshi commented on Oct 8, 2026

    @hajimehoshi
    Member

    Is this a proposal for a new API?

  2. marrasen commented on Oct 8, 2026

    @marrasen
    ContributorAuthor

    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.

  3. hajimehoshi commented on Oct 8, 2026

    @hajimehoshi
    Member

    I see. Let me have time to think about this. Thank you for the proposal!

  4. added this to the v3.6.0 milestone on Oct 8, 2026
  5. added a commit that references this issue on Nov 8, 2026
  6. hajimehoshi commented on Nov 8, 2026

    @hajimehoshi
    Member

    #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() int

    The part of the source being played now is the bytes read from it less UnplayedSize. This stays right after Play, after Pause and 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 that UnplayedSize does 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, UnplayedSize returns the same as BufferedSize.

    To report, a driver calls Mux.SetPlayed(frame, at) at least once a second while playing: frame frame of the mix, counting from the first frame read with ReadFloat32s, is played at time at. Mux.ClearPlayed clears the report, as when the stream is paused or replaced. Each driver has a TODO naming what it could use:

    • Windows, WASAPI: the frames written less the position IAudioClock::GetPosition reports. IAudioClock is not bound yet, and whether its position counts Bluetooth earbuds' delay needs checking.
    • Windows, WinMM: the frames written less the position waveOutGetPosition reports. It is not bound yet.
    • Linux, PulseAudio: jfreymuth/pulse's PlaybackStream has no latency query, but Client.RawRequest with proto.GetPlaybackLatency for the stream's StreamIndex returns the sink's latency and the stream's read and write indices.
    • Linux, ALSA: snd_pcm_delay, plus what the driver has read and not yet written. It is not bound yet.
    • macOS and iOS: the frames enqueued less the queue's position from AudioQueueGetCurrentTime, plus the output device's latency, which on iOS AVAudioSession's outputLatency gives. AudioQueueGetCurrentTime is not bound yet.
    • Web: AudioContext.getOutputTimestamp gives 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).

  7. changed the title [-]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[/+] on Nov 8, 2026
  8. changed the title [-]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 Nov 8, 2026
  9. marrasen commented on Nov 8, 2026

    @marrasen
    ContributorAuthor

    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.

  10. added a commit that references this issue on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions