Last active
February 22, 2026 00:57
-
-
Save genediazjr/782a105eb4befc4c13e5465da03ae132 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os from 'os'; | |
| import easymidi from 'easymidi'; | |
| import stringify from 'fast-safe-stringify'; | |
| import { WebSocketServer } from 'ws'; | |
| // import { appendFileSync } from 'fs'; | |
| const store = { started: 0, port: 8888, midiIn: 1, simulate: false, interval: 10 }; | |
| const server = new WebSocketServer({ port: store.port }); | |
| const usbmidi = easymidi.getInputs()[store.midiIn]; | |
| const netfaces = os.networkInterfaces(); | |
| if (!usbmidi) { | |
| console.error('MIDI INPUT NOT FOUND!'); | |
| process.exit(1); | |
| } | |
| console.log('MIDI INPUT FOUND:', usbmidi); | |
| store.midi = new easymidi.Input(usbmidi); | |
| store.midi.on('message', async msg => { | |
| if (msg?._type === 'start') { | |
| store.started = Date.now(); | |
| store.ws && store.ws.send(stringify({ started: store.started })); | |
| console.log('MIDI STARTED', store.started); | |
| } | |
| if (msg?._type === 'stop') { | |
| store.started = 0; | |
| store.ws && store.ws.send(stringify({ started: 0 })); | |
| console.log('MIDI STOPPED', store.started); | |
| } | |
| // const vals = Object.keys(msg).map(key => `${key}: ${msg[key]}`); | |
| // const log = `${usbmidi}: ${vals.join(', ')}`; | |
| // appendFileSync('midi-message.log', `${log}\n`); | |
| // console.log(log); | |
| }); | |
| server.on('connection', (ws, req) => { | |
| console.log('MIDI CLIENT CONNECTED:', req.socket.remoteAddress); | |
| store.ws = ws; | |
| ws.on('error', console.error); | |
| ws.on('message', msg => { | |
| console.log('MIDI CLIENT MESSAGE:', msg.length); | |
| ws.send(stringify({ started: store.started })); | |
| }); | |
| }); | |
| for (const name in netfaces) { | |
| for (const netface of netfaces[name]) { | |
| if (netface.family === 'IPv4' && !netface.internal) { | |
| console.log('MIDI SERVER IP:', netface.address); | |
| } | |
| } | |
| } | |
| console.log('MIDI SERVER PORT:', store.port); | |
| // simulate start stop infinitely | |
| if (store.simulate) { | |
| let toggle = false; | |
| setInterval(() => { | |
| toggle = !toggle; | |
| if (toggle) { | |
| store.started = Date.now(); | |
| store.ws && store.ws.send(stringify({ started: store.started })); | |
| console.log('MIDI STARTED', store.started); | |
| } else { | |
| store.started = 0; | |
| store.ws && store.ws.send(stringify({ started: 0 })); | |
| console.log('MIDI STOPPED', store.started); | |
| } | |
| }, store.interval * 6000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment