-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
drumtrack.js
55 lines (45 loc) · 1.33 KB
/
drumtrack.js
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
const midi = require('midi');
// Set up a new output.
const output = new midi.output();
let outputIndex;
for(var n=0;n<output.getPortCount(); n++) {
if(output.getPortName(n) === 'virtual1' || output.getPortName(n).indexOf('ZynAddSubFX')===0) {
outputIndex = n;
}
}
output.openPort(outputIndex);
const noteStringToNoteNumberMap =
new Array(128).fill(null).map((v, ndx) =>
(['c','c#','d','d#','e','f','f#','g','g#','a','a#','b'])[ndx%12]+''+Math.floor(ndx/12)
).reduce((prev, curr, ndx) => {
prev[curr] = ndx;
return prev;
}, {});
const midimessages = [
[1, 'c3'],
[0, 'g#3'],
[4, 'e3'],
[0, 'g#3'],
[1, 'c3'],
[3, 'g#3'],
[2, 'c3'],
[2, 'e3'],
[0, 'g#3'],
[3, 'c3']
].map(v => [v[0]*0.17, [0x91, noteStringToNoteNumberMap[v[1]], 127]]);
let accumulatedDeltaTime = Date.now();
let msgindex = 0;
const playEvent = (msg) => {
if(msg) {
output.sendMessage(msg);
}
if(msgindex<midimessages.length) {
const evt = midimessages[msgindex++];
msgindex = msgindex % midimessages.length;
const deltatime = evt[0];
const nextmessage = evt[1];
accumulatedDeltaTime += deltatime * 1000;
setTimeout(() => playEvent(nextmessage), accumulatedDeltaTime - Date.now());
}
};
playEvent();