Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/renderer/src/lib/components/WaveformDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
wf,
player.position,
player.display.zoom,
player.display.playheadAt,
player.displayGrid,
width,
height,
Expand All @@ -61,6 +62,7 @@
player.display.mode,
{
position: player.position,
playheadAt: player.display.playheadAt,
zoom: player.display.zoom,
cues: player.displayCues,
beatgrid: player.displayGrid,
Expand Down
5 changes: 3 additions & 2 deletions app/src/renderer/src/lib/components/deck/TempoLane.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
let flagShapes: FlagShape[] = []

const pxPerSecond = (): number => (player.waveform?.rate ?? 0) * player.display.zoom
const xOf = (ms: number): number => width / 2 + (ms / 1000 - player.position) * pxPerSecond()
const msAt = (x: number): number => (player.position + (x - width / 2) / Math.max(pxPerSecond(), 1e-6)) * 1000
const playheadX = (): number => width * player.display.playheadAt
const xOf = (ms: number): number => playheadX() + (ms / 1000 - player.position) * pxPerSecond()
const msAt = (x: number): number => (player.position + (x - playheadX()) / Math.max(pxPerSecond(), 1e-6)) * 1000
const localX = (e: MouseEvent): number => e.clientX - (e.currentTarget as HTMLElement).getBoundingClientRect().left
const nearest = (values: number[], target: number): number =>
values.reduce((best, value) => (Math.abs(value - target) < Math.abs(best - target) ? value : best))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import { getApp } from '$lib/context'
import {
DEFAULT_FALL_BLOCKS_PER_QUALITY,
DEFAULT_PLAYHEAD_PLACEMENT,
DEFAULT_QUALITIES,
FALL_BLOCKS_PER_QUALITY_STEP,
MAX_FALL_BLOCKS_PER_QUALITY,
MAX_QUALITY
MAX_QUALITY,
type PlayheadPlacement
} from '$lib/deck/display.svelte'
import { PLAYERS } from '$lib/hardware/players'
import { DEFAULT_LIBRARY_PREFS, type BrowserTrackStyle } from '$lib/library.svelte'
Expand All @@ -30,6 +32,10 @@
{ id: 'standard', label: 'Standard' },
{ id: 'alternative', label: 'Alternative' }
]
const PLAYHEAD_PLACEMENTS: Choice<PlayheadPlacement>[] = [
{ id: 'centre', label: 'Centre' },
{ id: 'leftThird', label: 'Left third' }
]

const defaults = allOf(
setting(
Expand Down Expand Up @@ -71,6 +77,11 @@
() => waveform.fallBlocksPerQuality,
DEFAULT_FALL_BLOCKS_PER_QUALITY,
(blocks) => waveform.setFallBlocksPerQuality(blocks)
),
setting(
() => waveform.playheadPlacement,
DEFAULT_PLAYHEAD_PLACEMENT,
(placement) => waveform.setPlayheadPlacement(placement)
)
)
</script>
Expand Down Expand Up @@ -119,6 +130,13 @@
</SettingsGroup>

<SettingsGroup title="Waveforms">
<SettingChoice
label="Playhead position"
description="Where the playhead sits in the scrolling waveform. Left third shows more of what is coming up."
options={PLAYHEAD_PLACEMENTS}
current={waveform.playheadPlacement}
onpick={(placement) => waveform.setPlayheadPlacement(placement)}
/>
{@render qualitySlider(
'Color quality',
'Draw the color detail waveform from the audio. Off uses the rekordbox analysis. Higher keeps more detail.',
Expand Down
23 changes: 22 additions & 1 deletion app/src/renderer/src/lib/deck/display.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Listeners } from '../listeners'
import { getSetting, setSetting } from '../settings'
import type { Waveform, WaveformMode } from '../waveform/data'
import type { WidthFraction } from '../waveform/frame'
import { gradientColors, pendingColors } from '../waveform/gradientColors'
import { rgbGradient } from '../waveform/rgbGradient.svelte'
import { type Percent, storedFlag, storeFlag, storedNumber, storedPercent } from './stored'
import { type Percent, storedChoice, storedFlag, storeFlag, storedNumber, storedPercent } from './stored'
import { WaveLoader, type WaveSamples } from './waves.svelte'

/** What changed in the waveform preferences, so each deck can decide whether to fetch again. */
Expand All @@ -27,6 +28,10 @@ export const FALL_BLOCKS_PER_QUALITY_STEP = 0.05

export type WaveformView = 'detail' | 'overview'

export type PlayheadPlacement = 'centre' | 'leftThird'
export const DEFAULT_PLAYHEAD_PLACEMENT: PlayheadPlacement = 'centre'
const PLAYHEAD_AT: Record<PlayheadPlacement, WidthFraction> = { centre: 1 / 2, leftThird: 1 / 3 }

const HEIGHT_SCALE_KEYS: Record<WaveformView, string> = {
detail: 'waveHeightScaleDetail',
overview: 'waveHeightScaleOverview'
Expand Down Expand Up @@ -57,6 +62,9 @@ export class WaveformPrefs {
overview: storedHeightScale('overview')
})
zoom = $state(Number(getSetting('waveformZoom')) || 1)
playheadPlacement = $state(
storedChoice<PlayheadPlacement>('playheadPlacement', ['centre', 'leftThird'], DEFAULT_PLAYHEAD_PLACEMENT)
)

private changed = new Listeners<[change: WaveformChange]>()

Expand Down Expand Up @@ -141,6 +149,15 @@ export class WaveformPrefs {
this.setHeightScale('overview', DEFAULT_HEIGHT_SCALE)
}

setPlayheadPlacement(placement: PlayheadPlacement): void {
this.playheadPlacement = placement
setSetting('playheadPlacement', placement)
}

get playheadAt(): WidthFraction {
return PLAYHEAD_AT[this.playheadPlacement]
}

setZoom(zoom: number): void {
this.zoom = Math.max(0.25, Math.min(64, zoom))
setSetting('waveformZoom', String(this.zoom))
Expand Down Expand Up @@ -188,6 +205,10 @@ export class WaveformDisplay {
return this.prefs.zoom
}

get playheadAt(): WidthFraction {
return this.prefs.playheadAt
}

get heightScales(): Record<WaveformView, number> {
return this.prefs.heightScales
}
Expand Down
17 changes: 8 additions & 9 deletions app/src/renderer/src/lib/deck/prefs.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSetting, setSetting } from '../settings'
import { setSetting } from '../settings'
import { WaveformPrefs } from './display.svelte'
import { storedFlag, storeFlag } from './stored'
import { storedChoice, storedFlag, storeFlag } from './stored'

export type CuePlacement = 'playhead' | 'cuePoint'
export type TempoReadout = 'absolute' | 'relative'
Expand All @@ -11,18 +11,17 @@ export const DEFAULT_DECK_PREFS = {
tempoReadout: 'absolute' as TempoReadout
}

function stored<T extends string>(key: string, ids: readonly T[], fallback: T): T {
const raw = getSetting(key)
return ids.find((id) => id === raw) ?? fallback
}

/** Deck preferences that hold for every deck at once, kept between sessions. */
export class DeckPrefs {
quantise = $state(storedFlag('quantise', DEFAULT_DECK_PREFS.quantise))
cuePlacement = $state(stored<CuePlacement>('cuePlacement', ['playhead', 'cuePoint'], DEFAULT_DECK_PREFS.cuePlacement))
cuePlacement = $state(
storedChoice<CuePlacement>('cuePlacement', ['playhead', 'cuePoint'], DEFAULT_DECK_PREFS.cuePlacement)
)
snapPlayhead = $state(storedFlag('snapPlayhead'))
unlimitedMemoryCues = $state(storedFlag('unlimitedMemoryCues'))
tempoReadout = $state(stored<TempoReadout>('tempoReadout', ['absolute', 'relative'], DEFAULT_DECK_PREFS.tempoReadout))
tempoReadout = $state(
storedChoice<TempoReadout>('tempoReadout', ['absolute', 'relative'], DEFAULT_DECK_PREFS.tempoReadout)
)
readonly waveform = new WaveformPrefs()

setQuantise(on: boolean): void {
Expand Down
5 changes: 5 additions & 0 deletions app/src/renderer/src/lib/deck/stored.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export function storeFlag(key: string, on: boolean): void {
setSetting(key, on ? '1' : '0')
}

export function storedChoice<T extends string>(key: string, ids: readonly T[], fallback: T): T {
const raw = getSetting(key)
return ids.find((id) => id === raw) ?? fallback
}

export function storedNumber(key: string, fallback: number, min: number, max: number): number {
const raw = getSetting(key)
if (raw == null) return fallback
Expand Down
8 changes: 4 additions & 4 deletions app/src/renderer/src/lib/waveform/detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import {
} from './markers'
import { drawSampleWave, waveStride } from './samples'

/** The analysis entries in view: `cssWidth` pixels centred on the playhead. */
function visibleRange(wf: Waveform, view: DetailView, cssWidth: number): [number, number] {
const center = view.position * wf.rate
const halfEntries = cssWidth / 2 / view.zoom
return [center - halfEntries, center + halfEntries]
const playheadEntry = view.position * wf.rate
const entriesInView = cssWidth / view.zoom
return [playheadEntry - entriesInView * view.playheadAt, playheadEntry + entriesInView * (1 - view.playheadAt)]
}

function paintWave(
Expand Down Expand Up @@ -75,6 +74,7 @@ export function drawDetail(
dpr,
from,
to,
playheadX: Math.round(pw * view.playheadAt),
toX: (seconds) => ((seconds * wf.rate - from) / (to - from)) * pw
}
paintLoops(frame)
Expand Down
4 changes: 4 additions & 0 deletions app/src/renderer/src/lib/waveform/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ export interface TempoFlagMark {
first: boolean
}

export type WidthFraction = number

/** What the scrolling detail waveform shows around the playhead. */
export interface DetailView {
position: number
playheadAt: WidthFraction
zoom: number
cues: Cue[]
beatgrid: Beatgrid | null
Expand All @@ -32,5 +35,6 @@ export interface Frame {
dpr: number
from: number
to: number
playheadX: number
toX: (seconds: number) => number
}
18 changes: 9 additions & 9 deletions app/src/renderer/src/lib/waveform/markers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cueColor, hotCueLetter } from '../format'
import type { Beatgrid, Cue } from '../types'
import { CUE_POINT_COLOR, LOOP_ACTIVE_COLOR, TEMPO_FLAG_COLOR } from './colors'
import type { Waveform } from './data'
import type { Frame } from './frame'
import type { Frame, WidthFraction } from './frame'

/** Beat handles, in CSS pixels. A vertical pill centered on each grid line, with a slightly larger hit box. */
const HANDLE_W = 6
Expand Down Expand Up @@ -120,6 +120,7 @@ export function gridHandleIndex(
wf: Waveform,
position: number,
zoom: number,
playheadAt: WidthFraction,
grid: Beatgrid | null,
cssWidth: number,
cssHeight: number,
Expand All @@ -129,13 +130,13 @@ export function gridHandleIndex(
if (!grid?.times.length || !(cssWidth > 0) || !(cssHeight > 0) || !(zoom > 0) || !(wf.rate > 0)) return -1
if (Math.abs(y - cssHeight / 2) > HANDLE_HIT_Y) return -1
const pxPerSecond = wf.rate * zoom
const half = cssWidth / 2
const startMs = (position - half / pxPerSecond) * 1000
const endMs = (position + half / pxPerSecond) * 1000
const playheadX = cssWidth * playheadAt
const startMs = (position - playheadX / pxPerSecond) * 1000
const endMs = (position + (cssWidth - playheadX) / pxPerSecond) * 1000
let best = -1
let bestDx = Infinity
eachVisibleBeat(grid.times, startMs, endMs, (i) => {
const dx = Math.abs(x - (half + (grid.times[i] / 1000 - position) * pxPerSecond))
const dx = Math.abs(x - (playheadX + (grid.times[i] / 1000 - position) * pxPerSecond))
if (dx <= HANDLE_HIT_X && dx < bestDx) {
bestDx = dx
best = i
Expand Down Expand Up @@ -231,8 +232,7 @@ export function paintCuePoint(frame: Frame): void {
}

export function paintPlayhead(frame: Frame): void {
const { ctx, pw, ph, dpr } = frame
const x = Math.round(pw / 2)
const { ctx, ph, dpr, playheadX: x } = frame
ctx.fillStyle = 'rgba(0,0,0,0.5)'
ctx.fillRect(x - dpr, 0, 3 * dpr, ph)
ctx.fillStyle = '#ffffff'
Expand All @@ -249,14 +249,14 @@ function nextCue(cues: Cue[], seconds: number): Cue | null {
}

function paintPill(frame: Frame, y: number, text: string, cue: Cue | null): void {
const { ctx, pw, dpr } = frame
const { ctx, dpr, playheadX } = frame
const height = Math.round(15 * dpr)
const pad = Math.round(5 * dpr)
const chip = cue ? Math.round(9 * dpr) : 0
const gap = cue ? Math.round(4 * dpr) : 0
const textWidth = Math.ceil(ctx.measureText(text).width)
const width = pad * 2 + chip + gap + textWidth
const left = Math.round(pw / 2 - width / 2)
const left = Math.round(playheadX - width / 2)
const mid = y + height / 2

ctx.fillStyle = 'rgba(10,11,14,0.82)'
Expand Down
Loading