Implemented looping

This commit is contained in:
Ben committed 2026-07-04 19:39:03 +02:00
1 parent f88f1932dc
commit 9dc020140a
12 files changed
+847 -143

No files matched your search

+56 -20
View File
@@ -1,18 +1,18 @@
import type { CuePoint } from '../api'
// Default marker colours by Traktor cue type, used when a cue has no explicit
// colour of its own.
// Marker colour by Traktor cue type. Type is authoritative here (the NML's
// stored per-cue colour is intentionally ignored) so each type reads consistently.
const TYPE_COLORS: Record<number, string> = {
0: '#4f8cff', // cue — accent blue
1: '#34d399', // fade-in — mint
2: '#34d399', // fade-out — mint
3: '#ffb020', // load — gold
5: '#f472b6', // loop — pink
0: '#3b82f6', // cue — blue
1: '#ff8c2f', // fade-in — orange
2: '#ff8c2f', // fade-out — orange
3: '#ffd60a', // load — yellow
5: '#22c55e', // loop — green
}
const OTHER_COLOR = '#8b93a3' // gray
export function cueColor(cue: CuePoint): string {
if (cue.color && /^#[0-9a-f]{6}$/i.test(cue.color)) return cue.color
return TYPE_COLORS[cue.type] ?? '#8b93a3'
return TYPE_COLORS[cue.type] ?? OTHER_COLOR
}
function withAlpha(hex: string, a: number): string {
@@ -22,6 +22,14 @@ function withAlpha(hex: string, a: number): string {
return `rgba(${r},${g},${b},${a})`
}
// Black or white text, whichever reads better on the given marker colour.
export function contrastText(hex: string): string {
const r = parseInt(hex.slice(1, 3), 16)
const g = parseInt(hex.slice(3, 5), 16)
const b = parseInt(hex.slice(5, 7), 16)
return 0.299 * r + 0.587 * g + 0.114 * b > 140 ? '#0a0b0f' : '#ffffff'
}
/**
* Draw cue/loop markers onto a canvas. `timeToX` maps a time (seconds) to a
* device-pixel x. With `labels`, a small numbered flag is drawn at the top for
@@ -37,7 +45,10 @@ export function drawCues(
labels: boolean,
): void {
const lineW = Math.max(2, Math.round(2 * dpr))
const flag = Math.round(13 * dpr)
const outline = Math.max(1, Math.round(dpr))
const flagW = Math.round(12 * dpr)
const flagH = Math.round(14 * dpr)
const half = Math.floor(lineW / 2)
for (const cue of cues) {
const color = cueColor(cue)
const x = Math.round(timeToX(cue.start))
@@ -46,26 +57,51 @@ export function drawCues(
if (cue.length > 0) {
const x2 = Math.round(timeToX(cue.start + cue.length))
if (x2 > 0 && x < w) {
ctx.fillStyle = withAlpha(color, 0.18)
ctx.fillStyle = withAlpha(color, 0.2)
ctx.fillRect(x, 0, Math.max(1, x2 - x), h)
}
}
if (x < -flag || x > w + flag) continue
ctx.fillStyle = color
ctx.fillRect(x - Math.floor(lineW / 2), 0, lineW, h)
if (x < -flagW || x > w + flagW) continue
if (labels && cue.hotcue >= 0) {
// Dark outline behind the line so it reads on any waveform colour.
ctx.fillStyle = 'rgba(0,0,0,0.7)'
ctx.fillRect(x - half - outline, 0, lineW + outline * 2, h)
ctx.fillStyle = color
ctx.fillRect(x - half, 0, lineW, h)
if (labels) {
// A flag tab at the top for every cue (outlined), with the hotcue number.
ctx.fillStyle = 'rgba(0,0,0,0.7)'
ctx.fillRect(x - half - outline, 0, flagW + outline * 2, flagH + outline * 2)
ctx.fillStyle = color
ctx.fillRect(x, 0, flag, flag)
ctx.fillStyle = '#0a0b0f'
ctx.font = `${Math.round(9 * dpr)}px system-ui, sans-serif`
ctx.textBaseline = 'top'
ctx.fillText(String(cue.hotcue + 1), x + Math.round(3 * dpr), Math.round(2 * dpr))
ctx.fillRect(x - half, 0, flagW, flagH)
if (cue.hotcue >= 0) {
ctx.fillStyle = contrastText(color)
ctx.font = `bold ${Math.round(10 * dpr)}px system-ui, sans-serif`
ctx.textBaseline = 'top'
ctx.fillText(String(cue.hotcue + 1), x - half + Math.round(2 * dpr), Math.round(2 * dpr))
}
}
}
}
/** Draw the active loop region: a translucent green band with bright edges. */
export function drawLoop(
ctx: CanvasRenderingContext2D,
startX: number,
endX: number,
h: number,
dpr: number,
): void {
const edge = Math.max(1, Math.round(2 * dpr))
ctx.fillStyle = 'rgba(34,197,94,0.18)'
ctx.fillRect(startX, 0, Math.max(1, endX - startX), h)
ctx.fillStyle = 'rgba(34,197,94,0.95)'
ctx.fillRect(startX, 0, edge, h)
ctx.fillRect(endX - edge, 0, edge, h)
}
/**
* Draw the beatgrid: beat lines extrapolated from the grid anchor at 60/bpm
* spacing, across the visible window [startSec, endSec]. Downbeats (every 4th
+152
View File
@@ -0,0 +1,152 @@
// Web Audio playback for the prep deck. Plays the already-decoded AudioBuffer
// through an AudioBufferSourceNode, which gives sample-accurate, gapless loops
// via native loopStart/loopEnd — something an <audio> element can't do.
//
// A source node is one-shot, so play/seek recreate it; position is derived from
// the AudioContext clock (high-resolution, so the playhead is smooth without any
// wall-clock interpolation). Loop bounds can be changed on the live node, so
// enabling a loop mid-playback is seamless.
export class PlaybackEngine {
private ctx: AudioContext | null = null
private buffer: AudioBuffer | null = null
private gain: GainNode | null = null
private source: AudioBufferSourceNode | null = null
private startedAt = 0 // ctx time when the current source started
private startOffset = 0 // buffer position (s) at that moment / paused position
private _playing = false
private loopOn = false
private loopStart = 0
private loopEnd = 0
private onEnded: (() => void) | null = null
load(ctx: AudioContext, buffer: AudioBuffer): void {
this.ctx = ctx
this.buffer = buffer
if (!this.gain) {
this.gain = ctx.createGain()
this.gain.connect(ctx.destination)
}
this.stopSource()
this._playing = false
this.startOffset = 0
this.loopOn = false
}
setOnEnded(cb: () => void): void {
this.onEnded = cb
}
get ready(): boolean {
return !!this.buffer
}
get playing(): boolean {
return this._playing
}
get duration(): number {
return this.buffer?.duration ?? 0
}
get loopEnabled(): boolean {
return this.loopOn
}
getPosition(): number {
if (!this.buffer || !this.ctx || !this._playing) return this.startOffset
let pos = this.startOffset + (this.ctx.currentTime - this.startedAt)
if (this.loopOn && this.loopEnd > this.loopStart && pos >= this.loopEnd) {
const len = this.loopEnd - this.loopStart
pos = this.loopStart + ((pos - this.loopStart) % len)
}
return Math.min(pos, this.buffer.duration)
}
private startSource(offset: number): void {
if (!this.ctx || !this.buffer || !this.gain) return
const src = this.ctx.createBufferSource()
src.buffer = this.buffer
if (this.loopOn && this.loopEnd > this.loopStart) {
src.loop = true
src.loopStart = this.loopStart
src.loopEnd = this.loopEnd
}
src.connect(this.gain)
src.onended = () => {
if (this.source === src) {
// Natural end (our own stops null out onended first).
this._playing = false
this.source = null
this.onEnded?.()
}
}
const off = Math.max(0, Math.min(offset, this.buffer.duration - 0.001))
src.start(0, off)
this.source = src
this.startedAt = this.ctx.currentTime
this.startOffset = offset
this._playing = true
}
private stopSource(): void {
if (this.source) {
this.source.onended = null
try {
this.source.stop()
} catch {
/* already stopped */
}
this.source.disconnect()
this.source = null
}
}
play(): void {
if (!this.ctx || !this.buffer || this._playing) return
void this.ctx.resume()
let off = this.startOffset
if (off >= this.buffer.duration - 0.01) off = 0 // restart from top if at the end
this.startSource(off)
}
pause(): void {
if (!this._playing) return
this.startOffset = this.getPosition()
this.stopSource()
this._playing = false
}
seek(t: number): void {
const clamped = Math.max(0, Math.min(t, this.duration))
if (this._playing) {
this.stopSource()
this.startOffset = clamped
this.startSource(clamped)
} else {
this.startOffset = clamped
}
}
/** Set the loop region and whether it's active. Bounds apply live if playing. */
setLoop(start: number, end: number, enabled: boolean): void {
this.loopStart = start
this.loopEnd = end
this.loopOn = enabled
if (this.source) {
if (enabled && end > start) {
this.source.loop = true
this.source.loopStart = start
this.source.loopEnd = end
} else {
this.source.loop = false
}
}
// If the playhead is already past the loop, jump into it so it engages now.
if (enabled && this._playing) {
const pos = this.getPosition()
if (pos >= end) this.seek(start)
}
}
setLoopEnabled(enabled: boolean): void {
this.setLoop(this.loopStart, this.loopEnd, enabled)
}
}