Skip to content

Commit 95d3d81

Browse files
committed
This change enables the control of several identical devices which have the same port name. An internal port numbering was added, which works via the getInput and getOutput function and links the internal names with the actual ports of node-midi.
1 parent 53da92a commit 95d3d81

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ class Input extends EventEmitter {
4040
this._pendingSysex = false;
4141
this._sysex = [];
4242
this.name = name;
43+
this.inputPortNumberedNames = getInputs();
4344

4445
if (virtual) {
4546
this._input.openVirtualPort(name);
4647
} else {
4748
const numInputs = this._input.getPortCount();
4849
let found = false;
4950
for (let i = 0; i < numInputs; i++) {
50-
if (name === this._input.getPortName(i)) {
51+
if (name === this.inputPortNumberedNames[i]) {
5152
found = true;
5253
this._input.openPort(i);
5354
}
@@ -196,14 +197,15 @@ class Output {
196197
constructor(name, virtual) {
197198
this._output = new midi.output();
198199
this.name = name;
200+
this.outputPortNumberedNames = getOutputs();
199201

200202
if (virtual) {
201203
this._output.openVirtualPort(name);
202204
} else {
203205
const numOutputs = this._output.getPortCount();
204206
let found = false;
205207
for (let i = 0; i < numOutputs; i++) {
206-
if (name === this._output.getPortName(i)) {
208+
if (name === this.outputPortNumberedNames[i]) {
207209
found = true;
208210
this._output.openPort(i);
209211
}
@@ -282,7 +284,14 @@ const getInputs = () => {
282284
const input = new midi.input();
283285
const inputs = [];
284286
for (let i = 0; i < input.getPortCount(); i++) {
285-
inputs.push(input.getPortName(i));
287+
var counter = 0;
288+
const portName = input.getPortName(i);
289+
var numberedPortName = portName;
290+
while(inputs.includes(numberedPortName)) {
291+
counter++;
292+
numberedPortName = portName + counter;
293+
}
294+
inputs.push(numberedPortName);
286295
}
287296
input.closePort();
288297
return inputs;
@@ -292,7 +301,14 @@ const getOutputs = () => {
292301
const output = new midi.output();
293302
const outputs = [];
294303
for (let i = 0; i < output.getPortCount(); i++) {
295-
outputs.push(output.getPortName(i));
304+
var counter = 0;
305+
const portName = output.getPortName(i);
306+
var numberedPortName = portName;
307+
while(outputs.includes(numberedPortName)) {
308+
counter++;
309+
numberedPortName = portName + counter;
310+
}
311+
outputs.push(numberedPortName);
296312
}
297313
output.closePort();
298314
return outputs;

0 commit comments

Comments
 (0)