|
| 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("Sample bundle for <the-service-name> 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 | +}; |
0 commit comments