update cached midi values correctly

This commit is contained in:
Daniel Sommer 2022-03-25 12:30:17 +01:00
parent be71257260
commit 78ea74b066
2 changed files with 102 additions and 104 deletions

View file

@ -115,6 +115,35 @@ function setInfo(value) {
setValue(constants.CACHE_INFO, 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 = { module.exports = {
fill, fill,
clear, clear,
@ -135,5 +164,6 @@ module.exports = {
setCurrentPedals, setCurrentPedals,
getInfo, getInfo,
clearInfo, clearInfo,
setInfo setInfo,
updateControl
} }

View file

@ -25,15 +25,6 @@ function getBanks() {
if (banks != undefined) { if (banks != undefined) {
return resolve(banks); 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') util.httpGET(global.config.modep.host, global.config.modep.port, '/banks')
.then(function (banks) { .then(function (banks) {
for (let index = 0; index < banks.length; index++) { for (let index = 0; index < banks.length; index++) {
@ -70,22 +61,6 @@ function getPedalboards() {
if (pedalboards != undefined) { if (pedalboards != undefined) {
return resolve(pedalboards); 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') util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list')
.then(function (pedalboards) { .then(function (pedalboards) {
let id = 1; let id = 1;
@ -128,16 +103,6 @@ function getCurrentPedalboard() {
if (currentPedalboard != undefined && currentPedalboard.id != undefined) { if (currentPedalboard != undefined && currentPedalboard.id != undefined) {
return resolve(currentPedalboard); 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') util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current')
.then(getPedalboardByBundle) .then(getPedalboardByBundle)
.then(function (currentPedalboard) { .then(function (currentPedalboard) {
@ -234,71 +199,73 @@ function getCurrentPedals() {
async function parseCurrentPedalboard(currentPedalboard) { async function parseCurrentPedalboard(currentPedalboard) {
let pedals = []; let pedals = [];
const defaultPedalboard = await getDefaultPedalboard(); const defaultPedalboard = await getDefaultPedalboard();
if (defaultPedalboard.id !== currentPedalboard.id) { if (defaultPedalboard.id === currentPedalboard.id) {
let startTime = new Date(); cache.setCurrentPedals(pedals);
logger.debug('parsing current pedalboard...'); blinky.setStatus(pedals);
if (currentPedalboard.uri === undefined) { return pedals;
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);
});
});
} }
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); cache.setCurrentPedals(pedals);
blinky.setStatus(pedals); blinky.setStatus(pedals);
return pedals; return pedals;
@ -313,22 +280,23 @@ function setControlByRequest(pedalId, requestParams) {
let value = parseInt(requestParams.get('value')); let value = parseInt(requestParams.get('value'));
if (value === undefined) { if (value === undefined) {
reject('could not handle POST - missing parameter \'value\'', 400); 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); reject('parameter \'value\' is not a number', 400);
} }
getPedalControlById(pedalId, controlId) getPedalControlById(pedalId, controlId)
.then(function (control) { .then(function (control) {
resolve(setControl(pedalId, control, value)); resolve(setControl(control, value));
}) })
.catch(reject); .catch(reject);
}); });
} }
async function setControl(pedalId, control, value) { async function setControl(control, value) {
if (control === undefined || control.midi === undefined) { if (control === undefined || control.midi === undefined) {
throw new Error('control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings'); 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); control.midi.value = await osc.send(control.midi.controller, control.midi.channel, value);
cache.updateControl(control);
} }
function setPedalboardById(pedalboardId) { function setPedalboardById(pedalboardId) {
@ -432,7 +400,7 @@ async function toggleBypass(pedalId) {
value = 127; value = 127;
} }
try { try {
await setControl(pedalId, bypass, value); await setControl(bypass, value);
blinky.setPedalStatus(pedal); blinky.setPedalStatus(pedal);
} catch (err) { } catch (err) {
throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err); throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err);