From 78ea74b06675c93f8fd4ae73ec3d9210ab076dfc Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 25 Mar 2022 12:30:17 +0100 Subject: [PATCH] update cached midi values correctly --- libs/cache.js | 32 +++++++++- libs/modep.js | 174 ++++++++++++++++++++------------------------------ 2 files changed, 102 insertions(+), 104 deletions(-) diff --git a/libs/cache.js b/libs/cache.js index d6bd177..cb33ed8 100644 --- a/libs/cache.js +++ b/libs/cache.js @@ -115,6 +115,35 @@ function setInfo(value) { setValue(constants.CACHE_INFO, value); } +function updateControl(pedalId, control) { + if (pedalId === undefined || control === undefined) { + return; + } + const pedals = getValue(constants.CACHE_PEDALS); + if (pedals === undefined) { + return; + } + let pedal; + for (let pedalIndex = 0; pedalIndex < pedals.length; pedalIndex++) { + if (pedals[pedalIndex].id !== pedalId) { + continue; + } + pedal = pedals[index]; + break; + } + if (pedal === undefined) { + return; + } + for (let controlIndex = 0; controlIndex < pedal.controls.length; controlIndex++) { + if (pedal.controls[controlIndex].id !== control.index) { + continue; + } + pedal.controls[controlIndex] = control; + setValue(constants.CACHE_PEDALS, pedals); + return; + } +} + module.exports = { fill, clear, @@ -135,5 +164,6 @@ module.exports = { setCurrentPedals, getInfo, clearInfo, - setInfo + setInfo, + updateControl } \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index b80402e..df09231 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -25,15 +25,6 @@ function getBanks() { if (banks != undefined) { return resolve(banks); } - - // FAKE DATA - // let fake = [{ "title": "The Button", "pedalboards": [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }] }]; - // for (let index = 0; index < fake.length; index++) { - // fake.id = index; - // } - // banks = util.sortById(fake); - // return resolve(fake); - util.httpGET(global.config.modep.host, global.config.modep.port, '/banks') .then(function (banks) { for (let index = 0; index < banks.length; index++) { @@ -70,22 +61,6 @@ function getPedalboards() { if (pedalboards != undefined) { return resolve(pedalboards); } - // FAKE DATA - // let fake = [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }]; - // let id = 1; - // for (let index = 0; index < fake.length; index++) { - // let pedalboard = fake[index]; - // if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) { - // pedalboard.id = 0; - // defaultPedalboard = pedalboard; - // continue; - // } - // pedalboard.id = id; - // id++; - // } - // pedalboards = util.sortById(fake); - // return resolve(fake); - util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list') .then(function (pedalboards) { let id = 1; @@ -128,16 +103,6 @@ function getCurrentPedalboard() { if (currentPedalboard != undefined && currentPedalboard.id != undefined) { return resolve(currentPedalboard); } - // FAKE DATA - // let fake = '/var/modep/pedalboards/FUZZ.pedalboard'; - // getPedalboardByBundle(fake) - // .then(function (pedalboard) { - // currentPedalboard = pedalboard; - // return resolve(pedalboard); - // }) - // .catch(reject); - - // PRODUCTION util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current') .then(getPedalboardByBundle) .then(function (currentPedalboard) { @@ -234,71 +199,73 @@ function getCurrentPedals() { async function parseCurrentPedalboard(currentPedalboard) { let pedals = []; const defaultPedalboard = await getDefaultPedalboard(); - if (defaultPedalboard.id !== currentPedalboard.id) { - let startTime = new Date(); - logger.debug('parsing current pedalboard...'); - if (currentPedalboard.uri === undefined) { - throw new Error('could not determine current pedalboard config file'); - } - // FAKE DATA - let file = path.resolve(path.dirname(__dirname) + '/dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1)); - // let file = path.resolve(currentPedalboard.uri.replace('file://', '')); - pedals = await new Promise((resolve, reject) => { - fs.readFile(file, function (err, data) { - if (err) { - return reject('could not parse current pedalboard file \'' + file + '\' >>> ' + err); - } - let json = ttl2jsonld(data.toString())['@graph']; - let id = 0; - let currentPedals = []; - for (let index = 0; index < json.length; index++) { - let tmp = json[index]; - if (!tmp['lv2:prototype']) { - continue; - } - let name = tmp['@id']; - currentPedals.push({ id: id, name: name, controls: [] }); - id++; - } - for (let index = 0; index < json.length; index++) { - let tmp = json[index]; - let name = tmp['@id']; - let value = tmp['ingen:value']; - if (value == undefined) { - continue; - } - let pedal = undefined; - for (let pedalIndex = 0; pedalIndex < currentPedals.length; pedalIndex++) { - if (!name.startsWith(currentPedals[pedalIndex].name)) { - continue; - } - pedal = currentPedals[pedalIndex]; - break; - } - if (!pedal) { - continue; - } - id = pedal.controls.length; - name = name.replace(pedal.name + '/', ''); - if (name == constants.CONTROL_BYPASS) { - value = value; - } else { - value = value['@value']; - } - let control = { id: id, name: name, value: value }; - pedal.controls.push(control); - id++; - let midi = tmp['midi:binding']; - if (!midi) { - continue; - } - control.midi = { channel: midi['midi:channel'], controller: midi['midi:controllerNumber'] } - } - logger.debug('parsing current pedalboard file \'' + file + '\' took ' + util.timeDiff(startTime) + 'ms') - resolve(currentPedals); - }); - }); + if (defaultPedalboard.id === currentPedalboard.id) { + cache.setCurrentPedals(pedals); + blinky.setStatus(pedals); + return pedals; } + + let startTime = new Date(); + logger.debug('parsing current pedalboard \'' + currentPedalboard.title + '\'...'); + if (currentPedalboard.uri === undefined) { + throw new Error('could not determine current pedalboard config file'); + } + // FAKE DATA + let file = path.resolve(path.dirname(__dirname) + '/dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1)); + // let file = path.resolve(currentPedalboard.uri.replace('file://', '')); + pedals = await new Promise((resolve, reject) => { + fs.readFile(file, (err, data) => { + if (err) { + return reject('could not parse current pedalboard file \'' + file + '\' >>> ' + err); + } + let json = ttl2jsonld(data.toString())['@graph']; + let id = 0; + let currentPedals = []; + for (let index = 0; index < json.length; index++) { + let tmp = json[index]; + if (tmp['lv2:prototype'] === undefined) { + continue; + } + let name = tmp['@id']; + currentPedals.push({ id: id, name: name, controls: [] }); + id++; + } + for (let index = 0; index < json.length; index++) { + let tmp = json[index]; + let name = tmp['@id']; + let value = tmp['ingen:value']; + if (value === undefined) { + continue; + } + let pedal = undefined; + for (let pedalIndex = 0; pedalIndex < currentPedals.length; pedalIndex++) { + if (!name.startsWith(currentPedals[pedalIndex].name)) { + continue; + } + pedal = currentPedals[pedalIndex]; + break; + } + if (pedal === undefined) { + continue; + } + id = pedal.controls.length; + name = name.replace(pedal.name + '/', ''); + if (name !== constants.CONTROL_BYPASS) { + value = value['@value']; + } + let control = { id, name, value }; + pedal.controls.push(control); + id++; + let midi = tmp['midi:binding']; + if (midi === undefined) { + continue; + } + control.midi = { channel: midi['midi:channel'], controller: midi['midi:controllerNumber'] } + } + logger.debug('parsing current pedalboard file \'' + file + '\' took ' + util.timeDiff(startTime) + 'ms') + resolve(currentPedals); + }); + }); cache.setCurrentPedals(pedals); blinky.setStatus(pedals); return pedals; @@ -313,22 +280,23 @@ function setControlByRequest(pedalId, requestParams) { let value = parseInt(requestParams.get('value')); if (value === undefined) { reject('could not handle POST - missing parameter \'value\'', 400); - } else if (Number.isNaN(value)) { + } else if (isNaN(value)) { reject('parameter \'value\' is not a number', 400); } getPedalControlById(pedalId, controlId) .then(function (control) { - resolve(setControl(pedalId, control, value)); + resolve(setControl(control, value)); }) .catch(reject); }); } -async function setControl(pedalId, control, value) { +async function setControl(control, value) { if (control === undefined || control.midi === undefined) { throw new Error('control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings'); } control.midi.value = await osc.send(control.midi.controller, control.midi.channel, value); + cache.updateControl(control); } function setPedalboardById(pedalboardId) { @@ -432,7 +400,7 @@ async function toggleBypass(pedalId) { value = 127; } try { - await setControl(pedalId, bypass, value); + await setControl(bypass, value); blinky.setPedalStatus(pedal); } catch (err) { throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err);