mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Fixed looping issues and tied it to hotcue system
This commit is contained in:
1 parent
9dc020140a
commit
fdcae5e0f5
6 files changed
+119
-37
No files matched your search
+8
-2
@@ -218,8 +218,14 @@ export const api = {
|
||||
audioUrl: (trackId: string) => `/api/tracks/audio?track_id=${encodeURIComponent(trackId)}`,
|
||||
trackCues: (trackId: string) =>
|
||||
getJSON<TrackCues>(`/api/tracks/cues?track_id=${encodeURIComponent(trackId)}`),
|
||||
createHotcue: (trackId: string, slot: number, start: number, type: number) =>
|
||||
send<TrackCues>('POST', '/api/tracks/hotcue', { track_id: trackId, slot, start, type }),
|
||||
createHotcue: (trackId: string, slot: number, start: number, type: number, length = 0) =>
|
||||
send<TrackCues>('POST', '/api/tracks/hotcue', {
|
||||
track_id: trackId,
|
||||
slot,
|
||||
start,
|
||||
type,
|
||||
length,
|
||||
}),
|
||||
setHotcueType: (trackId: string, slot: number, type: number) =>
|
||||
send<TrackCues>('PATCH', '/api/tracks/hotcue', { track_id: trackId, slot, type }),
|
||||
deleteHotcue: (trackId: string, slot: number) =>
|
||||
|
||||
@@ -28,6 +28,8 @@ const CUE_TYPES: { value: number; label: string }[] = [
|
||||
{ value: 2, label: 'Fade-Out' },
|
||||
{ value: 3, label: 'Load' },
|
||||
]
|
||||
const LOOP_TYPE = 5
|
||||
const LOOP_SIZES = [1 / 32, 1 / 16, 1 / 8, 1 / 4, 1 / 2, 1, 2, 4, 8, 16, 32]
|
||||
|
||||
/**
|
||||
* The DJ-style "prep strip" across the top of the window: transport controls on
|
||||
@@ -178,6 +180,16 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
setCurrent(t)
|
||||
}
|
||||
|
||||
// User-driven seek from the waveforms: navigating away drops the active loop
|
||||
// (otherwise it would just pull playback back into the loop region).
|
||||
const seekManual = (t: number) => {
|
||||
if (loopActive) {
|
||||
playbackRef.current?.setLoopEnabled(false)
|
||||
setLoopActive(false)
|
||||
}
|
||||
seek(t)
|
||||
}
|
||||
|
||||
// ---- scratch ----------------------------------------------------------
|
||||
const finalizeScratch = () => {
|
||||
cancelAnimationFrame(scratchRafRef.current)
|
||||
@@ -269,7 +281,16 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
const eng = playbackRef.current
|
||||
const bpm = cueData?.bpm ?? null
|
||||
if (!eng || !eng.ready || !bpm || bpm <= 0) return
|
||||
const start = snapTime(eng.getPosition())
|
||||
// Pressing the size of the loop that's already playing disables it.
|
||||
if (loopActive && activeBeats === beats) {
|
||||
eng.setLoopEnabled(false)
|
||||
setLoopActive(false)
|
||||
setCurrent(eng.getPosition())
|
||||
return
|
||||
}
|
||||
// Resizing an active loop keeps its start locked; only a fresh loop starts
|
||||
// at the current playhead.
|
||||
const start = loopActive && loopRegion ? loopRegion.start : snapTime(eng.getPosition())
|
||||
engagLoop(start, start + beats * (60 / bpm), beats)
|
||||
}
|
||||
|
||||
@@ -306,17 +327,47 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
qc.invalidateQueries({ queryKey: ['playlist'] })
|
||||
}
|
||||
|
||||
// Map a loop length (seconds) back to a preset beat count for the size
|
||||
// highlight, snapping to the nearest preset when it's close (float tolerance).
|
||||
const beatsForLength = (length: number): number | null => {
|
||||
const bpm = cueData?.bpm ?? null
|
||||
if (!bpm || bpm <= 0) return null
|
||||
const beats = length / (60 / bpm)
|
||||
let best: number | null = null
|
||||
let bestDiff = Infinity
|
||||
for (const s of LOOP_SIZES) {
|
||||
const diff = Math.abs(s - beats)
|
||||
if (diff < bestDiff) {
|
||||
bestDiff = diff
|
||||
best = s
|
||||
}
|
||||
}
|
||||
return best != null && bestDiff <= beats * 0.05 ? best : null
|
||||
}
|
||||
|
||||
const onSlotClick = async (slot: number) => {
|
||||
if (!track) return
|
||||
const cue = hotcueAt(slot)
|
||||
if (cue) {
|
||||
seek(cue.start)
|
||||
// A loop hotcue jumps to its start AND re-engages a loop of its length.
|
||||
if (cue.type === LOOP_TYPE && cue.length > 0) {
|
||||
seek(cue.start)
|
||||
engagLoop(cue.start, cue.start + cue.length, beatsForLength(cue.length))
|
||||
} else {
|
||||
seek(cue.start)
|
||||
}
|
||||
setSelectedSlot(slot)
|
||||
return
|
||||
}
|
||||
try {
|
||||
const t = snapTime(playbackRef.current?.getPosition() ?? current)
|
||||
applyCueEdit(await api.createHotcue(track.id, slot, t, 0))
|
||||
// Setting a hotcue while a loop is active stores it as a loop hotcue.
|
||||
if (loopActive && loopRegion) {
|
||||
const len = loopRegion.end - loopRegion.start
|
||||
applyCueEdit(await api.createHotcue(track.id, slot, loopRegion.start, LOOP_TYPE, len))
|
||||
} else {
|
||||
const t = snapTime(playbackRef.current?.getPosition() ?? current)
|
||||
applyCueEdit(await api.createHotcue(track.id, slot, t, 0))
|
||||
}
|
||||
setSelectedSlot(slot)
|
||||
} catch (e) {
|
||||
onError?.((e as Error).message)
|
||||
@@ -402,7 +453,7 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
bpm={cueData?.bpm ?? null}
|
||||
gridAnchor={cueData?.grid_anchor ?? null}
|
||||
loop={activeLoop}
|
||||
onSeek={seek}
|
||||
onSeek={seekManual}
|
||||
onScratchStart={onScratchStart}
|
||||
onScratchMove={onScratchMove}
|
||||
onScratchEnd={onScratchEnd}
|
||||
@@ -421,7 +472,7 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
duration={duration}
|
||||
cues={cueData?.cues ?? []}
|
||||
loop={activeLoop}
|
||||
onSeek={seek}
|
||||
onSeek={seekManual}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
@@ -451,19 +502,23 @@ export function PrepStrip({ track, onError }: Props) {
|
||||
{selectedCue ? `Hotcue ${(selectedSlot ?? 0) + 1}` : 'No hotcue selected'}
|
||||
</span>
|
||||
<div className="flex-1" />
|
||||
<select
|
||||
value={selectedCue ? selectedCue.type : ''}
|
||||
disabled={!selectedCue}
|
||||
onChange={(e) => changeSelectedType(Number(e.target.value))}
|
||||
className="rounded border border-line bg-ink-850 px-2 py-0.5 text-text outline-none focus:border-accent disabled:opacity-40"
|
||||
>
|
||||
{!selectedCue && <option value="">—</option>}
|
||||
{CUE_TYPES.map((t) => (
|
||||
<option key={t.value} value={t.value}>
|
||||
{t.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
{selectedCue && selectedCue.type === LOOP_TYPE ? (
|
||||
<span className="rounded border border-line px-2 py-0.5 text-mint">Loop</span>
|
||||
) : (
|
||||
<select
|
||||
value={selectedCue ? selectedCue.type : ''}
|
||||
disabled={!selectedCue}
|
||||
onChange={(e) => changeSelectedType(Number(e.target.value))}
|
||||
className="rounded border border-line bg-ink-850 px-2 py-0.5 text-text outline-none focus:border-accent disabled:opacity-40"
|
||||
>
|
||||
{!selectedCue && <option value="">—</option>}
|
||||
{CUE_TYPES.map((t) => (
|
||||
<option key={t.value} value={t.value}>
|
||||
{t.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
<button
|
||||
onClick={deleteSelected}
|
||||
disabled={!selectedCue}
|
||||
|
||||
@@ -125,8 +125,19 @@ export class PlaybackEngine {
|
||||
}
|
||||
}
|
||||
|
||||
// Snapshot the true buffer position into (startOffset, startedAt) so the
|
||||
// position model stays correct after the loop params change. Must be called
|
||||
// BEFORE mutating loopOn/loopStart/loopEnd (it reads the current/old params).
|
||||
private reanchor(): void {
|
||||
if (this._playing && this.ctx) {
|
||||
this.startOffset = this.getPosition()
|
||||
this.startedAt = this.ctx.currentTime
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the loop region and whether it's active. Bounds apply live if playing. */
|
||||
setLoop(start: number, end: number, enabled: boolean): void {
|
||||
this.reanchor() // capture the real position under the OLD loop state first
|
||||
this.loopStart = start
|
||||
this.loopEnd = end
|
||||
this.loopOn = enabled
|
||||
@@ -139,10 +150,10 @@ export class PlaybackEngine {
|
||||
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)
|
||||
// If the playhead is already at/past the loop end, jump into it (the live
|
||||
// node won't loop back on its own from beyond loopEnd).
|
||||
if (enabled && end > start && this.startOffset >= end) {
|
||||
this.seek(start)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user