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

Priorize exact midi device name match in case of an ambiguous pattern #531

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions services/nodecg-io-midi-input/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ class MidiService extends ServiceBundle<MidiInputServiceConfig, MidiInputService
devices.push(device);
}
});

if (devices.length === 0) {
return error("No device matched the configured pattern!");
return error("No device matched the configured pattern.");
}
if (devices.length > 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.");
}
}

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

let deviceName: string | null = null;
easymidi.getInputs().forEach((device) => {
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: Should be

easymidi.getInputs().forEach((device) => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦. Fixed with #550

if (device.includes(config.device) && deviceName === null) {
deviceName = device;
}
});
}

logger.info(`Connecting to MIDI input device ${deviceName}.`);
if (deviceName !== null) {
Expand Down
23 changes: 17 additions & 6 deletions services/nodecg-io-midi-output/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ class MidiService extends ServiceBundle<MidiOutputServiceConfig, MidiOutputServi
devices.push(device);
}
});

if (devices.length === 0) {
return error("No device matched the configured pattern.");
}
if (devices.length > 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.");
}
}
Expand All @@ -48,11 +52,18 @@ class MidiService extends ServiceBundle<MidiOutputServiceConfig, MidiOutputServi
logger.info(`Checking device name "${config.device}".`);

let deviceName: string | null = null;
easymidi.getOutputs().forEach((device) => {
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) {
Expand Down