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

Commit 487549e

Browse files
authored
Merge pull request #114 from StefanSchmelz/sample/midi-io-echo-random-vel
fixed branch issues for new sample
2 parents 6a32479 + a4e21a3 commit 487549e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

samples/midi-io/extension/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { NodeCG } from "nodecg/types/server";
2+
import { requireService } from "nodecg-io-core/extension/serviceClientWrapper";
3+
import { MidiInputServiceClient } from "nodecg-io-midi-input/extension";
4+
import { MidiOutputServiceClient } from "nodecg-io-midi-output/extension";
5+
import { Input, Output } from "easymidi";
6+
7+
module.exports = function (nodecg: NodeCG) {
8+
nodecg.log.info("midi-io sample bundle started");
9+
10+
const inputService = requireService<MidiInputServiceClient>(nodecg, "midi-input");
11+
const outputService = requireService<MidiOutputServiceClient>(nodecg, "midi-output");
12+
13+
let midiInput: null | Input = null;
14+
let midiOutput: null | Output = null;
15+
16+
inputService?.onAvailable((client) => {
17+
nodecg.log.info("Midi-input client has been updated.");
18+
midiInput = client.getNativeClient();
19+
if (midiOutput != null) {
20+
setListeners(midiInput, midiOutput);
21+
}
22+
});
23+
outputService?.onAvailable((client) => {
24+
nodecg.log.info("Midi-output client has been updated.");
25+
midiOutput = client.getNativeClient();
26+
if (midiInput != null) {
27+
setListeners(midiInput, midiOutput);
28+
}
29+
});
30+
31+
inputService?.onUnavailable(() => nodecg.log.info("Midi-input client has been unset."));
32+
outputService?.onUnavailable(() => nodecg.log.info("Midi-output client has been unset."));
33+
// Copy from "samples/midi-input/extension/index.ts"
34+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
35+
function printMessage(msg: any, event: string) {
36+
let str = "";
37+
for (const prop in msg) {
38+
str += prop + " " + msg[prop].toString() + " ";
39+
}
40+
nodecg.log.info(event + " " + str);
41+
}
42+
43+
function setListeners(inp: Input, out: Output) {
44+
inp.on("cc", (msg) => {
45+
printMessage(msg, "cc");
46+
if (msg.value > 63) {
47+
msg.value = Math.round(Math.random() * 127);
48+
}
49+
out.send("cc", msg);
50+
});
51+
inp.on("noteon", (msg) => {
52+
printMessage(msg, "noteon");
53+
if (msg.velocity != 0) {
54+
msg.velocity = Math.round(Math.random() * 127);
55+
}
56+
out.send("noteon", msg);
57+
});
58+
inp.on("noteoff", (msg) => {
59+
printMessage(msg, "noteoff");
60+
out.send("noteoff", msg);
61+
});
62+
}
63+
};

samples/midi-io/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "midi-io",
3+
"version": "0.1.0",
4+
"nodecg": {
5+
"compatibleRange": "^1.1.1",
6+
"bundleDependencies": {
7+
"nodecg-io-midi-input": "0.1.0",
8+
"nodecg-io-midi-output": "0.1.0"
9+
}
10+
},
11+
"scripts": {
12+
"build": "tsc",
13+
"watch": "tsc -w",
14+
"list": "node ../midi-input/helperscripts/listDevices.js"
15+
},
16+
"license": "MIT",
17+
"dependencies": {
18+
"nodecg-io-midi-input": "0.1.0",
19+
"nodecg-io-midi-output": "0.1.0",
20+
"nodecg-io-core": "0.1.0",
21+
"@types/node": "^14.6.4",
22+
"nodecg": "^1.6.1",
23+
"typescript": "^4.0.2"
24+
}
25+
}

samples/midi-io/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig.common.json"
3+
}

0 commit comments

Comments
 (0)