LabDAW

Spectrogram Lane

The spectrograms of a lane's clips on one canvas and one WebGL context, computed on the GPU at every zoom by gl-spectrogram, on the frequency axis of your choice, with a frequency axis to go beside them.

Installation

pnpm dlx shadcn@latest add @audio-ui/clip-spectrogram

Usage

SpectrogramLane draws the spectrograms of all of a lane's clips on one canvas, each in its own part of it, placed as the clips are on the lane. Instead of peaks it draws the frequencies of the recording, each column an FFT computed on the GPU by gl-spectrogram, so a steady tone is one row thin and a click one column thin at any zoom. It is a thin React wrapper over @/lib/webgl/spectrogram, which does the drawing and has no React in it. All the clips share one WebGL context, and clips of one recording share what is on the GPU for it: a browser keeps some sixteen and drops the oldest past that, so a canvas a clip would lose its picture once a sound is cut into enough pieces. The audio editor switches its lane between the waveform and the spectrogram from its View menu.

import { SpectrogramLane } from "@/components/audio-ui/clip-spectrogram";

<SpectrogramLane
  className="h-full text-foreground"
  clips={clips} // { id, source, start, length, gain, sampleRate, samples, segments }[], start in seconds on the lane
  scale="log" // "log" octaves, "mel", "erb" or "lin" hertz
  zoom={pixelsPerSecond}
/>;
PropType
clipsLaneClip[]The clips to draw: id, the source recording they read, where each starts and how length it is on the lane in seconds, its gain, the recording's sampleRate and samples ({ frames, read }, mixed to one), and segments, how it reads its recording (see Waveform Lane).
zoomnumberThe lane's pixels a second.
scale"log" | "mel" | "erb" | "lin"The frequency axis. Default "log".
pointerHertz{ set }Told the frequency under the pointer, or null off the picture.
bandSpectrogramBandThe frequencies shown, as shares of the axis. Default all.

Over a clip the pointer reads the cell under it, its frequency and level, in a small label. SpectrogramAxis draws the frequency axis down the left edge of the lane, as audio.js does: labels roundest first (20k, 2k, 200, 20, then between as room allows), each with its tick at the row its frequency sits on, shorter ticks between, and the frequency under the pointer marked on it. Put it in the lane with the clips, on the same scale; it holds still as the lane scrolls. The pointer's frequency goes from the lane to the axis through a pointerHertz source (createSource<number | null>(null)): give the same one to both.

Zooming the frequencies

band is the frequencies the pictures show, as shares of its axis from 0 (the floor) to 1 (the Nyquist limit), whatever the axis spaces its rows by: a createSource<readonly [number, number]>([0, 1]) given to the clips and the axis. Shift and the wheel over the picture zoom it about the pointer, the wheel over the axis moves it, Ctrl, Command or a pinch over the axis zooms it, and a double-click on the axis shows all of it again. The axis labels what is in view: round values, 1-2-5 a decade across a wide span, even steps across a narrow one. zoomBand and panBand are the arithmetic, to drive it from your own controls.

const [band] = useState(() => createSource<readonly [number, number]>([0, 1]));

<SpectrogramLane band={band} {...rest} />;
<SpectrogramAxis band={band} pointer={pointerHertz} sampleRate={rate} scale="log" />;

It needs WebGL 2 and draws nothing without it.

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