-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
playback.js
34 lines (25 loc) · 839 Bytes
/
playback.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
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("ZynAddSub")===0) {
outputIndex = n;
}
}
output.openPort(outputIndex);
const midimessages = JSON.parse(require('fs').readFileSync('recording.json'));
let accumulatedDeltaTime = Date.now();
const playEvent = (msg) => {
if(msg) {
output.sendMessage(msg);
}
if(midimessages.length>0) {
const evt = midimessages.shift();
const deltatime = evt[0];
const nextmessage = evt[1];
accumulatedDeltaTime += deltatime * 1000;
setTimeout(() => playEvent(nextmessage), accumulatedDeltaTime - Date.now());
}
};
playEvent();