WebAudio / web-audio-api

The Web Audio API v1.0, developed by the W3C Audio WG
https://webaudio.github.io/web-audio-api/
Other
1.04k stars 165 forks source link

Average AudioContext.outputLatency #2563

Closed henbos closed 3 months ago

henbos commented 6 months ago

Describe the feature Output latency is an instantaneous value. Some apps might be interested in the average, minimum and maximum output latency over an app-defined interval without having to do high frequency polling. Avoiding high frequency polling when not needed should not only be convenient but also have performance benefits.

Is there a prototype? No but outputLatency is shipped so shimming based on high frequency polling is possible.

Describe the feature in more detail Output latency can be measured via AudioContext.outputLatency, which can be useful for synchronization use cases when polling at high frequency:

The outputLatency attribute value may change while the context is running or the associated audio output device changes. It is useful to query this value frequently when accurate synchronization is required.

In Chromium, this can change all the time.

But apps may want to monitor this less frequently in which case an infrequent poll of outputLatency may not be as representative. In MediaStreamTrackAudioStats, the instantaneous latency value is complemented with a min, max and average latency since the last time the app called resetLatency(), allowing the app to easily control what periods of time this should be measured over.

Should WebAudio use the same pattern for its latency? Proposal:

partial interface AudioContext {
   // Today
   readonly attribute double outputLatency;
   // Proposal
   readonly attribute double averageOutputLatency;
   readonly attribute double minimumOutputLatency;
   readonly attribute double maximumOutputLatency;
   undefined resetOutputLatency();
};
henbos commented 6 months ago

WDYT @o1ka @hoch @padenot ?

chrisguttandin commented 6 months ago

I was curious how fluctuant the values are in practice. I built a little demo to check it.

https://stackblitz.com/edit/js-jfkhex?file=index.js,index.html

When testing this on macOS with the internal speakers I get a constant outputLatency value for Chrome and Firefox. The demo doesn't work in Safari because it doesn't implement outputLatency and has a bug in its getOutputTimestamp() implementation.

My test is obviously very simple and not really a real life audio app. @henbos Is there anything obvious I could do to provoke a changing outputLatency?

o1ka commented 6 months ago

@chrisguttandin non-default AudioContextOptions should help to trigger outputLatency changes (ex.: 44.1 kHz sample rate, "playback" latency hint).

o1ka commented 6 months ago

@henbos SG, though I'd rather call it resetOutputLatencyStats(), to not confuse with outputLatency.

hoch commented 3 months ago

Relatedly https://github.com/WICG/proposals/issues/142

hoch commented 3 months ago

Since this idea is pursued by the WICG issue link, I believe we can close this.