← OfflineAudioContext incremental rendering
v158 · web audio · method reference
OfflineAudioContext.startRendering(chunkSize)
Renders the next chunkSize sample-frames of the context’s audio graph into a new AudioBuffer and returns a promise for it. Call it repeatedly to render a long or open-ended graph a chunk at a time; with no argument on a finite context it behaves exactly like the classic one-shot startRendering().
Syntax
// Web Audio API 1.1, OfflineAudioContext (IDL verbatim)
Promise<AudioBuffer> startRendering(optional unsigned long? chunkSize = null);
// Call forms
ctx.startRendering() // finite: all remaining frames; indefinite: one render quantum
ctx.startRendering(null) // same as omitting chunkSize
ctx.startRendering(chunkSize) // next chunkSize frames, rounded up to the render quantum size
Chromium declares this as two overloads, startRendering() and [RuntimeEnabled=OfflineAudioContextIncrementalRendering] startRendering(unsigned long? chunkSize), both implemented by the same startOfflineRendering routine; the observable call forms are identical to the spec’s single declaration.
Parameters
chunkSizeoptional, nullableunsigned long, defaultnull-
“The number of sample-frames to render during this call.” How the actual
startRenderingbuffer length is derived fromchunkSize:startRendering chunkSize resolution Context lengthchunkSizenull / omittedchunkSize= nfinite remaining frames ( length− committed)n rounded up to a multiple of the render quantum, then trimmed to the remaining frames nullone render quantum n rounded up to a multiple of the render quantum - Render quantum size is 128 frames unless the context was built with an integer
renderSizeHint(then it is that value). So with the default,startRendering(300)resolves with 384 frames; WPT checks 300 → 512 with a 256-frame quantum. - Final chunk trimming. Chromium and the WPT suite trim the last chunk of a finite render to the exact remaining frames (length 1000, chunks of 300 with a 256 quantum resolve 512 then 488). The Editor’s Draft text as fetched applies the round-up after choosing the remaining length, which read literally could overshoot
length; rely on the tested, trimmed behavior. - Type conversion.
chunkSizeis a plain WebIDLunsigned long(no[EnforceRange]): fractions truncate, negative numbers wrap modulo 232 (-1becomes 4294967295) andNaN/Infinitybecome 0 — which then rejects (see exceptions). Pass a positive integer.
- Render quantum size is 128 frames unless the context was built with an integer
CalculateEffectiveChunkSize in Chromium offline_audio_context.cc; WPT offlineaudiocontext-incremental-rendering.html; WebIDL ConvertToInt
Return value
A Promise that fulfills with an AudioBuffer whose numberOfChannels and sampleRate match the context and whose length is the resolved chunk length above. Each startRendering call gets its own new buffer; buffers from successive calls hold consecutive frames of the same render, with no gap and no overlap (WPT feeds an index ramp through the graph and checks chunk k holds frames k×Q through (k+1)×Q−1). Promises fulfil in call order.
Exceptions
startRendering never throws synchronously; every failure is a rejected promise.
| Rejects with | When | Source |
|---|---|---|
InvalidStateError | The document is not fully active. | Spec |
InvalidStateError | The context is "closed" — close() was called, or a finite render already delivered its last chunk. | Spec, Chromium |
InvalidStateError | Finite context whose frames are already all committed to earlier calls, even if those chunks have not finished rendering yet. | Spec, WPT |
NotSupportedError | chunkSize is 0 (including NaN/Infinity after conversion). Chromium: “The requested chunk size cannot be zero.”; the spec reaches the same result because a zero-length AudioBuffer cannot be constructed. | Chromium, spec |
NotSupportedError | Indefinite context where rounding chunkSize up overflows 232−1 frames (“The requested chunk size is too large.”). | Chromium |
NotSupportedError | The chunk’s AudioBuffer cannot be allocated (“startRendering failed to create AudioBuffer(…)”) — choose a smaller chunkSize. | Spec (buffer exception), Chromium |
AbortError | A queued or in-progress chunk is cancelled because close() ran or the context is torn down (“Audio context is going away”). | Chromium, WPT |
Behavior change from Chrome ≤ 157: a second startRendering() call used to reject with InvalidStateError (“cannot call startRendering more than once”). With the flag on, extra calls on a finite context are accepted until every frame is committed.
startOfflineRendering and RejectPendingRenderingResolvers in Chromium offline_audio_context.cc; WPT offlineaudiocontext-close.html
Requirements and context
- Receiver: an
OfflineAudioContext.[Exposed=Window]— there is no worker exposure; Chromium refuses to create the context outside a window. - No permission, user activation or secure context is required.
- The owning document must be fully active.
- Chrome 158+, gated by the
OfflineAudioContextIncrementalRenderingruntime flag (statusstablein runtime_enabled_features.json5). On older builds the argument is ignored and the whole render is returned at once — feature-detect with'close' in OfflineAudioContext.prototype.
Lifecycle and state transitions
- Construction.
stateis"suspended"; nothing is rendered or committed. - Each call allocates its buffer immediately, adds its length to the committed-frame count (finite contexts) and joins a FIFO queue. The first queued call moves
stateto"running"and starts the rendering thread. - Between chunks the graph does not advance: when the queue empties, rendering waits for the next call.
statestays"running"(WPT asserts this);currentTimereflects the frames rendered so far. suspend(t)scheduled at a chunk boundary is not applied until the next chunk starts: the next call suspends before its first quantum,statebecomes"suspended", and that chunk completes afterresume().- Finite completion. Once every frame has been rendered, the context closes:
statebecomes"closed"and later calls reject withInvalidStateError. An indefinite context never completes; end it withclose(). - Re-entrancy. Calling
startRendering()orclose()from inside a chunk’s fulfilment (even synchronously, from athengetter) is safe: the queue is not duplicated and frames are delivered exactly once.
The legacy complete event
oncomplete / the complete event (an OfflineAudioCompletionEvent with a renderedBuffer) is not a whole-render result in chunked mode. Chromium fires it once, after the final chunk of a finite render, and its renderedBuffer is that final chunk only. It never fires for an indefinite context or after close() (WPT checks the latter). The Editor’s Draft text as fetched places the “fire complete” step beside, not inside, the “rendered frames ≥ length” check, so read literally it would fire after every finite chunk. Use the promises, not the event, with startRendering(chunkSize).
OnChunkRendered, IsRenderComplete and FireCompletionEvent in Chromium offline_audio_context.cc; WPT incremental-rendering (state and suspend tests); WPT close
Examples
Render a finite graph in ten-second startRendering chunks
const ctx = new OfflineAudioContext({
numberOfChannels: 2,
sampleRate: 48000,
length: 48000 * 600, // ten minutes
});
const src = new AudioBufferSourceNode(ctx, { buffer: decodedTrack });
src.connect(ctx.destination);
src.start();
const parts = [];
let rendered = 0;
while (rendered < ctx.length) {
const chunk = await ctx.startRendering(48000 * 10); // 480000 frames
rendered += chunk.length;
parts.push(await encodeChunk(chunk)); // keep the encoded bytes, drop the AudioBuffer
}
// ctx.state === "closed"; one more startRendering() would reject (InvalidStateError)
Queue the next chunk before processing the current one
// Calls queue in order, so the render thread can work while you encode.
let next = ctx.startRendering(16384);
for (;;) {
const chunk = await next;
const more = ctx.state !== 'closed' && ctx.currentTime < stopAtSeconds;
if (more) next = ctx.startRendering(16384);
await encodeChunk(chunk);
if (!more) break;
}
if (ctx.state !== 'closed') await ctx.close();
Handling startRendering rejections
try {
const chunk = await ctx.startRendering(chunkSize);
} catch (err) {
if (err.name === 'AbortError') { /* close() cancelled this queued chunk */ }
else if (err.name === 'NotSupportedError') { /* 0, too large, or allocation failed */ }
else if (err.name === 'InvalidStateError') { /* closed or every frame already requested */ }
}
Source: gendn-derived from the startRendering(chunkSize) algorithm and behavior asserted by WPT incremental-rendering; a live chunk console runs in the showcase demo
Browser compatibility
| Browser | chunkSize argument | Notes |
|---|---|---|
| Chrome / Chrome Android / WebView | 158 | Interim: ChromeStatus milestones; BCD has no chunkSize sub-feature (checked 2026-11-22) |
| Edge | Unknown | Chromium-based; not separately recorded |
| Firefox | No | Argument ignored; whole render returned once |
| Safari | No | Argument ignored; whole render returned once |
One-shot startRendering() (no argument) is supported everywhere OfflineAudioContext is — see BCD api/OfflineAudioContext.json.
Security and privacy
Chunking returns the same samples a one-shot render would; it exposes no new data and needs no permission. It narrows a resource-exhaustion risk: the page no longer has to allocate the whole render (the explainer notes that exceeding memory can get the tab terminated), and each chunk’s allocation is bounded by chunkSize. The Web Audio privacy considerations on DSP-output fingerprinting apply unchanged.
Specifications
Web Audio API 1.1 — startRendering(chunkSize), added by WebAudio/web-audio-api#2675. Tracking: ChromeStatus 5109900936085504.