Skip to content

Commit

Permalink
much improved device name matching with fuzzy search
Browse files Browse the repository at this point in the history
  • Loading branch information
unthingable committed Sep 28, 2020
1 parent 76a0f32 commit e9871c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions desktop/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"electron-packager": "^14.2.1"
},
"dependencies": {
"fuse.js": "^6.4.1",
"node-osc": "^4.1.8"
},
"standard": {
Expand Down
19 changes: 12 additions & 7 deletions desktop/sources/scripts/core/io/midi.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ function Midi (client) {
console.log('MIDI', `Select Input Device: ${this.inputDevice().name}`)
}

const Fuse = require('fuse.js')

this.setDeviceByName = function(paramStr) {
// find devices by name and move them to the desired slot
const parts = paramStr.match(/([io])(\d+)-(.*)/)
Expand All @@ -175,9 +177,11 @@ function Midi (client) {
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 }
const fuse = new Fuse(arr.map(x => x.name), { includeScore: true })
const search = fuse.search(s).filter(x => x.score < 0.8)
if (search.length > 0) {
console.log('Device matched', search[0])
return search[0].refIndex
}
return -1
}
Expand All @@ -187,18 +191,19 @@ function Midi (client) {
}

if (parts[1] === 'i') {
console.log('before', this.inputs)
console.log('inputs before', this.inputs.map(x => x.name))
if (index >= this.inputs.length) { return }
const foundIndex = findDevice(this.inputs, nameStr)
if (foundIndex >= 0) { move(this.inputs, foundIndex, index) }
console.log(this.inputs)
console.log('inputs after', this.inputs.map(x => x.name))
} else {
console.log('before', this.outputs)
console.log('outputs before', this.outputs.map(x => x.name))
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)
console.log('outputs after', this.outputs.map(x => x.name))
}
client.update()
}

this.outputDevice = function () {
Expand Down

0 comments on commit e9871c9

Please sign in to comment.