id: rb-waveform-crop feature: 3-waveform-crop branch: feat/3-waveform-crop stage: design status: implemented; updated after code review (findings 1-10) created: 2026-09-10

Design — Waveform and crop

Implements prd.md. Pure logic in src/waveform/, Node-testable; React only wires pointer events and draws.

1. Data

// src/waveform/selection.ts
export interface Selection { startS: number; endS: number }   // seconds, absolute in the clip
export const MIN_WINDOW_S = 1
export const MAX_WINDOW_S = 10
export const MIN_CLIP_S = 1        // phase-2 load gate, same number as MIN_WINDOW_S

The clip is never modified. The selection is state next to the clip; the analysis input is derived: sliceClip(clip, selection): AudioClip.

2. Pure modules

2.1 selection.ts

2.2 peaks.ts

export interface Peaks { min: Float32Array; max: Float32Array }   // one entry per column
export function computePeaks(samples: Float32Array, fromSample: number, toSample: number, columns: number): Peaks

For each column c, the sample span is [from + c*span/columns, from + (c+1)*span/columns); min and max over it. Empty spans (more columns than samples) repeat the single sample. O(n) in the span length. No caching here; the component caches.

2.3 Slicing (in the audio layer)

sliceClip(clip, startS, endS) lives in src/audio/decode.ts, beside trimClip, which delegates to it. One rounding rule for every cut: the start sample is round(startS * rate) and the count is round((endS - startS) * rate), so a window of a given length always yields the same number of samples wherever it sits. (Rounding both ends independently made the count jitter by one as the window moved — code review finding 2.)

assertMinLength(clip) sits there too and throws InputError('too-short') when durationS < MIN_CLIP_S. Keeping both in audio/ means the audio layer never imports from waveform/; MIN_CLIP_S is defined in audio/types.ts.

2.4 Phase-2 touch points

3. Components

src/ui/
  WaveformCanvas.tsx   one canvas, draws a time range, optional handles, pointer events
  WaveformBlock.tsx    composes overview + detail for long clips, single canvas otherwise, plus the readout

3.1 WaveformCanvas

Props: clip, range: { fromS, toS } (visible time range), selection, onChange(sel), positionS?, handles: boolean, height.

Drawing, two layers:

Sizing: CSS width 100 %, ResizeObserver on the wrapper, backing store at devicePixelRatio. touch-action: none on the canvas.

Pointer mapping: xToS(x) = fromS + (x / width) * (toS - fromS); inverse for drawing.

Pointer handling (onPointerDown + setPointerCapture, onPointerMove, onPointerUp/Cancel):

3.2 WaveformBlock

3.3 State (useAudioInput)

Adds selection and setSelection; setClip resets it to defaultSelection. Exposes windowClip = useMemo(() => sliceClip(clip, selection)). togglePlay calls player.play(selection.startS, selection.endS); the rAF tick reads position() which is now absolute, so the indicator lands inside the window. Changing the selection while playing stops playback.

3.4 Layout (InputScreen)

Upload / Record row → StatusLine → WaveformBlock (when a clip is loaded and not recording) → Player. Nothing else moves.

4. Tests (test/)

5. Out of scope reminders

No zoom or pinch, no markers, no persistence, no analysis. AudioClip contract unchanged.