mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Changed theme to liquid glass
This commit is contained in:
1 parent
950352f7ec
commit
2810f24563
52 files changed
+1353
-529
No files matched your search
@@ -1,39 +1,56 @@
|
||||
// Frequency-colored waveform analysis (Traktor "Spectrum" theme).
|
||||
// Frequency waveform analysis + painting.
|
||||
//
|
||||
// The audio is downmixed to mono and split into three frequency bands with
|
||||
// native BiquadFilters (via OfflineAudioContext — fast, off the main thread).
|
||||
// Per output column we keep each band's RMS energy plus the amplitude peak.
|
||||
// Each band is mapped to a target colour on Traktor's warm→cool ramp
|
||||
// (bass→orange, mids→violet, highs→cyan) and the three are blended by energy;
|
||||
// height = amplitude. Traktor's palette has NO green — mapping bands straight
|
||||
// to R/G/B (the naive approach) produces yellows/greens it never shows, so we
|
||||
// blend named palette colours instead. Tune the three constants below.
|
||||
// Per output column we keep each band's RMS energy, normalised to that band's
|
||||
// loudest moment in the track.
|
||||
//
|
||||
// Painting draws the three bands as LAYERS rather than blending them into one
|
||||
// colour per column: bass (orange) is drawn first and tallest, then mids
|
||||
// (violet) and highs (cyan) on top with a screen blend, so where bands overlap
|
||||
// they mix into light. A kick reads as an orange flare, a hat as a cyan tick,
|
||||
// and the structure of a track — where the bass drops out, where the highs
|
||||
// build — is visible at a glance. Still no green, matching Traktor's palette.
|
||||
|
||||
export interface WaveColumn {
|
||||
peak: number // 0..1 amplitude (column height)
|
||||
r: number // 0..1 final blended colour (already contrast-boosted)
|
||||
g: number
|
||||
b: number
|
||||
peak: number // 0..1 amplitude of the full mix
|
||||
low: number // 0..1 band energies, each normalised to its own maximum
|
||||
mid: number
|
||||
high: number
|
||||
}
|
||||
|
||||
// Band crossover choices. Independent filters, so band energies don't strictly
|
||||
// partition — that's fine, colour is normalised per column.
|
||||
// partition — that's fine, each band is normalised on its own.
|
||||
const LOW_HZ = 250
|
||||
const MID_HZ = 1200
|
||||
const MID_Q = 0.6
|
||||
const HIGH_HZ = 5000
|
||||
|
||||
// Target colour per band on Traktor's ramp (0..1 RGB). Blended by band energy.
|
||||
const LOW_COL = [1.0, 0.30, 0.15] // bass → orange-red
|
||||
const MID_COL = [0.65, 0.18, 0.95] // mids → violet / magenta
|
||||
const HIGH_COL = [0.15, 0.65, 1.0] // highs → cyan-blue
|
||||
// Each layer's colour and its height relative to the waveform's half-height.
|
||||
// Bass is the envelope; mids and highs sit inside it so all three stay visible.
|
||||
const LAYERS: { band: 'low' | 'mid' | 'high'; color: string; scale: number; blend: GlobalCompositeOperation }[] = [
|
||||
{ band: 'low', color: 'rgba(255,138,61,0.95)', scale: 0.96, blend: 'source-over' },
|
||||
{ band: 'mid', color: 'rgba(143,107,255,0.8)', scale: 0.66, blend: 'screen' },
|
||||
{ band: 'high', color: 'rgba(94,231,255,0.85)', scale: 0.46, blend: 'screen' },
|
||||
]
|
||||
|
||||
export interface PaintOptions {
|
||||
/** Bar width and the gap after it, in DEVICE pixels. A gap of 0 paints a
|
||||
* solid waveform (the overview); a gap gives the main view its bar texture. */
|
||||
bar: number
|
||||
gap: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Paint a slice of the analysed columns across a canvas: source columns in the
|
||||
* fractional range [startFrac, endFrac) (of the whole track) are mapped across
|
||||
* the full canvas width, aggregating multiple columns per pixel (max peak,
|
||||
* averaged colour). Fractions outside [0,1] paint blank — used by the scrolling
|
||||
* main view so the lead-in/lead-out past the track edges stays empty.
|
||||
* the full canvas width. Fractions outside [0,1] paint blank — used by the
|
||||
* scrolling main view so the lead-in/lead-out past the track edges stays empty.
|
||||
*
|
||||
* Bars are binned in TRACK space, not screen space: bar k always covers the
|
||||
* same columns of audio, and only its x moves as the view scrolls. Binning by
|
||||
* screen pixel instead makes every bar's height flicker as the playhead
|
||||
* advances, because each frame regroups different columns under each bar.
|
||||
*/
|
||||
export function paintWave(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
@@ -42,38 +59,38 @@ export function paintWave(
|
||||
h: number,
|
||||
startFrac: number,
|
||||
endFrac: number,
|
||||
opts: PaintOptions = { bar: 1, gap: 0 },
|
||||
): void {
|
||||
const n = cols.length
|
||||
if (n === 0 || w === 0 || h === 0) return
|
||||
if (n === 0 || w === 0 || h === 0 || endFrac <= startFrac) return
|
||||
const mid = h / 2
|
||||
const span = (endFrac - startFrac) * n
|
||||
for (let x = 0; x < w; x++) {
|
||||
const c0 = startFrac * n + (x / w) * span
|
||||
const c1 = startFrac * n + ((x + 1) / w) * span
|
||||
const i0 = Math.floor(c0)
|
||||
const i1 = Math.max(i0 + 1, Math.ceil(c1))
|
||||
let peak = 0
|
||||
let r = 0
|
||||
let g = 0
|
||||
let b = 0
|
||||
let count = 0
|
||||
for (let i = i0; i < i1; i++) {
|
||||
if (i < 0 || i >= n) continue
|
||||
const col = cols[i]
|
||||
if (col.peak > peak) peak = col.peak
|
||||
r += col.r
|
||||
g += col.g
|
||||
b += col.b
|
||||
count++
|
||||
const colsPerPx = ((endFrac - startFrac) * n) / w
|
||||
const step = opts.bar + opts.gap
|
||||
const colsPerBar = step * colsPerPx
|
||||
const startCol = startFrac * n
|
||||
const k0 = Math.floor(Math.max(0, startCol) / colsPerBar)
|
||||
const k1 = Math.ceil(Math.min(n, endFrac * n) / colsPerBar)
|
||||
const prevOp = ctx.globalCompositeOperation
|
||||
for (const layer of LAYERS) {
|
||||
ctx.globalCompositeOperation = layer.blend
|
||||
ctx.fillStyle = layer.color
|
||||
const reach = (mid - 1) * layer.scale
|
||||
for (let k = k0; k < k1; k++) {
|
||||
const c0 = k * colsPerBar
|
||||
const i0 = Math.max(0, Math.floor(c0))
|
||||
const i1 = Math.min(n, Math.max(i0 + 1, Math.ceil(c0 + colsPerBar)))
|
||||
let v = 0
|
||||
for (let i = i0; i < i1; i++) {
|
||||
const e = cols[i][layer.band]
|
||||
if (e > v) v = e
|
||||
}
|
||||
if (v <= 0) continue
|
||||
const x = (c0 - startCol) / colsPerPx
|
||||
const half = Math.max(0.5, v * reach)
|
||||
ctx.fillRect(x, mid - half, opts.bar, half * 2)
|
||||
}
|
||||
if (count === 0) continue // outside the track → blank pixel
|
||||
r /= count
|
||||
g /= count
|
||||
b /= count
|
||||
const half = Math.max(0.5, peak * (mid - 1))
|
||||
ctx.fillStyle = `rgb(${(r * 255) | 0},${(g * 255) | 0},${(b * 255) | 0})`
|
||||
ctx.fillRect(x, mid - half, 1, half * 2)
|
||||
}
|
||||
ctx.globalCompositeOperation = prevOp
|
||||
}
|
||||
|
||||
export interface WaveformAnalysis {
|
||||
@@ -162,32 +179,40 @@ export async function analyzeWaveform(url: string, buckets?: number): Promise<Wa
|
||||
await accumBand(midE, 'bandpass', MID_HZ, MID_Q)
|
||||
await accumBand(highE, 'highpass', HIGH_HZ)
|
||||
|
||||
// Per-band normalisation, to the band's 99.5th-percentile column rather than
|
||||
// its single loudest (one clipped transient would otherwise shrink the whole
|
||||
// band). Each band then gets an EXPANDING curve: dance music keeps its bass
|
||||
// near full scale for whole sections, so a linear (or compressing) map draws
|
||||
// a solid wall where the eye wants to see the kick pulse. The bass curve is
|
||||
// steepest, since it carries the rhythm; highs stay near linear so hats and
|
||||
// risers keep their detail.
|
||||
const rms = (e: Float64Array) => {
|
||||
const out = new Float64Array(e.length)
|
||||
for (let b = 0; b < e.length; b++) out[b] = Math.sqrt(e[b] / (cnt[b] || 1))
|
||||
return out
|
||||
}
|
||||
const ceiling = (v: Float64Array) => {
|
||||
const sorted = Float64Array.from(v).sort()
|
||||
return Math.max(1e-9, sorted[Math.floor((sorted.length - 1) * 0.995)])
|
||||
}
|
||||
const lowR = rms(lowE)
|
||||
const midR = rms(midE)
|
||||
const highR = rms(highE)
|
||||
const maxL = ceiling(lowR)
|
||||
const maxM = ceiling(midR)
|
||||
const maxH = ceiling(highR)
|
||||
let maxPeak = 1e-6
|
||||
for (let b = 0; b < buckets; b++) if (peak[b] > maxPeak) maxPeak = peak[b]
|
||||
const curve = (v: number, k: number) => Math.pow(Math.min(1, v), k)
|
||||
|
||||
const cols: WaveColumn[] = new Array(buckets)
|
||||
for (let b = 0; b < buckets; b++) {
|
||||
const n = cnt[b] || 1
|
||||
const l = Math.sqrt(lowE[b] / n)
|
||||
const m = Math.sqrt(midE[b] / n)
|
||||
const h = Math.sqrt(highE[b] / n)
|
||||
// Normalise to the dominant band so colours stay vivid (brightness is not
|
||||
// tied to loudness — amplitude is shown by height, like Traktor). Gamma
|
||||
// sharpens the separation between bands.
|
||||
const mx = Math.max(l, m, h, 1e-9)
|
||||
const wl = Math.pow(l / mx, 1.3)
|
||||
const wm = Math.pow(m / mx, 1.3)
|
||||
const wh = Math.pow(h / mx, 1.3)
|
||||
// Blend the three band palette colours by weight, then normalise to the
|
||||
// brightest channel so the colour stays vivid (height carries loudness).
|
||||
let cr = wl * LOW_COL[0] + wm * MID_COL[0] + wh * HIGH_COL[0]
|
||||
let cg = wl * LOW_COL[1] + wm * MID_COL[1] + wh * HIGH_COL[1]
|
||||
let cb = wl * LOW_COL[2] + wm * MID_COL[2] + wh * HIGH_COL[2]
|
||||
const cmax = Math.max(cr, cg, cb, 1e-9)
|
||||
cr /= cmax
|
||||
cg /= cmax
|
||||
cb /= cmax
|
||||
cols[b] = { peak: peak[b] / maxPeak, r: cr, g: cg, b: cb }
|
||||
cols[b] = {
|
||||
peak: peak[b] / maxPeak,
|
||||
low: curve(lowR[b] / maxL, 1.8),
|
||||
mid: curve(midR[b] / maxM, 1.3),
|
||||
high: curve(highR[b] / maxH, 1.05),
|
||||
}
|
||||
}
|
||||
return { cols, buffer: audio }
|
||||
}
|
||||
Reference in new issue
Block a user