Skip to content

Instantly share code, notes, and snippets.

@polyclick
Last active January 10, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polyclick/10e1437d052cf14018cb5aaa01529ff4 to your computer and use it in GitHub Desktop.
Save polyclick/10e1437d052cf14018cb5aaa01529ff4 to your computer and use it in GitHub Desktop.
var http = require('http')
var fs = require('fs')
var socket = require('socket.io')
var midi = require('midi')
var app = http.createServer()
var io = socket(app)
app.listen(5000)
///////////////////////////////////////////////////////////////////////////////
//// SOCKETS & CLIENTS
///////////////////////////////////////////////////////////////////////////////
let clients = []
// a new socket client has connected
io.on(`connection`, (client) => {
clients.push(client)
console.log(`-> New socket client connected, total: ${clients.length}`)
// listen for disconnects of this client
client.on(`disconnect`, () => {
clients.splice(clients.indexOf(client), 1)
console.log(`-> Socket client disconnected, total: ${clients.length}`)
})
})
///////////////////////////////////////////////////////////////////////////////
//// MIDI INPUT
///////////////////////////////////////////////////////////////////////////////
// open the first available midi input port
let midiInput = new midi.input()
midiInput.openPort(0)
midiInput.ignoreTypes(true, true, true)
// listen for midi messages, forward messages to all clients
midiInput.on('message', (deltaTime, message) => {
// wipe message [ 176, 1, 127 ]
if(message[0] === 176 && message[1] === 1)
return clients.forEach((c) => { c.emit(`control`, `wipe`) })
// reload message [ 176, 2, 127 ]
if(message[0] === 176 && message[1] === 2)
return clients.forEach((c) => { c.emit(`control`, `reload`) })
// note on message [144, ...]
if(message[0] === 144)
return clients.forEach((c) => { c.emit(`midi`, message) })
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment