Lab

useAudioSignal

An AnalyserNode as live signals for a waveform: the loudest sample of the moment, frequency bands and the raw samples, read every frame without re-rendering

pnpm dlx shadcn@latest add @audio-ui/use-audio-signal
import { useAudioSignal } from "@/hooks/use-audio-signal";
Fileshooks/use-audio-signal.ts
Depends onNothing
npm@audio-ui/engine
Used byNothing
ExportKindWhat it is
AudioSignalSourceinterfaceA live signal of levels, 0-1 each, in the shape of an external store.
AudioSignalinterface
UseAudioSignalOptionsinterface
useAudioSignalfunctionReads an AnalyserNode every animation frame (paused automatically while the tab is in the background) into three live signals: level, the loudest sample of the moment; bands, the spectrum as log-spaced frequency bands; and samples, the raw waveform.

Usage

An AnalyserNode as three live signals for a live waveform: level, the loudest sample of the moment, for a waveform that scrolls; bands, the spectrum as log-spaced bands, for bars that dance; and samples, the raw waveform, for a scope. Each is read only while something is subscribed to it.

import { LiveWaveform } from "@/components/audio-ui/waveform";
import { useAudioSignal } from "@/hooks/use-audio-signal";

const signal = useAudioSignal(analyser, { bandCount: 32 });

<LiveWaveform source={signal.level} />;
<LiveWaveform shape="scope" source={signal.samples} />;

It returns stable sources, so sampling never re-renders the component that calls it; without an analyser they fall silent. See Waveform.

Built by Ouest Labs, the source code is available on GitHub.
RSS