diff --git a/desktop/sources/scripts/commander.js b/desktop/sources/scripts/commander.js index 71ee1dd1..d014fbcc 100644 --- a/desktop/sources/scripts/commander.js +++ b/desktop/sources/scripts/commander.js @@ -32,6 +32,11 @@ function Commander (client) { client.io.midi.selectOutput(p.x) if (p.y !== null) { client.io.midi.selectInput(p.y) } }, + mmidi: (p) => { + // remap device ids by name + // example: $midi:i1-iac1;i2-midipipe;o2-midipipe + p.parts.forEach(d => client.io.midi.setDeviceByName(d)) + }, ip: (p) => { client.io.setIp(p.str) }, cc: (p) => { client.io.cc.setOffset(p.int) }, pg: (p) => { client.io.cc.stack.push({ channel: clamp(p.ints[0], 0, 15), bank: p.ints[1], sub: p.ints[2], pgm: clamp(p.ints[3], 0, 127), type: 'pg' }); client.io.cc.run() }, diff --git a/desktop/sources/scripts/core/io/midi.js b/desktop/sources/scripts/core/io/midi.js index 9de95da4..b43c06f6 100644 --- a/desktop/sources/scripts/core/io/midi.js +++ b/desktop/sources/scripts/core/io/midi.js @@ -166,6 +166,41 @@ function Midi (client) { console.log('MIDI', `Select Input Device: ${this.inputDevice().name}`) } + this.setDeviceByName = function(paramStr) { + // find devices by name and move them to the desired slot + const parts = paramStr.match(/([io])(\d+)-(.*)/) + console.log(parts) + if (!parts || parts.length !== 4) { return } + const index = parseInt(parts[2]) + const nameStr = parts[3] + + const findDevice = function(arr, s) { + const names = arr.map(x => x.name.toLowerCase().replace(/[^a-z0-9\s]/gi, '')) + for (var i in names) { + if (names[i].search(nameStr) === 0) { return i } + } + return -1 + } + + const move = function(arr, from, to) { + arr.splice(to, 0, arr.splice(from, 1)[0]) + } + + if (parts[1] === 'i') { + console.log('before', this.inputs) + if (index >= this.inputs.length) { return } + const foundIndex = findDevice(this.inputs, nameStr) + if (foundIndex >= 0) { move(this.inputs, foundIndex, index) } + console.log(this.inputs) + } else { + console.log('before', this.outputs) + if (index >= this.outputs.length) { return } + const foundIndex = findDevice(this.outputs, nameStr) + if (foundIndex >= 0) { move(this.outputs, foundIndex, index) } + console.log('after', this.outputs) + } + } + this.outputDevice = function () { return this.outputs[this.outputIndex] }