Plot examples
Explore interactive charts and editors built with Plot.
Each demo shows a complete component, with source below it.
Parametric EQ
Each band is a draggable marker; application state holds the bands. The total is a Plot.Area, and each band is a faint dashed Plot.Line with hideInReadout. All of them set extend, so the curve runs to the frame even with a handle at 20 Hz or 20 kHz. For a more complete editor, see the equalizer reference.
import { Plot, formatters } from '@soundor/components';
import { useState } from 'react';
interface Band {
hz: number;
db: number;
/** Narrower for a higher Q. */
q: number;
}
// A bell on the log-frequency axis: the gain at the band's centre, falling
// off with the distance in octaves.
const bell = (band: Band, hz: number) =>
band.db *
Math.exp(-(Math.log2(hz / band.hz) ** 2) / (2 * (0.9 / band.q) ** 2));
const frequencies = Array.from(
{ length: 128 },
(_, i) => 20 * 1000 ** (i / 127),
);
/** Each band on its own, quiet and out of the readout: a part of yours. */
function BandCurves({ bands }: { bands: readonly Band[] }) {
return bands.map((band, i) => (
<Plot.Line
key={i}
extend
dash={[3, 3]}
opacity={0.45}
hideInReadout
points={frequencies.map((hz) => [hz, bell(band, hz)])}
/>
));
}
export default function Demo() {
const [bands, setBands] = useState<Band[]>([
{ hz: 90, db: 5, q: 1.2 },
{ hz: 1000, db: -6, q: 0.9 },
{ hz: 6500, db: 7, q: 1.4 },
]);
return (
<Plot
width={440}
height={220}
title="Parametric EQ"
note="Drag a band"
accessibilityLabel="Parametric EQ"
>
<Plot.XAxis
domain={[20, 20_000]}
scale="log"
ticks="decades"
format={formatters.frequency}
/>
<Plot.YAxis
domain={[-12, 12]}
ticks={[-12, -6, 0, 6, 12]}
format={formatters.decibels}
/>
<Plot.Grid />
<Plot.Area
label="Total"
baseline={0}
// Reaches the frame, past the handles' safe zone.
extend
points={frequencies.map((hz) => [
hz,
bands.reduce((sum, band) => sum + bell(band, hz), 0),
])}
/>
<BandCurves bands={bands} />
{bands.map((band, i) => (
<Plot.Marker
key={i}
x={band.hz}
y={band.db}
label={String(i + 1)}
onMove={([hz, db]) =>
setBands((all) =>
all.map((b, j) => (j === i ? { ...b, hz, db } : b)),
)
}
/>
))}
</Plot>
);
}plot-eq.tsx, running in the web runtime.SourceLive spectrum
Two series, each with a source the demo updates 30 times a second. The plot redraws once per frame, with no React render.
import {
Plot,
formatters,
type PlotSamples,
type PlotSource,
} from '@soundor/components';
import { useEffect } from 'react';
type Point = [number, number];
const bins = 160;
const hz = (i: number) => 20 * 1000 ** (i / (bins - 1));
/**
* A store a plot reads: a snapshot, and whoever wants to know when it
* changes. In a plug-in this is fed by the audio side.
*/
function createSpectrum(): PlotSource<PlotSamples> & {
points: Point[];
publish(): void;
} {
const points: Point[] = Array.from({ length: bins }, (_, i) => [hz(i), -90]);
const snapshot = { points };
const listeners = new Set<() => void>();
return {
points,
get: () => snapshot,
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
publish() {
for (const listener of listeners) listener();
},
};
}
// One source for each series: which data is which is in the markup.
const input = createSpectrum();
const output = createSpectrum();
export default function Demo() {
useEffect(() => {
let t = 0;
const timer = setInterval(() => {
t += 0.05;
for (let i = 0; i < bins; i++) {
const f = hz(i);
const tilt = -22 - 4.5 * Math.log2(f / 100);
const tone =
18 *
Math.exp(-(Math.log2(f / (220 + 140 * Math.sin(t))) ** 2) / 0.04);
const noise = Math.sin(i * 17 + t * 13) * 3.5;
const level = Math.max(-90, tilt + tone + noise);
input.points[i]![1] = level;
// The output is the input through a low-pass at 4 kHz.
output.points[i]![1] = Math.max(
-90,
level - 24 * Math.max(0, Math.log2(f / 4000)),
);
}
input.publish();
output.publish();
}, 33);
return () => clearInterval(timer);
}, []);
return (
<Plot
width={440}
height={220}
title="Response and spectrum"
note="Simulated data"
>
<Plot.XAxis
domain={[20, 20_000]}
scale="log"
ticks="decades"
format={formatters.frequency}
/>
<Plot.YAxis
id="spectrum"
position="right"
domain={[-90, 0]}
ticks={[-90, -60, -30, 0]}
format={(level) => `${level} dBFS`}
/>
<Plot.YAxis
domain={[-12, 12]}
ticks={[-12, 0, 12]}
format={formatters.decibels}
/>
<Plot.Grid />
<Plot.Line label="Input" source={input} yAxis="spectrum" />
<Plot.Area
label="Output"
source={output}
yAxis="spectrum"
baseline={-90}
/>
<Plot.Line
label="Response"
points={[
[20, 0],
[2000, 0],
[4000, -3],
[10000, -7],
[20000, -12],
]}
/>
<Plot.Legend />
<Plot.Readout />
</Plot>
);
}plot-spectrum.tsx, running in the web runtime.SourceLevel bands with peak hold
Plot.Bars shows the levels; Plot.Points shows the peaks. hideInReadout keeps a series out of the hover readout.
import { Plot, formatters } from '@soundor/components';
import { useEffect, useState } from 'react';
// Third-octave band centres, 25 Hz to 16 kHz.
const centres = Array.from({ length: 28 }, (_, i) => 25 * 2 ** (i / 3));
export default function Demo() {
const [levels, setLevels] = useState<number[]>(() => centres.map(() => -60));
const [peaks, setPeaks] = useState<number[]>(() => centres.map(() => -60));
useEffect(() => {
let t = 0;
const timer = setInterval(() => {
t += 0.1;
const next = centres.map((f, i) => {
const tilt = -14 - 3 * Math.log2(f / 100);
const beat = 14 * Math.max(0, Math.sin(t * 2 + i * 0.5)) ** 3;
return Math.min(0, tilt + beat + (Math.random() - 0.5) * 5);
});
setLevels(next);
// A peak holds, then falls.
setPeaks((held) => held.map((peak, i) => Math.max(next[i]!, peak - 0.6)));
}, 60);
return () => clearInterval(timer);
}, []);
return (
<Plot width={440} height={200} title="Third-octave analyser">
<Plot.XAxis
domain={[20, 20_000]}
scale="log"
ticks={[100, 1000, 10_000]}
format={formatters.frequency}
/>
<Plot.YAxis
domain={[-60, 0]}
ticks={[-60, -40, -20, 0]}
format={formatters.decibels}
/>
<Plot.Grid />
<Plot.Bars
label="Level"
baseline={-60}
points={centres.map((f, i) => [f, levels[i]!])}
/>
<Plot.Points
label="Peak"
radius={1.5}
hideInReadout
points={centres.map((f, i) => [f, peaks[i]!])}
/>
<Plot.Readout />
</Plot>
);
}plot-bars.tsx, running in the web runtime.SourceCompressor curve
The marker sets the threshold, the Slider sets the ratio, and the title's note shows both. The marker stays on the curve because the handler uses only the x coordinate of the drag.
import { Plot, Slider, formatters } from '@soundor/components';
import { View } from '@soundor/react';
import { useState } from 'react';
// Input level in, output level out: above the threshold, the ratio.
const transfer = (input: number, threshold: number, ratio: number) =>
input <= threshold ? input : threshold + (input - threshold) / ratio;
const levels = [-60, -40, -20, 0];
const unity: [number, number][] = [
[-60, -60],
[0, 0],
];
export default function Demo() {
const [threshold, setThreshold] = useState(-24);
const [ratio, setRatio] = useState(4);
return (
<View style={{ gap: 12 }}>
<Plot
width={300}
height={220}
title="Compressor"
note={`${formatters.decibels(threshold)} · ${ratio.toFixed(1)}:1`}
accessibilityLabel="Compressor transfer curve"
>
<Plot.XAxis
domain={[-60, 0]}
ticks={levels}
format={formatters.decibels}
/>
<Plot.YAxis
domain={[-60, 0]}
ticks={levels}
format={formatters.decibels}
/>
<Plot.Grid />
{/* 1:1, for reference. */}
<Plot.Line points={unity} dash={[4, 4]} opacity={0.5} hideInReadout />
<Plot.Area
label="Output"
baseline={-60}
points={[-60, threshold, 0].map((x) => [
x,
transfer(x, threshold, ratio),
])}
/>
<Plot.Marker
x={threshold}
y={threshold}
label="T"
// Only the threshold moves: the handle stays on the curve.
onMove={([x]) =>
setThreshold(Math.round(Math.min(-6, Math.max(-54, x))))
}
/>
</Plot>
<Slider
label="Ratio"
min={1}
max={20}
value={ratio}
onChange={setRatio}
width={300}
/>
</View>
);
}plot-compressor.tsx, running in the web runtime.SourceEnvelope editor
The first marker sets a time; the second sets a time and a level. A Plot.VerticalLine marks where the key is released.
import { Plot, formatters } from '@soundor/components';
import { useState } from 'react';
const hold = 1.2; // Seconds the key is held.
const clamp = (value: number, min: number, max: number) =>
Math.min(max, Math.max(min, value));
export default function Demo() {
const [attack, setAttack] = useState(0.2);
const [decay, setDecay] = useState(0.35);
const [sustain, setSustain] = useState(0.6);
const [release, setRelease] = useState(0.6);
const peak = attack;
const settle = attack + decay;
const end = hold + release;
return (
<Plot
width={440}
height={200}
title="Amplitude envelope"
note="Drag the points"
accessibilityLabel="Amplitude envelope"
>
<Plot.XAxis
domain={[0, 2]}
ticks={[0, 0.5, 1, 1.5, 2]}
format={(s) => formatters.time(s * 1000)}
/>
<Plot.YAxis
domain={[0, 1.1]}
ticks={[0, 0.5, 1]}
format={formatters.percent}
/>
<Plot.Grid />
<Plot.Area
label="Envelope"
baseline={0}
points={[
[0, 0],
[peak, 1],
[settle, sustain],
[hold, sustain],
[end, 0],
]}
/>
<Plot.Marker
x={peak}
y={1}
label="A"
onMove={([x]) => setAttack(clamp(x, 0.01, 0.8))}
/>
<Plot.Marker
x={settle}
y={sustain}
label="D"
onMove={([x, y]) => {
setDecay(clamp(x - attack, 0.02, 1));
setSustain(clamp(y, 0, 1));
}}
/>
<Plot.Marker
x={end}
y={0}
label="R"
onMove={([x]) => setRelease(clamp(x - hold, 0.02, 0.8))}
/>
{/* Where the key is released. */}
<Plot.VerticalLine value={hold} color="#8d8d9b" dash={[3, 3]} />
</Plot>
);
}plot-envelope.tsx, running in the web runtime.SourceWaveform with playhead
Plot.Band draws the region between the lowest and highest sample of each slice. The playhead is a marker: dragging it pauses playback with onMoveStart and resumes with onMoveEnd.
import { Plot } from '@soundor/components';
import { useEffect, useState } from 'react';
const length = 4; // Seconds.
const steps = 320;
// What a waveform overview is: the lowest and highest sample of each slice.
const lower: [number, number][] = [];
const upper: [number, number][] = [];
for (let i = 0; i < steps; i++) {
const t = (i / (steps - 1)) * length;
const swell = 0.25 + 0.75 * Math.abs(Math.sin(t * 1.6)) ** 1.5;
const grain = 0.75 + 0.25 * Math.sin(i * 12.9898);
lower.push([t, -swell * grain]);
upper.push([t, swell * (0.8 + 0.2 * Math.cos(i * 4.1))]);
}
export default function Demo() {
const [time, setTime] = useState(0.8);
const [scrubbing, setScrubbing] = useState(false);
useEffect(() => {
if (scrubbing) return;
const timer = setInterval(
() => setTime((now) => (now + 0.04) % length),
40,
);
return () => clearInterval(timer);
}, [scrubbing]);
return (
<Plot
width={440}
height={170}
title="Sample"
note="Drag the playhead"
accessibilityLabel="Sample playhead"
>
<Plot.XAxis
domain={[0, length]}
ticks={[0, 1, 2, 3, 4]}
format={(s) => `${s} s`}
/>
<Plot.YAxis domain={[-1, 1]} ticks={[]} />
<Plot.Grid />
<Plot.Band label="Sample" lower={lower} upper={upper} />
<Plot.Marker
x={time}
y={0}
label="▶"
onMoveStart={() => setScrubbing(true)}
onMove={([x]) => setTime(x)}
onMoveEnd={() => setScrubbing(false)}
/>
</Plot>
);
}plot-waveform.tsx, running in the web runtime.SourceStep automation
Plot.Steps holds each value until the next point, which suits sequencers and automation. One small marker per step makes every step draggable.
import { Plot, formatters } from '@soundor/components';
import { useState } from 'react';
const bars = 16;
const clamp = (value: number) => Math.min(1, Math.max(0, value));
export default function Demo() {
// One value per step, 0 to 1: held until the next step.
const [steps, setSteps] = useState<number[]>(() =>
Array.from(
{ length: bars },
(_, i) => Math.round((0.5 + 0.4 * Math.sin(i / 2.2)) * 20) / 20,
),
);
return (
<Plot
width={440}
height={180}
title="Cutoff automation"
note="Drag a step"
accessibilityLabel="Cutoff automation, 16 steps"
>
<Plot.XAxis
domain={[0, bars]}
ticks={[0, 4, 8, 12, 16]}
format={(step) => `Step ${step}`}
/>
<Plot.YAxis
domain={[0, 1]}
ticks={[0, 0.5, 1]}
format={formatters.percent}
/>
<Plot.Grid />
<Plot.Steps
label="Cutoff"
points={[...steps, steps.at(-1)!].map((value, i) => [i, value])}
/>
{/* A handle at the middle of each step. */}
{steps.map((value, i) => (
<Plot.Marker
key={i}
x={i + 0.5}
y={value}
radius={4}
onMove={([, y]) =>
setSteps((all) => all.map((v, j) => (j === i ? clamp(y) : v)))
}
/>
))}
</Plot>
);
}plot-automation.tsx, running in the web runtime.SourceCombined with an XYPad
A Plot and an XYPad can share state, so the picture follows the gesture. See the XYPad example.