← 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.
accepted values
| You pass | Result |
|---|---|
omitted / undefined | Default null — undefined-length context (Chrome 158+). Engines without the feature throw TypeError (“Required member is undefined”) |
null | Undefined-length context |
| integer ≥ 1 | Finite context of that many frames (classic behaviour) |
0 | NotSupportedError (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.
Create, CreateOfflineAudioContext); explainer; Web IDL
observable behaviour
ctx.lengthreturnsnulland staysnullhowever many chunks you render.startRendering()with no argument renders one render quantum (128 frames by default);startRendering(n)renders n rounded up to a quantum multiple. There is no “remaining frames” limit, so no call is ever rejected for running out of frames.- The context never closes itself and never fires
complete. suspend(t)accepts any future time; the “must be before the total render duration” check is skipped.
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
| Exception | When |
|---|---|
TypeError | Engine 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 |
NotSupportedError | Engine 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 |
NotSupportedError | length is 0 or converts to 0 (Infinity, NaN); also bad numberOfChannels/sampleRate |
InvalidStateError | The 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).
context and exposure
OfflineAudioContextis[Exposed=Window]; no secure-context, permission or activation requirement.- Chromium accepts a missing/
nulllengthonly when theOfflineAudioContextIncrementalRenderingruntime feature is on (status: "stable"in runtime_enabled_features.json5, Chrome 158+). - Detect with
typeof OfflineAudioContext.prototype.close === "function"before relying onlength: null; there is no separate detection for the dictionary change.
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
| Feature | Chrome | Edge | Firefox | Safari | Android / WebView |
|---|---|---|---|---|---|
length: null / omitted (interim, ChromeStatus) | 158 | 158 | No (no BCD key) | No (no BCD key) | 158 |
length attribute (BCD; nullable from 158) | 51 | 14 | 49 | 14.1 | mirrors 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
- Overview — OfflineAudioContext incremental rendering
- MDN — OfflineAudioContext() constructor (documents
lengthas required)