Implemented raw waveform mode

This commit is contained in:
patben8 committed 2026-10-15 10:26:41 +02:00
1 parent 1464b1e211
commit 8230722105
10 files changed
+862 -16

No files matched your search

+49 -5
View File
@@ -1,11 +1,19 @@
import { useEffect, useRef } from 'react'
import type { CuePoint } from '../api'
import type { BeatGrid } from '../lib/beatgrid'
import { Icon } from '../lib/icons'
import { drawBeatgrid, drawCuePoint, drawCues, drawLoop, drawLoopIn, drawPlayhead } from '../lib/cues'
import { laneScales, makeSpans, paintRaw, rawSpans, tintAt, type RawWave } from '../lib/rawWave'
import { paintLane, paintWave, type WaveColumn } from '../lib/waveform'
interface Props {
cols: WaveColumn[]
/** The samples, for the raw view. */
raw?: RawWave | null
/** Draw the raw signal as a thin line instead of bars (the toggle under
the zoom buttons; owned by the parent, which persists it). */
rawMode?: boolean
onRawModeChange?: (raw: boolean) => void
currentTime: number
duration: number
cues: CuePoint[]
@@ -36,6 +44,8 @@ interface Props {
export interface StemLane {
data: Float32Array
/** The stem's samples, for the raw view. */
raw?: RawWave | null
name: string
color: string
/** Not muted. */
@@ -50,7 +60,7 @@ const ALT = typeof navigator !== 'undefined' && /Mac/.test(navigator.platform) ?
const DRAG_THRESHOLD = 3 // px before a press becomes a scratch (vs a click-seek)
export const MIN_SEC = 2 // most zoomed-in (seconds across the view)
export const MIN_SEC = 0.25 // most zoomed-in (seconds across the view): a kick's attack spans dozens of px
export const MAX_SEC = 64 // most zoomed-out
export const DEFAULT_SEC = 16
@@ -62,6 +72,9 @@ export const DEFAULT_SEC = 16
*/
export function MainWaveform({
cols,
raw = null,
rawMode = false,
onRawModeChange,
currentTime,
duration,
cues,
@@ -89,6 +102,7 @@ export function MainWaveform({
const dragRef = useRef<{ startX: number; startTime: number; lastT: number; moved: boolean } | null>(
null,
)
const spansRef = useRef(makeSpans())
// Draw closes over the latest props/state; a ref lets the ResizeObserver call
// the current version without re-subscribing.
@@ -111,7 +125,25 @@ export function MainWaveform({
// 2 px bars with a 1 px gap: the texture that makes the scrolling view
// read as discrete energy rather than a smear.
const bars = { bar: Math.max(1, Math.round(2 * dpr)), gap: Math.max(1, Math.round(dpr)) }
if (lanes && lanes.length) {
// The raw line's minimum stroke, so silence and flat stretches still read.
const thick = Math.max(1, 1.25 * dpr)
const spans = spansRef.current
if (rawMode && lanes && lanes.length && lanes.every((l) => l.raw)) {
const laneH = h / lanes.length
const scales = laneScales(lanes.map((l) => l.raw!.peak), laneH / 2 - thick)
lanes.forEach((lane, k) => {
rawSpans(lane.raw!, startSec, endSec, w, spans)
ctx.globalAlpha = lane.audible ? 0.92 : 0.18
paintRaw(ctx, spans, k * laneH, laneH, scales[k], thick, lane.color)
})
ctx.globalAlpha = 1
ctx.fillStyle = 'rgba(255,255,255,0.06)'
for (let k = 1; k < lanes.length; k++) ctx.fillRect(0, Math.round(k * laneH), w, Math.max(1, Math.round(dpr)))
} else if (rawMode && !lanes?.length && raw) {
rawSpans(raw, startSec, endSec, w, spans)
const scale = (h / 2 - thick) / Math.max(raw.peak, 1e-6)
paintRaw(ctx, spans, 0, h, scale, thick, (t) => tintAt(cols, duration, t))
} else if (lanes && lanes.length) {
// Stem lanes, Traktor-style: each stem its own strip in its own colour.
const laneH = h / lanes.length
lanes.forEach((lane, k) => {
@@ -263,9 +295,10 @@ export function MainWaveform({
</div>
)}
{/* Zoom controls (Traktor-style +/-). stopPropagation on down so tapping a
button doesn't begin a scratch drag. */}
<div className="absolute right-2 top-2 flex flex-col gap-1">
{/* Zoom in · raw/bars · zoom out, centred on the waveform's height.
stopPropagation on down so tapping a button doesn't begin a scratch
drag. */}
<div className="absolute right-2 top-1/2 flex -translate-y-1/2 flex-col gap-1">
<button
onPointerDown={(e) => e.stopPropagation()}
onClick={zoomIn}
@@ -275,6 +308,17 @@ export function MainWaveform({
>
+
</button>
<button
onPointerDown={(e) => e.stopPropagation()}
onClick={() => onRawModeChange?.(!rawMode)}
aria-pressed={rawMode}
aria-label="Raw waveform"
className="btn-glass flex h-6 w-6 items-center justify-center rounded-lg text-text backdrop-blur-md"
title={rawMode ? 'Raw waveform — click for bars' : 'Bars — click for the raw waveform: the exact shape of every transient'}
>
{/* The icon shows the mode in force: bars, or the raw line. */}
<Icon name={rawMode ? 'sine' : 'wave'} size={14} />
</button>
<button
onPointerDown={(e) => e.stopPropagation()}
onClick={zoomOut}
+24 -6
View File
@@ -21,6 +21,7 @@ import { CUE_TYPE_LABELS, cueTypeColor } from '../lib/cues'
import { keyColor } from '../lib/format'
import { readOnlyShort, readOnlyNotice } from '../lib/platformCopy'
import { analyzeStems, analyzeWaveform, type WaveColumn } from '../lib/waveform'
import type { RawWave } from '../lib/rawWave'
import { setAmbientFromArt } from '../lib/ambient'
import { Icon } from '../lib/icons'
import { ScratchEngine } from '../lib/scratchEngine'
@@ -114,6 +115,9 @@ export function PrepStrip({
const [duration, setDuration] = useState(0)
const [ready, setReady] = useState(false)
const [cols, setCols] = useState<WaveColumn[] | null>(null)
// The raw waveform's data: the mix's samples, or one per stem lane.
const [raw, setRaw] = useState<RawWave | null>(null)
const [stemRaws, setStemRaws] = useState<RawWave[] | null>(null)
// A stem track: its stems (from the FILE), their lanes, and which are muted
// — reset per track, as Traktor's stem deck does. There is no separate solo
// state: solo is a shortcut that SETS the mutes, so every stem stays freely
@@ -130,6 +134,7 @@ export function PrepStrip({
const [selectedSlot, setSelectedSlot] = useState<number | null>(null)
const [cuePoint, setCuePoint] = useState(0) // floating "CUE" point (frontend-only)
const [secPerView, setSecPerView] = useState(DEFAULT_SEC) // main-waveform zoom (persisted)
const [rawWave, setRawWave] = useState(false) // main waveform: raw line instead of bars (persisted)
// ONE size, in beats, for beat loops and beat jump alike (persisted).
const [beatSize, setBeatSize] = useState(4)
const [loopMode, setLoopMode] = useState<LoopMode>('beat')
@@ -164,7 +169,7 @@ export function PrepStrip({
const trackId = track?.id ?? null
// Main-waveform zoom persists to userprefs.json so it survives track switches
// Main-waveform zoom (and the raw/bars toggle) persists to userprefs.json so it survives track switches
// (MainWaveform unmounts during each load) and app restarts. Hydrate once from
// the shared ['prefs'] query, then persist changes through the shared,
// debounced prefs writer (as App's column layout does).
@@ -178,11 +183,12 @@ export function PrepStrip({
if (typeof z === 'number' && isFinite(z)) {
setSecPerView(Math.min(MAX_SEC, Math.max(MIN_SEC, z)))
}
if ((prefsQuery.data as { mainWaveRaw?: unknown }).mainWaveRaw === true) setRawWave(true)
}, [prefsQuery.data])
useEffect(() => {
if (!zoomHydratedRef.current) return // don't clobber saved prefs pre-hydration
writePrefs({ mainZoomSec: secPerView })
}, [secPerView, writePrefs])
writePrefs({ mainZoomSec: secPerView, mainWaveRaw: rawWave })
}, [secPerView, rawWave, writePrefs])
// The loop/jump size persists the same way (survives track switches +
// restarts). `beatJumpBeats` is its name from when only beat jump had a size;
@@ -271,11 +277,15 @@ export function PrepStrip({
scratchRef.current = null
if (!trackId) {
setCols(null)
setRaw(null)
setStemRaws(null)
setWaveStatus('loading')
return
}
let cancelled = false
setCols(null)
setRaw(null)
setStemRaws(null)
setStems(null)
setStemLanes(null)
setWaveStatus('loading')
@@ -286,15 +296,17 @@ export function PrepStrip({
.then(async (found) => {
if (found.length) {
const res = await analyzeStems(found.map((_, k) => trackStemUrl(origin, trackId, k)))
return { cols: res.cols, buffers: res.buffers, lanes: res.lanes, stems: found }
return { cols: res.cols, buffers: res.buffers, lanes: res.lanes, stems: found, raw: null, raws: res.raws }
}
const res = await analyzeWaveform(trackAudioUrl(origin, trackId))
return { cols: res.cols, buffers: [res.buffer], lanes: null, stems: null }
return { cols: res.cols, buffers: [res.buffer], lanes: null, stems: null, raw: res.raw, raws: null }
})
load
.then((res) => {
if (cancelled) return
setCols(res.cols)
setRaw(res.raw)
setStemRaws(res.raws)
setStems(res.stems)
setStemLanes(res.lanes)
setWaveStatus('ready')
@@ -917,7 +929,9 @@ export function PrepStrip({
const activeMarkerIndex = grid ? grid.markerIndexAt(current) : -1
const activeMarker = activeMarkerIndex >= 0 ? grid!.markers[activeMarkerIndex] : null
// ~4px of the current zoom, so "on the marker" stays usable at 2s and 64s/view.
const markerHitSec = Math.max(0.01, secPerView / 250)
// ~4 px of the view either side, so "at a marker" stays a marker-sized
// target at deep zoom instead of 4% of a 0.25 s view.
const markerHitSec = Math.max(0.001, secPerView / 250)
const atMarker = !!grid && grid.isOnMarker(current, markerHitSec)
const beforeFirstMarker = !!grid && current < grid.markers[0].start - GRID_EPS
@@ -1097,6 +1111,7 @@ export function PrepStrip({
stems && stemLanes
? stems.map((st, k) => ({
data: stemLanes[k],
raw: stemRaws?.[k] ?? null,
name: st.name,
color: st.color,
audible: stemLevels[k] > 0,
@@ -1390,6 +1405,9 @@ export function PrepStrip({
{showWaves ? (
<MainWaveform
cols={cols}
raw={raw}
rawMode={rawWave}
onRawModeChange={setRawWave}
currentTime={current}
duration={duration}
cues={cueData?.cues ?? []}