Spaces:
Sleeping
Sleeping
MIDI patches: a colour per channel, a timbre per patch family
Browse files- README.md +8 -0
- globe_template.html +34 -12
- gm.py +63 -0
- ling_dance.py +9 -5
- ling_globe.py +16 -4
- ling_musicbox.py +26 -11
README.md
CHANGED
|
@@ -79,6 +79,14 @@ The **Dice and board (3D)** tab runs the globe's system on other shapes, and tur
|
|
| 79 |
- **Shapes.** A picture is projected straight on to a tetrahedron (d4), cube (d6), octahedron (d8), dodecahedron (d12) or icosahedron (d20), to the globe, or onto a flat **board** like a Reactable table. It is packed with dials and tiles, face by face (`ling_dice.py`), and wired into the same circuit that plays a MIDI file or a recording (or the tune the signature writes). The page is the globe page: drag to spin, click a dial to send a pulse.
|
| 80 |
- **3D objects (.glb).** Every dial and tile is its own object in a binary glTF (`ling_gltf.py`) that opens in Blender (File > Import > glTF 2.0), Unity, Godot, three.js and model-viewer. Dials can be cylinders, cones, domes, spheres, hexagonal prisms, rings, cubes, pyramids, or a different shape for each key (cone for asm, sphere for lit, cube for bind, cylinder for ctl, dome for call, hexagonal prism for fn, ring for mod). Tiles are slabs; signal dials stand taller. Wires are thin tubes in the key's colour. Each object carries its key, tone, signal number and name as custom properties. They stand on the die, a ball, or a round or square board, sized in metres (0.9 m across for the board, 0.3 m for the rest, or your own).
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
## Run
|
| 83 |
|
| 84 |
```
|
|
|
|
| 79 |
- **Shapes.** A picture is projected straight on to a tetrahedron (d4), cube (d6), octahedron (d8), dodecahedron (d12) or icosahedron (d20), to the globe, or onto a flat **board** like a Reactable table. It is packed with dials and tiles, face by face (`ling_dice.py`), and wired into the same circuit that plays a MIDI file or a recording (or the tune the signature writes). The page is the globe page: drag to spin, click a dial to send a pulse.
|
| 80 |
- **3D objects (.glb).** Every dial and tile is its own object in a binary glTF (`ling_gltf.py`) that opens in Blender (File > Import > glTF 2.0), Unity, Godot, three.js and model-viewer. Dials can be cylinders, cones, domes, spheres, hexagonal prisms, rings, cubes, pyramids, or a different shape for each key (cone for asm, sphere for lit, cube for bind, cylinder for ctl, dome for call, hexagonal prism for fn, ring for mod). Tiles are slabs; signal dials stand taller. Wires are thin tubes in the key's colour. Each object carries its key, tone, signal number and name as custom properties. They stand on the die, a ball, or a round or square board, sized in metres (0.9 m across for the board, 0.3 m for the rest, or your own).
|
| 81 |
|
| 82 |
+
## MIDI patches and channels
|
| 83 |
+
|
| 84 |
+
A MIDI file's patch (instrument) changes are read, so every note knows which General MIDI patch was selected on its channel when it sounded (`gm.py`; channel 10 is the drum kit).
|
| 85 |
+
|
| 86 |
+
- **A colour for each channel.** Drums are red; the other channels take one of the seven key colours each, in the order they first sound. On the globe, dice and board page a pulse lights its dials in the colour of the channel that sent it, and a legend under the title lists each channel with its patch. In the music box the track round the frame and the piano roll draw each channel's notes in its colour.
|
| 87 |
+
- **A timbre for each patch family.** The synth (in the page and in the music box's sound) plays the 16 General MIDI families differently: a plucked triangle for pianos, a bright held square for organs, filtered saws for guitars, basses, strings, ensembles and brass, soft reeds and pipes, slow pads, and so on. Patch changes midway through a piece change the sound from that note on.
|
| 88 |
+
- **Choreography.** With several channels, the colour-turning sections of the enhanced choreography follow the note's channel colour instead of its pitch class.
|
| 89 |
+
|
| 90 |
## Run
|
| 91 |
|
| 92 |
```
|
globe_template.html
CHANGED
|
@@ -39,6 +39,7 @@ button[aria-pressed="true"]{border-color:var(--mod);color:var(--mod)}
|
|
| 39 |
#play{left:12px;right:12px;bottom:12px;max-width:none;display:grid;grid-template-columns:auto auto 1fr auto;gap:10px;align-items:center}
|
| 40 |
#bar{height:14px;border:1px solid var(--ink);position:relative;cursor:pointer}
|
| 41 |
#bar i{position:absolute;left:0;top:0;bottom:0;width:0;background:var(--mod);opacity:.55}
|
|
|
|
| 42 |
#tune{color:var(--ink);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:38vw}
|
| 43 |
#fail{position:fixed;inset:0;display:none;place-items:center;text-align:center;padding:24px}
|
| 44 |
@media (max-width:760px){#keys{display:none}#ctl{width:170px}#read{left:12px;right:12px;bottom:64px}}
|
|
@@ -47,7 +48,7 @@ button[aria-pressed="true"]{border-color:var(--mod);color:var(--mod)}
|
|
| 47 |
<body>
|
| 48 |
<canvas id="gl" role="img" aria-label="A globe packed with dials and tiles showing __ABOUT__. Drag to spin it."></canvas>
|
| 49 |
<div id="fail"><p>This globe needs WebGL 2, which this browser does not offer.</p></div>
|
| 50 |
-
<section class="panel" id="head" aria-label="About"><div id="title"></div><h1>__TITLE__</h1><p id="about"></p><p>Drag to spin · scroll or pinch to zoom · click a dial to send a pulse</p></section>
|
| 51 |
<section class="panel" id="ctl" aria-label="Controls">
|
| 52 |
<button id="bSee" aria-pressed="false">See through</button><button id="bSpin" aria-pressed="true">Turn slowly</button>
|
| 53 |
<button id="bWire" aria-pressed="true">Wires</button><button id="bGround" aria-pressed="false">White ground</button>
|
|
@@ -203,10 +204,10 @@ const S = nodes.map((n, i) => ({ i, role: KEYS[n.role], level: 0, angle: Math.ra
|
|
| 203 |
swap: 0, swapTo: 0, tess: 0, scale: 0, alpha: 1, bright: 0, fireT: -9, fireV: 0, latch: false, ticks: 0, nextTick: 0, tickV: 0, lastPitch: null, seen: -1, pulses: 0, sum: 0 }));
|
| 204 |
for (const s of S) s.target = s.angle;
|
| 205 |
let queue = [], pulseId = 0, simT = 0;
|
| 206 |
-
function send(n, v, pitch, t, dur, id, hop) { if (v >= 0.07 && hop < 14) queue.push({ t, n, v, pitch, dur, id, hop }); }
|
| 207 |
-
function forward(s, v, pitch, t, dur, id, hop) { const r = nodes[s.i].role; for (const o of nodes[s.i].out) send(o, v * GAIN[r], pitch, t + LATE[r], dur, id, hop + 1); }
|
| 208 |
function fire(ev) {
|
| 209 |
-
const s = S[ev.n], { v, pitch, t, dur, id, hop } = ev;
|
| 210 |
if (s.seen === id) return; s.seen = id; // one pulse reaches a dial once
|
| 211 |
s.fireT = t; s.fireV = v; s.level = Math.max(s.level, v); s.sum += v; s.scale = 1; s.pulses++;
|
| 212 |
if (v > 0.8) s.tess = 1; // driven hard: its meter tiles split in four
|
|
@@ -220,7 +221,8 @@ function fire(ev) {
|
|
| 220 |
s.target += (d === 0 ? nodes[s.i].dir * 0.25 : Math.sign(d) * (0.35 + 0.22 * Math.min(Math.abs(d), 7))); break; }
|
| 221 |
default: s.sat = 1.35; // bus
|
| 222 |
}
|
| 223 |
-
|
|
|
|
| 224 |
}
|
| 225 |
function pulse(n, v, pitch, dur) { send(n, v, pitch === undefined ? 60 : pitch, simT, dur || 0.3, ++pulseId, 0); }
|
| 226 |
function step(dt) {
|
|
@@ -253,7 +255,7 @@ function parseMidi(buf) {
|
|
| 253 |
if (str(4) !== 'MThd') throw new Error('not a MIDI file');
|
| 254 |
const hl = d.getUint32(p); p += 4; p += 2; const ntr = d.getUint16(p); p += 2; const div = d.getUint16(p); p += 2; p += hl - 6;
|
| 255 |
if (div & 0x8000) throw new Error('SMPTE time is not supported');
|
| 256 |
-
const tempos = [], raw = [];
|
| 257 |
for (let tr = 0; tr < ntr && p < d.byteLength - 8; tr++) {
|
| 258 |
if (str(4) !== 'MTrk') break; const len = d.getUint32(p); p += 4; const end = p + len; let tick = 0, run = 0; const on = {};
|
| 259 |
const vlq = () => { let v = 0, b; do { b = d.getUint8(p++); v = (v << 7) | (b & 127); } while (b & 128); return v; };
|
|
@@ -266,19 +268,35 @@ function parseMidi(buf) {
|
|
| 266 |
else if (hi === 0x90 || hi === 0x80) { const note = d.getUint8(p++), vel = d.getUint8(p++), key = ch * 128 + note;
|
| 267 |
if (hi === 0x90 && vel > 0) on[key] = [tick, vel];
|
| 268 |
else if (on[key]) { raw.push([on[key][0], tick, note, on[key][1] / 127, ch]); delete on[key]; } }
|
| 269 |
-
else if (hi === 0xc0 || hi === 0xd0) p += 1; else p += 2;
|
| 270 |
}
|
| 271 |
p = end;
|
| 272 |
}
|
| 273 |
tempos.sort((a, b) => a[0] - b[0]); if (!tempos.length || tempos[0][0] > 0) tempos.unshift([0, 500000]);
|
| 274 |
const sec = tick => { let t = 0, last = 0, us = 500000; for (const [tk, u] of tempos) { if (tk >= tick) break; t += (tk - last) * us / div / 1e6; last = tk; us = u; } return t + (tick - last) * us / div / 1e6; };
|
| 275 |
-
|
|
|
|
|
|
|
| 276 |
}
|
| 277 |
let notes = [], songLen = 1, lo = 48, hi = 84, nextNote = 0, songT = 0, playing = true;
|
| 278 |
const strobes = S.filter(s => s.role === 'asm').map(s => s.i);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
function setTune(list, name) {
|
| 280 |
notes = list; songLen = Math.max(1, ...list.map(n => n[0] + n[1])) + 0.6; nextNote = 0; songT = 0; queue = [];
|
| 281 |
const ps = list.filter(n => n[4] !== 9).map(n => n[2]); lo = Math.min(...ps, 127); hi = Math.max(...ps, 0); if (hi <= lo) hi = lo + 1;
|
|
|
|
| 282 |
$('tune').textContent = name + ' · ' + list.length + ' notes';
|
| 283 |
}
|
| 284 |
// low notes enter near the root, high notes far from it; drums enter at the strobes
|
|
@@ -290,9 +308,13 @@ function voice(n, when) {
|
|
| 290 |
const [t, dur, pitch, vel, ch] = n, g = ac.createGain(); g.connect(master);
|
| 291 |
if (ch === 9) { const src = ac.createBufferSource(); src.buffer = noise; const f = ac.createBiquadFilter(); f.type = pitch < 40 ? 'lowpass' : 'highpass'; f.frequency.value = pitch < 40 ? 180 : 4000 + pitch * 40;
|
| 292 |
src.connect(f); f.connect(g); g.gain.setValueAtTime(vel * 0.5, when); g.gain.exponentialRampToValueAtTime(0.001, when + 0.14); src.start(when); src.stop(when + 0.16); return; }
|
| 293 |
-
const
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
}
|
| 297 |
function soundOn() {
|
| 298 |
if (!ac) { ac = new (window.AudioContext || window.webkitAudioContext)(); master = ac.createGain(); master.gain.value = 0.7; master.connect(ac.destination);
|
|
@@ -312,7 +334,7 @@ function advance(dt) { // move the tune
|
|
| 312 |
if (!playing) { au.pause(); return; }
|
| 313 |
songT += dt; if (songT >= songLen) { songT = 0; nextNote = 0; if (haveAudio) au.currentTime = 0; }
|
| 314 |
while (nextNote < notes.length && notes[nextNote][0] <= songT) {
|
| 315 |
-
const n = notes[nextNote++]; send(dialFor(n), 0.35 + 0.65 * n[3], n[2], simT, n[1], ++pulseId, 0);
|
| 316 |
if (sound && ac && hear === 'midi') voice(n, ac.currentTime + 0.02);
|
| 317 |
}
|
| 318 |
if (sound && hear === 'recording' && haveAudio) {
|
|
|
|
| 39 |
#play{left:12px;right:12px;bottom:12px;max-width:none;display:grid;grid-template-columns:auto auto 1fr auto;gap:10px;align-items:center}
|
| 40 |
#bar{height:14px;border:1px solid var(--ink);position:relative;cursor:pointer}
|
| 41 |
#bar i{position:absolute;left:0;top:0;bottom:0;width:0;background:var(--mod);opacity:.55}
|
| 42 |
+
#chans{margin:4px 0 0;line-height:1.5;color:var(--ink)}#chans b{font-weight:600}
|
| 43 |
#tune{color:var(--ink);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:38vw}
|
| 44 |
#fail{position:fixed;inset:0;display:none;place-items:center;text-align:center;padding:24px}
|
| 45 |
@media (max-width:760px){#keys{display:none}#ctl{width:170px}#read{left:12px;right:12px;bottom:64px}}
|
|
|
|
| 48 |
<body>
|
| 49 |
<canvas id="gl" role="img" aria-label="A globe packed with dials and tiles showing __ABOUT__. Drag to spin it."></canvas>
|
| 50 |
<div id="fail"><p>This globe needs WebGL 2, which this browser does not offer.</p></div>
|
| 51 |
+
<section class="panel" id="head" aria-label="About"><div id="title"></div><h1>__TITLE__</h1><p id="about"></p><p>Drag to spin · scroll or pinch to zoom · click a dial to send a pulse</p><p id="chans" aria-label="Channels and patches"></p></section>
|
| 52 |
<section class="panel" id="ctl" aria-label="Controls">
|
| 53 |
<button id="bSee" aria-pressed="false">See through</button><button id="bSpin" aria-pressed="true">Turn slowly</button>
|
| 54 |
<button id="bWire" aria-pressed="true">Wires</button><button id="bGround" aria-pressed="false">White ground</button>
|
|
|
|
| 204 |
swap: 0, swapTo: 0, tess: 0, scale: 0, alpha: 1, bright: 0, fireT: -9, fireV: 0, latch: false, ticks: 0, nextTick: 0, tickV: 0, lastPitch: null, seen: -1, pulses: 0, sum: 0 }));
|
| 205 |
for (const s of S) s.target = s.angle;
|
| 206 |
let queue = [], pulseId = 0, simT = 0;
|
| 207 |
+
function send(n, v, pitch, t, dur, id, hop, ch = -1) { if (v >= 0.07 && hop < 14) queue.push({ t, n, v, pitch, dur, id, hop, ch }); }
|
| 208 |
+
function forward(s, v, pitch, t, dur, id, hop, ch) { const r = nodes[s.i].role; for (const o of nodes[s.i].out) send(o, v * GAIN[r], pitch, t + LATE[r], dur, id, hop + 1, ch); }
|
| 209 |
function fire(ev) {
|
| 210 |
+
const s = S[ev.n], { v, pitch, t, dur, id, hop, ch } = ev;
|
| 211 |
if (s.seen === id) return; s.seen = id; // one pulse reaches a dial once
|
| 212 |
s.fireT = t; s.fireV = v; s.level = Math.max(s.level, v); s.sum += v; s.scale = 1; s.pulses++;
|
| 213 |
if (v > 0.8) s.tess = 1; // driven hard: its meter tiles split in four
|
|
|
|
| 221 |
s.target += (d === 0 ? nodes[s.i].dir * 0.25 : Math.sign(d) * (0.35 + 0.22 * Math.min(Math.abs(d), 7))); break; }
|
| 222 |
default: s.sat = 1.35; // bus
|
| 223 |
}
|
| 224 |
+
if (ch >= 0 && s.role !== 'call' && chHue[ch] !== undefined) s.hue = (1 + chHue[ch]) * 0.8; // lit in the colour of the channel that sent the pulse
|
| 225 |
+
forward(s, v, pitch, t, dur, id, hop, ch); // then on along its wires: how strongly and how late follows its key
|
| 226 |
}
|
| 227 |
function pulse(n, v, pitch, dur) { send(n, v, pitch === undefined ? 60 : pitch, simT, dur || 0.3, ++pulseId, 0); }
|
| 228 |
function step(dt) {
|
|
|
|
| 255 |
if (str(4) !== 'MThd') throw new Error('not a MIDI file');
|
| 256 |
const hl = d.getUint32(p); p += 4; p += 2; const ntr = d.getUint16(p); p += 2; const div = d.getUint16(p); p += 2; p += hl - 6;
|
| 257 |
if (div & 0x8000) throw new Error('SMPTE time is not supported');
|
| 258 |
+
const tempos = [], raw = [], progs = [];
|
| 259 |
for (let tr = 0; tr < ntr && p < d.byteLength - 8; tr++) {
|
| 260 |
if (str(4) !== 'MTrk') break; const len = d.getUint32(p); p += 4; const end = p + len; let tick = 0, run = 0; const on = {};
|
| 261 |
const vlq = () => { let v = 0, b; do { b = d.getUint8(p++); v = (v << 7) | (b & 127); } while (b & 128); return v; };
|
|
|
|
| 268 |
else if (hi === 0x90 || hi === 0x80) { const note = d.getUint8(p++), vel = d.getUint8(p++), key = ch * 128 + note;
|
| 269 |
if (hi === 0x90 && vel > 0) on[key] = [tick, vel];
|
| 270 |
else if (on[key]) { raw.push([on[key][0], tick, note, on[key][1] / 127, ch]); delete on[key]; } }
|
| 271 |
+
else if (hi === 0xc0 || hi === 0xd0) { if (hi === 0xc0) progs.push([tick, ch, d.getUint8(p)]); p += 1; } else p += 2;
|
| 272 |
}
|
| 273 |
p = end;
|
| 274 |
}
|
| 275 |
tempos.sort((a, b) => a[0] - b[0]); if (!tempos.length || tempos[0][0] > 0) tempos.unshift([0, 500000]);
|
| 276 |
const sec = tick => { let t = 0, last = 0, us = 500000; for (const [tk, u] of tempos) { if (tk >= tick) break; t += (tk - last) * us / div / 1e6; last = tk; us = u; } return t + (tick - last) * us / div / 1e6; };
|
| 277 |
+
progs.sort((x, y) => x[0] - y[0]);
|
| 278 |
+
const patchAt = (tick, ch) => { if (ch === 9) return 128; let cur = 0; for (const [t, c, pr] of progs) { if (t > tick) break; if (c === ch) cur = pr; } return cur; }; // the patch selected on the channel then
|
| 279 |
+
return raw.map(([a, b, n, v, c]) => [sec(a), Math.max(0.05, sec(b) - sec(a)), n, v, c, patchAt(a, c)]).sort((x, y) => x[0] - y[0]);
|
| 280 |
}
|
| 281 |
let notes = [], songLen = 1, lo = 48, hi = 84, nextNote = 0, songT = 0, playing = true;
|
| 282 |
const strobes = S.filter(s => s.role === 'asm').map(s => s.i);
|
| 283 |
+
let chHue = {};
|
| 284 |
+
function channelHues(list) { // as gm.channel_hues: drums red, the others one colour each in the order they first sound
|
| 285 |
+
const first = {}; for (const n of list) if (first[n[4]] === undefined) first[n[4]] = n[0];
|
| 286 |
+
const hues = {}; let k = 0;
|
| 287 |
+
for (const ch of Object.keys(first).map(Number).sort((a, b) => (a === 9 ? -1 : b === 9 ? 1 : first[a] - first[b]))) { if (ch === 9) hues[ch] = 0; else { k++; hues[ch] = k % 7; } }
|
| 288 |
+
return hues;
|
| 289 |
+
}
|
| 290 |
+
function renderChannels(list) {
|
| 291 |
+
const prog = {}, count = {}; for (const n of list) { count[n[4]] = (count[n[4]] || 0) + 1; if (prog[n[4]] === undefined) prog[n[4]] = n[4] === 9 ? 128 : (n[5] || 0); }
|
| 292 |
+
const nm = p => p >= 128 ? 'Drum kit' : (D.gm.names[p] || 'patch ' + p);
|
| 293 |
+
$('chans').innerHTML = Object.keys(count).map(Number).sort((a, b) => a - b).map(ch =>
|
| 294 |
+
'<span style="white-space:nowrap"><b style="color:var(--' + KEYS[chHue[ch]] + ')">●</b> ' + (ch + 1) + ' ' + nm(prog[ch]) + '</span>').join(' ');
|
| 295 |
+
}
|
| 296 |
function setTune(list, name) {
|
| 297 |
notes = list; songLen = Math.max(1, ...list.map(n => n[0] + n[1])) + 0.6; nextNote = 0; songT = 0; queue = [];
|
| 298 |
const ps = list.filter(n => n[4] !== 9).map(n => n[2]); lo = Math.min(...ps, 127); hi = Math.max(...ps, 0); if (hi <= lo) hi = lo + 1;
|
| 299 |
+
chHue = channelHues(list); renderChannels(list);
|
| 300 |
$('tune').textContent = name + ' · ' + list.length + ' notes';
|
| 301 |
}
|
| 302 |
// low notes enter near the root, high notes far from it; drums enter at the strobes
|
|
|
|
| 308 |
const [t, dur, pitch, vel, ch] = n, g = ac.createGain(); g.connect(master);
|
| 309 |
if (ch === 9) { const src = ac.createBufferSource(); src.buffer = noise; const f = ac.createBiquadFilter(); f.type = pitch < 40 ? 'lowpass' : 'highpass'; f.frequency.value = pitch < 40 ? 180 : 4000 + pitch * 40;
|
| 310 |
src.connect(f); f.connect(g); g.gain.setValueAtTime(vel * 0.5, when); g.gain.exponentialRampToValueAtTime(0.001, when + 0.14); src.start(when); src.stop(when + 0.16); return; }
|
| 311 |
+
const [wave, att, tau, rel, cut] = D.gm.timbres[Math.min(127, n[5] || 0) >> 3] || D.gm.timbres[0]; // the family's timbre: wave, attack, decay, release, lowpass
|
| 312 |
+
const o = ac.createOscillator(); o.type = wave; o.frequency.value = 440 * Math.pow(2, (pitch - 69) / 12);
|
| 313 |
+
if (cut) { const f = ac.createBiquadFilter(); f.type = 'lowpass'; f.frequency.value = cut; o.connect(f); f.connect(g); } else o.connect(g);
|
| 314 |
+
const top = vel * (wave === 'sawtooth' || wave === 'square' ? 0.12 : 0.22);
|
| 315 |
+
g.gain.setValueAtTime(0.0001, when); g.gain.linearRampToValueAtTime(top, when + Math.max(att, 0.004));
|
| 316 |
+
if (tau) g.gain.setTargetAtTime(0.0001, when + att, tau); else { g.gain.setValueAtTime(top, when + Math.max(dur, att)); g.gain.setTargetAtTime(0.0001, when + Math.max(dur, att), rel / 3); }
|
| 317 |
+
o.start(when); o.stop(when + dur + rel + 0.15);
|
| 318 |
}
|
| 319 |
function soundOn() {
|
| 320 |
if (!ac) { ac = new (window.AudioContext || window.webkitAudioContext)(); master = ac.createGain(); master.gain.value = 0.7; master.connect(ac.destination);
|
|
|
|
| 334 |
if (!playing) { au.pause(); return; }
|
| 335 |
songT += dt; if (songT >= songLen) { songT = 0; nextNote = 0; if (haveAudio) au.currentTime = 0; }
|
| 336 |
while (nextNote < notes.length && notes[nextNote][0] <= songT) {
|
| 337 |
+
const n = notes[nextNote++]; send(dialFor(n), 0.35 + 0.65 * n[3], n[2], simT, n[1], ++pulseId, 0, n[4]);
|
| 338 |
if (sound && ac && hear === 'midi') voice(n, ac.currentTime + 0.02);
|
| 339 |
}
|
| 340 |
if (sound && hear === 'recording' && haveAudio) {
|
gm.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""General MIDI patches: names, instrument families, a timbre for each family, and a colour for each channel.
|
| 2 |
+
|
| 3 |
+
A note is [start s, length s, pitch, velocity 0..1, channel, program]; the program (0..127, or 128 for the drum kit on channel 10) is the patch
|
| 4 |
+
that was selected on its channel when it sounded. Notes found by the FFT have no sixth field: they count as program 0.
|
| 5 |
+
"""
|
| 6 |
+
from ling_hatch import KEYS
|
| 7 |
+
|
| 8 |
+
NAMES = (
|
| 9 |
+
'Acoustic Grand Piano,Bright Acoustic Piano,Electric Grand Piano,Honky-tonk Piano,Electric Piano 1,Electric Piano 2,Harpsichord,Clavinet,'
|
| 10 |
+
'Celesta,Glockenspiel,Music Box,Vibraphone,Marimba,Xylophone,Tubular Bells,Dulcimer,'
|
| 11 |
+
'Drawbar Organ,Percussive Organ,Rock Organ,Church Organ,Reed Organ,Accordion,Harmonica,Tango Accordion,'
|
| 12 |
+
'Nylon Guitar,Steel Guitar,Jazz Guitar,Clean Guitar,Muted Guitar,Overdriven Guitar,Distortion Guitar,Guitar Harmonics,'
|
| 13 |
+
'Acoustic Bass,Finger Bass,Pick Bass,Fretless Bass,Slap Bass 1,Slap Bass 2,Synth Bass 1,Synth Bass 2,'
|
| 14 |
+
'Violin,Viola,Cello,Contrabass,Tremolo Strings,Pizzicato Strings,Orchestral Harp,Timpani,'
|
| 15 |
+
'String Ensemble 1,String Ensemble 2,Synth Strings 1,Synth Strings 2,Choir Aahs,Voice Oohs,Synth Voice,Orchestra Hit,'
|
| 16 |
+
'Trumpet,Trombone,Tuba,Muted Trumpet,French Horn,Brass Section,Synth Brass 1,Synth Brass 2,'
|
| 17 |
+
'Soprano Sax,Alto Sax,Tenor Sax,Baritone Sax,Oboe,English Horn,Bassoon,Clarinet,'
|
| 18 |
+
'Piccolo,Flute,Recorder,Pan Flute,Blown Bottle,Shakuhachi,Whistle,Ocarina,'
|
| 19 |
+
'Square Lead,Saw Lead,Calliope Lead,Chiff Lead,Charang Lead,Voice Lead,Fifths Lead,Bass and Lead,'
|
| 20 |
+
'New Age Pad,Warm Pad,Polysynth Pad,Choir Pad,Bowed Pad,Metallic Pad,Halo Pad,Sweep Pad,'
|
| 21 |
+
'Rain,Soundtrack,Crystal,Atmosphere,Brightness,Goblins,Echoes,Sci-Fi,'
|
| 22 |
+
'Sitar,Banjo,Shamisen,Koto,Kalimba,Bagpipe,Fiddle,Shanai,'
|
| 23 |
+
'Tinkle Bell,Agogo,Steel Drums,Woodblock,Taiko Drum,Melodic Tom,Synth Drum,Reverse Cymbal,'
|
| 24 |
+
'Guitar Fret Noise,Breath Noise,Seashore,Bird Tweet,Telephone Ring,Helicopter,Applause,Gunshot').split(',')
|
| 25 |
+
FAMILIES = ['Piano', 'Chromatic percussion', 'Organ', 'Guitar', 'Bass', 'Strings', 'Ensemble', 'Brass', 'Reed', 'Pipe', 'Synth lead', 'Synth pad',
|
| 26 |
+
'Synth effects', 'Ethnic', 'Percussive', 'Sound effects']
|
| 27 |
+
# per family: (wave, attack s, decay time constant s (0: held), release s, lowpass Hz (0: none))
|
| 28 |
+
TIMBRES = [('triangle', 0.005, 1.6, 0.25, 0), ('sine', 0.002, 0.6, 0.3, 0), ('square', 0.01, 0.0, 0.05, 2400), ('sawtooth', 0.004, 0.9, 0.2, 2200),
|
| 29 |
+
('sawtooth', 0.008, 1.2, 0.15, 700), ('sawtooth', 0.12, 0.0, 0.3, 3200), ('sawtooth', 0.15, 0.0, 0.35, 2600), ('sawtooth', 0.05, 0.0, 0.12, 2400),
|
| 30 |
+
('square', 0.03, 0.0, 0.12, 1800), ('sine', 0.06, 0.0, 0.15, 0), ('square', 0.01, 0.0, 0.08, 3500), ('sawtooth', 0.35, 0.0, 0.5, 1500),
|
| 31 |
+
('sawtooth', 0.2, 0.0, 0.6, 1200), ('triangle', 0.005, 1.0, 0.2, 0), ('square', 0.001, 0.25, 0.05, 1500), ('sine', 0.1, 0.8, 0.4, 0)]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def name(program: int) -> str:
|
| 35 |
+
return 'Drum kit' if program >= 128 else NAMES[max(0, min(127, int(program)))]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def program_of(note) -> int:
|
| 39 |
+
return 128 if note[4] == 9 else (int(note[5]) if len(note) > 5 else 0)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def timbre(program: int):
|
| 43 |
+
return TIMBRES[(min(127, max(0, int(program)))) >> 3]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def channel_hues(notes) -> dict:
|
| 47 |
+
"""channel -> which of the seven key colours (an index into KEYS). The drum channel is the first colour, red; the other channels follow
|
| 48 |
+
in the order they first sound, one colour each, going round the seven."""
|
| 49 |
+
first = {}
|
| 50 |
+
for n in notes: first.setdefault(int(n[4]), n[0])
|
| 51 |
+
hues = {}; k = 0
|
| 52 |
+
for ch in sorted(first, key=lambda c: (c != 9, first[c])):
|
| 53 |
+
if ch == 9: hues[ch] = 0
|
| 54 |
+
else: k += 1; hues[ch] = k % 7
|
| 55 |
+
return hues
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def channels(notes) -> list:
|
| 59 |
+
"""The channels of a tune, for a legend: [{'channel', 'hue' (index into KEYS), 'key', 'program', 'patch', 'notes'}]"""
|
| 60 |
+
hues = channel_hues(notes); prog = {}; count = {}
|
| 61 |
+
for n in notes:
|
| 62 |
+
c = int(n[4]); count[c] = count.get(c, 0) + 1; prog.setdefault(c, program_of(n))
|
| 63 |
+
return [{'channel': c + 1, 'hue': hues[c], 'key': KEYS[hues[c]], 'program': prog[c], 'patch': name(prog[c]), 'notes': count[c]} for c in sorted(hues)]
|
ling_dance.py
CHANGED
|
@@ -20,6 +20,8 @@ import math
|
|
| 20 |
|
| 21 |
import numpy as np
|
| 22 |
|
|
|
|
|
|
|
| 23 |
MOVES = ['ripple', 'waterfall', 'sweep', 'march', 'spiral', 'kaleidoscope']
|
| 24 |
WARM = {0, 1, 2} # asm, lit, bind: red, orange, yellow; the rest are cool
|
| 25 |
|
|
@@ -53,18 +55,19 @@ class Show:
|
|
| 53 |
def __init__(self, sc, nodes, view, notes, T):
|
| 54 |
self.x, self.y, self.r, self.th, self.hue, self.seen = positions(sc, nodes, view)
|
| 55 |
self.N = len(nodes); self.T = T
|
| 56 |
-
self.beat, self.t0 = beat_of(notes)
|
| 57 |
ps = [n[2] for n in notes if n[4] != 9] or [60]; self.lo, self.hi = min(ps), max(ps) if max(ps) > min(ps) else min(ps) + 1
|
| 58 |
self.out = [[] for _ in range(self.N)]; self.row = np.floor((self.y + 1) * 4.0).astype(int); self.k = 0
|
| 59 |
|
| 60 |
def section(self, t):
|
| 61 |
return int(max(0.0, t - self.t0) // (8 * self.beat)) # two bars of four beats
|
| 62 |
|
| 63 |
-
def lane(self, pitch, sec):
|
| 64 |
"""Colour turning: in some sections a note keeps to its own hue (and the complement), in some it leaves that hue out."""
|
| 65 |
mode = sec % 4
|
| 66 |
if mode in (0, 2): return np.ones(self.N, bool)
|
| 67 |
-
h = int((pitch % 12) * 7 // 12)
|
|
|
|
| 68 |
return keep if mode == 1 else ~keep
|
| 69 |
|
| 70 |
# ---- the moves: each gives (delay seconds, gain 0..1) for every dial; dials with gain <= 0 stay dark
|
|
@@ -101,11 +104,12 @@ class Show:
|
|
| 101 |
for n in idx: self.out[n].append(((t + float(delay[n])) % self.T, float(min(1.0, v0 * g[n])), pitch, length))
|
| 102 |
|
| 103 |
def run(self, notes):
|
| 104 |
-
for i,
|
|
|
|
| 105 |
sec = self.section(t); mv = MOVES[sec % len(MOVES)]; all_ = np.ones(self.N, bool)
|
| 106 |
if ch == 9: # drums: rings, whatever else is going on
|
| 107 |
d, g = self.ripple(p, inward=p >= 40); self.add(t, v, p, ln, d, g * (0.7 if mv == 'march' else 1.0), all_); continue
|
| 108 |
-
keep = self.lane(p, sec)
|
| 109 |
if mv == 'ripple': d, g = self.ripple(p, inward=(i % 3 == 2))
|
| 110 |
elif mv == 'waterfall': d, g = self.waterfall(p, i)
|
| 111 |
elif mv == 'sweep': d, g = self.sweep(p, i)
|
|
|
|
| 20 |
|
| 21 |
import numpy as np
|
| 22 |
|
| 23 |
+
import gm
|
| 24 |
+
|
| 25 |
MOVES = ['ripple', 'waterfall', 'sweep', 'march', 'spiral', 'kaleidoscope']
|
| 26 |
WARM = {0, 1, 2} # asm, lit, bind: red, orange, yellow; the rest are cool
|
| 27 |
|
|
|
|
| 55 |
def __init__(self, sc, nodes, view, notes, T):
|
| 56 |
self.x, self.y, self.r, self.th, self.hue, self.seen = positions(sc, nodes, view)
|
| 57 |
self.N = len(nodes); self.T = T
|
| 58 |
+
self.beat, self.t0 = beat_of(notes); self.chue = gm.channel_hues(notes)
|
| 59 |
ps = [n[2] for n in notes if n[4] != 9] or [60]; self.lo, self.hi = min(ps), max(ps) if max(ps) > min(ps) else min(ps) + 1
|
| 60 |
self.out = [[] for _ in range(self.N)]; self.row = np.floor((self.y + 1) * 4.0).astype(int); self.k = 0
|
| 61 |
|
| 62 |
def section(self, t):
|
| 63 |
return int(max(0.0, t - self.t0) // (8 * self.beat)) # two bars of four beats
|
| 64 |
|
| 65 |
+
def lane(self, pitch, sec, ch=-1):
|
| 66 |
"""Colour turning: in some sections a note keeps to its own hue (and the complement), in some it leaves that hue out."""
|
| 67 |
mode = sec % 4
|
| 68 |
if mode in (0, 2): return np.ones(self.N, bool)
|
| 69 |
+
h = self.chue[ch] if (ch in self.chue and len(self.chue) > 1) else int((pitch % 12) * 7 // 12) # with several channels, a note keeps to its channel's colour
|
| 70 |
+
keep = (self.hue == h) | (self.hue == (h + 3) % 7)
|
| 71 |
return keep if mode == 1 else ~keep
|
| 72 |
|
| 73 |
# ---- the moves: each gives (delay seconds, gain 0..1) for every dial; dials with gain <= 0 stay dark
|
|
|
|
| 104 |
for n in idx: self.out[n].append(((t + float(delay[n])) % self.T, float(min(1.0, v0 * g[n])), pitch, length))
|
| 105 |
|
| 106 |
def run(self, notes):
|
| 107 |
+
for i, note in enumerate(notes):
|
| 108 |
+
t, ln, p, v, ch = note[:5]
|
| 109 |
sec = self.section(t); mv = MOVES[sec % len(MOVES)]; all_ = np.ones(self.N, bool)
|
| 110 |
if ch == 9: # drums: rings, whatever else is going on
|
| 111 |
d, g = self.ripple(p, inward=p >= 40); self.add(t, v, p, ln, d, g * (0.7 if mv == 'march' else 1.0), all_); continue
|
| 112 |
+
keep = self.lane(p, sec, int(ch))
|
| 113 |
if mv == 'ripple': d, g = self.ripple(p, inward=(i % 3 == 2))
|
| 114 |
elif mv == 'waterfall': d, g = self.waterfall(p, i)
|
| 115 |
elif mv == 'sweep': d, g = self.sweep(p, i)
|
ling_globe.py
CHANGED
|
@@ -44,6 +44,7 @@ from PIL import Image
|
|
| 44 |
from scipy.spatial import cKDTree
|
| 45 |
|
| 46 |
import ling_hatch as lh
|
|
|
|
| 47 |
import ling_tiles as lt
|
| 48 |
import ling_map as lm
|
| 49 |
from ling_hatch import KEYS, EMPTY, THEMES
|
|
@@ -505,7 +506,7 @@ def read_midi(path) -> list:
|
|
| 505 |
if d[:4] != b'MThd': raise ValueError('%s is not a MIDI file' % Path(path).name)
|
| 506 |
hl = int.from_bytes(d[4:8], 'big'); ntr = int.from_bytes(d[10:12], 'big'); div = int.from_bytes(d[12:14], 'big'); p = 8 + hl
|
| 507 |
if div & 0x8000: raise ValueError('SMPTE time is not supported')
|
| 508 |
-
tempos, raw = [], []
|
| 509 |
for _ in range(ntr):
|
| 510 |
if d[p:p + 4] != b'MTrk': break
|
| 511 |
end = p + 8 + int.from_bytes(d[p + 4:p + 8], 'big'); p += 8; tick = 0; run = 0; on = {}
|
|
@@ -532,7 +533,9 @@ def read_midi(path) -> list:
|
|
| 532 |
note, vel = d[p], d[p + 1]; p += 2; key = ch * 128 + note
|
| 533 |
if hi == 0x90 and vel > 0: on[key] = (tick, vel)
|
| 534 |
elif key in on: raw.append((on[key][0], tick, note, on[key][1] / 127, ch)); del on[key]
|
| 535 |
-
elif hi in (0xc0, 0xd0):
|
|
|
|
|
|
|
| 536 |
else: p += 2
|
| 537 |
p = end
|
| 538 |
tempos.sort()
|
|
@@ -544,7 +547,16 @@ def read_midi(path) -> list:
|
|
| 544 |
if at >= tk: break
|
| 545 |
t += (at - last) * us / div / 1e6; last, us = at, u
|
| 546 |
return t + (tk - last) * us / div / 1e6
|
| 547 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
|
| 549 |
|
| 550 |
def read_audio(path, rate: int = 22050):
|
|
@@ -1258,7 +1270,7 @@ def scene_page(sc: dict, g: GlobeOptions, progress=None):
|
|
| 1258 |
audio_src = g.wav or (g.midi if (g.midi and not is_midi) else '') # the recording the page can play instead of its synth
|
| 1259 |
try: audio = audio_data_uri(audio_src) if audio_src else ''
|
| 1260 |
except Exception: audio = ''
|
| 1261 |
-
data = {'audio': audio, 'audio_name': Path(audio_src).name if audio else '', 'name': name, 'title': title, 'kind': sc['kind'], 'solid': 1 if sc.get('solid') else 0, 'keys': KEYS, 'themes': themes, 'counts': counts,
|
| 1262 |
'elements': _b64(E), 'n_elements': int(len(E)), 'templates': tmpl, 'nodes': nodes,
|
| 1263 |
'wire_pts': _b64(wp), 'wires': wt, 'legend': legend_markup(), 'title_svg': title_markup(title),
|
| 1264 |
'sha3_256': params['sha3_256'], 'algorithm': params['algorithm'],
|
|
|
|
| 44 |
from scipy.spatial import cKDTree
|
| 45 |
|
| 46 |
import ling_hatch as lh
|
| 47 |
+
import gm
|
| 48 |
import ling_tiles as lt
|
| 49 |
import ling_map as lm
|
| 50 |
from ling_hatch import KEYS, EMPTY, THEMES
|
|
|
|
| 506 |
if d[:4] != b'MThd': raise ValueError('%s is not a MIDI file' % Path(path).name)
|
| 507 |
hl = int.from_bytes(d[4:8], 'big'); ntr = int.from_bytes(d[10:12], 'big'); div = int.from_bytes(d[12:14], 'big'); p = 8 + hl
|
| 508 |
if div & 0x8000: raise ValueError('SMPTE time is not supported')
|
| 509 |
+
tempos, raw, progs = [], [], []
|
| 510 |
for _ in range(ntr):
|
| 511 |
if d[p:p + 4] != b'MTrk': break
|
| 512 |
end = p + 8 + int.from_bytes(d[p + 4:p + 8], 'big'); p += 8; tick = 0; run = 0; on = {}
|
|
|
|
| 533 |
note, vel = d[p], d[p + 1]; p += 2; key = ch * 128 + note
|
| 534 |
if hi == 0x90 and vel > 0: on[key] = (tick, vel)
|
| 535 |
elif key in on: raw.append((on[key][0], tick, note, on[key][1] / 127, ch)); del on[key]
|
| 536 |
+
elif hi in (0xc0, 0xd0):
|
| 537 |
+
if hi == 0xc0: progs.append((tick, ch, d[p])) # a patch change
|
| 538 |
+
p += 1
|
| 539 |
else: p += 2
|
| 540 |
p = end
|
| 541 |
tempos.sort()
|
|
|
|
| 547 |
if at >= tk: break
|
| 548 |
t += (at - last) * us / div / 1e6; last, us = at, u
|
| 549 |
return t + (tk - last) * us / div / 1e6
|
| 550 |
+
progs.sort()
|
| 551 |
+
|
| 552 |
+
def patch_at(tick, ch): # the patch selected on this channel when the note sounded (128: the drum kit)
|
| 553 |
+
if ch == 9: return 128
|
| 554 |
+
cur = 0
|
| 555 |
+
for t, c, pr in progs:
|
| 556 |
+
if t > tick: break
|
| 557 |
+
if c == ch: cur = pr
|
| 558 |
+
return cur
|
| 559 |
+
return sorted([[sec(a), max(0.05, sec(b) - sec(a)), n, v, c, patch_at(a, c)] for a, b, n, v, c in raw])
|
| 560 |
|
| 561 |
|
| 562 |
def read_audio(path, rate: int = 22050):
|
|
|
|
| 1270 |
audio_src = g.wav or (g.midi if (g.midi and not is_midi) else '') # the recording the page can play instead of its synth
|
| 1271 |
try: audio = audio_data_uri(audio_src) if audio_src else ''
|
| 1272 |
except Exception: audio = ''
|
| 1273 |
+
data = {'gm': {'names': gm.NAMES, 'families': gm.FAMILIES, 'timbres': gm.TIMBRES}, 'audio': audio, 'audio_name': Path(audio_src).name if audio else '', 'name': name, 'title': title, 'kind': sc['kind'], 'solid': 1 if sc.get('solid') else 0, 'keys': KEYS, 'themes': themes, 'counts': counts,
|
| 1274 |
'elements': _b64(E), 'n_elements': int(len(E)), 'templates': tmpl, 'nodes': nodes,
|
| 1275 |
'wire_pts': _b64(wp), 'wires': wt, 'legend': legend_markup(), 'title_svg': title_markup(title),
|
| 1276 |
'sha3_256': params['sha3_256'], 'algorithm': params['algorithm'],
|
ling_musicbox.py
CHANGED
|
@@ -40,6 +40,8 @@ import ling_tiles as lt
|
|
| 40 |
import ling_map as lm
|
| 41 |
import ling_globe as lg
|
| 42 |
import ling_dance as ld
|
|
|
|
|
|
|
| 43 |
import sizes
|
| 44 |
from ling_hatch import KEYS, THEMES, EMPTY
|
| 45 |
|
|
@@ -73,7 +75,7 @@ def window(notes, b: BoxOptions):
|
|
| 73 |
"""The part of the tune the sheet plays, moved to start at 0. -> (notes, length in seconds)"""
|
| 74 |
t0 = max(0.0, b.start); end = max((n[0] + n[1] for n in notes), default=1.0)
|
| 75 |
T = (end - t0 if b.seconds <= 0 else min(b.seconds, end - t0)); T = max(2.0, T)
|
| 76 |
-
return [[n[0] - t0, min(n[1], T - (n[0] - t0)), n[2], n[3], n[4]] for n in notes if t0 <= n[0] < t0 + T], T
|
| 77 |
|
| 78 |
|
| 79 |
def arrivals(notes, entry, nodes, T, speed: float = 1.0):
|
|
@@ -541,11 +543,11 @@ class Track:
|
|
| 541 |
def track_svg(tr: Track, notes, entry, nodes, T, theme, geo):
|
| 542 |
"""The band's two rules, every note on it, and the playhead that travels it. -> (still, moving, css)"""
|
| 543 |
th = THEMES[theme]; base, lit, dull, ans = role_colours(theme); line = geo['line']; x, y, w, h = tr.rect; bd = tr.band
|
| 544 |
-
ps = [n[2] for n in notes if n[4] != 9] or [60]; lo, hi = min(ps), max(ps); span = max(1, hi - lo); by = {}
|
| 545 |
for n, e in zip(notes, entry):
|
| 546 |
d0 = n[0] / T * tr.P; d1 = d0 + max(min(n[1], T - n[0]) / T * tr.P, 1.5 * line)
|
| 547 |
off = (-0.42 if n[4] == 9 else -0.3 + 0.7 * (n[2] - lo) / span) * bd
|
| 548 |
-
by.setdefault(nodes[e]['role'], []).append(tr.run(d0, d1, off))
|
| 549 |
rules = 'M%d %dh%dv%dh%dz' % (x, y, w, h, -w) + 'M%d %dh%dv%dh%dz' % (x + bd, y + bd, w - 2 * bd, h - 2 * bd, -(w - 2 * bd))
|
| 550 |
still = '<g id="track" stroke-width="%d"><path stroke="%s" stroke-width="%d" d="%s"/>%s</g>' % (
|
| 551 |
round(1.5 * line), th['ink'], line, rules, ''.join('<path stroke="%s" d="%s"/>' % (base[k], ''.join(v)) for k, v in sorted(by.items())))
|
|
@@ -578,12 +580,12 @@ def roll_svg(rect, notes, entry, nodes, T, span_s, theme, geo, pxu):
|
|
| 578 |
pad = 9 * pxu; ix, iy, iw, ih = x + pad, y + pad, w - 2 * pad, h - 2 * pad
|
| 579 |
now = ix + 0.22 * iw; pps = iw / max(1.0, span_s); L = T * pps
|
| 580 |
ps = [n[2] for n in notes if n[4] != 9] or [60]; lo, hi = min(ps), max(ps); lanes = hi - lo + 1; drum = 0.1 * ih
|
| 581 |
-
lane = (ih - drum - 2 * line) / lanes; by = {}
|
| 582 |
for n, e in zip(notes, entry):
|
| 583 |
bx = n[0] * pps; bw = max(min(n[1], T - n[0]) * pps, 2 * line)
|
| 584 |
if n[4] == 9: byy, bh = iy + ih - drum + 0.15 * drum, 0.7 * drum; bw = max(2 * line, 0.05 * pps)
|
| 585 |
else: bh = max(lane * 0.8, 1.5 * line); byy = iy + (hi - n[2]) * lane + (lane - bh) / 2
|
| 586 |
-
by.setdefault(nodes[e]['role'], []).append('M%d %dh%dv%dh%dz' % (round(bx), round(byy), round(bw), round(bh), -round(bw)))
|
| 587 |
strip = ''.join('<path stroke="%s" d="%s"/>' % (base[k], ''.join(v)) for k, v in sorted(by.items()))
|
| 588 |
guides = ''.join('M%d %dh%d' % (ix, round(iy + (hi - p) * lane + lane / 2), iw) for p in range(lo + (-lo) % 12, hi + 1, 12)) # one rule per octave (the Cs)
|
| 589 |
guides += 'M%d %dh%d' % (ix, round(iy + ih - drum), iw)
|
|
@@ -675,19 +677,32 @@ def sheet(sc, notes, T, o: lh.Options, b: BoxOptions, theme: str, pl: dict, prog
|
|
| 675 |
|
| 676 |
|
| 677 |
# =========================================================================== sound, and the page that plays both
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
def synth(notes, T, rate=22050) -> bytes:
|
| 679 |
-
"""The tune as sound, plainly:
|
|
|
|
| 680 |
n = int(T * rate) + rate // 2; buf = np.zeros(n, np.float32); rng = np.random.default_rng(87)
|
| 681 |
-
for
|
| 682 |
-
i0 = int(t * rate)
|
| 683 |
if ch == 9:
|
| 684 |
m = int(0.12 * rate); x = rng.uniform(-1, 1, m).astype(np.float32) * np.exp(-np.arange(m) / (0.03 * rate))
|
| 685 |
if pitch < 40: x = np.convolve(x, np.ones(24) / 24, 'same') * 2.5
|
| 686 |
seg = x * vel * 0.35
|
| 687 |
else:
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
seg = (wav * env * vel * 0.2).astype(np.float32)
|
| 692 |
j1 = min(n, i0 + len(seg)); buf[i0:j1] += seg[:j1 - i0]
|
| 693 |
peak = float(np.abs(buf).max()) or 1.0; pcm = (buf / max(1.0, peak / 0.95) * 32767).astype('<i2')
|
|
|
|
| 40 |
import ling_map as lm
|
| 41 |
import ling_globe as lg
|
| 42 |
import ling_dance as ld
|
| 43 |
+
import gm
|
| 44 |
+
from scipy import signal as _sig
|
| 45 |
import sizes
|
| 46 |
from ling_hatch import KEYS, THEMES, EMPTY
|
| 47 |
|
|
|
|
| 75 |
"""The part of the tune the sheet plays, moved to start at 0. -> (notes, length in seconds)"""
|
| 76 |
t0 = max(0.0, b.start); end = max((n[0] + n[1] for n in notes), default=1.0)
|
| 77 |
T = (end - t0 if b.seconds <= 0 else min(b.seconds, end - t0)); T = max(2.0, T)
|
| 78 |
+
return [[n[0] - t0, min(n[1], T - (n[0] - t0)), n[2], n[3], n[4]] + list(n[5:]) for n in notes if t0 <= n[0] < t0 + T], T
|
| 79 |
|
| 80 |
|
| 81 |
def arrivals(notes, entry, nodes, T, speed: float = 1.0):
|
|
|
|
| 543 |
def track_svg(tr: Track, notes, entry, nodes, T, theme, geo):
|
| 544 |
"""The band's two rules, every note on it, and the playhead that travels it. -> (still, moving, css)"""
|
| 545 |
th = THEMES[theme]; base, lit, dull, ans = role_colours(theme); line = geo['line']; x, y, w, h = tr.rect; bd = tr.band
|
| 546 |
+
ps = [n[2] for n in notes if n[4] != 9] or [60]; lo, hi = min(ps), max(ps); span = max(1, hi - lo); by = {}; chue = gm.channel_hues(notes)
|
| 547 |
for n, e in zip(notes, entry):
|
| 548 |
d0 = n[0] / T * tr.P; d1 = d0 + max(min(n[1], T - n[0]) / T * tr.P, 1.5 * line)
|
| 549 |
off = (-0.42 if n[4] == 9 else -0.3 + 0.7 * (n[2] - lo) / span) * bd
|
| 550 |
+
by.setdefault(chue.get(int(n[4]), nodes[e]['role']), []).append(tr.run(d0, d1, off)) # each channel in its own colour
|
| 551 |
rules = 'M%d %dh%dv%dh%dz' % (x, y, w, h, -w) + 'M%d %dh%dv%dh%dz' % (x + bd, y + bd, w - 2 * bd, h - 2 * bd, -(w - 2 * bd))
|
| 552 |
still = '<g id="track" stroke-width="%d"><path stroke="%s" stroke-width="%d" d="%s"/>%s</g>' % (
|
| 553 |
round(1.5 * line), th['ink'], line, rules, ''.join('<path stroke="%s" d="%s"/>' % (base[k], ''.join(v)) for k, v in sorted(by.items())))
|
|
|
|
| 580 |
pad = 9 * pxu; ix, iy, iw, ih = x + pad, y + pad, w - 2 * pad, h - 2 * pad
|
| 581 |
now = ix + 0.22 * iw; pps = iw / max(1.0, span_s); L = T * pps
|
| 582 |
ps = [n[2] for n in notes if n[4] != 9] or [60]; lo, hi = min(ps), max(ps); lanes = hi - lo + 1; drum = 0.1 * ih
|
| 583 |
+
lane = (ih - drum - 2 * line) / lanes; by = {}; chue = gm.channel_hues(notes)
|
| 584 |
for n, e in zip(notes, entry):
|
| 585 |
bx = n[0] * pps; bw = max(min(n[1], T - n[0]) * pps, 2 * line)
|
| 586 |
if n[4] == 9: byy, bh = iy + ih - drum + 0.15 * drum, 0.7 * drum; bw = max(2 * line, 0.05 * pps)
|
| 587 |
else: bh = max(lane * 0.8, 1.5 * line); byy = iy + (hi - n[2]) * lane + (lane - bh) / 2
|
| 588 |
+
by.setdefault(chue.get(int(n[4]), nodes[e]['role']), []).append('M%d %dh%dv%dh%dz' % (round(bx), round(byy), round(bw), round(bh), -round(bw)))
|
| 589 |
strip = ''.join('<path stroke="%s" d="%s"/>' % (base[k], ''.join(v)) for k, v in sorted(by.items()))
|
| 590 |
guides = ''.join('M%d %dh%d' % (ix, round(iy + (hi - p) * lane + lane / 2), iw) for p in range(lo + (-lo) % 12, hi + 1, 12)) # one rule per octave (the Cs)
|
| 591 |
guides += 'M%d %dh%d' % (ix, round(iy + ih - drum), iw)
|
|
|
|
| 677 |
|
| 678 |
|
| 679 |
# =========================================================================== sound, and the page that plays both
|
| 680 |
+
def _wave(kind, f, tt):
|
| 681 |
+
ph = (f * tt) % 1.0
|
| 682 |
+
if kind == 'sine': return np.sin(2 * math.pi * ph)
|
| 683 |
+
if kind == 'triangle': return 4 * np.abs(ph - 0.5) - 1
|
| 684 |
+
if kind == 'square': return np.where(ph < 0.5, 1.0, -1.0) * 0.6
|
| 685 |
+
return (2 * ph - 1) * 0.7 # sawtooth
|
| 686 |
+
|
| 687 |
+
|
| 688 |
def synth(notes, T, rate=22050) -> bytes:
|
| 689 |
+
"""The tune as sound, plainly: each note in the timbre of its patch's family (a plucked triangle for a piano, a slow filtered saw for strings,
|
| 690 |
+
a held square for an organ and so on), a burst of noise per drum. -> WAV bytes"""
|
| 691 |
n = int(T * rate) + rate // 2; buf = np.zeros(n, np.float32); rng = np.random.default_rng(87)
|
| 692 |
+
for note in notes:
|
| 693 |
+
t, d, pitch, vel, ch = note[:5]; i0 = int(t * rate)
|
| 694 |
if ch == 9:
|
| 695 |
m = int(0.12 * rate); x = rng.uniform(-1, 1, m).astype(np.float32) * np.exp(-np.arange(m) / (0.03 * rate))
|
| 696 |
if pitch < 40: x = np.convolve(x, np.ones(24) / 24, 'same') * 2.5
|
| 697 |
seg = x * vel * 0.35
|
| 698 |
else:
|
| 699 |
+
kind, att, tau, rel, cut = gm.timbre(gm.program_of(note))
|
| 700 |
+
m = int((d + rel + 0.05) * rate); tt = np.arange(m) / rate; f = 440 * 2 ** ((pitch - 69) / 12)
|
| 701 |
+
wav = _wave(kind, f, tt)
|
| 702 |
+
if cut:
|
| 703 |
+
al = math.exp(-2 * math.pi * min(cut, rate / 2.2) / rate); wav = _sig.lfilter([1 - al], [1, -al], wav)
|
| 704 |
+
env = np.minimum(tt / max(att, 0.002), 1) * np.where(tt < d, 1.0, np.exp(-(tt - d) / max(rel / 3, 0.01)))
|
| 705 |
+
env = env * (np.exp(-tt / tau) if tau else 1.0) * (1.0 if tau else np.exp(-tt / 6.0))
|
| 706 |
seg = (wav * env * vel * 0.2).astype(np.float32)
|
| 707 |
j1 = min(n, i0 + len(seg)); buf[i0:j1] += seg[:j1 - i0]
|
| 708 |
peak = float(np.abs(buf).max()) or 1.0; pcm = (buf / max(1.0, peak / 0.95) * 32767).astype('<i2')
|