-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
69 lines (61 loc) · 2.79 KB
/
Copy pathcore.js
File metadata and controls
69 lines (61 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ============================================================================
core.js — pure, dependency-free helpers shared by the UI and the tests.
SPDX-License-Identifier: GPL-3.0-or-later
Copyright (C) 2026 Joakim Langkilde
No DOM, no MIDI, no app state — just deterministic functions. Loaded as a
classic <script> before app.js (so these become the globals the app uses),
and require()-able by the Node test suite (test/core.test.js).
============================================================================ */
const NOTE_NAMES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
// clamp a value into [0, 1]
function clamp01(v) {
return v < 0 ? 0 : v > 1 ? 1 : v;
}
// knob rotation in degrees for a 0..1 value (-135deg .. +135deg)
function rotFor(v) {
return -135 + v * 270;
}
// is version string a strictly newer than b? (semver major.minor.patch, leading 'v' optional)
function semverGt(a, b) {
const pa = String(a)
.replace(/^v/, '')
.split('.')
.map((n) => parseInt(n, 10) || 0);
const pb = String(b)
.replace(/^v/, '')
.split('.')
.map((n) => parseInt(n, 10) || 0);
for (let i = 0; i < 3; i++) {
if ((pa[i] || 0) > (pb[i] || 0)) return true;
if ((pa[i] || 0) < (pb[i] || 0)) return false;
}
return false;
}
// SVG path for a hanging-wire patch cable between two points
function cablePath(sx, sy, dx, dy) {
const mx = (sx + dx) / 2,
sag = 10 + Math.abs(dx - sx) * 0.06;
return `M ${sx} ${sy} C ${mx} ${sy + sag} ${mx} ${dy + sag} ${dx} ${dy}`;
}
// MIDI note number -> name with octave (middle C = C4 at note 60)
function noteName(n) {
return NOTE_NAMES[((n % 12) + 12) % 12] + (Math.floor(n / 12) - 1);
}
// decode a raw MIDI message into { cat, text } for the activity log
function describeMidi(d) {
const hi = d[0] & 0xf0;
if (hi === 0x90 && d[2] > 0)
return { cat: 'notes', text: `Note On ${noteName(d[1])} v${d[2]}` };
if (hi === 0x80 || (hi === 0x90 && d[2] === 0))
return { cat: 'notes', text: `Note Off ${noteName(d[1])}` };
if (hi === 0xb0) return { cat: 'cc', text: `CC ${d[1]} → ${d[2]}` };
if (hi === 0xe0) return { cat: 'other', text: `Pitch Bend ${((d[2] << 7) | d[1]) - 8192}` };
if (hi === 0xc0) return { cat: 'other', text: `Program ${d[1]}` };
if (hi === 0xd0) return { cat: 'other', text: `Ch Pressure ${d[1]}` };
if (hi === 0xa0) return { cat: 'other', text: `Poly AT ${noteName(d[1])} ${d[2]}` };
return { cat: 'other', text: `0x${d[0].toString(16)}` };
}
// Node test harness can require() this; the browser loads it as a classic script.
if (typeof module !== 'undefined' && module.exports) {
module.exports = { NOTE_NAMES, clamp01, rotFor, semverGt, cablePath, noteName, describeMidi };
}