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

Virtual midi devices #254

Merged
merged 1 commit into from
Oct 11, 2021
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
1 change: 0 additions & 1 deletion nodecg-io-debug/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 36 additions & 23 deletions nodecg-io-midi-input/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as easymidi from "easymidi";

interface MidiInputServiceConfig {
device: string;
virtual?: boolean;
}

export type MidiInputServiceClient = easymidi.Input;
Expand All @@ -16,37 +17,49 @@ class MidiService extends ServiceBundle<MidiInputServiceConfig, MidiInputService
async validateConfig(config: MidiInputServiceConfig): Promise<Result<void>> {
const devices: Array<string> = new Array<string>();

easymidi.getInputs().forEach((device) => {
if (device.includes(config.device)) {
devices.push(device);
// Virtual devices can always be created, easymidi will find a
// free name for the matching output
if (!config.virtual) {
easymidi.getInputs().forEach((device) => {
if (device.includes(config.device)) {
devices.push(device);
}
});
if (devices.length === 0) {
return error("No device matched the configured pattern!");
}
if (devices.length > 1) {
return error("The configured pattern is ambiguous!");
}
});

if (devices.length === 0) {
return error("No device matched the configured pattern!");
}
if (devices.length > 1) {
return error("The configured pattern is ambiguous!");
}

return emptySuccess();
}

async createClient(config: MidiInputServiceConfig): Promise<Result<MidiInputServiceClient>> {
this.nodecg.log.info(`Checking device name "${config.device}".`);
let deviceName: string | null = null;
easymidi.getInputs().forEach((device) => {
if (device.includes(config.device) && deviceName === null) {
deviceName = device;
}
});

this.nodecg.log.info(`Connecting to MIDI input device ${deviceName}.`);
if (deviceName !== null) {
const client = new easymidi.Input(deviceName);
this.nodecg.log.info(`Successfully connected to MIDI input device ${deviceName}.`);
if (config.virtual) {
this.nodecg.log.info(`Creating virtual MIDI input device ${config.device}.`);
const client = new easymidi.Input(config.device, true);
this.nodecg.log.info(`Successfully created virtual MIDI input device ${config.device}.`);
return success(client);
} else {
return error("Could not connect to the configured device!");
this.nodecg.log.info(`Checking device name "${config.device}".`);

let deviceName: string | null = null;
easymidi.getInputs().forEach((device) => {
if (device.includes(config.device) && deviceName === null) {
deviceName = device;
}
});

this.nodecg.log.info(`Connecting to MIDI input device ${deviceName}.`);
if (deviceName !== null) {
const client = new easymidi.Input(deviceName);
this.nodecg.log.info(`Successfully connected to MIDI input device ${deviceName}.`);
return success(client);
} else {
return error("Could not connect to the configured device!");
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions nodecg-io-midi-input/midi-input-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"device": {
"type": "string",
"description": "The MIDI device id"
},
"virtual": {
"type": "boolean",
"description": "true to create a virtual device."
}
},
"required": ["device"]
Expand Down
60 changes: 36 additions & 24 deletions nodecg-io-midi-output/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as easymidi from "easymidi";

interface MidiOutputServiceConfig {
device: string;
virtual?: boolean;
}

export type MidiOutputServiceClient = easymidi.Output;
Expand All @@ -16,38 +17,49 @@ class MidiService extends ServiceBundle<MidiOutputServiceConfig, MidiOutputServi
async validateConfig(config: MidiOutputServiceConfig): Promise<Result<void>> {
const devices: Array<string> = new Array<string>();

easymidi.getOutputs().forEach((device) => {
if (device.includes(config.device)) {
devices.push(device);
// Virtual devices can always be created, easymidi will find a
// free name for the matching input
if (!config.virtual) {
easymidi.getOutputs().forEach((device) => {
if (device.includes(config.device)) {
devices.push(device);
}
});
if (devices.length === 0) {
return error("No device matched the configured pattern.");
}
if (devices.length > 1) {
return error("The configured pattern is ambiguous.");
}
});

if (devices.length === 0) {
return error("No device matched the configured pattern.");
}
if (devices.length > 1) {
return error("The configured pattern is ambiguous.");
}

return emptySuccess();
}

async createClient(config: MidiOutputServiceConfig): Promise<Result<MidiOutputServiceClient>> {
this.nodecg.log.info(`Checking device name "${config.device}".`);
let deviceName: string | null = null;
easymidi.getOutputs().forEach((device) => {
if (device.includes(config.device) && deviceName === null) {
deviceName = device;
}
});

this.nodecg.log.info(`Connecting to MIDI output device ${deviceName}.`);
if (deviceName !== null) {
const client = new easymidi.Output(deviceName);
this.nodecg.log.info(`Successfully connected to MIDI output device ${deviceName}.`);

if (config.virtual) {
this.nodecg.log.info(`Creating virtual MIDI output device ${config.device}.`);
const client = new easymidi.Output(config.device, true);
this.nodecg.log.info(`Successfully created virtual MIDI output device ${config.device}.`);
return success(client);
} else {
return error("Could not connect to the configured device!");
this.nodecg.log.info(`Checking device name "${config.device}".`);

let deviceName: string | null = null;
easymidi.getOutputs().forEach((device) => {
if (device.includes(config.device) && deviceName === null) {
deviceName = device;
}
});

this.nodecg.log.info(`Connecting to MIDI output device ${deviceName}.`);
if (deviceName !== null) {
const client = new easymidi.Output(deviceName);
this.nodecg.log.info(`Successfully connected to MIDI output device ${deviceName}.`);
return success(client);
} else {
return error("Could not connect to the configured device!");
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions nodecg-io-midi-output/midi-output-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"device": {
"type": "string",
"description": "The MIDI device id"
},
"virtual": {
"type": "boolean",
"description": "true to create a virtual device."
}
},
"required": ["device"]
Expand Down
Loading