← 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.

Source: Web Audio API 1.1 §1.3.3 — startRendering(chunkSize); Chromium offline_audio_context.idl

Parameters

chunkSize optional, nullable unsigned long, default null

“The number of sample-frames to render during this call.” How the actual startRendering buffer length is derived from chunkSize:

startRendering chunkSize resolution
Context lengthchunkSize null / omittedchunkSize = n
finiteremaining frames (length − committed)n rounded up to a multiple of the render quantum, then trimmed to the remaining frames
nullone render quantumn 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. chunkSize is a plain WebIDL unsigned long (no [EnforceRange]): fractions truncate, negative numbers wrap modulo 232 (-1 becomes 4294967295) and NaN/Infinity become 0 — which then rejects (see exceptions). Pass a positive integer.
Source: startRendering(chunkSize) algorithm and argument table; render quantum size; 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.

Source: startRendering(chunkSize) — “Resolve the promise created by startRendering() with buffer”; WPT offlineaudiocontext-incremental-rendering.html

Exceptions

startRendering never throws synchronously; every failure is a rejected promise.

startRendering(chunkSize) rejections
Rejects withWhenSource
InvalidStateErrorThe document is not fully active.Spec
InvalidStateErrorThe context is "closed" — close() was called, or a finite render already delivered its last chunk.Spec, Chromium
InvalidStateErrorFinite context whose frames are already all committed to earlier calls, even if those chunks have not finished rendering yet.Spec, WPT
NotSupportedErrorchunkSize 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
NotSupportedErrorIndefinite context where rounding chunkSize up overflows 232−1 frames (“The requested chunk size is too large.”).Chromium
NotSupportedErrorThe chunk’s AudioBuffer cannot be allocated (“startRendering failed to create AudioBuffer(…)”) — choose a smaller chunkSize.Spec (buffer exception), Chromium
AbortErrorA 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.

Source: startRendering(chunkSize) algorithm steps 1–7; startOfflineRendering and RejectPendingRenderingResolvers in Chromium offline_audio_context.cc; WPT offlineaudiocontext-close.html

Requirements and context

Source: Web Audio API 1.1 §1.3 IDL; Chromium offline_audio_context.cc; explainer

Lifecycle and state transitions

  1. Construction. state is "suspended"; nothing is rendered or committed.
  2. 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 state to "running" and starts the rendering thread.
  3. Between chunks the graph does not advance: when the queue empties, rendering waits for the next call. state stays "running" (WPT asserts this); currentTime reflects the frames rendered so far.
  4. suspend(t) scheduled at a chunk boundary is not applied until the next chunk starts: the next call suspends before its first quantum, state becomes "suspended", and that chunk completes after resume().
  5. Finite completion. Once every frame has been rendered, the context closes: state becomes "closed" and later calls reject with InvalidStateError. An indefinite context never completes; end it with close().
  6. Re-entrancy. Calling startRendering() or close() from inside a chunk’s fulfilment (even synchronously, from a then getter) 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).

Source: begin offline rendering steps; 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

startRendering(chunkSize) support
BrowserchunkSize argumentNotes
Chrome / Chrome Android / WebView158Interim: ChromeStatus milestones; BCD has no chunkSize sub-feature (checked 2026-11-22)
EdgeUnknownChromium-based; not separately recorded
FirefoxNoArgument ignored; whole render returned once
SafariNoArgument ignored; whole render returned once

One-shot startRendering() (no argument) is supported everywhere OfflineAudioContext is — see BCD api/OfflineAudioContext.json.

Source: ChromeStatus API record; BCD; webstatus.dev

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.

Source: Web Audio API 1.1 §8; explainer

Specifications

Web Audio API 1.1 — startRendering(chunkSize), added by WebAudio/web-audio-api#2675. Tracking: ChromeStatus 5109900936085504.