Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.
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
63 changes: 63 additions & 0 deletions samples/midi-io/extension/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { NodeCG } from "nodecg/types/server";
import { requireService } from "nodecg-io-core/extension/serviceClientWrapper";
import { MidiInputServiceClient } from "nodecg-io-midi-input/extension";
import { MidiOutputServiceClient } from "nodecg-io-midi-output/extension";
import { Input, Output } from "easymidi";

module.exports = function (nodecg: NodeCG) {
nodecg.log.info("midi-io sample bundle started");

const inputService = requireService<MidiInputServiceClient>(nodecg, "midi-input");
const outputService = requireService<MidiOutputServiceClient>(nodecg, "midi-output");

let midiInput: null | Input = null;
let midiOutput: null | Output = null;

inputService?.onAvailable((client) => {
nodecg.log.info("Midi-input client has been updated.");
midiInput = client.getNativeClient();
if (midiOutput != null) {
setListeners(midiInput, midiOutput);
}
});
outputService?.onAvailable((client) => {
nodecg.log.info("Midi-output client has been updated.");
midiOutput = client.getNativeClient();
if (midiInput != null) {
setListeners(midiInput, midiOutput);
}
});

inputService?.onUnavailable(() => nodecg.log.info("Midi-input client has been unset."));
outputService?.onUnavailable(() => nodecg.log.info("Midi-output client has been unset."));
// Copy from "samples/midi-input/extension/index.ts"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function printMessage(msg: any, event: string) {
let str = "";
for (const prop in msg) {
str += prop + " " + msg[prop].toString() + " ";
}
nodecg.log.info(event + " " + str);
}

function setListeners(inp: Input, out: Output) {
inp.on("cc", (msg) => {
printMessage(msg, "cc");
if (msg.value > 63) {
msg.value = Math.round(Math.random() * 127);
}
out.send("cc", msg);
});
inp.on("noteon", (msg) => {
printMessage(msg, "noteon");
if (msg.velocity != 0) {
msg.velocity = Math.round(Math.random() * 127);
}
out.send("noteon", msg);
});
inp.on("noteoff", (msg) => {
printMessage(msg, "noteoff");
out.send("noteoff", msg);
});
}
};
25 changes: 25 additions & 0 deletions samples/midi-io/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "midi-io",
"version": "0.1.0",
"nodecg": {
"compatibleRange": "^1.1.1",
"bundleDependencies": {
"nodecg-io-midi-input": "0.1.0",
"nodecg-io-midi-output": "0.1.0"
}
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"list": "node ../midi-input/helperscripts/listDevices.js"
},
"license": "MIT",
"dependencies": {
"nodecg-io-midi-input": "0.1.0",
"nodecg-io-midi-output": "0.1.0",
"nodecg-io-core": "0.1.0",
"@types/node": "^14.6.4",
"nodecg": "^1.6.1",
"typescript": "^4.0.2"
}
}
3 changes: 3 additions & 0 deletions samples/midi-io/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.common.json"
}