Skip to content

Commit

Permalink
support for sending midi back over socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdadams committed Dec 10, 2023
1 parent da649c2 commit 5b5b766
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 39 deletions.
4 changes: 4 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ global.startRescanInterval = function() {

global.RESCAN_INTERVAL = null;

global.sendMIDIBack = function(midiObj) {
API.sendMIDIBack(midiObj);

}

unhandled();
//debug();
contextMenu();
Expand Down
84 changes: 46 additions & 38 deletions midi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@ 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) {
navigator.requestMIDIAccess().then(function(webmidi) {
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

Expand All @@ -47,7 +43,8 @@ function GetPorts(showNotification) {

notifications.showNotification({
title: `${global.MIDI_OUTPUTS.length} MIDI Output Ports Found.`,
body: bodyText
body: bodyText,
showNotification: true
});
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -397,39 +394,48 @@ 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;
}
}
}
}

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) {
Expand Down Expand Up @@ -529,6 +535,8 @@ function receiveMIDI(midiArg) {
}

processMIDI(midiObj);
console.log(midiObj);
global.sendMIDIBack(midiObj);
}

function processMIDI(midiObj) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 5b5b766

Please sign in to comment.