Skip to content

Commit

Permalink
add mmidi command
Browse files Browse the repository at this point in the history
`mmidi` allows reordring MIDI devices. Format:

`mmidi:<i or o><device number>-<match string>;..`

Matches are done against lowercased device names with non-alphanimerics removed.

Example:

`mmidi:i0-iac1` will find a device named "IAC Driver Loopback 1" and move it to position 0.
  • Loading branch information
web-flow committed Sep 27, 2020
1 parent 7840a5e commit a957191
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions desktop/sources/scripts/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down
35 changes: 35 additions & 0 deletions desktop/sources/scripts/core/io/midi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down

0 comments on commit a957191

Please sign in to comment.