# Waveform Lane

> The waveforms of a lane's clips on one canvas and one WebGL context, drawn on the GPU by gl-waveform: exact peaks and the RMS inside, a line through the samples zoomed in, plain or as the three bands of the sound.

## Installation

**CLI**

```bash
npx shadcn@latest add @audio-ui/waveform-lane
```

**Manual**

**1. Copy and paste the following code into your project.**

```tsx title="components/audio-ui/waveform-lane.tsx"
import { type ComponentProps, useLayoutEffect, useMemo, useRef } from "react";
import { cn } from "@/lib/utils";
import {
  createWaveformLane,
  type Lane,
  type WaveformClip,
  type WaveformInputs,
  type WaveformLayout,
  type WaveformLook,
} from "@/lib/webgl/waveform";

/**
 * The waveforms of a lane's clips on one canvas and one WebGL context, drawn
 * on the GPU by `gl-waveform` through `@/webgl/waveform`: every pixel
 * column shows the exact lowest and highest of its samples with the RMS
 * inside, and zoomed in a line through the samples, however long the
 * recording. Put it in the lane's box, `zoom` being the lane's pixels a
 * second. A recording with several channels is drawn a row each once the rows
 * are tall enough to read (`layout`), as a DAW does. With `look="bands"` each clip is drawn as the three bands of its
 * sound one over the other, the lows warm and the highs cool. A clip is read
 * in stretches (`segments`), as a DAW paints a region; a reversed clip is
 * not drawn here, and `hasWebGL2` of `@/webgl/lane` says whether to draw
 * this way at all. With `shared`, it draws on the page's one WebGL context and
 * copies the picture onto its canvas, so a page draws as many lanes as it has
 * clips, as an arrangement's clips each draw their own.
 */
function WaveformLane({
  className,
  clips,
  layout = "auto",
  look = "plain",
  shared = false,
  zoom,
  ...props
}: Omit<ComponentProps<"canvas">, "ref"> & {
  clips: readonly WaveformClip[];
  /** How a recording's channels are laid out: one waveform (`mix`), a row each (`split`), or a row each once the rows are tall enough to read (`auto`), as a DAW does. @default "auto" */
  layout?: WaveformLayout;
  look?: WaveformLook;
  /** Draw on the page's one WebGL context, copied onto this canvas: for a lane a clip, past the sixteen contexts a browser keeps. Read once, as the lane starts. @default false */
  shared?: boolean;
  /** The lane's pixels a second. Leave it out for a clip's own lane: its clips fit its box. */
  zoom?: number;
}) {
  const ref = useRef<HTMLCanvasElement>(null);
  const lane = useRef<Lane<WaveformInputs> | null>(null);
  const inputs = useMemo<WaveformInputs>(
    () => ({ clips, layout, look, zoom }),
    [clips, layout, look, zoom]
  );
  const first = useRef({ inputs, shared });
  useLayoutEffect(() => {
    const canvas = ref.current;
    if (!canvas) {
      return;
    }
    lane.current = createWaveformLane(canvas, first.current.inputs, {
      shared: first.current.shared,
    });
    return () => {
      lane.current?.destroy();
      lane.current = null;
    };
  }, []);
  useLayoutEffect(() => {
    lane.current?.update(inputs);
  }, [inputs]);
  return (
    <canvas
      className={cn("pointer-events-none absolute inset-y-0", className)}
      data-slot="timeline-clip-waveform-gl"
      ref={ref}
      {...props}
    />
  );
}

export type { WaveformClip, WaveformLayout, WaveformLook };
export { WaveformLane };
```

**2. Update the import paths to match your project setup.**

**Agent**

Paste this prompt to your agent. It does the rest: it installs the skill, adds the component and wires it to your audio.

```text
Install the audio/ui skill with `npx skills add ouestlabs/audio-ui`, then follow it to add the waveform lane from audio/ui to my project and wire it to my audio code. Read the docs at https://www.audio-ui.dev/llms.txt first.
```

To let the agent read the docs live, connect the [MCP server](/docs/mcp). More in [Agent Skill](/docs/agents).

## Usage

`WaveformLane` draws the waveforms 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. Drawn by `gl-waveform` on the GPU, every pixel column shows the exact lowest and highest of its samples with the RMS inside, and zoomed in it is a line through the samples, with a dot on each: at any zoom, over recordings of any length. It is a thin React wrapper over [`@/lib/webgl/waveform`](/docs/webgl/waveform), which does the drawing and has no React in it: a custom element or a script starts the same lane. All the clips share one WebGL context, and clips of one recording share what is on the GPU for it. The audio editor switches its lane between this, the spectrogram and the waveform by band from its View menu.

```tsx
import { WaveformLane } from "@/components/audio-ui/waveform-lane";

<WaveformLane
  className="h-full text-foreground"
  clips={clips} // { id, source, start, length, gain, peak, sampleRate, samples, segments, bands? }[]
  look="plain" // or "bands"
  zoom={pixelsPerSecond}
/>;
```

| Prop | Type | |
| --- | --- | --- |
| `clips` | `WaveformClip[]` | The clips to draw: `id`, the `source` recording they read, where each `start`s and how `length` it is on the lane in seconds, its `gain`, the recording's `peak` (its loudest sample, 0 to 1, drawn as tall as the lane), `sampleRate` and `samples` (`{ frames, read }`, mixed to one), `segments` (how it reads its recording, see below) and `bands` for the band look. |
| `zoom` | `number` | The lane's pixels a second. Leave it out for a clip's own lane: its clips fit its box. |
| `shared` | `boolean` | Draw on the page's one WebGL context and copy the picture onto this canvas, for a lane a clip: a page then draws as many as it has clips, past the sixteen contexts a browser keeps, and clips of one recording share what is on the GPU for it. Read once, as the lane starts. Default `false`. |
| `layout` | `"auto" \| "mix" \| "split"` | How a recording's channels are laid out: one waveform, a row each, or a row each once the rows are tall enough to read, as a DAW does. A clip gives its `channels`, one reader each, for the rows. Default `"auto"`. |
| `look` | `"plain" \| "bands"` | One waveform, or the three bands of the sound one over the other. Default `"plain"`. |

With `look="bands"` a clip is drawn as its sound split in three by a Linkwitz-Riley crossover, at 250 Hz and 2.5 kHz (`splitBands` of [`@/lib/engine/analysis`](/docs/engine/analysis)): the lows warm, the mids in the ink's own colour and the highs cool, the lows tall behind the highs, as DJ software colours a sound. Each band is drawn as tall as its own loudest sample reaches (`peaks`), so a band that is quiet in the mix still shows. `bands` is called once a recording and may split it lazily: the split holds three copies of the mix.

A clip says how it reads its recording as `segments(from, to)`: the stretches of the clip between two times of it, each a straight read, as a DAW paints a region. [`clipSegments`](/docs/lib/arrangement) of the arrangement library gives them for a clip that plays on from its offset, one for each turn of its loop and each stretch between its warp markers. A stretch read backwards is not drawn, so a reversed clip is [`ClipWaveform`](/docs/components/base/arrangement)'s, which draws a clip as it plays, as it is where `hasWebGL2` of `@/lib/webgl/lane` says WebGL 2 is missing. The color is the text color.
