import { useEffect, useRef, useState, type RefObject } from 'react' import { mixer } from '../../lib/audio/mixer' import { faderGain, filterSweep, eqGain, gainToDb, TRIM_RANGE_DB, EQ_CUT_DB } from '../../lib/audio/mixerMath' import { FX_LABELS, FX_TYPES, setChannel, setMixer, useMixer, type Channel, type FxSettings, type FxType, } from '../../lib/audio/mixerState' import { Icon } from '../../lib/icons' import { ContextMenu } from '../ContextMenu' import { Fader, Knob } from './Controls' /** The meters' scale (dBFS). */ const METER_FLOOR = -48 const METER_TOP = 3 /** Knob sizes (px): as large as the column allows — the EQ block is two * across, the colour knob beside it larger. */ const EQ_KNOB = 40 const COLOR_KNOB = 48 const fmtDb = (db: number, places = 1) => !isFinite(db) ? '−∞ dB' : `${db > 0 ? '+' : db < 0 ? '−' : ''}${Math.abs(db).toFixed(places)} dB` function describeFx(fx: FxType, c: number, set: FxSettings): string { const a = Math.round(Math.abs(c) * 100) if (Math.abs(c) < 0.02) return `${FX_LABELS[fx]}: off (turn left or right)` switch (fx) { case 'filter': { const s = filterSweep(c)! const hz = s.freq >= 1000 ? `${(s.freq / 1000).toFixed(1)} kHz` : `${Math.round(s.freq)} Hz` return `Filter: ${s.type === 'lowpass' ? 'low-pass' : 'high-pass'} at ${hz} · resonance ${Math.round(set.filterResonance * 100)}%` } case 'echo': return `Echo ${a}% · ${beatsLabel(set.echoBeats)} · ${c < 0 ? 'darker' : 'thinner'} repeats` case 'space': return `Space ${a}% · ${set.spaceSize} s · ${c < 0 ? 'darker' : 'thinner'}` case 'noise': return `Noise ${a}% · ${c < 0 ? 'low-pass' : 'high-pass'} sweep` case 'crush': return `Crush ${a}% · drive ×${set.crushDrive}` } } function beatsLabel(b: number): string { return b === 1 ? '1 beat' : b === 0.75 ? '3/4 beat' : b === 0.5 ? '1/2 beat' : b === 0.25 ? '1/4 beat' : '1/8 beat' } /** * The mixer, between the decks in performance mode (decided 2026-11-02): per * channel TRIM (on top of the track's auto gain), HI / MID / LOW, a colour * effect with its own selector (its one parameter is an app setting, in * Settings ▸ Performance mode…), a fader and its meter; between * them the master meter and the limiter's light; the crossfader beneath. The * values live in `mixerState` and the audio in `mixer.ts`. */ export function MixerPanel({ autoGain }: { autoGain: Record }) { const meterRefs = { A: useRef(null), B: useRef(null), master: useRef(null), } const limitRef = useRef(null) // One frame loop for every meter, writing the DOM directly — sixty React // renders a second for three bars would be waste. useEffect(() => { let raf = 0 const held = { A: METER_FLOOR, B: METER_FLOOR, master: METER_FLOOR } let limitUntil = 0 const tick = (now: number) => { const lv = mixer().levels() for (const k of ['A', 'B', 'master'] as const) { const db = Math.max(METER_FLOOR, lv[k]) held[k] = db >= held[k] ? db : Math.max(db, held[k] - 0.6) // fall back ~36 dB/s const el = meterRefs[k].current if (el) { el.style.height = `${((held[k] - METER_FLOOR) / (METER_TOP - METER_FLOOR)) * 100}%` el.style.background = held[k] > -1 ? 'var(--color-pink)' : held[k] > -9 ? 'var(--color-gold)' : 'var(--color-mint)' } } // Only while the master carries sound: on silence, Chrome's compressor // reports a phantom reduction (14 dB, decaying over ~2 s, measured just // after performance mode opens) that limits nothing. if (lv.limiting > 0.5 && lv.master > -60) limitUntil = now + 600 if (limitRef.current) limitRef.current.dataset.on = String(now < limitUntil) raf = requestAnimationFrame(tick) } raf = requestAnimationFrame(tick) return () => cancelAnimationFrame(raf) // The refs are stable. // eslint-disable-next-line react-hooks/exhaustive-deps }, []) const crossfader = useMixer((s) => s.crossfader) const sharpness = useMixer((s) => s.sharpness) return (
LIM
A setMixer({ crossfader: Math.abs(v) < 0.03 ? 0 : v })} label="Crossfader" describe={(v) => `Crossfader (curve ${Math.round(sharpness * 100)}% sharp — Settings ▸ Performance mode…) — ${ Math.abs(v) < 0.03 ? 'centre' : v < 0 ? `toward A ${Math.round(-v * 100)}%` : `toward B ${Math.round(v * 100)}%` }` } className="flex-1" /> B
) } function MeterBar({ fillRef }: { fillRef: RefObject }) { return (
) } /** One channel: its knobs, then (toward the middle) its fader and meter. */ function Strip({ ch, autoGain, meterRef, mirrored = false, }: { ch: Channel autoGain: number | null meterRef: RefObject mirrored?: boolean }) { const s = useMixer((m) => m[ch]) const eqKill = useMixer((m) => m.eqKill) const fxSettings = useMixer((m) => m.fx) const [fxMenu, setFxMenu] = useState<{ x: number; y: number } | null>(null) const set = (patch: Parameters[1]) => setChannel(ch, patch) const describeEq = (band: string) => (v: number) => { const g = eqGain(v, eqKill) return `${band} ${Math.abs(v) < 0.01 ? 'flat' : g === 0 ? 'killed' : fmtDb(gainToDb(g))}${ v < 0 ? ` (full left: ${eqKill ? 'kill' : `${EQ_CUT_DB} dB`})` : '' }` } const eq = (band: 'high' | 'mid' | 'low', label: string, name: string) => ( set({ [band]: Math.abs(v) < 0.03 ? 0 : v })} label={label} describe={describeEq(name)} /> ) // GAIN (the state's `trim`: on top of the track's auto gain) and the EQ as a // 2×2 block; beside it, toward the fader, the colour effect: its picker // above a larger COLOR knob (decided 2026-11-03). const knobs = (
set({ trim: Math.abs(v) < 0.3 ? 0 : v })} label="Gain" describe={(v) => `Gain ${fmtDb(v)}${autoGain != null ? ` on the track's auto gain (${fmtDb(autoGain)})` : ' (this track has no auto gain)'}` } /> {eq('high', 'Hi', 'High')} {eq('mid', 'Mid', 'Mid')} {eq('low', 'Low', 'Low')}
set({ color: Math.abs(v) < 0.02 ? 0 : v })} label="Color" describe={(v) => describeFx(s.fx, v, fxSettings)} />
) const fader = (
set({ fader: v })} label={`Channel ${ch} fader`} describe={(v) => `Channel ${ch}: ${fmtDb(gainToDb(faderGain(v)))}`} className="flex-1" /> {ch}
) const meter = (
) return (
{knobs} {fader} {meter} {fxMenu && ( setFxMenu(null)} items={[ ...FX_TYPES.map((t) => ({ label: FX_LABELS[t], hint: t === s.fx ? '✓' : undefined, onClick: () => set({ fx: t }), })), ]} /> )}
) }