svelte-audio-renderer

svelte-audio-renderer

A Svelte custom renderer for the Web Audio API. Tags are audio nodes, attributes are their properties, and nesting is connect().

Synth.svelte
<svelte:options customRenderer="svelte-audio-renderer" />

<script lang="ts">
	let { cutoff = 800 } = $props();
</script>

<gain gain={0.2}>
	<biquadFilter type="lowpass" frequency={cutoff} Q={4}>
		<oscillator type="sawtooth" frequency={110}></oscillator>
	</biquadFilter>
</gain>

Change cutoff and the filter follows. Wrap a node in {#if} and it starts and stops. Reuse a chain as a component like any other.

It is a 1:1 mapping of the Web Audio API and adds no concepts of its own. What you know about Web Audio applies here, and every tag and attribute maps back to a Web Audio call.

pnpm add svelte-audio-renderer

Custom renderers are experimental in Svelte (svelte@^5.59), and their API may change before the flag is removed.

Features

  • Every node. All the create…() methods of BaseAudioContext are elements, including audioWorklet.
  • Reactive params. Attributes set AudioParams and properties, and removing one restores its default.
  • Lifecycle built in. Sources start when they are added and stop when they are removed. Every node disconnects on removal.
  • Modulation. param="frequency" connects a child into its parent's AudioParam.
  • Typed. svelte-check checks every tag and attribute against TypeScript's Web Audio types.
  • SSR safe and offline ready. Pages render on the server, and the same components render into an OfflineAudioContext.

Next steps