Audio frequency band visualizer with agent state animations. Renders a row of animated bars driven by a MediaStream via Web Audio's AnalyserNode, with built-in demo mode.
Audio Frequency Visualizer
Real-time frequency band visualization with animated state transitions
Installation
npx shadcn-svelte@latest add https://sv11.ui.twango.dev/r/bar-visualizer.json Usage
<script lang="ts">
import { BarVisualizer } from "$lib/registry/ui/bar-visualizer";
</script>
<BarVisualizer /> Examples
Basic Usage
Pipe a live MediaStream into the visualizer. The FFT slice is analysed on every frame and mapped onto barCount bars.
<script lang="ts">
import { BarVisualizer } from "$lib/registry/ui/bar-visualizer";
let stream: MediaStream | null = $state(null);
</script>
<BarVisualizer state="listening" barCount={15} mediaStream={stream} /> Demo Mode
Set demo to swap live audio for a synthetic oscillating pattern. Handy for previews where no microphone is available.
<BarVisualizer state="speaking" demo centerAlign /> Agent State
Drive the highlight sequence and animation cadence from an agent lifecycle value. "thinking" pulses, "connecting" sweeps, "listening" and "speaking" emphasise the active bars.
<script lang="ts">
import { BarVisualizer, type AgentState } from "$lib/registry/ui/bar-visualizer";
let state: AgentState = $state("connecting");
</script>
<BarVisualizer {state} demo /> Height Range
Tune the usable vertical band by adjusting minHeight and maxHeight (both percentages of the container).
<BarVisualizer state="speaking" demo minHeight={15} maxHeight={90} /> API Reference
Notes
- Analysis uses a Web Audio
AnalyserNodewithfftSize: 2048, sampling a mid-range slice of the spectrum and averaging each chunk into a bar. demooverridesmediaStream— the synthetic pattern is always active whendemoistrue, regardless of stream.- The state sequencer runs independently of volume sampling, so
stateanimates even without audio input. - Bars update on
requestAnimationFramewith a throttled diff check (changes under0.01are ignored) to minimise re-renders. centerAlignswaps the flex alignment fromitems-endtoitems-center, growing bars equally above and below the midline.