From 72edb1a159f39909cae9e405c72015c8c103b8a8 Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Mar 2022 16:30:00 +0100 Subject: [PATCH] cache bypassed pedalboards - currently wip --- dev/FUZZ.ttl | 4 ++-- libs/blinky.js | 36 ++++++++++++++++++++---------------- libs/cache.js | 40 ++++++++++++++++++++++++++++++++++++++-- libs/constants.js | 1 + libs/modep.js | 30 ++++++++++++++++++++---------- 5 files changed, 81 insertions(+), 30 deletions(-) diff --git a/dev/FUZZ.ttl b/dev/FUZZ.ttl index 17be179..ec743dd 100644 --- a/dev/FUZZ.ttl +++ b/dev/FUZZ.ttl @@ -92,8 +92,8 @@ _:b10 ingen:value 1 ; midi:binding [ - midi:channel 0 ; - midi:controllerNumber 0 ; + midi:channel 1 ; + midi:controllerNumber 2 ; a midi:Controller ; ] ; a lv2:ControlPort , diff --git a/libs/blinky.js b/libs/blinky.js index 32311ac..9cec2c0 100644 --- a/libs/blinky.js +++ b/libs/blinky.js @@ -45,24 +45,28 @@ async function setActive(active) { } async function setPedalStatus(pedal) { - if (!enabled || pedal === undefined || pedal.controls === undefined) { - return; - } - for (let index = 0; index < pedal.controls.length; index++) { - const bypass = pedal.controls[index]; - if (bypass.name !== CONTROL_BYPASS) { - continue; + try { + if (!enabled || pedal === undefined || pedal.controls === undefined) { + return; } - return httpPOST( - global.config.blinky.host, - global.config.blinky.port, - 'set', - { - blinkstick: "strip", - index: pedal.id, - color: getPedalColor(pedal.name, (bypass.value === undefined || bypass.value === 0)) + for (let index = 0; index < pedal.controls.length; index++) { + const bypass = pedal.controls[index]; + if (bypass.name !== CONTROL_BYPASS) { + continue; } - ); + await httpPOST( + global.config.blinky.host, + global.config.blinky.port, + 'set', + { + blinkstick: "strip", + index: pedal.id, + color: getPedalColor(pedal.name, (bypass.value === undefined || bypass.value === 0)) + } + ); + } + } catch (err) { + logger.error('[BLINKY] ' + err); } } diff --git a/libs/cache.js b/libs/cache.js index dcf748f..598d61f 100644 --- a/libs/cache.js +++ b/libs/cache.js @@ -61,7 +61,7 @@ function validLifetime(timestamp) { } function getValue(key) { - var value = cache.get(key); + let value = cache.get(key); if (value === undefined) { return undefined; } @@ -156,6 +156,38 @@ function setInfo(value) { setValue(constants.CACHE_INFO, value); } +function setBypassedPedal(pedalId) { + if (isPedalBypassed(pedalId)) { + return; + } + let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED); + if (bypassedPedals === undefined) { + bypassedPedals = []; + } + bypassedPedals.push(pedalId); + cache.set(constants.CACHE_PEDALS_BYPASSED, bypassedPedals); +} + +function isPedalBypassed(pedalId) { + let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED); + if (bypassedPedals === undefined) { + return; + } + return bypassedPedals.includes(pedalId); +} + +function removeBypassedPedal(pedalId) { + if (!isPedalBypassed(pedalId)) { + return; + } + let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED); + bypassedPedals.splice(bypassedPedals.indexOf(pedalId), 1); +} + +function clearBypassedPedals() { + cache.delete(constants.CACHE_PEDALS_BYPASSED); +} + module.exports = { isActive, getLifetime, @@ -178,5 +210,9 @@ module.exports = { setCurrentPedals, getInfo, clearInfo, - setInfo + setInfo, + setBypassedPedal, + isPedalBypassed, + removeBypassedPedal, + clearBypassedPedals } \ No newline at end of file diff --git a/libs/constants.js b/libs/constants.js index eeab937..cf05f0a 100644 --- a/libs/constants.js +++ b/libs/constants.js @@ -40,6 +40,7 @@ exports.CACHE_PEDALBOARDS = "pedalboards"; exports.CACHE_PEDALBOARD_DEFAULT = "pedalboard_default"; exports.CACHE_PEDALBOARD_CURRENT = "pedalboard_current"; exports.CACHE_PEDALS = "pedals"; +exports.CACHE_PEDALS_BYPASSED = "pedals_bypassed" exports.PEDALBOARD_DEFAULT = '/var/modep/pedalboards/default.pedalboard'; diff --git a/libs/modep.js b/libs/modep.js index b63d937..afec0cf 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -241,8 +241,8 @@ async function parseCurrentPedalboard(currentPedalboard) { 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://', '')); + 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) { @@ -426,16 +426,26 @@ async function toggleBypass(pedalId) { } return; } - // TODO: Cache aktualisieren mit An/Aus-Wert - let pedal = await getCurrentPedalById(pedalId); - let bypass = getBypassControlFromPedal(pedal); - if (bypass.value === undefined || bypass.value === 0) { - bypass.value = 0; - } else { - bypass.value = 127; + const pedal = await getCurrentPedalById(pedalId); + const bypass = getBypassControlFromPedal(pedal); + const bypassState = cache.isPedalBypassed(pedal.id); + let value = 0; + if (bypassState === true) { + value = 127; + } else if (bypassState === false) { + value = 0; + } else if (bypassState === undefined) { + if (bypass.value !== undefined && bypass.value !== 0) { + value = 127; + } } try { - await setControl(bypass, bypass.value); + await setControl(bypass, value); + if (value === 0) { + cache.setBypassedPedal(pedal.id); + } else { + cache.removeBypassedPedal(pedal.id); + } blinky.setPedalStatus(pedal); } catch (err) { throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err);