← OfflineAudioContext incremental rendering

v158 · dictionary member + attribute · OfflineAudioContext

Undefined-length rendering: length: null

OfflineAudioContextOptions.length is now nullable and defaults to null. A context created without a length has no end: it renders whatever you ask for with startRendering(chunkSize), chunk after chunk, until you call close(). Its read-only length attribute reports null. New in Chrome 158 (ChromeStatus 5109900936085504).

syntax

new OfflineAudioContext({ numberOfChannels, sampleRate })                 // length omitted → null
new OfflineAudioContext({ numberOfChannels, sampleRate, length: null })
ctx.length   // null for an undefined-length context, otherwise the frame count

Spec IDL (OfflineAudioContextOptions and OfflineAudioContext.length, verbatim excerpts):

dictionary OfflineAudioContextOptions {
    unsigned long? length = null;
    // other members unchanged
};

interface OfflineAudioContext : BaseAudioContext {
    readonly attribute unsigned long? length;
    // other members unchanged
};

Chromium ships the same nullable member (offline_audio_context_options.idl) and attribute (offline_audio_context.idl). The positional constructor OfflineAudioContext(numberOfChannels, length, sampleRate) is unchanged: its length is a non-nullable unsigned long, so undefined-length rendering is only reachable through the options dictionary.

Source: spec dictionary; spec attribute; Chromium dictionary IDL

accepted values

OfflineAudioContextOptions.length
You passResult
omitted / undefinedDefault null — undefined-length context (Chrome 158+). Engines without the feature throw TypeError (“Required member is undefined”)
nullUndefined-length context
integer ≥ 1Finite context of that many frames (classic behaviour)
0NotSupportedError (number of frames below 1)
Infinity (the explainer’s draft spelling)Converts to 0 under Web IDL ConvertToInt, so NotSupportedError. Use null

Other members keep their rules: sampleRate is required and must be in the supported range, numberOfChannels 1–32 in Chromium, renderSizeHint sets the render quantum (and therefore the default chunk) size.

Source: offline_audio_context.cc (Create, CreateOfflineAudioContext); explainer; Web IDL

observable behaviour

Source: startRendering algorithm; offline_audio_context.cc (IsRenderComplete, suspendContext); WPT

the length attribute

OfflineAudioContext.length is a read-only unsigned long?. It returns the length the context was constructed with — a frame count for a finite context, null for an undefined-length one — and does not change as chunks render (WPT asserts both). It takes no input and raises no exception on read. Because it is a getter-only accessor, assigning to it is ignored in sloppy-mode script and throws TypeError in strict mode / modules. Code that did arithmetic on ctx.length must now handle null:

const totalSeconds = ctx.length === null ? Infinity : ctx.length / ctx.sampleRate;
const progress = ctx.length === null ? null : renderedFrames / ctx.length;
Source: spec attribute (“For undefined-length rendering, this attribute SHOULD be set to null”); WPT

errors

Construction errors related to length
ExceptionWhen
TypeErrorEngine without the feature (current Firefox/Safari, Chromium before 158) and length omitted — the member was required. Chromium with the feature disabled throws this for null too
NotSupportedErrorEngine where length is still a non-nullable unsigned long and you pass null: Web IDL converts null to 0, which is rejected as a zero-frame render
NotSupportedErrorlength is 0 or converts to 0 (Infinity, NaN); also bad numberOfChannels/sampleRate
InvalidStateErrorThe document is not fully active

Once constructed, an undefined-length context’s only “length” failure mode is memory: each chunk is still one AudioBuffer, so very large chunkSize values reject with NotSupportedError (see startRendering errors).

Source: offline_audio_context.cc; spec constructor

context and exposure

lifecycle

"suspended" after construction → "running" on the first startRendering → stays "running" (or "suspended" at a scheduled suspend()) for as many chunks as you request → "closed" only when you call close() or the document is torn down. There is no natural end, so you own cleanup: always close the context, for example in a finally block, or its rendering thread lives until the page goes away.

examples

Choose the context shape by feature detection

function makeContext(sampleRate, channels, knownFrames) {
  const incremental = typeof OfflineAudioContext.prototype.close === "function";
  if (!incremental && knownFrames == null) {
    throw new Error("Open-ended offline rendering needs Chrome 158+");
  }
  return new OfflineAudioContext({
    numberOfChannels: channels,
    sampleRate,
    length: incremental ? null : knownFrames,  // null = undefined-length
  });
}

Render until a condition, then close

const ctx = new OfflineAudioContext({ numberOfChannels: 2, sampleRate: 48000 });
console.assert(ctx.length === null);
buildLiveGraph(ctx);
try {
  while (!userStopped) {
    sink.write(await ctx.startRendering(4800));   // 100 ms chunks (rounded to 4864 frames)
  }
} finally {
  await ctx.close();
}
Source: explainer usage example (adapted from its Infinity draft to the shipped null); buildLiveGraph, sink, userStopped are placeholders

browser compatibility

FeatureChromeEdgeFirefoxSafariAndroid / WebView
length: null / omitted (interim, ChromeStatus)158158No (no BCD key)No (no BCD key)158
length attribute (BCD; nullable from 158)51144914.1mirrors Chrome

BCD records the length attribute but has no subfeature for a null value; the interim row uses the ChromeStatus ship milestones.

security and privacy

No new information is exposed: an undefined-length context renders the same samples a finite one would. The new risk is unbounded work — a page can keep a rendering thread busy indefinitely — which is no different from looping one-shot renders, but means pages should close contexts they finish with. Spec Privacy Considerations apply unchanged.

see also