mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented beatgrid and cuepoint display in waveform
This commit is contained in:
1 parent
5454784cff
commit
f88f1932dc
7 files changed
+246
-9
No files matched your search
@@ -1,10 +1,15 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type { CuePoint } from '../api'
|
||||
import { drawBeatgrid, drawCues } from '../lib/cues'
|
||||
import { paintWave, type WaveColumn } from '../lib/waveform'
|
||||
|
||||
interface Props {
|
||||
cols: WaveColumn[]
|
||||
currentTime: number
|
||||
duration: number
|
||||
cues: CuePoint[]
|
||||
bpm: number | null
|
||||
gridAnchor: number | null
|
||||
onSeek: (t: number) => void
|
||||
onScratchStart: () => void
|
||||
onScratchMove: (t: number) => void
|
||||
@@ -27,6 +32,9 @@ export function MainWaveform({
|
||||
cols,
|
||||
currentTime,
|
||||
duration,
|
||||
cues,
|
||||
bpm,
|
||||
gridAnchor,
|
||||
onSeek,
|
||||
onScratchStart,
|
||||
onScratchMove,
|
||||
@@ -58,9 +66,18 @@ export function MainWaveform({
|
||||
|
||||
if (cols.length && duration > 0) {
|
||||
const half = secPerView / 2
|
||||
const startFrac = (currentTime - half) / duration
|
||||
const endFrac = (currentTime + half) / duration
|
||||
paintWave(ctx, cols, w, h, startFrac, endFrac)
|
||||
const startSec = currentTime - half
|
||||
const endSec = currentTime + half
|
||||
paintWave(ctx, cols, w, h, startSec / duration, endSec / duration)
|
||||
|
||||
// Beatgrid, then cue markers (extrapolated across the visible window).
|
||||
if (bpm && gridAnchor != null) {
|
||||
drawBeatgrid(ctx, bpm, gridAnchor, startSec, endSec, w, h, dpr)
|
||||
}
|
||||
if (cues.length) {
|
||||
const timeToX = (t: number) => ((t - startSec) / secPerView) * w
|
||||
drawCues(ctx, cues, w, h, dpr, timeToX, true)
|
||||
}
|
||||
}
|
||||
|
||||
// Fixed centre playhead.
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { useCallback, useEffect, useRef } from 'react'
|
||||
import type { CuePoint } from '../api'
|
||||
import { drawCues } from '../lib/cues'
|
||||
import { paintWave, type WaveColumn } from '../lib/waveform'
|
||||
|
||||
interface Props {
|
||||
cols: WaveColumn[]
|
||||
currentTime: number
|
||||
duration: number
|
||||
cues: CuePoint[]
|
||||
onSeek: (t: number) => void
|
||||
}
|
||||
|
||||
@@ -13,12 +16,13 @@ interface Props {
|
||||
* offscreen canvas) with a moving playhead + dimmed played region. Click to
|
||||
* seek anywhere in the track.
|
||||
*/
|
||||
export function OverviewWaveform({ cols, currentTime, duration, onSeek }: Props) {
|
||||
export function OverviewWaveform({ cols, currentTime, duration, cues, onSeek }: Props) {
|
||||
const wrapRef = useRef<HTMLDivElement>(null)
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null)
|
||||
const cacheRef = useRef<HTMLCanvasElement | null>(null)
|
||||
const timeRef = useRef(0)
|
||||
const durRef = useRef(0)
|
||||
const cuesRef = useRef<CuePoint[]>(cues)
|
||||
|
||||
const renderCache = useCallback(() => {
|
||||
const canvas = canvasRef.current
|
||||
@@ -49,7 +53,10 @@ export function OverviewWaveform({ cols, currentTime, duration, onSeek }: Props)
|
||||
const px = Math.round((timeRef.current / dur) * w)
|
||||
ctx.fillStyle = 'rgba(10,11,15,0.55)'
|
||||
ctx.fillRect(0, 0, px, h)
|
||||
const pw = Math.max(2, Math.round(2 * (window.devicePixelRatio || 1)))
|
||||
// Cue markers (no beatgrid/labels in the overview).
|
||||
const dpr = window.devicePixelRatio || 1
|
||||
drawCues(ctx, cuesRef.current, w, h, dpr, (t) => (t / dur) * w, false)
|
||||
const pw = Math.max(2, Math.round(2 * dpr))
|
||||
ctx.fillStyle = '#e6e9ef'
|
||||
ctx.fillRect(px - Math.floor(pw / 2), 0, pw, h)
|
||||
}
|
||||
@@ -86,6 +93,12 @@ export function OverviewWaveform({ cols, currentTime, duration, onSeek }: Props)
|
||||
draw()
|
||||
}, [currentTime, duration, draw])
|
||||
|
||||
// Redraw when the cue set changes (new track).
|
||||
useEffect(() => {
|
||||
cuesRef.current = cues
|
||||
draw()
|
||||
}, [cues, draw])
|
||||
|
||||
const seek = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (!duration) return
|
||||
const rect = e.currentTarget.getBoundingClientRect()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { api, type Track } from '../api'
|
||||
import { api, type Track, type TrackCues } from '../api'
|
||||
import { analyzeWaveform, type WaveColumn } from '../lib/waveform'
|
||||
import { ScratchEngine } from '../lib/scratchEngine'
|
||||
import { MainWaveform } from './MainWaveform'
|
||||
@@ -33,6 +33,7 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
const [loadError, setLoadError] = useState(false)
|
||||
const [cols, setCols] = useState<WaveColumn[] | null>(null)
|
||||
const [waveStatus, setWaveStatus] = useState<'loading' | 'ready' | 'error'>('loading')
|
||||
const [cueData, setCueData] = useState<TrackCues | null>(null)
|
||||
|
||||
// Scratch engine (Web Audio) — holds the decoded buffer for the loaded track.
|
||||
const audioCtxRef = useRef<AudioContext | null>(null)
|
||||
@@ -81,6 +82,27 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
}
|
||||
}, [trackId])
|
||||
|
||||
// Fetch beatgrid + cue markers for the loaded track.
|
||||
useEffect(() => {
|
||||
if (!trackId) {
|
||||
setCueData(null)
|
||||
return
|
||||
}
|
||||
let cancelled = false
|
||||
setCueData(null)
|
||||
api
|
||||
.trackCues(trackId)
|
||||
.then((d) => {
|
||||
if (!cancelled) setCueData(d)
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setCueData(null)
|
||||
})
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [trackId])
|
||||
|
||||
const toggle = () => {
|
||||
const el = audioRef.current
|
||||
if (!el || !track) return
|
||||
@@ -94,18 +116,33 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
}
|
||||
}
|
||||
|
||||
// Smoothly advance the playhead/time while playing (timeupdate is only ~4Hz).
|
||||
// Smoothly advance the playhead while playing. HTMLAudioElement.currentTime
|
||||
// only updates in coarse steps (tens of ms), which looks choppy when zoomed in,
|
||||
// so we interpolate with the wall clock between the media clock's ticks and
|
||||
// re-anchor whenever it actually advances.
|
||||
useEffect(() => {
|
||||
if (!playing) return
|
||||
let raf = 0
|
||||
let lastRaw = -1
|
||||
let anchorAudio = 0
|
||||
let anchorPerf = 0
|
||||
const tick = () => {
|
||||
const el = audioRef.current
|
||||
if (el) setCurrent(el.currentTime)
|
||||
if (el) {
|
||||
const raw = el.currentTime
|
||||
if (raw !== lastRaw) {
|
||||
lastRaw = raw
|
||||
anchorAudio = raw
|
||||
anchorPerf = performance.now()
|
||||
}
|
||||
const est = anchorAudio + (performance.now() - anchorPerf) / 1000
|
||||
setCurrent(duration ? Math.min(est, duration) : est)
|
||||
}
|
||||
raf = requestAnimationFrame(tick)
|
||||
}
|
||||
raf = requestAnimationFrame(tick)
|
||||
return () => cancelAnimationFrame(raf)
|
||||
}, [playing])
|
||||
}, [playing, duration])
|
||||
|
||||
const seek = (t: number) => {
|
||||
const el = audioRef.current
|
||||
@@ -241,6 +278,9 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
cols={cols}
|
||||
currentTime={current}
|
||||
duration={duration}
|
||||
cues={cueData?.cues ?? []}
|
||||
bpm={cueData?.bpm ?? null}
|
||||
gridAnchor={cueData?.grid_anchor ?? null}
|
||||
onSeek={seek}
|
||||
onScratchStart={onScratchStart}
|
||||
onScratchMove={onScratchMove}
|
||||
@@ -258,6 +298,7 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
cols={cols}
|
||||
currentTime={current}
|
||||
duration={duration}
|
||||
cues={cueData?.cues ?? []}
|
||||
onSeek={seek}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user