soundroo 0.7.0 → 0.8.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/AGENTS.md +3 -1
- package/README.md +11 -3
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/assets/reverb/hall.wav +0 -0
- package/assets/samples/SOURCES.md +233 -333
- package/assets/samples/samples.json +51856 -363
- package/bin/soundroo.js +25 -3
- package/docs/composing.md +24 -7
- package/docs/headless.md +19 -6
- package/package.json +1 -1
- package/src/arrangement.js +2 -1
- package/src/audio/convolution.js +121 -0
- package/src/audio/library.js +194 -0
- package/src/audio/midi-import.js +174 -0
- package/src/audio/midi.js +27 -4
- package/src/audio/sampler.js +208 -52
- package/src/audio/synth.js +129 -6
- package/src/catalog.js +6 -6
- package/src/cloud/payload.js +2 -1
- package/src/headless.js +37 -9
- package/src/identity.js +14 -4
- package/src/project.js +20 -7
- package/src/render.js +121 -46
- package/src/samples-install.js +103 -0
- package/src/score.js +72 -15
- package/src/timeline.js +50 -0
- package/src/version.js +1 -1
- package/assets/samples/brass/39-1-1.wav +0 -0
- package/assets/samples/brass/41-1-1.wav +0 -0
- package/assets/samples/brass/45-1-1.wav +0 -0
- package/assets/samples/brass/46-1-1.wav +0 -0
- package/assets/samples/brass/50-1-1.wav +0 -0
- package/assets/samples/brass/53-1-1.wav +0 -0
- package/assets/samples/brass/57-1-1.wav +0 -0
- package/assets/samples/brass/60-1-1.wav +0 -0
- package/assets/samples/brass/63-1-1.wav +0 -0
- package/assets/samples/brass/67-1-1.wav +0 -0
- package/assets/samples/brass/70-1-1.wav +0 -0
- package/assets/samples/brass/74-1-1.wav +0 -0
- package/assets/samples/brass/77-1-1.wav +0 -0
- package/assets/samples/brass/81-1-1.wav +0 -0
- package/assets/samples/closed-hat/42-1-1.wav +0 -0
- package/assets/samples/closed-hat/42-1-2.wav +0 -0
- package/assets/samples/closed-hat/42-2-1.wav +0 -0
- package/assets/samples/closed-hat/42-2-2.wav +0 -0
- package/assets/samples/closed-hat/42-3-1.wav +0 -0
- package/assets/samples/closed-hat/42-3-2.wav +0 -0
- package/assets/samples/crash/49-1-1.wav +0 -0
- package/assets/samples/crash/49-2-1.wav +0 -0
- package/assets/samples/electric-bass/24-1-1.wav +0 -0
- package/assets/samples/electric-bass/24-2-1.wav +0 -0
- package/assets/samples/electric-bass/26-1-1.wav +0 -0
- package/assets/samples/electric-bass/26-2-1.wav +0 -0
- package/assets/samples/electric-bass/29-1-1.wav +0 -0
- package/assets/samples/electric-bass/29-2-1.wav +0 -0
- package/assets/samples/electric-bass/32-1-1.wav +0 -0
- package/assets/samples/electric-bass/32-2-1.wav +0 -0
- package/assets/samples/electric-bass/35-1-1.wav +0 -0
- package/assets/samples/electric-bass/35-2-1.wav +0 -0
- package/assets/samples/electric-bass/38-1-1.wav +0 -0
- package/assets/samples/electric-bass/38-2-1.wav +0 -0
- package/assets/samples/electric-bass/41-1-1.wav +0 -0
- package/assets/samples/electric-bass/41-2-1.wav +0 -0
- package/assets/samples/electric-bass/44-1-1.wav +0 -0
- package/assets/samples/electric-bass/44-2-1.wav +0 -0
- package/assets/samples/electric-bass/47-1-1.wav +0 -0
- package/assets/samples/electric-bass/47-2-1.wav +0 -0
- package/assets/samples/electric-bass/50-1-1.wav +0 -0
- package/assets/samples/electric-bass/50-2-1.wav +0 -0
- package/assets/samples/electric-bass/53-1-1.wav +0 -0
- package/assets/samples/electric-bass/53-2-1.wav +0 -0
- package/assets/samples/electric-bass/56-1-1.wav +0 -0
- package/assets/samples/electric-bass/56-2-1.wav +0 -0
- package/assets/samples/electric-bass/59-1-1.wav +0 -0
- package/assets/samples/electric-bass/59-2-1.wav +0 -0
- package/assets/samples/electric-bass/62-1-1.wav +0 -0
- package/assets/samples/electric-bass/62-2-1.wav +0 -0
- package/assets/samples/guitar-clean/40-1-1.wav +0 -0
- package/assets/samples/guitar-clean/41-1-1.wav +0 -0
- package/assets/samples/guitar-clean/45-1-1.wav +0 -0
- package/assets/samples/guitar-clean/48-1-1.wav +0 -0
- package/assets/samples/guitar-clean/50-1-1.wav +0 -0
- package/assets/samples/guitar-clean/52-1-1.wav +0 -0
- package/assets/samples/guitar-clean/55-1-1.wav +0 -0
- package/assets/samples/guitar-clean/59-1-1.wav +0 -0
- package/assets/samples/guitar-clean/61-1-1.wav +0 -0
- package/assets/samples/guitar-clean/64-1-1.wav +0 -0
- package/assets/samples/guitar-clean/67-1-1.wav +0 -0
- package/assets/samples/guitar-clean/71-1-1.wav +0 -0
- package/assets/samples/guitar-clean/72-1-1.wav +0 -0
- package/assets/samples/guitar-clean/74-1-1.wav +0 -0
- package/assets/samples/guitar-clean/77-1-1.wav +0 -0
- package/assets/samples/guitar-clean/80-1-1.wav +0 -0
- package/assets/samples/guitar-clean/82-1-1.wav +0 -0
- package/assets/samples/guitar-clean/85-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/40-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/41-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/45-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/48-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/50-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/52-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/55-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/59-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/61-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/64-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/67-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/71-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/72-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/74-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/77-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/80-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/82-1-1.wav +0 -0
- package/assets/samples/guitar-crunch/85-1-1.wav +0 -0
- package/assets/samples/open-hat/46-1-1.wav +0 -0
- package/assets/samples/open-hat/46-2-1.wav +0 -0
- package/assets/samples/piano/102-1-1.wav +0 -0
- package/assets/samples/piano/102-2-1.wav +0 -0
- package/assets/samples/piano/105-1-1.wav +0 -0
- package/assets/samples/piano/105-2-1.wav +0 -0
- package/assets/samples/piano/108-1-1.wav +0 -0
- package/assets/samples/piano/108-2-1.wav +0 -0
- package/assets/samples/piano/21-1-1.wav +0 -0
- package/assets/samples/piano/21-2-1.wav +0 -0
- package/assets/samples/piano/24-1-1.wav +0 -0
- package/assets/samples/piano/24-2-1.wav +0 -0
- package/assets/samples/piano/27-1-1.wav +0 -0
- package/assets/samples/piano/27-2-1.wav +0 -0
- package/assets/samples/piano/30-1-1.wav +0 -0
- package/assets/samples/piano/30-2-1.wav +0 -0
- package/assets/samples/piano/33-1-1.wav +0 -0
- package/assets/samples/piano/33-2-1.wav +0 -0
- package/assets/samples/piano/36-1-1.wav +0 -0
- package/assets/samples/piano/36-2-1.wav +0 -0
- package/assets/samples/piano/39-1-1.wav +0 -0
- package/assets/samples/piano/39-2-1.wav +0 -0
- package/assets/samples/piano/42-1-1.wav +0 -0
- package/assets/samples/piano/42-2-1.wav +0 -0
- package/assets/samples/piano/45-1-1.wav +0 -0
- package/assets/samples/piano/45-2-1.wav +0 -0
- package/assets/samples/piano/48-1-1.wav +0 -0
- package/assets/samples/piano/48-2-1.wav +0 -0
- package/assets/samples/piano/51-1-1.wav +0 -0
- package/assets/samples/piano/51-2-1.wav +0 -0
- package/assets/samples/piano/54-1-1.wav +0 -0
- package/assets/samples/piano/54-2-1.wav +0 -0
- package/assets/samples/piano/57-1-1.wav +0 -0
- package/assets/samples/piano/57-2-1.wav +0 -0
- package/assets/samples/piano/60-1-1.wav +0 -0
- package/assets/samples/piano/60-2-1.wav +0 -0
- package/assets/samples/piano/63-1-1.wav +0 -0
- package/assets/samples/piano/63-2-1.wav +0 -0
- package/assets/samples/piano/66-1-1.wav +0 -0
- package/assets/samples/piano/66-2-1.wav +0 -0
- package/assets/samples/piano/69-1-1.wav +0 -0
- package/assets/samples/piano/69-2-1.wav +0 -0
- package/assets/samples/piano/72-1-1.wav +0 -0
- package/assets/samples/piano/72-2-1.wav +0 -0
- package/assets/samples/piano/75-1-1.wav +0 -0
- package/assets/samples/piano/75-2-1.wav +0 -0
- package/assets/samples/piano/78-1-1.wav +0 -0
- package/assets/samples/piano/78-2-1.wav +0 -0
- package/assets/samples/piano/81-1-1.wav +0 -0
- package/assets/samples/piano/81-2-1.wav +0 -0
- package/assets/samples/piano/84-1-1.wav +0 -0
- package/assets/samples/piano/84-2-1.wav +0 -0
- package/assets/samples/piano/87-1-1.wav +0 -0
- package/assets/samples/piano/87-2-1.wav +0 -0
- package/assets/samples/piano/90-1-1.wav +0 -0
- package/assets/samples/piano/90-2-1.wav +0 -0
- package/assets/samples/piano/93-1-1.wav +0 -0
- package/assets/samples/piano/93-2-1.wav +0 -0
- package/assets/samples/piano/96-1-1.wav +0 -0
- package/assets/samples/piano/96-2-1.wav +0 -0
- package/assets/samples/piano/99-1-1.wav +0 -0
- package/assets/samples/piano/99-2-1.wav +0 -0
- package/assets/samples/ride/51-1-1.wav +0 -0
- package/assets/samples/ride/51-2-1.wav +0 -0
- package/assets/samples/rock-kick/36-1-1.wav +0 -0
- package/assets/samples/rock-kick/36-1-2.wav +0 -0
- package/assets/samples/rock-kick/36-2-1.wav +0 -0
- package/assets/samples/rock-kick/36-2-2.wav +0 -0
- package/assets/samples/rock-kick/36-3-1.wav +0 -0
- package/assets/samples/rock-kick/36-3-2.wav +0 -0
- package/assets/samples/rock-kick/36-4-1.wav +0 -0
- package/assets/samples/rock-kick/36-4-2.wav +0 -0
- package/assets/samples/rock-snare/38-1-1.wav +0 -0
- package/assets/samples/rock-snare/38-1-2.wav +0 -0
- package/assets/samples/rock-snare/38-2-1.wav +0 -0
- package/assets/samples/rock-snare/38-2-2.wav +0 -0
- package/assets/samples/rock-snare/38-3-1.wav +0 -0
- package/assets/samples/rock-snare/38-3-2.wav +0 -0
- package/assets/samples/rock-snare/38-4-1.wav +0 -0
- package/assets/samples/rock-snare/38-4-2.wav +0 -0
- package/assets/samples/strings/36-1-1.wav +0 -0
- package/assets/samples/strings/40-1-1.wav +0 -0
- package/assets/samples/strings/43-1-1.wav +0 -0
- package/assets/samples/strings/47-1-1.wav +0 -0
- package/assets/samples/strings/50-1-1.wav +0 -0
- package/assets/samples/strings/53-1-1.wav +0 -0
- package/assets/samples/strings/55-1-1.wav +0 -0
- package/assets/samples/strings/59-1-1.wav +0 -0
- package/assets/samples/strings/62-1-1.wav +0 -0
- package/assets/samples/strings/65-1-1.wav +0 -0
- package/assets/samples/strings/69-1-1.wav +0 -0
- package/assets/samples/strings/72-1-1.wav +0 -0
- package/assets/samples/strings/76-1-1.wav +0 -0
- package/assets/samples/strings/79-1-1.wav +0 -0
- package/assets/samples/strings/83-1-1.wav +0 -0
- package/assets/samples/strings/86-1-1.wav +0 -0
- package/assets/samples/tom/41-1-1.wav +0 -0
- package/assets/samples/tom/41-2-1.wav +0 -0
- package/assets/samples/tom/45-1-1.wav +0 -0
- package/assets/samples/tom/45-2-1.wav +0 -0
- package/assets/samples/tom/48-1-1.wav +0 -0
- package/assets/samples/tom/48-2-1.wav +0 -0
- package/assets/samples/tom/50-1-1.wav +0 -0
- package/assets/samples/tom/50-2-1.wav +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Standard MIDI files into Soundroo scores (version 3). Notes keep their exact
|
|
2
|
+
// timing, velocity and length; tempo changes, the sustain pedal (controller
|
|
3
|
+
// 64) and markers come along; General MIDI programs pick the closest Soundroo
|
|
4
|
+
// instrument and channel 10 is split into the pieces of the drum kit.
|
|
5
|
+
|
|
6
|
+
function readVariable(bytes, at) {
|
|
7
|
+
let value = 0, length = 0;
|
|
8
|
+
for (;;) {
|
|
9
|
+
const byte = bytes[at + length++];
|
|
10
|
+
if (byte === undefined) throw new Error('The MIDI file ends in the middle of an event.');
|
|
11
|
+
value = (value << 7) | (byte & 127);
|
|
12
|
+
if (!(byte & 128)) return [value, length];
|
|
13
|
+
if (length > 4) throw new Error('The MIDI file has an invalid length field.');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Parses SMF bytes: {division, tracks: [[{tick, type, ...}]]}. */
|
|
18
|
+
export function parseMidi(buffer) {
|
|
19
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
20
|
+
const text = (at, length) => String.fromCharCode(...bytes.subarray(at, at + length));
|
|
21
|
+
const u32 = at => ((bytes[at] << 24) >>> 0) + (bytes[at + 1] << 16) + (bytes[at + 2] << 8) + bytes[at + 3];
|
|
22
|
+
const u16 = at => (bytes[at] << 8) | bytes[at + 1];
|
|
23
|
+
if (bytes.length < 14 || text(0, 4) !== 'MThd') throw new Error('This is not a standard MIDI file (no MThd header).');
|
|
24
|
+
const headerLength = u32(4), format = u16(8), count = u16(10), division = u16(12);
|
|
25
|
+
if (format > 1) throw new Error('Only MIDI format 0 and 1 files are supported.');
|
|
26
|
+
if (division & 0x8000) throw new Error('MIDI files timed in SMPTE frames are not supported; export with ticks per quarter note.');
|
|
27
|
+
const tracks = [];
|
|
28
|
+
let at = 8 + headerLength;
|
|
29
|
+
for (let t = 0; t < count && at + 8 <= bytes.length; t++) {
|
|
30
|
+
if (text(at, 4) !== 'MTrk') throw new Error('The MIDI file has a damaged track.');
|
|
31
|
+
const length = u32(at + 4), end = Math.min(bytes.length, at + 8 + length);
|
|
32
|
+
let p = at + 8, tick = 0, status = 0;
|
|
33
|
+
const events = [];
|
|
34
|
+
while (p < end) {
|
|
35
|
+
const [delta, used] = readVariable(bytes, p);
|
|
36
|
+
p += used; tick += delta;
|
|
37
|
+
let byte = bytes[p];
|
|
38
|
+
if (byte === 0xff) {
|
|
39
|
+
const type = bytes[p + 1];
|
|
40
|
+
const [size, sizeLength] = readVariable(bytes, p + 2);
|
|
41
|
+
const data = bytes.subarray(p + 2 + sizeLength, p + 2 + sizeLength + size);
|
|
42
|
+
p += 2 + sizeLength + size;
|
|
43
|
+
if (type === 0x51 && size === 3) events.push({ tick, type: 'tempo', bpm: 60000000 / ((data[0] << 16) | (data[1] << 8) | data[2]) });
|
|
44
|
+
else if (type === 0x03) events.push({ tick, type: 'name', text: new TextDecoder().decode(data) });
|
|
45
|
+
else if (type === 0x06) events.push({ tick, type: 'marker', text: new TextDecoder().decode(data) });
|
|
46
|
+
else if (type === 0x2f) break;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (byte === 0xf0 || byte === 0xf7) {
|
|
50
|
+
const [size, sizeLength] = readVariable(bytes, p + 1);
|
|
51
|
+
p += 1 + sizeLength + size;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (byte & 0x80) { status = byte; p++; } else if (!status) throw new Error('The MIDI file uses running status without a status byte.');
|
|
55
|
+
const kind = status & 0xf0, channel = status & 0x0f;
|
|
56
|
+
const one = bytes[p], two = bytes[p + 1];
|
|
57
|
+
if (kind === 0xc0 || kind === 0xd0) { p += 1; if (kind === 0xc0) events.push({ tick, type: 'program', channel, program: one }); continue; }
|
|
58
|
+
p += 2;
|
|
59
|
+
if (kind === 0x90 && two > 0) events.push({ tick, type: 'on', channel, pitch: one, velocity: two });
|
|
60
|
+
else if (kind === 0x80 || kind === 0x90) events.push({ tick, type: 'off', channel, pitch: one });
|
|
61
|
+
else if (kind === 0xb0) events.push({ tick, type: 'control', channel, control: one, value: two });
|
|
62
|
+
}
|
|
63
|
+
tracks.push(events);
|
|
64
|
+
at = at + 8 + length;
|
|
65
|
+
}
|
|
66
|
+
return { division, tracks };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// General MIDI program families to Soundroo instruments.
|
|
70
|
+
function instrumentFor(program) {
|
|
71
|
+
if (program <= 3 || program === 6 || program === 7) return 'piano';
|
|
72
|
+
if (program <= 5) return 'electric-piano';
|
|
73
|
+
if (program === 12 || program === 13) return 'marimba';
|
|
74
|
+
if (program <= 15) return 'bell';
|
|
75
|
+
if (program <= 23) return 'organ';
|
|
76
|
+
if (program <= 28) return 'guitar-clean';
|
|
77
|
+
if (program <= 31) return 'guitar-crunch';
|
|
78
|
+
if (program <= 37) return 'electric-bass';
|
|
79
|
+
if (program <= 39) return 'bass';
|
|
80
|
+
if (program <= 51) return program === 47 ? 'tom' : 'strings';
|
|
81
|
+
if (program <= 54) return 'pad';
|
|
82
|
+
if (program <= 63) return 'brass';
|
|
83
|
+
if (program <= 79) return 'fm-lead';
|
|
84
|
+
if (program <= 87) return program === 80 ? 'chip-lead' : 'analog-lead';
|
|
85
|
+
if (program <= 103) return 'pad';
|
|
86
|
+
if (program <= 111) return 'pluck';
|
|
87
|
+
if (program <= 119) return 'marimba';
|
|
88
|
+
return 'noise';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// General MIDI drum notes (channel 10) to the pieces of the kit.
|
|
92
|
+
function drumFor(pitch) {
|
|
93
|
+
if (pitch === 35 || pitch === 36) return 'rock-kick';
|
|
94
|
+
if (pitch === 37 || pitch === 38 || pitch === 40) return 'rock-snare';
|
|
95
|
+
if (pitch === 39) return 'clap';
|
|
96
|
+
if (pitch === 42 || pitch === 44) return 'closed-hat';
|
|
97
|
+
if (pitch === 46) return 'open-hat';
|
|
98
|
+
if ([41, 43, 45, 47, 48, 50].includes(pitch)) return 'tom';
|
|
99
|
+
if ([49, 52, 55, 57].includes(pitch)) return 'crash';
|
|
100
|
+
if ([51, 53, 59].includes(pitch)) return 'ride';
|
|
101
|
+
if ([54, 69, 70, 82].includes(pitch)) return 'shaker';
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const COLORS = ['#e0472b', '#3e7cb1', '#f2a541', '#5b8c5a', '#9b5de5', '#00a6a6', '#f15bb5', '#8d6346'];
|
|
106
|
+
const slug = value => String(value).toLowerCase().normalize('NFKD').replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'imported';
|
|
107
|
+
const round = (value, places = 6) => Math.round(value * 10 ** places) / 10 ** places;
|
|
108
|
+
|
|
109
|
+
/** A version 3 score from MIDI bytes. Options: id, title. Returns {score,
|
|
110
|
+
* skipped}, where skipped counts drum notes with no kit piece. */
|
|
111
|
+
export function midiToScore(buffer, { id, title } = {}) {
|
|
112
|
+
const { division, tracks } = parseMidi(buffer);
|
|
113
|
+
const beat = tick => round(tick / division);
|
|
114
|
+
const all = tracks.flatMap((events, track) => events.map(event => ({ ...event, track })));
|
|
115
|
+
const tempos = all.filter(e => e.type === 'tempo').sort((a, b) => a.tick - b.tick);
|
|
116
|
+
// MIDI plays at 120 BPM until a tempo event says otherwise.
|
|
117
|
+
const bpm = round(Math.min(400, Math.max(20, tempos.find(e => e.tick === 0)?.bpm ?? 120)), 3);
|
|
118
|
+
const tempo = [];
|
|
119
|
+
for (const event of tempos) {
|
|
120
|
+
const at = beat(event.tick), value = round(Math.min(400, Math.max(20, event.bpm)), 3);
|
|
121
|
+
if (at <= 0) continue;
|
|
122
|
+
const last = tempo[tempo.length - 1]?.bpm ?? bpm;
|
|
123
|
+
if (Math.abs(value - last) < 0.01) continue;
|
|
124
|
+
if (tempo.length && tempo[tempo.length - 1].beat === at) tempo[tempo.length - 1].bpm = value;
|
|
125
|
+
else tempo.push({ beat: at, bpm: value });
|
|
126
|
+
}
|
|
127
|
+
const names = new Map();
|
|
128
|
+
for (const event of all) if (event.type === 'name' && !names.has(event.track)) names.set(event.track, event.text.trim());
|
|
129
|
+
// Notes per (track, channel) for melodic parts and per kit piece for drums.
|
|
130
|
+
const groups = new Map(), open = new Map(), programs = new Map(), pedals = new Map();
|
|
131
|
+
let skipped = 0, lastTick = 0;
|
|
132
|
+
const events = all.sort((a, b) => a.tick - b.tick || (a.type === 'off' ? -1 : 0) - (b.type === 'off' ? -1 : 0));
|
|
133
|
+
for (const event of events) {
|
|
134
|
+
if (event.type === 'program') programs.set(`${event.track}:${event.channel}`, event.program);
|
|
135
|
+
// The sustain pedal belongs to the channel: a piano's two staves often sit in separate tracks on one channel.
|
|
136
|
+
if (event.type === 'control' && event.control === 64) {
|
|
137
|
+
const key = event.channel, list = pedals.get(key) ?? [];
|
|
138
|
+
const down = event.value >= 64, last = list[list.length - 1];
|
|
139
|
+
if (down && (!last || last.end !== undefined)) list.push({ start: event.tick });
|
|
140
|
+
else if (!down && last && last.end === undefined) last.end = event.tick;
|
|
141
|
+
pedals.set(key, list);
|
|
142
|
+
}
|
|
143
|
+
if (event.type === 'on') open.set(`${event.track}:${event.channel}:${event.pitch}`, [...(open.get(`${event.track}:${event.channel}:${event.pitch}`) ?? []), event]);
|
|
144
|
+
if (event.type === 'off') {
|
|
145
|
+
const key = `${event.track}:${event.channel}:${event.pitch}`, starts = open.get(key);
|
|
146
|
+
const start = starts?.shift();
|
|
147
|
+
if (!start) continue;
|
|
148
|
+
lastTick = Math.max(lastTick, event.tick);
|
|
149
|
+
const drum = event.channel === 9;
|
|
150
|
+
const instrument = drum ? drumFor(event.pitch) : instrumentFor(programs.get(`${event.track}:${event.channel}`) ?? 0);
|
|
151
|
+
if (!instrument) { skipped++; continue; }
|
|
152
|
+
const group = drum ? `drums:${instrument}` : `${event.track}:${event.channel}`;
|
|
153
|
+
if (!groups.has(group)) groups.set(group, { instrument, track: event.track, channel: event.channel, drum, notes: [] });
|
|
154
|
+
groups.get(group).notes.push({ pitch: event.pitch, beat: beat(start.tick), duration: Math.max(1 / 64, beat(event.tick - start.tick)), velocity: round(Math.max(1, start.velocity) / 127, 4) });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (!groups.size) throw new Error('The MIDI file has no notes Soundroo can play.');
|
|
158
|
+
const end = Math.max(beat(lastTick), ...[...pedals.values()].flat().map(span => beat(span.end ?? lastTick)));
|
|
159
|
+
// Room after the last note for releases and ringing strings.
|
|
160
|
+
const beats = Math.ceil((end + 2) / 4) * 4;
|
|
161
|
+
const used = new Set();
|
|
162
|
+
const parts = [...groups.values()].map((group, index) => {
|
|
163
|
+
const base = slug(group.drum ? group.instrument : names.get(group.track) || group.instrument);
|
|
164
|
+
let partId = base, n = 2;
|
|
165
|
+
while (used.has(partId)) partId = `${base}-${n++}`;
|
|
166
|
+
used.add(partId);
|
|
167
|
+
const pedal = group.drum ? [] : (pedals.get(group.channel) ?? []).map(span => ({ beat: beat(span.start), beats: round(beat(span.end ?? lastTick) - beat(span.start)) })).filter(span => span.beats > 0);
|
|
168
|
+
return { id: partId, name: group.drum ? group.instrument : names.get(group.track) || group.instrument, instrument: group.instrument, volume: group.drum ? 0.8 : 0.9, color: COLORS[index % COLORS.length], notes: group.notes.sort((a, b) => a.beat - b.beat || a.pitch - b.pitch), ...(pedal.length ? { pedal } : {}) };
|
|
169
|
+
});
|
|
170
|
+
const markers = all.filter(e => e.type === 'marker').sort((a, b) => a.tick - b.tick);
|
|
171
|
+
const sections = markers.map((marker, i) => ({ id: `${slug(marker.text) || 'section'}-${i + 1}`, name: marker.text.trim() || `Section ${i + 1}`, beat: beat(marker.tick), beats: round((markers[i + 1] ? beat(markers[i + 1].tick) : beats) - beat(marker.tick)) })).filter(s => s.beats > 0 && s.beat < beats);
|
|
172
|
+
const score = { version: 3, id: slug(id ?? title ?? 'imported'), title: title ?? 'Imported MIDI', bpm, beats, ...(tempo.length ? { tempo } : {}), master: { volume: 0.8, reverb: 0.25 }, ...(sections.length ? { sections } : {}), parts };
|
|
173
|
+
return { score, skipped };
|
|
174
|
+
}
|
package/src/audio/midi.js
CHANGED
|
@@ -14,13 +14,31 @@ function metadata(type, text) {
|
|
|
14
14
|
return Buffer.concat([Buffer.from([255, type]), variable(bytes.length), bytes]);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
function tempoEvent(bpm) {
|
|
18
|
+
const bytes = Buffer.alloc(6); bytes.set([255, 81, 3]); bytes.writeUIntBE(Math.round(60000000 / bpm), 3, 3);
|
|
19
|
+
return bytes;
|
|
20
|
+
}
|
|
21
|
+
// MIDI has no tempo glide; a ramp becomes a tempo change every sixteenth note.
|
|
22
|
+
const RAMP_STEP = 0.25;
|
|
23
|
+
|
|
17
24
|
// A standard format-1 MIDI file with a conductor track and one track per part.
|
|
18
|
-
// Callers pass normalized note events in quarter-note beats
|
|
19
|
-
|
|
25
|
+
// Callers pass normalized note events in quarter-note beats, tempo changes
|
|
26
|
+
// [{beat, bpm, ramp}] after beat 0 and per track sustain-pedal spans [{beat, beats}].
|
|
27
|
+
export function encodeMidi({ title = 'Soundroo', bpm = 120, tempo = [], tracks = [], sections = [] }) {
|
|
20
28
|
if (!Number.isFinite(bpm) || bpm < 20 || bpm > 400) throw new Error('MIDI tempo must be between 20 and 400 BPM.');
|
|
21
29
|
if (tracks.length > 64) throw new Error('MIDI export supports at most 64 instrument tracks.');
|
|
22
|
-
const
|
|
23
|
-
|
|
30
|
+
const conductor = [{ tick: 0, bytes: metadata(3, title), order: 0 }, { tick: 0, bytes: tempoEvent(bpm), order: 1 }];
|
|
31
|
+
let previous = { beat: 0, bpm };
|
|
32
|
+
for (const change of tempo) {
|
|
33
|
+
if (change.ramp) {
|
|
34
|
+
for (let beat = previous.beat + RAMP_STEP; beat < change.beat - 1e-9; beat += RAMP_STEP) {
|
|
35
|
+
const value = previous.bpm + (change.bpm - previous.bpm) * (beat - previous.beat) / (change.beat - previous.beat);
|
|
36
|
+
conductor.push({ tick: Math.round(beat * DIVISION), bytes: tempoEvent(value), order: 1 });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
conductor.push({ tick: Math.round(change.beat * DIVISION), bytes: tempoEvent(change.bpm), order: 1 });
|
|
40
|
+
previous = change;
|
|
41
|
+
}
|
|
24
42
|
for (const section of sections) conductor.push({ tick: Math.round(section.beat * DIVISION), bytes: metadata(6, section.name), order: 2 });
|
|
25
43
|
const makeTrack = events => {
|
|
26
44
|
events.sort((a, b) => a.tick - b.tick || a.order - b.order);
|
|
@@ -52,6 +70,11 @@ export function encodeMidi({ title = 'Soundroo', bpm = 120, tracks = [], section
|
|
|
52
70
|
events.push({ tick: start, bytes: Buffer.from([144 | resolved, pitch, Math.max(1, Math.round(velocity * 127))]), order: 3 });
|
|
53
71
|
events.push({ tick: end, bytes: Buffer.from([128 | resolved, pitch, 0]), order: 2 });
|
|
54
72
|
}
|
|
73
|
+
// The sustain pedal is controller 64: up before down at the same tick, so touching spans lift it briefly.
|
|
74
|
+
for (const span of track.pedal ?? []) {
|
|
75
|
+
events.push({ tick: Math.round(span.beat * DIVISION), bytes: Buffer.from([176 | resolved, 64, 127]), order: 2.5 });
|
|
76
|
+
events.push({ tick: Math.round((span.beat + span.beats) * DIVISION), bytes: Buffer.from([176 | resolved, 64, 0]), order: 1.5 });
|
|
77
|
+
}
|
|
55
78
|
body.push(makeTrack(events));
|
|
56
79
|
}
|
|
57
80
|
const header = Buffer.alloc(6); header.writeUInt16BE(1, 0); header.writeUInt16BE(body.length, 2); header.writeUInt16BE(DIVISION, 4);
|
package/src/audio/sampler.js
CHANGED
|
@@ -1,62 +1,218 @@
|
|
|
1
|
-
// Recorded instruments
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// plays the nearest recorded pitch at the matching layer, resampled to its
|
|
5
|
-
// own pitch. Strings and brass loop a steady stretch while the note is held.
|
|
6
|
-
import { readFileSync } from 'node:fs';
|
|
7
|
-
import { parseWav, toMono } from './pcm.js';
|
|
1
|
+
// Recorded instruments, played from the full recordings in sample packs
|
|
2
|
+
// (library.js, assets/samples/SOURCES.md): the grand piano with its own
|
|
3
|
+
// player, and every other recorded instrument mapped as SFZ-like regions.
|
|
8
4
|
import { Voice, Env, WAVES } from './synth.js';
|
|
5
|
+
import { SampleStream } from './library.js';
|
|
9
6
|
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
const dbGain = db => Math.pow(10, db / 20);
|
|
8
|
+
const midiVelocity = velocity => Math.max(1, Math.min(127, Math.round(velocity * 127)));
|
|
9
|
+
// SFZ amp_veltrack: velocity scales the level along a square curve, by this share.
|
|
10
|
+
const velocityGain = (track, vel) => 1 - track + track * (vel / 127) ** 2;
|
|
11
|
+
// The time constant of an exponential fade that is 60 dB down after `seconds`.
|
|
12
|
+
const fadeTau = seconds => Math.max(0.002, seconds / 6.9);
|
|
13
13
|
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return spec;
|
|
14
|
+
function regionFor(regions, key, vel) {
|
|
15
|
+
const matches = regions.filter(r => vel >= r.lovel && vel <= r.hivel);
|
|
16
|
+
return matches.find(r => key >= r.lokey && key <= r.hikey)
|
|
17
|
+
?? matches.reduce((best, r) => (best && Math.abs(best.key - key) <= Math.abs(r.key - key) ? best : r), null);
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
20
|
+
// A voice stops once the rest of its recording, at the voice's gain, stays
|
|
21
|
+
// below this level: far under the noise floor of any render.
|
|
22
|
+
const SILENT_DB = -100;
|
|
23
|
+
|
|
24
|
+
// The frames of a recording that can still be heard at `gain`.
|
|
25
|
+
function audibleFrames(recording, gain) {
|
|
26
|
+
const below = 20 * Math.log10(Math.max(gain, 1e-12)) + recording.peak - SILENT_DB;
|
|
27
|
+
if (!recording.tails || below > 120) return recording.frames;
|
|
28
|
+
return recording.tails[Math.max(0, Math.ceil(below / 10) - 2)];
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const spec = instrument(id);
|
|
39
|
-
const wanted = pitch ?? spec.roots[0];
|
|
40
|
-
const root = spec.roots.reduce((best, candidate) => Math.abs(candidate - wanted) < Math.abs(best - wanted) ? candidate : best);
|
|
41
|
-
const layer = Math.max(0, spec.layers.findIndex(top => velocity <= top));
|
|
42
|
-
const take = spec.rr > 1 ? 1 + Math.floor(rng.next() * spec.rr) : 1;
|
|
43
|
-
const name = `${root}-${layer + 1}-${take}`;
|
|
44
|
-
const { data, rate } = recording(id, name);
|
|
45
|
-
const loop = spec.loops?.[name] ?? null;
|
|
46
|
-
const ratio = Math.pow(2, Math.max(-maxShift, Math.min(maxShift, wanted - root)) / 12);
|
|
47
|
-
const gain = level * Math.pow(velocity / spec.layers[layer], 0.6) * (doubled ? Math.SQRT1_2 : 1);
|
|
48
|
-
const length = data.length / (rate * ratio);
|
|
49
|
-
const amp = () => {
|
|
50
|
-
const envelope = new Env(0).set(0, t);
|
|
51
|
-
if (attack > 0) envelope.lin(gain, t + attack); else envelope.set(gain, t);
|
|
52
|
-
if (decay > 0) envelope.target(gain * 0.025, t + attack, decay);
|
|
53
|
-
return timed ? envelope.target(0, t + Math.max(attack, duration), release / 3) : envelope;
|
|
54
|
-
};
|
|
55
|
-
const end = timed ? t + Math.max(attack, duration) + release * 2.4 : t + length;
|
|
56
|
-
const voice = new Voice(t, loop ? end : Math.min(end, t + length));
|
|
57
|
-
// The second take of a doubled note starts 15 ms into the recording so the two don't sound like one.
|
|
58
|
-
const takes = doubled ? [[-doubled / 2, 0], [doubled / 2, Math.round(rate * 0.015)]] : [[0, 0]];
|
|
59
|
-
for (const [detune, start] of takes) voice.layer({ wave: WAVES.sample, data, step: (rate / syn.sr) * ratio, start, loop, detune, amp: amp() });
|
|
31
|
+
/** One recording as a stereo voice from time t until `end` (seconds), at
|
|
32
|
+
* `ratio` times its own speed, shaped by `amp`, whose loudest value is
|
|
33
|
+
* `gain`. Returns the voice. */
|
|
34
|
+
function playRecording(syn, lane, recording, t, ratio, amp, end, gain, { filter = null, loop = null } = {}) {
|
|
35
|
+
const voice = new Voice(t, Math.min(end, loop ? Infinity : t + audibleFrames(recording, gain) / (ratio * syn.sr)));
|
|
36
|
+
voice.stereo = true;
|
|
37
|
+
voice.layer({ wave: WAVES.stream, source: new SampleStream({ ...recording, loop }), step: ratio, start: 0, loop, amp });
|
|
60
38
|
voice.filter = filter;
|
|
61
39
|
syn.add(lane, voice);
|
|
40
|
+
return voice;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Fades a sounding voice to silence over `seconds` from time t.
|
|
44
|
+
function fadeOut(syn, voice, envelope, t, seconds) {
|
|
45
|
+
envelope.events = envelope.events.filter(event => event[1] < t);
|
|
46
|
+
envelope.target(0, t, fadeTau(seconds));
|
|
47
|
+
voice.t1 = Math.min(voice.t1, t + seconds);
|
|
48
|
+
voice.end = Math.min(voice.end, Math.round(voice.t1 * syn.sr));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The grand piano (Salamander Grand Piano V3, mapped as its SFZ program):
|
|
52
|
+
* sixteen recorded loudness layers per note, dampers that fall when both the
|
|
53
|
+
* key and the sustain pedal are up, no dampers on the highest notes, the
|
|
54
|
+
* hammer and string sounds of a released key, and pedal noises. Notes must
|
|
55
|
+
* arrive in time order; finish() adds the sounds still waiting. */
|
|
56
|
+
export class PianoPlayer {
|
|
57
|
+
constructor(syn, lane, library, { level = 1, rng }) {
|
|
58
|
+
this.syn = syn;
|
|
59
|
+
this.lane = lane;
|
|
60
|
+
this.library = library;
|
|
61
|
+
this.spec = library.instrument('piano');
|
|
62
|
+
this.level = level;
|
|
63
|
+
this.rng = rng;
|
|
64
|
+
this.sounding = new Map();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
recording(name) {
|
|
68
|
+
return this.library.recording(this.spec.pack, name);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A key struck at t (seconds) with velocity 0–1; its damper falls at damperAt. */
|
|
72
|
+
note(t, key, velocity, damperAt) {
|
|
73
|
+
const { syn, spec } = this;
|
|
74
|
+
const vel = midiVelocity(velocity);
|
|
75
|
+
const region = regionFor(spec.regions, key, vel);
|
|
76
|
+
const recording = this.recording(region.sample);
|
|
77
|
+
const ratio = Math.pow(2, (key - region.key) / 12 + region.tune / 1200) * recording.rate / syn.sr;
|
|
78
|
+
const gain = this.level * velocityGain(spec.veltrack, vel);
|
|
79
|
+
const previous = this.sounding.get(key);
|
|
80
|
+
if (previous && previous.voice.t1 > t) {
|
|
81
|
+
// Striking the key again takes over the string: the earlier sound fades, and its damper never fell.
|
|
82
|
+
fadeOut(syn, previous.voice, previous.envelope, t, spec.restrike);
|
|
83
|
+
previous.releases = [];
|
|
84
|
+
} else if (previous) this.releaseSounds(previous);
|
|
85
|
+
// The strings from F#6 up have no dampers (the SFZ gives them a long release): they ring out naturally.
|
|
86
|
+
const undamped = region.release >= 3;
|
|
87
|
+
const envelope = new Env(0).set(gain, t);
|
|
88
|
+
if (!undamped) envelope.target(0, damperAt, fadeTau(region.release));
|
|
89
|
+
const voice = playRecording(syn, this.lane, recording, t, ratio, envelope, undamped ? Infinity : damperAt + region.release, gain);
|
|
90
|
+
const held = Math.max(0, damperAt - t);
|
|
91
|
+
const releases = spec.releaseRegions.filter(r => key >= r.lokey && key <= r.hikey && vel >= r.lovel && vel <= r.hivel)
|
|
92
|
+
.map(r => ({ region: r, at: damperAt, gain: this.level * dbGain(r.gain - r.rtDecay * held) * velocityGain(r.veltrack, vel) }));
|
|
93
|
+
this.sounding.set(key, { voice, envelope, releases, key });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// The hammer and string sounds of a key whose damper has fallen.
|
|
97
|
+
releaseSounds(entry) {
|
|
98
|
+
for (const { region, at, gain } of entry.releases) {
|
|
99
|
+
if (gain < 1e-5) continue;
|
|
100
|
+
const recording = this.recording(region.sample);
|
|
101
|
+
const ratio = Math.pow(2, (entry.key - region.key) / 12) * recording.rate / this.syn.sr;
|
|
102
|
+
playRecording(this.syn, this.lane, recording, at, ratio, new Env(0).set(gain, at), Infinity, gain);
|
|
103
|
+
}
|
|
104
|
+
entry.releases = [];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The pedal going down (or up) at t. */
|
|
108
|
+
pedal(t, down) {
|
|
109
|
+
const names = down ? this.spec.pedal.down : this.spec.pedal.up;
|
|
110
|
+
const name = names[Math.floor(this.rng.next() * names.length) % names.length];
|
|
111
|
+
const gain = this.level * dbGain(down ? this.spec.pedal.downGain : this.spec.pedal.upGain);
|
|
112
|
+
const recording = this.recording(name);
|
|
113
|
+
playRecording(this.syn, this.lane, recording, t, recording.rate / this.syn.sr, new Env(0).set(gain, t), Infinity, gain);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
finish() {
|
|
117
|
+
for (const entry of this.sounding.values()) this.releaseSounds(entry);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Instruments mapped as SFZ-like regions (assets/samples/samples.json,
|
|
122
|
+
* model "regions"): drums, bass, guitars, strings and brass. A note plays the
|
|
123
|
+
* region for its key and velocity, choosing among alternative takes at
|
|
124
|
+
* random without repeating the last one; release regions sound when it ends.
|
|
125
|
+
* Notes must arrive in time order. */
|
|
126
|
+
export class RegionPlayer {
|
|
127
|
+
constructor(syn, lane, library, instrument, { level = 1, rng }) {
|
|
128
|
+
this.syn = syn;
|
|
129
|
+
this.lane = lane;
|
|
130
|
+
this.library = library;
|
|
131
|
+
this.spec = library.instrument(instrument);
|
|
132
|
+
this.level = level;
|
|
133
|
+
this.rng = rng;
|
|
134
|
+
this.last = new Map();
|
|
135
|
+
// Sounding voices per key (all keys share one list for unpitched drums): [{voice, envelope}].
|
|
136
|
+
this.voices = new Map();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// At most spec.polyphony voices sound per key: a new note fades the oldest over 50 ms.
|
|
140
|
+
limit(key, t) {
|
|
141
|
+
const max = this.spec.polyphony;
|
|
142
|
+
if (!max) return;
|
|
143
|
+
const list = (this.voices.get(key) ?? []).filter(entry => entry.voice.t1 > t);
|
|
144
|
+
while (list.length >= max) {
|
|
145
|
+
const oldest = list.shift();
|
|
146
|
+
fadeOut(this.syn, oldest.voice, oldest.envelope, t, 0.05);
|
|
147
|
+
}
|
|
148
|
+
this.voices.set(key, list);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
pick(regions, key, vel, avoid) {
|
|
152
|
+
const usable = regions.filter(r => vel >= r.lovel && vel <= r.hivel);
|
|
153
|
+
let matches = usable.filter(r => key >= r.lokey && key <= r.hikey);
|
|
154
|
+
if (!matches.length && usable.length) {
|
|
155
|
+
const nearest = usable.reduce((best, r) => Math.abs(r.key - key) < Math.abs(best.key - key) ? r : best);
|
|
156
|
+
matches = usable.filter(r => r.key === nearest.key);
|
|
157
|
+
}
|
|
158
|
+
const fresh = matches.length > 1 ? matches.filter(r => r.sample !== avoid) : matches;
|
|
159
|
+
return fresh[Math.floor(this.rng.next() * fresh.length) % fresh.length] ?? null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
gainFor(region, vel) {
|
|
163
|
+
// SFZ amp_velcurve_<top>=1: the level rises in a straight line to full at the layer's top velocity.
|
|
164
|
+
const top = region.velTop ?? (this.spec.layerVelocity === 'linear' ? region.hivel : null);
|
|
165
|
+
const curve = top ? Math.min(1, vel / top) : velocityGain(this.spec.veltrack ?? 1, vel);
|
|
166
|
+
return this.level * dbGain(region.gain ?? 0) * curve;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A note from t to offAt (seconds). Options: attack, release (seconds after
|
|
170
|
+
* offAt), decay (a held note fades toward silence with this time constant),
|
|
171
|
+
* doubled (cents between two takes for a section sound), filter, tiltDb. */
|
|
172
|
+
note(t, key, velocity, offAt, { attack = 0, release = this.spec.release ?? 0.3, decay = 0, doubled = 0, filter = null, tiltDb = 0 } = {}) {
|
|
173
|
+
const { syn, spec } = this;
|
|
174
|
+
const vel = midiVelocity(velocity);
|
|
175
|
+
const region = this.pick(spec.regions, key, vel, this.last.get(key));
|
|
176
|
+
if (!region) return;
|
|
177
|
+
this.last.set(key, region.sample);
|
|
178
|
+
const voiceKey = spec.unpitched ? 0 : key;
|
|
179
|
+
this.limit(voiceKey, t);
|
|
180
|
+
const gain = this.gainFor(region, vel) * dbGain(tiltDb) * (doubled ? Math.SQRT1_2 : 1);
|
|
181
|
+
const takes = [[region, 0, 0]];
|
|
182
|
+
if (doubled) {
|
|
183
|
+
// A section: a second take, a few cents apart, starting a little later so the two don't sound like one.
|
|
184
|
+
const other = this.pick(spec.regions, key, vel, region.sample) ?? region;
|
|
185
|
+
takes[0][1] = -doubled / 2;
|
|
186
|
+
takes.push([other, doubled / 2, other === region ? 0.015 : 0.008]);
|
|
187
|
+
}
|
|
188
|
+
for (const [take, cents, delay] of takes) {
|
|
189
|
+
const recording = this.library.recording(spec.pack, take.sample);
|
|
190
|
+
// Each take is retuned from its own recorded pitch (alternative takes share the region's key).
|
|
191
|
+
const takeShift = spec.unpitched ? 0 : Math.max(-(spec.maxShift ?? 127), Math.min(spec.maxShift ?? 127, key - take.key));
|
|
192
|
+
const ratio = Math.pow(2, takeShift / 12 + (take.tune ?? 0) / 1200 + cents / 1200) * recording.rate / syn.sr;
|
|
193
|
+
const start = t + delay;
|
|
194
|
+
const envelope = new Env(0).set(0, start);
|
|
195
|
+
if (attack > 0) envelope.lin(gain, start + attack); else envelope.set(gain, start);
|
|
196
|
+
if (decay > 0) envelope.target(gain * 0.025, start + attack, decay);
|
|
197
|
+
let end = Infinity;
|
|
198
|
+
if (!spec.oneShot) {
|
|
199
|
+
// The release control keeps its meaning from earlier engines: a fade with a third of it as time constant.
|
|
200
|
+
const stop = Math.max(start + attack, offAt), time = Math.max(0.005, release);
|
|
201
|
+
envelope.target(0, stop, time / 3);
|
|
202
|
+
end = stop + time * 2.4;
|
|
203
|
+
}
|
|
204
|
+
const loop = take.loop ?? null;
|
|
205
|
+
const voice = playRecording(syn, this.lane, recording, start, ratio, envelope, end, gain, { filter: filter ? { ...filter } : null, loop });
|
|
206
|
+
if (spec.polyphony) this.voices.get(voiceKey).push({ voice, envelope });
|
|
207
|
+
}
|
|
208
|
+
if (!spec.oneShot && spec.releaseRegions?.length) {
|
|
209
|
+
const rel = this.pick(spec.releaseRegions, key, vel, null);
|
|
210
|
+
if (rel) {
|
|
211
|
+
const held = Math.max(0, offAt - t), level = this.gainFor(rel, vel) * dbGain(-(rel.rtDecay ?? 0) * held) * dbGain(tiltDb);
|
|
212
|
+
const recording = this.library.recording(spec.pack, rel.sample);
|
|
213
|
+
const ratio = Math.pow(2, (key - rel.key) / 12) * recording.rate / syn.sr;
|
|
214
|
+
if (level > 1e-5) playRecording(syn, this.lane, recording, offAt, ratio, new Env(0).set(level, offAt), Infinity, level);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
62
218
|
}
|