BaseAudioContext: currentTime property

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

The currentTime read-only property of the BaseAudioContext interface returns a double representing the elapsed time in the context's audio timeline in seconds that can be used for scheduling audio playback, visualizing timelines, etc. It starts at 0.

While the context is running, this value advances in increments of one audio rendering block, or render quantum, and represents the start of the next block to be processed. For a block of 128 sample frames, this is approximately 2.9 ms at a sampleRate of 44.1 kHz, or 2.7 ms at 48 kHz. Repeated reads can return the same value until the next block is processed.

This audio timeline is separate from the system clock used by Date.now(). It stops advancing while the context is suspended. For an OfflineAudioContext, it advances as audio is rendered, independently of elapsed real time.

Value

A floating-point number representing the current time in seconds.

Examples

js
const audioCtx = new AudioContext();
// Older webkit/blink browsers require a prefix

// …

console.log(audioCtx.currentTime);

Reduced time precision

To offer protection against timing attacks and fingerprinting, the precision of audioCtx.currentTime may be reduced depending on browser settings.

The value of audioCtx.currentTime is based on the number of audio sample frames processed. In Chrome and Safari, the browser does not apply additional timer rounding to this value.

In Firefox, the privacy.reduceTimerPrecision preference is enabled by default and uses a rounding interval of 1 ms, or 0.02 ms in cross-origin-isolated contexts. However, Firefox first compares the duration of a 128-frame audio block with the interval configured by privacy.resistFingerprinting.reduceTimerPrecision.microseconds (1 ms by default), regardless of cross-origin isolation. If the block duration is greater, Firefox skips timer rounding. At common sample rates such as 44.1 kHz and 48 kHz, the default value therefore follows the audio block timing rather than being rounded.

If privacy.resistFingerprinting is enabled, the rounding interval is 16.667 ms or the interval configured by privacy.resistFingerprinting.reduceTimerPrecision.microseconds, whichever is larger. In this case, the audio block duration check also uses this larger interval.

For example, these are possible values in Firefox:

js
// Audio block timing at 48 kHz with default settings
audioCtx.currentTime;
// Might be:
// 0.0026666666666666666
// 0.005333333333333333
// 0.008
// …

// Reduced time precision with `privacy.resistFingerprinting` enabled
audioCtx.currentTime;
// Might be:
// 0.050001
// 0.066668
// 0.083335
// …

Specifications

Specification
Web Audio API
# dom-baseaudiocontext-currenttime

Browser compatibility

See also