From 34bf2be97b8832037856a5fa1b0b2ad67aff001e Mon Sep 17 00:00:00 2001 From: Daniel Huber Date: Wed, 30 Mar 2022 20:38:21 +0200 Subject: [PATCH] Priorize exact midi device name match in case of an ambiguous pattern --- .../nodecg-io-midi-input/extension/index.ts | 27 +++++++++++++------ .../nodecg-io-midi-output/extension/index.ts | 23 +++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/services/nodecg-io-midi-input/extension/index.ts b/services/nodecg-io-midi-input/extension/index.ts index 14e609570..167322b95 100644 --- a/services/nodecg-io-midi-input/extension/index.ts +++ b/services/nodecg-io-midi-input/extension/index.ts @@ -27,11 +27,15 @@ class MidiService extends ServiceBundle 1) { - return error("The configured pattern is ambiguous!"); + + // If we have a device with the exact same name we prioritize it and use that device. + // If we have no exact match an ambiguous pattern is not allowed. + if (devices.length > 1 && !devices.includes(config.device)) { + return error("The configured pattern is ambiguous."); } } @@ -48,11 +52,18 @@ class MidiService extends ServiceBundle { - if (device.includes(config.device) && deviceName === null) { - deviceName = device; - } - }); + const allDevices = easymidi.getInputs(); + if (allDevices.includes(config.device)) { + // If we have a device with the correct name we use that device. + deviceName = config.device; + } else { + // Otherwise we find a device which contains the pattern. + easymidi.getOutputs().forEach((device) => { + if (device.includes(config.device) && deviceName === null) { + deviceName = device; + } + }); + } logger.info(`Connecting to MIDI input device ${deviceName}.`); if (deviceName !== null) { diff --git a/services/nodecg-io-midi-output/extension/index.ts b/services/nodecg-io-midi-output/extension/index.ts index a16d3afe0..304c24687 100644 --- a/services/nodecg-io-midi-output/extension/index.ts +++ b/services/nodecg-io-midi-output/extension/index.ts @@ -27,10 +27,14 @@ class MidiService extends ServiceBundle 1) { + + // If we have a device with the exact same name we prioritize it and use that device. + // If we have no exact match an ambiguous pattern is not allowed. + if (devices.length > 1 && !devices.includes(config.device)) { return error("The configured pattern is ambiguous."); } } @@ -48,11 +52,18 @@ class MidiService extends ServiceBundle { - if (device.includes(config.device) && deviceName === null) { - deviceName = device; - } - }); + const allDevices = easymidi.getOutputs(); + if (allDevices.includes(config.device)) { + // If we have a device with the correct name we use that device. + deviceName = config.device; + } else { + // Otherwise we find a device which contains the pattern. + easymidi.getOutputs().forEach((device) => { + if (device.includes(config.device) && deviceName === null) { + deviceName = device; + } + }); + } logger.info(`Connecting to MIDI output device ${deviceName}.`); if (deviceName !== null) {