@trippnology/mixshell 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +294 -2
- package/assets/mixshell-screenshot.png +0 -0
- package/package.json +73 -5
- package/src/alsa-pulse.conf +20 -0
- package/src/alsa-setup.js +15 -0
- package/src/components/browser.js +193 -0
- package/src/components/config.js +168 -0
- package/src/components/durations.js +151 -0
- package/src/components/library.js +679 -0
- package/src/components/mediakeys.js +222 -0
- package/src/components/mpris.js +309 -0
- package/src/components/player.js +412 -0
- package/src/components/playlist.js +335 -0
- package/src/components/session.js +96 -0
- package/src/constants.js +164 -0
- package/src/engines/ffmpeg.js +601 -0
- package/src/engines/index.js +14 -0
- package/src/engines/webaudio.js +317 -0
- package/src/index.js +188 -0
- package/src/mediakeys/macos-nowplaying.js +227 -0
- package/src/ui/app.js +56 -0
- package/src/ui/artPane.js +314 -0
- package/src/ui/confirmPrompt.js +34 -0
- package/src/ui/constants.js +133 -0
- package/src/ui/controller.js +1641 -0
- package/src/ui/dom.js +9 -0
- package/src/ui/goToPrompt.js +105 -0
- package/src/ui/index.html +78 -0
- package/src/ui/libraryPanel.js +415 -0
- package/src/ui/listView.js +209 -0
- package/src/ui/onboarding.js +353 -0
- package/src/ui/playlistPanel.js +207 -0
- package/src/ui/settingsPanel.js +473 -0
- package/src/ui/shortcuts.js +30 -0
- package/src/ui/splash.js +302 -0
- package/src/ui/style.css +532 -0
- package/src/ui/subtabs.js +31 -0
- package/src/ui/visualizer/bars.js +74 -0
- package/src/ui/visualizer/circles.js +64 -0
- package/src/ui/visualizer/color.js +61 -0
- package/src/ui/visualizer/cube.js +105 -0
- package/src/ui/visualizer/grid.js +68 -0
- package/src/ui/visualizer/index.js +65 -0
- package/src/ui/visualizer/onset.js +130 -0
- package/src/ui/visualizer/spectrum.js +34 -0
- package/src/ui/visualizer/vu.js +71 -0
- package/src/ui/visualizer/waveform.js +58 -0
- package/src/utils/audioAnalyzer.js +362 -0
- package/src/utils/audioDevices.js +222 -0
- package/src/utils/cellart.js +75 -0
- package/src/utils/debug.js +7 -0
- package/src/utils/durationProbe.js +48 -0
- package/src/utils/folderArt.js +46 -0
- package/src/utils/format.js +20 -0
- package/src/utils/id3.js +84 -0
- package/src/utils/pathInput.js +76 -0
- package/src/utils/playlistIO.js +176 -0
- package/src/utils/terminalGraphics.js +47 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Parse the colour forms the computed styles produce: hex and rgb().
|
|
2
|
+
// Returns [r, g, b] or null (system colour names from the terminal theme
|
|
3
|
+
// can't be interpolated — callers fall back to a single band).
|
|
4
|
+
function parseColor(value) {
|
|
5
|
+
const s = String(value).trim().toLowerCase();
|
|
6
|
+
let m = s.match(/^#([0-9a-f]{3})$/);
|
|
7
|
+
if (m) {
|
|
8
|
+
return [0, 1, 2].map((i) => parseInt(m[1][i] + m[1][i], 16));
|
|
9
|
+
}
|
|
10
|
+
m = s.match(/^#([0-9a-f]{6})$/);
|
|
11
|
+
if (m) {
|
|
12
|
+
return [0, 2, 4].map((i) => parseInt(m[1].slice(i, i + 2), 16));
|
|
13
|
+
}
|
|
14
|
+
m = s.match(
|
|
15
|
+
/^rgba?\(\s*(\d+)\s*[, ]\s*(\d+)\s*[, ]\s*(\d+)(?:\s*[,/]\s*[\d.]+)?\s*\)$/,
|
|
16
|
+
);
|
|
17
|
+
if (m) {
|
|
18
|
+
return [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function toHex([r, g, b]) {
|
|
24
|
+
const c = (v) => Math.round(v).toString(16).padStart(2, '0');
|
|
25
|
+
return `#${c(r)}${c(g)}${c(b)}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// K colours from accent (bottom) to playing (top). Falls back to a single
|
|
29
|
+
// band when either endpoint is a colour we can't interpolate.
|
|
30
|
+
export function gradientBands(accent, playing, k) {
|
|
31
|
+
const a = parseColor(accent);
|
|
32
|
+
const b = parseColor(playing);
|
|
33
|
+
if (a === null || b === null) {
|
|
34
|
+
return [a ? toHex(a) : b ? toHex(b) : accent];
|
|
35
|
+
}
|
|
36
|
+
const bands = [];
|
|
37
|
+
for (let i = 0; i < k; i++) {
|
|
38
|
+
const t = k === 1 ? 0 : i / (k - 1);
|
|
39
|
+
bands.push(
|
|
40
|
+
toHex([
|
|
41
|
+
a[0] + (b[0] - a[0]) * t,
|
|
42
|
+
a[1] + (b[1] - a[1]) * t,
|
|
43
|
+
a[2] + (b[2] - a[2]) * t,
|
|
44
|
+
]),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return bands;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Ring fade: 3 colour steps from the ring colour toward the theme bg;
|
|
51
|
+
// gradientBands' single-band fallback covers unparseable names.
|
|
52
|
+
export function fadeBands(ring, bg) {
|
|
53
|
+
return gradientBands(ring, bg, 3).reverse();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Shared lookup for the flat colour bands: every renderer clamps its index
|
|
57
|
+
// to the available bands. A single-band fallback has no row for the upper
|
|
58
|
+
// runs, so reuse its only band instead of assigning undefined to fillStyle.
|
|
59
|
+
export function bandAt(bands, i) {
|
|
60
|
+
return bands[Math.max(0, Math.min(bands.length - 1, i))];
|
|
61
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { bandAt, gradientBands } from './color.js';
|
|
2
|
+
import { emitHalfCells, halfLine, makeHalfGrid } from './grid.js';
|
|
3
|
+
import { updateCircles, updateCube } from './onset.js';
|
|
4
|
+
|
|
5
|
+
// Corners of the ±1 cube: 0-3 back face (z = -1), 4-7 front face (z = 1).
|
|
6
|
+
const CUBE_VERTICES = [
|
|
7
|
+
[-1, -1, -1],
|
|
8
|
+
[1, -1, -1],
|
|
9
|
+
[1, 1, -1],
|
|
10
|
+
[-1, 1, -1],
|
|
11
|
+
[-1, -1, 1],
|
|
12
|
+
[1, -1, 1],
|
|
13
|
+
[1, 1, 1],
|
|
14
|
+
[-1, 1, 1],
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
// 12 edges: back ring, front ring, and the four connectors.
|
|
18
|
+
const CUBE_EDGES = [
|
|
19
|
+
[0, 1],
|
|
20
|
+
[1, 2],
|
|
21
|
+
[2, 3],
|
|
22
|
+
[3, 0],
|
|
23
|
+
[4, 5],
|
|
24
|
+
[5, 6],
|
|
25
|
+
[6, 7],
|
|
26
|
+
[7, 4],
|
|
27
|
+
[0, 4],
|
|
28
|
+
[1, 5],
|
|
29
|
+
[2, 6],
|
|
30
|
+
[3, 7],
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
export const cube = {
|
|
34
|
+
needsSpectrum: true,
|
|
35
|
+
box: 'square',
|
|
36
|
+
update(data, viz, now) {
|
|
37
|
+
const hits = updateCircles(viz, data.frequencyBins, now, data.sampleRate);
|
|
38
|
+
updateCube(viz, hits);
|
|
39
|
+
},
|
|
40
|
+
// Wireframe cube, half-edge from pulse; latest hit colours the edges.
|
|
41
|
+
draw(grid, colors, viz) {
|
|
42
|
+
const cols = grid.cols;
|
|
43
|
+
const rows = grid.rows;
|
|
44
|
+
if (cols < 5 || rows < 5) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const cx = (cols - 1) / 2;
|
|
49
|
+
const cy = (rows - 1) / 2;
|
|
50
|
+
const ratio = Math.max(1, viz.cellRatio);
|
|
51
|
+
// Half-edge budget: the widest projected corner reach is ~1.92
|
|
52
|
+
// half-edges at FOV 4 (perspective magnification peaks with the
|
|
53
|
+
// corner just off-axis, not at the viewer-facing extreme), so a
|
|
54
|
+
// budget of maxR/2 keeps every rotation on-grid.
|
|
55
|
+
const maxHalf = Math.min((cols - 1) / 2, ((rows - 1) / 2) * ratio) / 2;
|
|
56
|
+
const base = Math.max(1.2, maxHalf * 0.25);
|
|
57
|
+
const half = base + viz.cube.pulse * (maxHalf - base);
|
|
58
|
+
|
|
59
|
+
const sinX = Math.sin(viz.cube.ax);
|
|
60
|
+
const cosX = Math.cos(viz.cube.ax);
|
|
61
|
+
const sinY = Math.sin(viz.cube.ay);
|
|
62
|
+
const cosY = Math.cos(viz.cube.ay);
|
|
63
|
+
// FOV > √3 keeps the perspective divide finite at every angle.
|
|
64
|
+
const FOV = 4;
|
|
65
|
+
|
|
66
|
+
const pts = [];
|
|
67
|
+
for (const [x, y, z] of CUBE_VERTICES) {
|
|
68
|
+
// Rotate X then Y, then divide toward the viewer.
|
|
69
|
+
const y1 = y * cosX - z * sinX;
|
|
70
|
+
const z1 = y * sinX + z * cosX;
|
|
71
|
+
const x2 = x * cosY + z1 * sinY;
|
|
72
|
+
const z2 = -x * sinY + z1 * cosY;
|
|
73
|
+
const s = (FOV / (FOV - z2)) * half;
|
|
74
|
+
// Same aspect trade as the circles rings: divide the row span
|
|
75
|
+
// by the cell ratio so faces read square on tall cells.
|
|
76
|
+
pts.push({ x: cx + x2 * s, y: cy + (y1 * s) / ratio, z: z2 });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const edgeColor =
|
|
80
|
+
viz.cube.color === 'accent' ? colors.accent : colors.playing;
|
|
81
|
+
// bands[0] pure, bands[1] halfway to bg; system names → one band.
|
|
82
|
+
const palette = gradientBands(edgeColor, colors.bg, 3);
|
|
83
|
+
const full = bandAt(palette, 0);
|
|
84
|
+
const dim = bandAt(palette, 1);
|
|
85
|
+
|
|
86
|
+
// Far edges write first: near halves overwrite them in the
|
|
87
|
+
// buffer, so a shared cell can keep a far half beside a near
|
|
88
|
+
// half. Rows double into the half-row grid; the endpoints' z
|
|
89
|
+
// sum orders and shades each edge (the midpoint in disguise —
|
|
90
|
+
// no temp objects).
|
|
91
|
+
const buf = makeHalfGrid(cols, rows);
|
|
92
|
+
const zsum = (e) => pts[e[0]].z + pts[e[1]].z;
|
|
93
|
+
for (const [a, b] of [...CUBE_EDGES].sort((p, q) => zsum(p) - zsum(q))) {
|
|
94
|
+
halfLine(
|
|
95
|
+
buf,
|
|
96
|
+
pts[a].x,
|
|
97
|
+
pts[a].y * 2,
|
|
98
|
+
pts[b].x,
|
|
99
|
+
pts[b].y * 2,
|
|
100
|
+
zsum([a, b]) > 0 ? full : dim,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
emitHalfCells(grid, buf);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Cellgrid-only rule (invariant 1): every visualizer mode draws on the
|
|
2
|
+
// termdom cellgrid #vizCanvas — direct per-cell writes, whole-cell coords,
|
|
3
|
+
// half-block glyphs for 2× vertical resolution. No per-rect 2d fills and
|
|
4
|
+
// no kitty pixel path; album art stays on the plain-2d #artCanvas (the
|
|
5
|
+
// engine's auto fallback is the capability layer).
|
|
6
|
+
|
|
7
|
+
// Half-grid: cols × rows×2 colour slots (top/bottom per cell). Emitting
|
|
8
|
+
// each cell's pair is still pure cell ops — the 2d-bitmap ban targets
|
|
9
|
+
// the O(bitmap) rasterizer, not glyph compositing.
|
|
10
|
+
export function makeHalfGrid(cols, rows) {
|
|
11
|
+
return { cols, rows, slots: new Array(cols * rows * 2).fill(null) };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Bresenham in half-rows; later calls overwrite earlier slots, so write
|
|
15
|
+
// far first to let near halves win shared cells. Bounds guard catches
|
|
16
|
+
// rounding artefacts.
|
|
17
|
+
export function halfLine(buf, x0, y0, x1, y1, color) {
|
|
18
|
+
let x = Math.round(x0);
|
|
19
|
+
let y = Math.round(y0);
|
|
20
|
+
const endX = Math.round(x1);
|
|
21
|
+
const endY = Math.round(y1);
|
|
22
|
+
const dx = Math.abs(endX - x);
|
|
23
|
+
const dy = -Math.abs(endY - y);
|
|
24
|
+
const stepX = x < endX ? 1 : -1;
|
|
25
|
+
const stepY = y < endY ? 1 : -1;
|
|
26
|
+
let err = dx + dy;
|
|
27
|
+
for (;;) {
|
|
28
|
+
if (x >= 0 && x < buf.cols && y >= 0 && y < buf.rows * 2) {
|
|
29
|
+
buf.slots[y * buf.cols + x] = color;
|
|
30
|
+
}
|
|
31
|
+
if (x === endX && y === endY) break;
|
|
32
|
+
const e2 = 2 * err;
|
|
33
|
+
if (e2 >= dy) {
|
|
34
|
+
err += dy;
|
|
35
|
+
x += stepX;
|
|
36
|
+
}
|
|
37
|
+
if (e2 <= dx) {
|
|
38
|
+
err += dx;
|
|
39
|
+
y += stepY;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Emit the half grid onto the cellgrid: a fillRect for a uniform cell,
|
|
45
|
+
// one block glyph over the cleared cell for a single half ('▀' top /
|
|
46
|
+
// '▄' bottom — same cleared-bg reliance as the VU labels), bg fill plus
|
|
47
|
+
// '▀' when the halves carry different colours. O(cells) writes.
|
|
48
|
+
export function emitHalfCells(ctx, buf) {
|
|
49
|
+
for (let y = 0; y < buf.rows; y++) {
|
|
50
|
+
for (let x = 0; x < buf.cols; x++) {
|
|
51
|
+
const top = buf.slots[y * 2 * buf.cols + x];
|
|
52
|
+
const bottom = buf.slots[(y * 2 + 1) * buf.cols + x];
|
|
53
|
+
if (top === null && bottom === null) continue;
|
|
54
|
+
if (top === bottom) {
|
|
55
|
+
ctx.fillStyle = top;
|
|
56
|
+
ctx.fillRect(x, y, 1, 1);
|
|
57
|
+
} else if (top === null || bottom === null) {
|
|
58
|
+
ctx.fillStyle = top === null ? bottom : top;
|
|
59
|
+
ctx.fillText(top === null ? '▄' : '▀', x, y);
|
|
60
|
+
} else {
|
|
61
|
+
ctx.fillStyle = bottom;
|
|
62
|
+
ctx.fillRect(x, y, 1, 1);
|
|
63
|
+
ctx.fillStyle = top;
|
|
64
|
+
ctx.fillText('▀', x, y);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { bars } from './bars.js';
|
|
2
|
+
import { circles } from './circles.js';
|
|
3
|
+
import { cube } from './cube.js';
|
|
4
|
+
import { freshBand } from './onset.js';
|
|
5
|
+
import { vu } from './vu.js';
|
|
6
|
+
import { waveform } from './waveform.js';
|
|
7
|
+
|
|
8
|
+
// Cellgrid-only rule and half-grid helpers live in ./grid.js; modes never
|
|
9
|
+
// touch a 2d context or the kitty path (album art owns #artCanvas).
|
|
10
|
+
|
|
11
|
+
// Mode registry: the only place the facade learns about renderers. Each
|
|
12
|
+
// mode owns its needsSpectrum flag, box shape and draw/update behaviour;
|
|
13
|
+
// modes never import each other or the facade.
|
|
14
|
+
export const MODES = { bars, waveform, vu, circles, cube };
|
|
15
|
+
|
|
16
|
+
export class Visualizer {
|
|
17
|
+
// Injectable clock for pulse decay tests.
|
|
18
|
+
constructor({ now = () => performance.now() } = {}) {
|
|
19
|
+
this.mode = 'bars';
|
|
20
|
+
this.data = { frequencyBins: [], amplitude: 0 };
|
|
21
|
+
// Render-detail cap; 0 = uncapped (standard stride).
|
|
22
|
+
this.maxBars = 0;
|
|
23
|
+
this._now = now;
|
|
24
|
+
// Circles bands are bin fractions; the analyzer stays mode-agnostic.
|
|
25
|
+
this.cellRatio = 2;
|
|
26
|
+
this._circlesAt = null;
|
|
27
|
+
// Floors scale with the band metric's range: the kick's peak-bin
|
|
28
|
+
// magnitudes run ~25× the snare's band-mean.
|
|
29
|
+
this.circles = {
|
|
30
|
+
kick: freshBand(0.18, 0.008, 0.006),
|
|
31
|
+
// Snare releases much faster than the kick thump.
|
|
32
|
+
snare: freshBand(0.08, 0.0015, 0.001),
|
|
33
|
+
};
|
|
34
|
+
// Cube pulse/colour/angles, advanced on the analysis clock.
|
|
35
|
+
this.cube = { pulse: 0, color: 'accent', ax: 0, ay: 0 };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
setMode(mode) {
|
|
39
|
+
this.mode = mode;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setMaxBars(n) {
|
|
43
|
+
this.maxBars = n;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Cell aspect from the controller; circles trade it for a smaller
|
|
47
|
+
// vertical radius. Defaults to 2:1 until measured.
|
|
48
|
+
setCellRatio(ratio) {
|
|
49
|
+
this.cellRatio = ratio;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
update(data) {
|
|
53
|
+
this.data = data;
|
|
54
|
+
MODES[this.mode]?.update?.(data, this, this._now());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Render the current mode onto the cellgrid (cols/rows in cells).
|
|
59
|
+
* @param {{accent: string, playing: string, muted: string, bg?: string}} colors
|
|
60
|
+
*/
|
|
61
|
+
drawCells(grid, colors) {
|
|
62
|
+
grid.clearRect(0, 0, grid.cols, grid.rows);
|
|
63
|
+
MODES[this.mode]?.draw?.(grid, colors, this);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Kick bin 1..~160Hz, snare ~1.5-6kHz: an edge at Hz h is
|
|
2
|
+
// round(n·h/(sr/2)), so the range holds at every fftSize and rate.
|
|
3
|
+
// Missing sampleRate falls back to the analyzer's 44100 default.
|
|
4
|
+
export function onsetBandEdges(binCount, sampleRate) {
|
|
5
|
+
const rate =
|
|
6
|
+
Number.isFinite(sampleRate) && sampleRate > 0 ? sampleRate : 44100;
|
|
7
|
+
const atHz = (hz) => Math.round((binCount * hz) / (rate / 2));
|
|
8
|
+
const kickHi = Math.max(2, Math.min(binCount - 1, atHz(160)));
|
|
9
|
+
const snareLo = Math.max(kickHi + 1, Math.min(binCount - 2, atHz(1500)));
|
|
10
|
+
const snareHi = Math.max(snareLo + 1, Math.min(binCount - 1, atHz(6000)));
|
|
11
|
+
return { kickHi, snareLo, snareHi };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Onset band state: pulse/level/top/sustain/hitAt plus decay/noise/flux
|
|
15
|
+
// floors. Kick and snare share the shape with different tunings.
|
|
16
|
+
export function freshBand(decayTau, noiseFloor, fluxFloor) {
|
|
17
|
+
return {
|
|
18
|
+
pulse: 0,
|
|
19
|
+
level: 0,
|
|
20
|
+
top: 0,
|
|
21
|
+
sustain: 0,
|
|
22
|
+
hitAt: -1e9,
|
|
23
|
+
decayTau,
|
|
24
|
+
noiseFloor,
|
|
25
|
+
fluxFloor,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Kick ≈ 40-160Hz (tonal → band peak), snare ≈ 1.5-6kHz (broadband,
|
|
30
|
+
// bins ~25× tinier → band mean). stepBand turns those energies into
|
|
31
|
+
// flux-gated, amplitude-locked hits.
|
|
32
|
+
export function updateCircles(viz, frequencyBins, now, sampleRate) {
|
|
33
|
+
const bins = frequencyBins;
|
|
34
|
+
const n = bins.length;
|
|
35
|
+
const dtSec =
|
|
36
|
+
viz._circlesAt == null
|
|
37
|
+
? 0
|
|
38
|
+
: Math.min(0.5, Math.max(0, (now - viz._circlesAt) / 1000));
|
|
39
|
+
viz._circlesAt = now;
|
|
40
|
+
|
|
41
|
+
const bandPeak = (lo, hi) => {
|
|
42
|
+
let peak = 0;
|
|
43
|
+
for (let b = lo; b <= hi; b++) {
|
|
44
|
+
if (bins[b] > peak) peak = bins[b];
|
|
45
|
+
}
|
|
46
|
+
return peak;
|
|
47
|
+
};
|
|
48
|
+
const bandMean = (lo, hi) => {
|
|
49
|
+
let sum = 0;
|
|
50
|
+
for (let b = lo; b <= hi; b++) sum += bins[b];
|
|
51
|
+
return sum / (hi - lo + 1);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Kick: bin 1 .. ~160Hz. At least two bins so one silent bin can't
|
|
55
|
+
// blank the band at the smallest fftSize.
|
|
56
|
+
const { kickHi, snareLo, snareHi } = onsetBandEdges(n, sampleRate);
|
|
57
|
+
|
|
58
|
+
const kickHit = stepBand(
|
|
59
|
+
viz.circles.kick,
|
|
60
|
+
n > 0 ? bandPeak(1, kickHi) : 0,
|
|
61
|
+
now,
|
|
62
|
+
dtSec,
|
|
63
|
+
);
|
|
64
|
+
const snareHit = stepBand(
|
|
65
|
+
viz.circles.snare,
|
|
66
|
+
n > 0 ? bandMean(snareLo, snareHi) : 0,
|
|
67
|
+
now,
|
|
68
|
+
dtSec,
|
|
69
|
+
);
|
|
70
|
+
return { dtSec, kickHit, snareHit };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function stepBand(band, energy, now, dtSec) {
|
|
74
|
+
let hit = false;
|
|
75
|
+
// Positive flux — sustained material rides the level, never gates.
|
|
76
|
+
const flux = Math.max(0, energy - band.level);
|
|
77
|
+
// Lock-in reference: the band's most powerful recent amplitude.
|
|
78
|
+
// Instant attack, τ ≈ 4s decay; gates and strength scale with the
|
|
79
|
+
// pre-attack value (prevTop) — comparing against the in-flight
|
|
80
|
+
// attack would make every rising hit read full strength.
|
|
81
|
+
band.top *= 0.5 ** (dtSec / 4);
|
|
82
|
+
const prevTop = band.top;
|
|
83
|
+
if (energy > band.top) {
|
|
84
|
+
band.top = energy;
|
|
85
|
+
}
|
|
86
|
+
const gate = Math.max(band.fluxFloor, prevTop * 0.2);
|
|
87
|
+
// Armed again once energy dips below the last hit's plateau (or
|
|
88
|
+
// after a hard timeout) — a sustained tone's slow attack can't
|
|
89
|
+
// re-trigger past the refractory window.
|
|
90
|
+
const armed = energy < band.sustain * 0.7 || now - band.hitAt > 400;
|
|
91
|
+
if (
|
|
92
|
+
energy > band.noiseFloor &&
|
|
93
|
+
flux > gate &&
|
|
94
|
+
energy > band.top * 0.3 &&
|
|
95
|
+
armed &&
|
|
96
|
+
now - band.hitAt > 120
|
|
97
|
+
) {
|
|
98
|
+
band.hitAt = now;
|
|
99
|
+
band.sustain = energy;
|
|
100
|
+
// Strength vs the pre-hit top: the strongest recent hit drives
|
|
101
|
+
// the full radius, weaker ones proportionally less.
|
|
102
|
+
band.pulse = Math.max(
|
|
103
|
+
band.pulse,
|
|
104
|
+
Math.min(1, Math.max(0.35, energy / Math.max(prevTop, 1e-6))),
|
|
105
|
+
);
|
|
106
|
+
hit = true;
|
|
107
|
+
}
|
|
108
|
+
// Fast trailing level (τ ≈ 0.06s): sustained tones stop producing
|
|
109
|
+
// flux inside the refractory window, dips between hits survive.
|
|
110
|
+
band.level += (energy - band.level) * (1 - Math.exp(-dtSec / 0.06));
|
|
111
|
+
// Release back toward the center; snare τ shorter than kick.
|
|
112
|
+
band.pulse *= 0.5 ** (dtSec / band.decayTau);
|
|
113
|
+
return hit;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Any hit lifts the cube pulse; the latest hit's band picks the edge
|
|
117
|
+
// colour. Angles advance on the analysis clock, not the draw loop.
|
|
118
|
+
export function updateCube(viz, { dtSec, kickHit, snareHit }) {
|
|
119
|
+
if (kickHit) {
|
|
120
|
+
viz.cube.pulse = Math.max(viz.cube.pulse, viz.circles.kick.pulse);
|
|
121
|
+
viz.cube.color = 'accent';
|
|
122
|
+
}
|
|
123
|
+
if (snareHit) {
|
|
124
|
+
viz.cube.pulse = Math.max(viz.cube.pulse, viz.circles.snare.pulse);
|
|
125
|
+
viz.cube.color = 'playing';
|
|
126
|
+
}
|
|
127
|
+
viz.cube.pulse *= 0.5 ** (dtSec / 0.3);
|
|
128
|
+
viz.cube.ax += 0.9 * dtSec;
|
|
129
|
+
viz.cube.ay += 1.3 * dtSec;
|
|
130
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Log-spaced bin edges for one bar per band. Bin 0 is DC; edges are forced
|
|
2
|
+
// strictly increasing so every bar owns at least one distinct bin.
|
|
3
|
+
export function bandEdges(numBars, numBins) {
|
|
4
|
+
const binRatio = numBins - 1;
|
|
5
|
+
const edges = [1];
|
|
6
|
+
for (let i = 1; i <= numBars; i++) {
|
|
7
|
+
const raw = Math.floor(binRatio ** (i / numBars));
|
|
8
|
+
edges.push(Math.max(raw, edges[i - 1] + 1));
|
|
9
|
+
}
|
|
10
|
+
return edges;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Bar values: log-spaced bands, peak magnitude per band, log10 height.
|
|
14
|
+
export function bandValues(frequencyBins, numBars) {
|
|
15
|
+
const edges = bandEdges(numBars, frequencyBins.length);
|
|
16
|
+
const maxBin = frequencyBins.length - 1;
|
|
17
|
+
|
|
18
|
+
// The multiplier tiers keep a full mix spanning the bar range at
|
|
19
|
+
// different bar counts. Calibrated against real music: per-bin
|
|
20
|
+
// peaks run ~0.02 (energy spread across the spectrum, unlike a pure
|
|
21
|
+
// tone's 0.12), so K=250 at 19 bars reads ~78% — hot enough to look
|
|
22
|
+
// live, with headroom so transient peaks don't pin the top.
|
|
23
|
+
const baseMultiplier = numBars <= 16 ? 500 : numBars <= 32 ? 250 : 125;
|
|
24
|
+
const values = [];
|
|
25
|
+
for (let i = 0; i < numBars; i++) {
|
|
26
|
+
let binValue = 0;
|
|
27
|
+
for (let b = edges[i]; b < edges[i + 1] && b <= maxBin; b++) {
|
|
28
|
+
if (frequencyBins[b] > binValue) binValue = frequencyBins[b];
|
|
29
|
+
}
|
|
30
|
+
const scaled = Math.log10(binValue * baseMultiplier + 1);
|
|
31
|
+
values.push(Math.min(1, scaled));
|
|
32
|
+
}
|
|
33
|
+
return values;
|
|
34
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { bandAt, gradientBands } from './color.js';
|
|
2
|
+
|
|
3
|
+
// VU: two ~half-width stereo bars + dB scale on a square canvas.
|
|
4
|
+
// Analyzer envelopes are read raw/absolute (VU gain ×5 lives there;
|
|
5
|
+
// auto-gain would max out at every steady volume); missing channels
|
|
6
|
+
// mirror mono. Gradient: playing at bottom, accent at top.
|
|
7
|
+
|
|
8
|
+
// Classic VU tick points, dB relative to meter full scale. The bar
|
|
9
|
+
// is linear in level and the analyzer clamps at its 0 VU reference,
|
|
10
|
+
// so a tick for dB d sits at level 10^(d/20) of the height.
|
|
11
|
+
export const DB_TICKS = [
|
|
12
|
+
[0, ' 0'],
|
|
13
|
+
[-3, '-3'],
|
|
14
|
+
[-6, '-6'],
|
|
15
|
+
[-10, '-10'],
|
|
16
|
+
[-20, '-20'],
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export const vu = {
|
|
20
|
+
needsSpectrum: false,
|
|
21
|
+
box: 'square',
|
|
22
|
+
draw(grid, colors, viz) {
|
|
23
|
+
const cols = grid.cols;
|
|
24
|
+
const rows = grid.rows;
|
|
25
|
+
if (cols < 2 || rows < 1) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
// Gap wide enough for the widest dB label plus a margin column on
|
|
29
|
+
// each side (DB_TICKS' '-20' is 3 cells → gap 5).
|
|
30
|
+
const gap = Math.max(...DB_TICKS.map(([, label]) => label.length)) + 2;
|
|
31
|
+
const barW = Math.floor((cols - gap) / 2);
|
|
32
|
+
if (barW < 1) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const rightX = barW + gap;
|
|
36
|
+
|
|
37
|
+
const { amplitudeL, amplitudeR } = viz.data;
|
|
38
|
+
const clamp = (v) => Math.min(1, Math.abs(v || 0));
|
|
39
|
+
const mono = clamp(viz.data.amplitude);
|
|
40
|
+
const levels = [
|
|
41
|
+
amplitudeL == null ? mono : clamp(amplitudeL),
|
|
42
|
+
amplitudeR == null ? mono : clamp(amplitudeR),
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
// Whole-row runs: VU stays off the half-block pipeline. The
|
|
46
|
+
// always-on near-full fill is ~17k cell writes/frame at 4K and
|
|
47
|
+
// starves audio even as plain fillRects; O(rows) wide fills avoid
|
|
48
|
+
// that. The eye tracks the top edge, which steps per row here.
|
|
49
|
+
const bands = gradientBands(colors.playing, colors.accent, rows);
|
|
50
|
+
for (let i = 0; i < 2; i++) {
|
|
51
|
+
const x = i === 0 ? 0 : rightX;
|
|
52
|
+
const width = i === 0 ? barW : cols - rightX;
|
|
53
|
+
const height = Math.round(levels[i] * rows);
|
|
54
|
+
if (height <= 0) continue;
|
|
55
|
+
for (let y = rows - height; y < rows; y++) {
|
|
56
|
+
grid.fillStyle = bandAt(bands, rows - 1 - y);
|
|
57
|
+
grid.fillRect(x, y, width, 1);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// dB labels sit where the bar top reaches that level; rows >= 10
|
|
62
|
+
// keeps ticks on-canvas and distinct (min spacing 1.85 rows).
|
|
63
|
+
if (rows >= 10) {
|
|
64
|
+
grid.fillStyle = colors.muted;
|
|
65
|
+
for (const [db, label] of DB_TICKS) {
|
|
66
|
+
const y = rows - Math.round(rows * 10 ** (db / 20));
|
|
67
|
+
grid.fillText(label, barW + 1, y);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { bandAt, gradientBands } from './color.js';
|
|
2
|
+
import { emitHalfCells, makeHalfGrid } from './grid.js';
|
|
3
|
+
import { bandValues } from './spectrum.js';
|
|
4
|
+
|
|
5
|
+
// Waveform: contiguous 1-cell columns from the same bandValues as bars,
|
|
6
|
+
// growing from the mid cell outward in half-rows; the gradient folds
|
|
7
|
+
// (accent at centre, playing at both extremes). No stride/gap or
|
|
8
|
+
// maxBars stretch — contiguous columns already fill the width.
|
|
9
|
+
export const waveform = {
|
|
10
|
+
needsSpectrum: true,
|
|
11
|
+
box: 'bars',
|
|
12
|
+
draw(grid, colors, viz) {
|
|
13
|
+
const { frequencyBins } = viz.data;
|
|
14
|
+
if (!frequencyBins || frequencyBins.length === 0) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const cols = grid.cols;
|
|
18
|
+
const rows = grid.rows;
|
|
19
|
+
if (cols < 4 || rows < 3) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const mid = Math.floor(rows / 2);
|
|
24
|
+
// Half-height per side in halves; odd row counts leave a half-cell
|
|
25
|
+
// tip, even counts stop short of the top.
|
|
26
|
+
const amp = rows - mid - 1;
|
|
27
|
+
const halfAmp = amp * 2;
|
|
28
|
+
|
|
29
|
+
const values = bandValues(frequencyBins, cols);
|
|
30
|
+
// Mirrored bands over half-height, bounds shared so no gaps.
|
|
31
|
+
const palette = gradientBands(
|
|
32
|
+
colors.accent,
|
|
33
|
+
colors.playing,
|
|
34
|
+
Math.min(amp, 12),
|
|
35
|
+
);
|
|
36
|
+
const bandBounds = palette.map((_, k) => [
|
|
37
|
+
Math.round((k * halfAmp) / palette.length),
|
|
38
|
+
Math.round(((k + 1) * halfAmp) / palette.length),
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const buf = makeHalfGrid(cols, rows);
|
|
42
|
+
for (let i = 0; i < cols; i++) {
|
|
43
|
+
const half = Math.round(values[i] * halfAmp);
|
|
44
|
+
for (let k = 0; k < palette.length; k++) {
|
|
45
|
+
const d0 = bandBounds[k][0];
|
|
46
|
+
const dTop = Math.min(half, bandBounds[k][1]);
|
|
47
|
+
if (dTop <= d0) continue;
|
|
48
|
+
// Count c mirrors above and below the mid cell.
|
|
49
|
+
for (let c = d0 + 1; c <= dTop; c++) {
|
|
50
|
+
const color = bandAt(palette, k);
|
|
51
|
+
buf.slots[(2 * mid - c + 1) * cols + i] = color;
|
|
52
|
+
buf.slots[(2 * mid + c) * cols + i] = color;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
emitHalfCells(grid, buf);
|
|
57
|
+
},
|
|
58
|
+
};
|