diff --git a/api.js b/api.js index 34bf6cb..fcafe84 100644 --- a/api.js +++ b/api.js @@ -152,6 +152,10 @@ class API { static sendControlStatus() { io.sockets.emit('control_status', config.get('allowControl')); } + + static sendMIDIBack(midiObj) { + io.sockets.emit('midi_back', midiObj); + } } module.exports = API; \ No newline at end of file diff --git a/contextmenu.js b/contextmenu.js index 5d10295..fd73db8 100644 --- a/contextmenu.js +++ b/contextmenu.js @@ -15,6 +15,10 @@ function buildContextMenu() { label: 'API running on port: ' + config.get('apiPort'), enabled: false }, + { + label: 'MIDI Input Ports Detected: ' + global.MIDI_INPUTS.length, + enabled: false + }, { label: 'MIDI Output Ports Detected: ' + global.MIDI_OUTPUTS.length, enabled: false diff --git a/index.js b/index.js index d69473f..b5b0a3b 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,11 @@ global.startRescanInterval = function() { global.RESCAN_INTERVAL = null; +global.sendMIDIBack = function(midiObj) { + API.sendMIDIBack(midiObj); + +} + unhandled(); //debug(); contextMenu(); diff --git a/midi.js b/midi.js index 3215cde..8c240b8 100644 --- a/midi.js +++ b/midi.js @@ -15,6 +15,7 @@ var logger = navigator.Widget({ _receive: function(msg) { console.log('virtual m function createVirtualMIDIPort() { navigator.addMidiOut('midi-relay', logger); + navigator.addMidiIn('midi-relay', logger); } function GetPorts(showNotification) { @@ -22,19 +23,14 @@ function GetPorts(showNotification) { let info = navigator.info(); global.MIDI_OUTPUTS = info.outputs; + global.MIDI_INPUTS = info.inputs; - //retain the 'opened' property when reloading the array - let temp_inputs = info.inputs; - for (let i = 0; i < temp_inputs.length; i++) { - let port = global.MIDI_INPUTS.find(({ name }) => name === temp_inputs[i].name); - if (port) { - if (port.opened) { - temp_inputs[i].opened = port.opened; - } - } + //open each midi port + for (let i = 0; i < global.MIDI_INPUTS.length; i++) { + OpenPort(global.MIDI_INPUTS[i].name); } - global.MIDI_INPUTS = temp_inputs; + console.log(global.MIDI_INPUTS); loadMIDITriggers(); //open the port used in the triggers @@ -47,7 +43,8 @@ function GetPorts(showNotification) { notifications.showNotification({ title: `${global.MIDI_OUTPUTS.length} MIDI Output Ports Found.`, - body: bodyText + body: bodyText, + showNotification: true }); } @@ -72,7 +69,7 @@ function refreshPorts(showNotification) { function sendMIDI(midiObj, callback) { if (midiObj.midiport) { - let midiPortObj = global.MIDI_OUTPUTS.find((port) => port.name == midiObj.midiport); + let midiPortObj = global.MIDI_OUTPUTS.find((port) => port.name == midiObj.midiport); //find the midi output port in the list of outputs if (midiPortObj) { if (midiObj.midicommand) { @@ -352,7 +349,7 @@ function AddToLog(midiPort, midiCommand, message) { relayObj.midicommand = midiCommand; relayObj.message = message; relayObj.datetime = Date.now(); - global.MIDIRelaysLog.push(relayObj); + //global.MIDIRelaysLog.push(relayObj); //commenting out for now, as I don't really think we need this feature anymore } function CheckLog(midiPort, midiCommand, message, time) { @@ -397,19 +394,7 @@ function loadMIDITriggers() { if (port) { if (!port.opened) { - navigator().openMidiIn(Triggers[i].midiport) - .or(function () { - //error opening - }) - .and(function () { - for (let j = 0; j < global.MIDI_INPUTS.length; j++) { - if (global.MIDI_INPUTS[j].name === this.name()) { - global.MIDI_INPUTS[j].opened = true; - break; - } - } - this.connect(receiveMIDI.bind({ 'midiport': Triggers[i].midiport })); - }); + OpenPort(Triggers[i].midiport); break; } } @@ -417,19 +402,40 @@ function loadMIDITriggers() { } function OpenPort(midiport) { - navigator().openMidiIn(midiport) - .or(function () { - //error opening - }) - .and(function () { - for (let i = 0; i < global.MIDI_INPUTS.length; i++) { - if (global.MIDI_INPUTS[i].name === this.name()) { - global.MIDI_INPUTS[i].opened = true; - break; + try { + navigator().openMidiIn(midiport) + .or(function () { + //error opening + }) + .and(function () { + for (let i = 0; i < global.MIDI_INPUTS.length; i++) { + if (global.MIDI_INPUTS[i].name === this.name()) { + global.MIDI_INPUTS[i].opened = true; + notifications.showNotification({ + title: `MIDI Port Already Opened: ${midiport}`, + body: `MIDI Port Already Opened: ${midiport}`, + showNotification: true + }); + break; + } } - } - this.connect(receiveMIDI.bind({ 'midiport': midiport })); - }); + + this.connect(receiveMIDI.bind({ 'midiport': midiport })); + + notifications.showNotification({ + title: `MIDI Port Opened: ${midiport}`, + body: `MIDI Port Opened: ${midiport}`, + showNotification: true + }); + }); + } + catch(error) { + notifications.showNotification({ + title: `Error opening MIDI port: ${midiport}`, + body: `Error opening MIDI port: ${midiport}\n\n${error}`, + showNotification: true + }); + } } function ClosePort(midiObj) { @@ -529,6 +535,8 @@ function receiveMIDI(midiArg) { } processMIDI(midiObj); + console.log(midiObj); + global.sendMIDIBack(midiObj); } function processMIDI(midiObj) { diff --git a/package.json b/package.json index 5008d3c..5c78a8c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "midi-relay", "productName": "midi-relay", - "version": "3.1.2", + "version": "3.2.0", "description": "Listens for HTTP requests with JSON payload and relays MIDI/MSC commands on local ports.", "license": "MIT", "repository": "josephdadams/midi-relay",