-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.js
140 lines (101 loc) · 1.98 KB
/
player.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var midi = require('midi'),
midiOut = new midi.output();
try {
midiOut.openPort(0);
} catch(error) {
midiOut.openVirtualPort('');
}
var start = 1208;
var BPM = 40;
var lines = require('./processed').data;
var i = start,
interval, line;
var noteDuration = 1000;
var notes = {
// C: 60,
// Cs: 61,
// D: 62,
// Ds: 63,
// E: 64,
// F: 65,
// Fs: 66,
// G: 67,
// Gs: 68,
// A: 69,
// As: 70,
// B: 71
C: 24,
Cs: 25,
D: 26,
Ds: 27,
E: 28,
F: 29,
Fs: 30,
G: 31,
Gs: 32,
A: 33,
As: 34,
B: 35
};
var scale = [
"C",
// "Cs", //
"D",
// "Ds", //
"E",
"F",
// "Fs", //
"G",
// "Gs", //
"A",
"As",
// "B" //
];
function playNote() {
line = lines[i % lines.length]; // loop forever
var octave = ((line.numberOfWords / scale.length | 0) % 10) - 2;
var noteName = scale[line.numberOfWords % scale.length];
var note = (notes[noteName] + (octave * 12));
var semi = lines[i-1].lastWord + "; " + line.firstWord;
console.log(i, semi, noteName, octave, "midi code:", note);
midiOut.sendMessage([144, note, 100]);
setTimeout(function() {
midiOut.sendMessage([128, note, 100]);
}, noteDuration + Math.random() * 1000 | 0);
i++;
}
function play() {
console.log("Play!");
playNote();
interval = setInterval(playNote, 1000/(BPM/60));
}
function stop() {
console.log("Stop!");
i = start;
clearInterval(interval);
}
// Set up a new input.
var input = new midi.input();
// Count the available input ports.
input.getPortCount();
// Get the name of a specified input port.
input.getPortName(0);
// Configure a callback.
input.on('message', function(deltaTime, message) {
// console.log('m:' + message + ' d:' + deltaTime);
switch (message[0]) {
case 250:
play();
break;
case 251:
play();
break;
case 252:
stop();
break;
default:
break;
}
});
// Open the first available input port.
input.openPort(0);