Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 0b9e78a

Browse files
authored
Priorize exact midi device name match in case of an ambiguous pattern (#531)
1 parent 92a7964 commit 0b9e78a

File tree

2 files changed

+36
-14
lines changed
  • services

2 files changed

+36
-14
lines changed

services/nodecg-io-midi-input/extension/index.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ class MidiService extends ServiceBundle<MidiInputServiceConfig, MidiInputService
2727
devices.push(device);
2828
}
2929
});
30+
3031
if (devices.length === 0) {
31-
return error("No device matched the configured pattern!");
32+
return error("No device matched the configured pattern.");
3233
}
33-
if (devices.length > 1) {
34-
return error("The configured pattern is ambiguous!");
34+
35+
// If we have a device with the exact same name we prioritize it and use that device.
36+
// If we have no exact match an ambiguous pattern is not allowed.
37+
if (devices.length > 1 && !devices.includes(config.device)) {
38+
return error("The configured pattern is ambiguous.");
3539
}
3640
}
3741

@@ -48,11 +52,18 @@ class MidiService extends ServiceBundle<MidiInputServiceConfig, MidiInputService
4852
logger.info(`Checking device name "${config.device}".`);
4953

5054
let deviceName: string | null = null;
51-
easymidi.getInputs().forEach((device) => {
52-
if (device.includes(config.device) && deviceName === null) {
53-
deviceName = device;
54-
}
55-
});
55+
const allDevices = easymidi.getInputs();
56+
if (allDevices.includes(config.device)) {
57+
// If we have a device with the correct name we use that device.
58+
deviceName = config.device;
59+
} else {
60+
// Otherwise we find a device which contains the pattern.
61+
easymidi.getOutputs().forEach((device) => {
62+
if (device.includes(config.device) && deviceName === null) {
63+
deviceName = device;
64+
}
65+
});
66+
}
5667

5768
logger.info(`Connecting to MIDI input device ${deviceName}.`);
5869
if (deviceName !== null) {

services/nodecg-io-midi-output/extension/index.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ class MidiService extends ServiceBundle<MidiOutputServiceConfig, MidiOutputServi
2727
devices.push(device);
2828
}
2929
});
30+
3031
if (devices.length === 0) {
3132
return error("No device matched the configured pattern.");
3233
}
33-
if (devices.length > 1) {
34+
35+
// If we have a device with the exact same name we prioritize it and use that device.
36+
// If we have no exact match an ambiguous pattern is not allowed.
37+
if (devices.length > 1 && !devices.includes(config.device)) {
3438
return error("The configured pattern is ambiguous.");
3539
}
3640
}
@@ -48,11 +52,18 @@ class MidiService extends ServiceBundle<MidiOutputServiceConfig, MidiOutputServi
4852
logger.info(`Checking device name "${config.device}".`);
4953

5054
let deviceName: string | null = null;
51-
easymidi.getOutputs().forEach((device) => {
52-
if (device.includes(config.device) && deviceName === null) {
53-
deviceName = device;
54-
}
55-
});
55+
const allDevices = easymidi.getOutputs();
56+
if (allDevices.includes(config.device)) {
57+
// If we have a device with the correct name we use that device.
58+
deviceName = config.device;
59+
} else {
60+
// Otherwise we find a device which contains the pattern.
61+
easymidi.getOutputs().forEach((device) => {
62+
if (device.includes(config.device) && deviceName === null) {
63+
deviceName = device;
64+
}
65+
});
66+
}
5667

5768
logger.info(`Connecting to MIDI output device ${deviceName}.`);
5869
if (deviceName !== null) {

0 commit comments

Comments
 (0)