-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi-play.mjs
49 lines (41 loc) · 1.5 KB
/
midi-play.mjs
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
import { button } from './ui-elements.mjs';
import { printAscii, fetchJson } from './utils.mjs';
import { getAudioContext, loadPatternSamples, playSample } from './sequencer.mjs';
import { DRUMS_NOTE_MAPPING, JWC } from './midi.mjs';
export const SAMPLE_TPL = `./audio_samples/jam_with_chrome/{N}.ogg`;
const recording0 = await fetchJson('./midis/rhcp-californication.json');
let recording = recording0.map(([t0, note, vel]) => {
const note2 = DRUMS_NOTE_MAPPING[note];
if (note2) {
// console.log(`OK ${note2}`);
}
else {
console.log(`ERR ${note} NOT MAPPED!`);
}
return note2 ? [t0, note2, vel] : undefined;
}).filter((arr) => Boolean(arr));
window.recording0 = recording0;
window.recording = recording;
button('play', async () => {
const audioCtx = getAudioContext();
const startT = 0.2;
const lilyNames = Array.from( new Set( recording.map(([t, note, vel]) => note) ) );
// console.log('lilyNames', lilyNames);
const pattern = {
tracks:
lilyNames.map((ly) => ({
name: ly,
url: SAMPLE_TPL.replace('{N}', JWC[ly])
})),
};
// console.log('pattern', pattern);
const samples = await loadPatternSamples(pattern, audioCtx);
//console.log('samples', samples);
let firstT;
for (const [t, note, vel] of recording) {
if (!firstT) firstT = t;
const t2 = t - firstT + startT;
//console.log(note, t2);
playSample(audioCtx, samples[note], t2, vel);
}
});