Analyzer
A spectrum analyzer: native streams carry the audio thread’s spectrum to a React UI.
Source: examples/analyzer/.
Run this page’s package-local commands from examples/analyzer/, unless noted otherwise.
examples/analyzer in the web runtime. Press ▶ in the bar under the plugin to hear it.Open full pageSourceA spectrum analyzer: the plugin's audio code publishes the spectrum and the peak of what it hears through native streams, and the React UI draws the newest once a frame. The same UI runs in both runtimes.
soundor.config.ts identity, both runtimes, two native streams:
`spectrum` (buffer<float32>, 128 bands) and `peak`
src/App.tsx the plugin UI: draws each spectrum into a Canvas,
shows the peak with useNativeStream
src/spectrum.ts decibels to bar heights, bars that fall slowly
lib/ the analysis, in Rust: an FFT on the audio thread,
written straight into the stream (src/lib.rs)The config declares what the audio code publishes:
native: {
streams: {
spectrum: { type: 'buffer<float32>', maxLength: 128 },
peak: { type: 'float32' },
},
},The audio thread publishes
lib/src/processor.rs keeps the last 2048 samples, and every 1024 it windows
them, runs an FFT (lib/src/fft.rs, its tables made in prepare) and
takes the loudest bin of each of 128 bands, written into the stream's own
storage and then published whole:
self.analyze(ctx.streams.spectrum.buffer_mut()); // the bands' levels, in dB
ctx.streams.spectrum.commit(BANDS);
// ...
ctx.streams.peak.publish(decibels(peak));Both calls run on the audio thread: they take no lock, never allocate and never wait for the UI. The UI never sees a spectrum half written, and one it had no frame for is skipped, not queued: at 96 kHz the plugin publishes about 94 spectra a second, and a 60 Hz UI draws 60 of them. With the editor closed nothing is delivered and nothing piles up.
The UI draws the latest
useEffect(() => {
const show = (spectrum: Float32Array) => {
bars.current = hold(bars.current, spectrum, FALL);
draw(canvas.current!.getContext('2d')!, size, bars.current);
};
const latest = streams.spectrum.get();
if (latest !== undefined) show(latest);
return streams.spectrum.subscribe(show);
}, [size]);The spectrum is drawn from the stream's subscriber, without rendering React again; the peak is ordinary React state:
const peak = useNativeStream(streams.peak);In the browser
pnpm dev:web # soundor dev web: the host, with an inspector
pnpm build:web # soundor build web: a static site in .soundor/dist/webPress ▶ to play the host's input. The same Rust analysis runs in the
browser, compiled to WebAssembly in an AudioWorklet, and its streams reach
the UI through the same soundor:native streams, about 60 times a second.
As a VST3 plugin
pnpm dev # soundor dev vst3: a debug build, opened in the VST3 host
pnpm build:vst3 # soundor build vst3: the VST3 in .soundor/dist/vst3In Soundor's repository, build the VST3 runtime's adapter first
(pnpm --filter @soundor/vst3-runtime build:native); soundor doctor checks
the rest.
Tests
pnpm test
pnpm typecheck
pnpm build:webThe tests render App and publish stream values with
publishStream(), as the plugin's
audio code would.