Spectrogram
A bounded live history of precomputed spectrum frames.
import {
Button,
Spectrogram,
type PlotSource,
type SpectrumFrame,
} from '@soundor/components';
import { useTheme } from '@soundor/components/theme';
import { View } from '@soundor/react';
import { useEffect, useState } from 'react';
function simulate() {
const values = new Float32Array(512).fill(-90);
const listeners = new Set<() => void>();
const frame: SpectrumFrame = { values, frequencyRange: [0, 24000] };
const source: PlotSource<SpectrumFrame> = {
get: () => frame,
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
};
return {
source,
step(t: number) {
const pitch = 45 * 18 ** ((Math.sin(t) + 1) / 2);
for (let i = 1; i < values.length; i++) {
const hz = (24000 * i) / (values.length - 1);
const harmonic = hz / pitch;
values[i] =
-83 +
(66 * Math.exp(-((harmonic - Math.round(harmonic)) ** 2) * 64)) /
Math.max(1, harmonic ** 0.55);
}
listeners.forEach((listener) => listener());
},
};
}
export default function Demo() {
const theme = useTheme();
const [simulation] = useState(simulate);
const [resetKey, reset] = useState(0);
const [vertical, setVertical] = useState(false);
useEffect(() => {
let t = 0;
const timer = setInterval(() => simulation.step((t += 0.03)), 33);
return () => clearInterval(timer);
}, [simulation]);
return (
<View style={{ gap: theme.spacing.spaceSm }}>
<Spectrogram
accessibilityLabel="Simulated spectrogram"
width={400}
height={185}
source={simulation.source}
resetKey={resetKey}
orientation={vertical ? 'vertical' : 'horizontal'}
/>
<View style={{ flexDirection: 'row', gap: theme.spacing.spaceSm }}>
<Button onPress={() => reset(resetKey + 1)}>Clear history</Button>
<Button onPress={() => setVertical(!vertical)}>
Change orientation
</Button>
</View>
</View>
);
}spectrogram.tsx, running in the web runtime.SourceThe demo uses simulated spectrum frames. Spectrogram consumes a
PlotSource<SpectrumFrame>. Each frame contains amplitudes in values and
the [firstHz, lastHz] frequencyRange of the evenly spaced bins. The source
can reuse its array: the component copies the displayed samples on publish.
<Spectrogram width={400} height={180} source={spectrum} resetKey={resetCount} />Use frequencyRange (20–20,000 Hz by default), frequencyScale (log or
linear), amplitudeRange (−90 to 0 by default), bins (2–512 displayed
samples), and historyLength (1–1024 time slots). Use a positive frequency
range for logarithmic mode. Set orientation to horizontal or vertical
for time left-to-right or top-to-bottom. The sweep wraps, overwriting the
oldest slot; the line indicates the next slot. Change resetKey to discard
history and restart with the current source snapshot.
Its default colors come from the theme; colorMap(fraction) supplies a custom
palette, and root and canvas are style slots. Give the image an
accessibilityLabel. Supply a separate accessible readout if the data is
essential to the task.
The standalone canvas repaints the new slots rather than shifting or
rebuilding all history for each frame. A resize or style change repaints
the retained history. The default history stores 72 KiB of amplitude data.
Run pnpm --filter @soundor/components exec vitest run src/components/spectrogram/history.test.ts
to measure bin remapping separately from canvas painting.
Props
Prop
Type
SpectrumFrame
The source supplies one of these frames per publish. See PlotSource for the source interface.
Prop
Type