mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented STEM playback
This commit is contained in:
1 parent
ae03a93f98
commit
ecc0e4c8df
13 files changed
+775
-61
No files matched your search
@@ -2,7 +2,7 @@ import { useEffect, useRef } from 'react'
|
||||
import type { CuePoint } from '../api'
|
||||
import type { BeatGrid } from '../lib/beatgrid'
|
||||
import { drawBeatgrid, drawCuePoint, drawCues, drawLoop, drawLoopIn, drawPlayhead } from '../lib/cues'
|
||||
import { paintWave, type WaveColumn } from '../lib/waveform'
|
||||
import { paintLane, paintWave, type WaveColumn } from '../lib/waveform'
|
||||
|
||||
interface Props {
|
||||
cols: WaveColumn[]
|
||||
@@ -28,8 +28,26 @@ interface Props {
|
||||
onScratchStart: () => void
|
||||
onScratchMove: (t: number) => void
|
||||
onScratchEnd: (t: number) => void
|
||||
/** A stem track: one lane per stem instead of the three-band mix view. */
|
||||
lanes?: StemLane[] | null
|
||||
/** Click = mute toggle; Alt/Option-click = solo (mute all the others). */
|
||||
onStemToggle?: (index: number, solo: boolean) => void
|
||||
}
|
||||
|
||||
export interface StemLane {
|
||||
data: Float32Array
|
||||
name: string
|
||||
color: string
|
||||
/** Not muted. */
|
||||
audible: boolean
|
||||
/** The only stem playing (drawn with a ring). */
|
||||
solo: boolean
|
||||
/** Its keyboard shortcut, for the tooltip ("Q"). */
|
||||
shortcut: string
|
||||
}
|
||||
|
||||
const ALT = typeof navigator !== 'undefined' && /Mac/.test(navigator.platform) ? 'Option' : 'Alt'
|
||||
|
||||
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)
|
||||
@@ -60,6 +78,8 @@ export function MainWaveform({
|
||||
onScratchStart,
|
||||
onScratchMove,
|
||||
onScratchEnd,
|
||||
lanes = null,
|
||||
onStemToggle,
|
||||
}: Props) {
|
||||
const wrapRef = useRef<HTMLDivElement>(null)
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null)
|
||||
@@ -90,10 +110,19 @@ export function MainWaveform({
|
||||
const endSec = currentTime + half
|
||||
// 2 px bars with a 1 px gap: the texture that makes the scrolling view
|
||||
// read as discrete energy rather than a smear.
|
||||
paintWave(ctx, cols, w, h, startSec / duration, endSec / duration, {
|
||||
bar: Math.max(1, Math.round(2 * dpr)),
|
||||
gap: Math.max(1, Math.round(dpr)),
|
||||
})
|
||||
const bars = { bar: Math.max(1, Math.round(2 * dpr)), gap: Math.max(1, Math.round(dpr)) }
|
||||
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) => {
|
||||
paintLane(ctx, lane.data, lane.color, w, k * laneH, laneH, startSec / duration, endSec / duration, bars,
|
||||
!lane.audible)
|
||||
})
|
||||
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 {
|
||||
paintWave(ctx, cols, w, h, startSec / duration, endSec / duration, bars)
|
||||
}
|
||||
|
||||
const timeToX = (t: number) => ((t - startSec) / secPerView) * w
|
||||
// Loop band (under the grid/cues), then beatgrid, then cue markers.
|
||||
@@ -208,6 +237,32 @@ export function MainWaveform({
|
||||
>
|
||||
<canvas ref={canvasRef} className="block h-full w-full" />
|
||||
|
||||
{/* One mute button per stem lane, at its left edge. stopPropagation on
|
||||
down, like the zoom buttons, so a click never starts a scratch. */}
|
||||
{lanes && lanes.length > 0 && (
|
||||
<div className="absolute inset-y-0 left-1.5 flex flex-col">
|
||||
{lanes.map((lane, k) => (
|
||||
<div key={k} className="flex flex-1 items-center">
|
||||
<button
|
||||
onPointerDown={(e) => e.stopPropagation()}
|
||||
onClick={(e) => onStemToggle?.(k, e.altKey)}
|
||||
title={`${lane.name} — click to mute, ${ALT}-click to mute all the others (${lane.shortcut})`}
|
||||
aria-pressed={lane.audible}
|
||||
className={`flex h-5 min-w-5 items-center justify-center rounded-md px-1 font-mono text-[10px] font-semibold backdrop-blur-md transition-opacity ${
|
||||
lane.audible ? '' : 'opacity-40'
|
||||
} ${lane.solo ? 'ring-1 ring-white/80' : ''}`}
|
||||
style={{
|
||||
color: lane.audible ? '#0b0b0f' : lane.color,
|
||||
background: lane.audible ? lane.color : 'rgba(255,255,255,0.06)',
|
||||
}}
|
||||
>
|
||||
{lane.name.slice(0, 1).toUpperCase()}
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</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">
|
||||
|
||||
@@ -8,16 +8,19 @@ import {
|
||||
type GridMarker,
|
||||
type Track,
|
||||
type TrackCues,
|
||||
type StemInfo,
|
||||
type TrackOrigin,
|
||||
trackAudioUrl,
|
||||
trackCuesFor,
|
||||
trackStemsFor,
|
||||
trackStemUrl,
|
||||
} from '../api'
|
||||
import { buildBeatGrid, GRID_EPS } from '../lib/beatgrid'
|
||||
import { slotLabeller, useCaps } from '../lib/capabilities'
|
||||
import { CUE_TYPE_LABELS, cueTypeColor } from '../lib/cues'
|
||||
import { keyColor } from '../lib/format'
|
||||
import { readOnlyShort, readOnlyNotice } from '../lib/platformCopy'
|
||||
import { analyzeWaveform, type WaveColumn } from '../lib/waveform'
|
||||
import { analyzeStems, analyzeWaveform, type WaveColumn } from '../lib/waveform'
|
||||
import { setAmbientFromArt } from '../lib/ambient'
|
||||
import { Icon } from '../lib/icons'
|
||||
import { ScratchEngine } from '../lib/scratchEngine'
|
||||
@@ -28,9 +31,12 @@ import { ContextMenu, type MenuItem } from './ContextMenu'
|
||||
import { BpmReadout, GridEditStrip, TempoControls, TempoFold } from './GridControls'
|
||||
import { HotcueBar } from './HotcueBar'
|
||||
import { LoopControls, LOOP_SIZES, type LoopMode } from './LoopControls'
|
||||
import { MainWaveform, MIN_SEC, MAX_SEC, DEFAULT_SEC } from './MainWaveform'
|
||||
import { MainWaveform, MIN_SEC, MAX_SEC, DEFAULT_SEC, type StemLane } from './MainWaveform'
|
||||
import { OverviewWaveform } from './OverviewWaveform'
|
||||
|
||||
/** Keys that mute stems 1-4 (Shift = solo), by `KeyboardEvent.code`. */
|
||||
const STEM_KEYS = ['KeyQ', 'KeyW', 'KeyE', 'KeyR']
|
||||
|
||||
interface Props {
|
||||
/** The track currently loaded into the prep deck, if any. */
|
||||
track: Track | null
|
||||
@@ -107,6 +113,13 @@ export function PrepStrip({
|
||||
const [duration, setDuration] = useState(0)
|
||||
const [ready, setReady] = useState(false)
|
||||
const [cols, setCols] = useState<WaveColumn[] | 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
|
||||
// mutable afterwards.
|
||||
const [stems, setStems] = useState<StemInfo[] | null>(null)
|
||||
const [stemLanes, setStemLanes] = useState<Float32Array[] | null>(null)
|
||||
const [stemMuted, setStemMuted] = useState<boolean[]>([])
|
||||
const [waveStatus, setWaveStatus] = useState<'loading' | 'ready' | 'error'>('loading')
|
||||
const [cueData, setCueData] = useState<TrackCues | null>(null)
|
||||
// The beatgrid, rebuilt whenever the cue data is replaced (every edit returns
|
||||
@@ -254,6 +267,7 @@ export function PrepStrip({
|
||||
previewRef.current = null
|
||||
setPreviewing(false)
|
||||
setCueHeld(false)
|
||||
setStemMuted([])
|
||||
}, [trackId])
|
||||
|
||||
// Recolour the whole app from this track's cover art. A device's art is not
|
||||
@@ -274,23 +288,40 @@ export function PrepStrip({
|
||||
}
|
||||
let cancelled = false
|
||||
setCols(null)
|
||||
setStems(null)
|
||||
setStemLanes(null)
|
||||
setWaveStatus('loading')
|
||||
analyzeWaveform(trackAudioUrl(origin, trackId))
|
||||
// A stem FILE loads its four stems and plays their sum (decided: always,
|
||||
// for every stem track); anything else loads as one buffer, as before.
|
||||
const load = trackStemsFor(origin, trackId)
|
||||
.catch(() => [] as StemInfo[])
|
||||
.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 }
|
||||
}
|
||||
const res = await analyzeWaveform(trackAudioUrl(origin, trackId))
|
||||
return { cols: res.cols, buffers: [res.buffer], lanes: null, stems: null }
|
||||
})
|
||||
load
|
||||
.then((res) => {
|
||||
if (cancelled) return
|
||||
setCols(res.cols)
|
||||
setStems(res.stems)
|
||||
setStemLanes(res.lanes)
|
||||
setWaveStatus('ready')
|
||||
const ctx = getCtx()
|
||||
const sc = new ScratchEngine()
|
||||
sc.load(res.buffer)
|
||||
if (res.stems) sc.loadStems(res.buffers, res.buffers.map(() => 1))
|
||||
else sc.load(res.buffers[0])
|
||||
sc.attach(getScratchCtx()) // own context; warm up so the first scratch isn't silent
|
||||
scratchRef.current = sc
|
||||
if (!playbackRef.current) {
|
||||
playbackRef.current = new PlaybackEngine()
|
||||
playbackRef.current.setOnEnded(() => setPlaying(false))
|
||||
}
|
||||
playbackRef.current.load(ctx, res.buffer)
|
||||
setDuration(res.buffer.duration)
|
||||
playbackRef.current.load(ctx, res.stems ? res.buffers : res.buffers[0])
|
||||
setDuration(res.buffers[0].duration)
|
||||
setReady(true)
|
||||
loadedIdRef.current = trackId
|
||||
// A play was requested (library row button) → start now that it's ready.
|
||||
@@ -1046,6 +1077,43 @@ export function PrepStrip({
|
||||
// (Shift+↑/↓ is the track table's extend-selection).
|
||||
// Digits are read from e.code (layout-/Shift-independent) and a ref holds the
|
||||
// latest handlers so the listener attaches once and never goes stale.
|
||||
// ---- stems: mute / solo ------------------------------------------------------
|
||||
// What each stem plays at: a muted stem is 0. Both engines follow it (the
|
||||
// scratch engine re-sums).
|
||||
const stemLevels = useMemo(() => (stems ?? []).map((_, k) => (stemMuted[k] ? 0 : 1)), [stems, stemMuted])
|
||||
const stemLevelKey = stemLevels.join(',')
|
||||
useEffect(() => {
|
||||
if (!stems) return
|
||||
playbackRef.current?.setGains(stemLevels)
|
||||
scratchRef.current?.setLevels(stemLevels)
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [stemLevelKey])
|
||||
// Solo = "mute every stem but this one". Soloing a stem that is already the
|
||||
// only one playing brings them all back (so solo twice undoes it).
|
||||
const toggleStem = (k: number, solo: boolean) => {
|
||||
if (!stems || k >= stems.length) return
|
||||
setStemMuted((m) => {
|
||||
if (!solo) return stems.map((_, i) => (i === k ? !m[i] : !!m[i]))
|
||||
const alone = stems.every((_, i) => (i === k ? !m[i] : !!m[i]))
|
||||
return stems.map((_, i) => (alone ? false : i !== k))
|
||||
})
|
||||
}
|
||||
// The ring on a lane button: that stem is the only one playing.
|
||||
const soloed = stemLevels.filter((l) => l > 0).length === 1 && stemLevels.length > 1
|
||||
? stemLevels.findIndex((l) => l > 0)
|
||||
: null
|
||||
const lanes: StemLane[] | null =
|
||||
stems && stemLanes
|
||||
? stems.map((st, k) => ({
|
||||
data: stemLanes[k],
|
||||
name: st.name,
|
||||
color: st.color,
|
||||
audible: stemLevels[k] > 0,
|
||||
solo: soloed === k,
|
||||
shortcut: STEM_KEYS[k] ? STEM_KEYS[k].slice(3) : '',
|
||||
}))
|
||||
: null
|
||||
|
||||
const shortcutsRef = useRef({
|
||||
toggle,
|
||||
onSlotPress,
|
||||
@@ -1058,6 +1126,7 @@ export function PrepStrip({
|
||||
prevMarker,
|
||||
nextMarker,
|
||||
slotCount,
|
||||
toggleStem,
|
||||
})
|
||||
shortcutsRef.current = {
|
||||
toggle,
|
||||
@@ -1071,6 +1140,7 @@ export function PrepStrip({
|
||||
prevMarker,
|
||||
nextMarker,
|
||||
slotCount,
|
||||
toggleStem,
|
||||
}
|
||||
useEffect(() => {
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
@@ -1097,6 +1167,14 @@ export function PrepStrip({
|
||||
shortcutsRef.current.onCuePress()
|
||||
return
|
||||
}
|
||||
// Q W E R: mute a stem; Shift+ solos it (a stem track only — elsewhere
|
||||
// there is nothing to toggle, and the keys stay free).
|
||||
const stemKey = STEM_KEYS.indexOf(e.code)
|
||||
if (stemKey >= 0) {
|
||||
e.preventDefault()
|
||||
shortcutsRef.current.toggleStem(stemKey, e.shiftKey)
|
||||
return
|
||||
}
|
||||
if (e.code === 'ArrowLeft') {
|
||||
e.preventDefault()
|
||||
if (e.shiftKey) shortcutsRef.current.prevMarker()
|
||||
@@ -1337,6 +1415,8 @@ export function PrepStrip({
|
||||
onScratchStart={onScratchStart}
|
||||
onScratchMove={onScratchMove}
|
||||
onScratchEnd={onScratchEnd}
|
||||
lanes={lanes}
|
||||
onStemToggle={toggleStem}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-xs text-faint">
|
||||
|
||||
@@ -10,6 +10,7 @@ interface Props {
|
||||
// says "Cmd/Ctrl" everywhere is harder to scan than one that names your key.
|
||||
const IS_MAC = /Mac|iPhone|iPad/.test(navigator.userAgent)
|
||||
const MOD = IS_MAC ? '⌘' : 'Ctrl'
|
||||
const ALT_KEY = IS_MAC ? '⌥' : 'Alt'
|
||||
|
||||
type Row = { keys: string[][]; action: string }
|
||||
type Section = { title: string; rows: Row[] }
|
||||
@@ -36,6 +37,9 @@ function sections(slots: number): Section[] {
|
||||
{ keys: [['Shift', '←'], ['Shift', '→']], action: 'Previous / next grid marker' },
|
||||
{ keys: [[pads]], action: 'Hotcue — set if empty, else jump (hold to preview)' },
|
||||
{ keys: [['Shift', pads]], action: 'Delete hotcue' },
|
||||
{ keys: [['Q'], ['W'], ['E'], ['R']], action: 'Stem track: mute / unmute stem 1–4' },
|
||||
{ keys: [['Shift', 'Q–R']], action: 'Stem track: solo — mute every other stem (again to unmute all)' },
|
||||
{ keys: [[ALT_KEY, 'Click', 'stem']], action: 'Solo a stem from its lane button' },
|
||||
{ keys: [['Drag waveform']], action: 'Scratch' },
|
||||
{ keys: [[MOD, 'Scroll']], action: 'Zoom the waveform' },
|
||||
{ keys: [['Right-click', 'pad']], action: 'Hotcue type, rename, delete' },
|
||||
|
||||
Reference in new issue
Block a user