-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.js
177 lines (151 loc) · 3.81 KB
/
node.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var midi = require("midi")
var Through = require('through')
module.exports = function(name, opts){
var stream = Through(write, end)
stream.close = stream.end
var input = stream.inputPort = getInput(name, opts)
var output = stream.outputPort = getOutput(name, opts)
if (!input || !output){
stream.emit('error', new Error('No midi device found with name ' + name))
}
input.on('message', function(deltaTime, data){
if (opts && opts.normalizeNotes) {
data = normalizeNotes(data)
}
stream.queue(data)
})
function write(data){
output.sendMessage(data)
this.emit('send', data)
}
function end(){
this.queue(null)
input.closePort()
output.closePort()
}
return stream
}
module.exports.openInput = function(name, opts){
var stream = Through(function () {}, end)
var input = stream.inputPort = getInput(name, opts)
if (!input){
stream.emit('error', new Error('No midi device found with name ' + name))
}
input.on('message', function(deltaTime, data){
if (opts && opts.normalizeNotes) {
data = normalizeNotes(data)
}
stream.queue(data)
})
function end(){
this.queue(null)
input.closePort()
}
return stream
}
module.exports.openOutput = function(name, opts){
var stream = Through(write, end)
var output = stream.outputPort = getOutput(name, opts)
if (!output){
stream.emit('error', new Error('No midi device found with name ' + name))
}
function write(data){
output.sendMessage(data)
this.emit('send', data)
}
function end(){
this.queue(null)
output.closePort()
}
return stream
}
module.exports.getPortNames = function(cb){
var used = {}
var names = {}
process.nextTick(function() {
try {
var input = new midi.input()
var count = input.getPortCount()
for (var i=0;i < count;i++){
var name = input.getPortName(i)
if (used[name]){
var i = used[name] += 1
names[name + '/' + i] = true
} else {
used[name] = 1
names[name] = true
}
}
used = {}
var output = new midi.output()
var count = output.getPortCount()
for (var i=0;i < count;i++){
var name = output.getPortName(i)
if (used[name]){
var i = used[name] += 1
names[name + '/' + i] = true
} else {
used[name] = 1
names[name] = true
}
}
cb&&cb(null, Object.keys(names))
} catch (ex){
cb&&cb(ex)
}
})
}
function getInput(name, opts){
if (typeof opts === 'number') opts = {index: opts}
var index = opts && opts.index || 0
var ignoreTiming = !opts || !opts.includeTiming
var port = new midi.input()
if (opts && opts.virtual){
port.openVirtualPort(name)
port.ignoreTypes(false, ignoreTiming, false)
return port
} else {
var count = port.getPortCount()
for (var i=0;i < count;i++){
if (port.getPortName(i) === name){
if (index){
index -= 1
} else {
port.openPort(i);
port.ignoreTypes(false, ignoreTiming, false)
return port;
}
}
}
}
return null;
}
function getOutput(name, opts){
if (typeof opts === 'number') opts = {index: opts}
var index = opts && opts.index || 0
var port = new midi.output();
if (opts && opts.virtual){
port.openVirtualPort(name)
return port
} else {
var count = port.getPortCount()
for (var i=0;i < count;i++){
if (port.getPortName(i) === name){
if (index){
index -= 1
} else {
port.openPort(i);
return port;
}
}
}
}
return null;
}
function normalizeNotes(data) {
if (data[0] >= 128 && data[0] < 128 + 16){
// convert note off events to 0 velocity note on events
data = [data[0]+16, data[1], 0]
}
return data
}