svelte-audio-renderer

Offline rendering

Mount audio components without a page with createRoot, for example into an OfflineAudioContext to render audio to a buffer.

Mount into a context

createRoot({ context }) is a mount target. Top-level nodes connect to that context's destination.

import { flushSync, mount, unmount } from 'svelte';
import { createRoot, renderer } from 'svelte-audio-renderer';
import Synth from './Synth.svelte';

const context = new OfflineAudioContext(2, 44100 * 2, 44100);
const app = mount(Synth, { renderer, target: createRoot({ context }) });
flushSync();

const buffer = await context.startRendering();
await unmount(app);

startRendering() renders as fast as the CPU allows. The Offline render example renders a chord and plays the buffer back.

In Node and in tests

With a Web Audio implementation for Node, such as node-web-audio-api:

  • Add the svelte-custom-renderer resolve condition.
  • Compile components for the client, not for SSR.
  • For <audioWorklet>, put AudioWorkletNode on globalThis, as browsers do.