2022-02-07 15:41:27 +01:00
|
|
|
const util = require('./util.js');
|
|
|
|
const constants = require('./constants.js');
|
|
|
|
const logger = require('./logger.js');
|
2022-02-08 14:07:59 +01:00
|
|
|
const cache = require('./cache.js');
|
2022-02-11 01:20:53 +01:00
|
|
|
const commands = require('./commands.js');
|
2022-03-08 00:51:58 +01:00
|
|
|
const blinky = require('./blinky.js');
|
2022-03-11 16:52:37 +01:00
|
|
|
const osc = require('./osc.js');
|
2022-02-07 15:41:27 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
let pedalboardIdBypassOrigin;
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function reset() {
|
|
|
|
await util.httpGET(global.config.modep.host, global.config.modep.port, '/reset');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getBanks() {
|
|
|
|
let banks = cache.getBanks();
|
|
|
|
if (banks !== undefined) {
|
|
|
|
return banks;
|
|
|
|
}
|
|
|
|
banks = await util.httpGET(global.config.modep.host, global.config.modep.port, '/banks');
|
|
|
|
for (let index = 0; index < banks.length; index++) {
|
|
|
|
let bank = banks[index];
|
|
|
|
bank.id = index;
|
|
|
|
}
|
|
|
|
banks = util.sortById(banks);
|
|
|
|
cache.setBanks(banks);
|
|
|
|
return banks;
|
2022-02-07 23:50:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getBankById(bankId) {
|
|
|
|
const banks = await getBanks();
|
|
|
|
for (let index = 0; index < banks.length; index++) {
|
|
|
|
if (banks[index].id != bankId) {
|
|
|
|
continue;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
return banks[index];
|
|
|
|
}
|
|
|
|
throw new Error('could not find bank by id \'' + bankId + '\'');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getPedalboards() {
|
|
|
|
let pedalboards = cache.getPedalboards();
|
|
|
|
if (pedalboards !== undefined) {
|
|
|
|
return pedalboards;
|
|
|
|
}
|
|
|
|
pedalboards = await util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list');
|
|
|
|
let id = 1;
|
|
|
|
for (let index = 0; index < pedalboards.length; index++) {
|
|
|
|
let pedalboard = pedalboards[index];
|
|
|
|
if (pedalboard.bundle === constants.PEDALBOARD_DEFAULT) {
|
|
|
|
pedalboard.id = 0;
|
|
|
|
defaultPedalboard = pedalboard;
|
|
|
|
continue;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
pedalboard.id = id;
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
pedalboards = util.sortById(pedalboards);
|
|
|
|
cache.setPedalboards(pedalboards);
|
|
|
|
return pedalboards;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getDefaultPedalboard() {
|
|
|
|
let defaultPedalboard = cache.getDefaultPedalboard();
|
|
|
|
if (defaultPedalboard !== undefined) {
|
|
|
|
return defaultPedalboard;
|
|
|
|
}
|
|
|
|
defaultPedalboard = await getPedalboardByBundle(constants.PEDALBOARD_DEFAULT);
|
|
|
|
cache.setDefaultPedalboard(defaultPedalboard);
|
|
|
|
return defaultPedalboard;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getCurrentPedalboard() {
|
|
|
|
let currentPedalboard = cache.getCurrentPedalboard();
|
|
|
|
if (currentPedalboard !== undefined) {
|
|
|
|
return currentPedalboard;
|
|
|
|
}
|
|
|
|
currentPedalboard = await getPedalboardByBundle(await util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current'));
|
|
|
|
cache.setCurrentPedalboard(currentPedalboard);
|
|
|
|
return currentPedalboard;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getPedalboardById(pedalboardId) {
|
|
|
|
const pedalboards = await getPedalboards();
|
|
|
|
if (pedalboards === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let index = 0; index < pedalboards.length; index++) {
|
|
|
|
if (pedalboards[index].id != pedalboardId) {
|
|
|
|
continue;
|
2022-02-07 23:50:16 +01:00
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
return pedalboards[index];
|
|
|
|
}
|
|
|
|
throw new Error('could not find pedalboard by id \'' + pedalboardId + '\'');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getPedalboardByBundle(pedalboardBundle) {
|
|
|
|
if (pedalboardBundle === undefined) {
|
|
|
|
return await getDefaultPedalboard();
|
|
|
|
}
|
|
|
|
const pedalboards = await getPedalboards();
|
|
|
|
for (let index = 0; index < pedalboards.length; index++) {
|
|
|
|
if (pedalboards[index].bundle != pedalboardBundle) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return pedalboards[index];
|
|
|
|
}
|
|
|
|
throw new Error('could not find pedalboard by bundle \'' + pedalboardBundle + '\'');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getPedalControlById(pedalId, controlId) {
|
|
|
|
const pedal = await getCurrentPedalById(pedalId);
|
|
|
|
for (let index = 0; index < pedal.controls.length; index++) {
|
|
|
|
if (pedal.controls[index].id != controlId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return pedal.controls[index];
|
|
|
|
}
|
|
|
|
throw new Error('could not find control for pedal \'' + pedalId + '\' by id \'' + controlId + '\'');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function getCurrentPedalById(id) {
|
|
|
|
const currentPedals = await getCurrentPedals();
|
|
|
|
for (let index = 0; index < currentPedals.length; index++) {
|
|
|
|
if (currentPedals[index].id != id) {
|
|
|
|
continue;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
return currentPedals[index];
|
|
|
|
}
|
|
|
|
throw new Error('could not find current pedal by id \'' + id + '\'');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCurrentPedals() {
|
|
|
|
let currentPedals = cache.getCurrentPedals();
|
|
|
|
if (currentPedals !== undefined) {
|
|
|
|
return currentPedals;
|
|
|
|
}
|
|
|
|
return await parseCurrentPedalboard(await getCurrentPedalboard());
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
async function parseCurrentPedalboard(currentPedalboard) {
|
|
|
|
let pedals = [];
|
|
|
|
const defaultPedalboard = await getDefaultPedalboard();
|
2022-03-25 12:30:17 +01:00
|
|
|
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
|
2022-03-25 13:54:43 +01:00
|
|
|
// let file = path.resolve(path.dirname(__dirname) + '/dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1));
|
|
|
|
let file = path.resolve(currentPedalboard.uri.replace('file://', ''));
|
2022-03-25 13:52:41 +01:00
|
|
|
const data = await new Promise((resolve, reject) => {
|
2022-03-25 12:30:17 +01:00
|
|
|
fs.readFile(file, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
return reject('could not parse current pedalboard file \'' + file + '\' >>> ' + err);
|
|
|
|
}
|
2022-03-25 13:52:41 +01:00
|
|
|
resolve(data);
|
2022-03-08 00:51:58 +01:00
|
|
|
});
|
2022-03-25 12:30:17 +01:00
|
|
|
});
|
2022-03-25 13:52:41 +01:00
|
|
|
let json = ttl2jsonld(data.toString())['@graph'];
|
|
|
|
let id = 0;
|
|
|
|
for (let index = 0; index < json.length; index++) {
|
|
|
|
let tmp = json[index];
|
|
|
|
if (tmp['lv2:prototype'] === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let name = tmp['@id'];
|
|
|
|
pedals.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 < pedals.length; pedalIndex++) {
|
|
|
|
if (!name.startsWith(pedals[pedalIndex].name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pedal = pedals[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')
|
2022-03-08 00:51:58 +01:00
|
|
|
cache.setCurrentPedals(pedals);
|
|
|
|
blinky.setStatus(pedals);
|
|
|
|
return pedals;
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function setControlByRequest(pedalId, requestParams) {
|
|
|
|
if (requestParams === undefined) {
|
|
|
|
throw new Error('could not handle POST missing all parameters', 400);
|
|
|
|
}
|
|
|
|
let controlId = requestParams.get('id');
|
|
|
|
if (controlId === undefined || controlId === null) {
|
|
|
|
throw new Error('could not handle POST - missing parameter \'id\'', 400);
|
|
|
|
}
|
|
|
|
if (isNaN(controlId)) {
|
|
|
|
throw new Error('parameter \'id\' is not a number', 400);
|
|
|
|
}
|
|
|
|
let value = requestParams.get('value');
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
throw new Error('could not handle POST - missing parameter \'value\'', 400);
|
|
|
|
}
|
|
|
|
if (isNaN(value)) {
|
|
|
|
throw new Error('parameter \'value\' is not a number', 400);
|
|
|
|
}
|
|
|
|
return await setControl(await getPedalControlById(pedalId, controlId), parseInt(value));
|
2022-02-08 13:46:02 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 12:30:17 +01:00
|
|
|
async function setControl(control, value) {
|
2022-03-08 00:51:58 +01:00
|
|
|
if (control === undefined || control.midi === undefined) {
|
|
|
|
throw new Error('control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings');
|
2022-03-25 01:10:50 +01:00
|
|
|
}
|
|
|
|
control.midi.value = await osc.send(control.midi.controller, control.midi.channel, value);
|
2022-03-25 12:30:17 +01:00
|
|
|
cache.updateControl(control);
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function setPedalboardById(pedalboardId) {
|
|
|
|
if (pedalboardId === undefined) {
|
|
|
|
throw new Error('no pedalboard id given');
|
|
|
|
}
|
|
|
|
if (isNaN(pedalboardId)) {
|
|
|
|
throw new Error('given pedalboard id \'' + pedalboardId + '\' is not a number');
|
|
|
|
}
|
|
|
|
return await setPedalboard(await getPedalboardById(pedalboardId));
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:52:41 +01:00
|
|
|
async function setPedalboard(pedalboard) {
|
|
|
|
if (pedalboard?.bundle === undefined) {
|
|
|
|
throw new Error('no bundle set for pedalboard \'' + pedalboard.title + '\'');
|
|
|
|
}
|
|
|
|
if (pedalboard.id === await getCurrentPedalboard().id) {
|
|
|
|
throw new Error('pedalboard with id \'' + currentPedalboard.id + '\' is already active');
|
|
|
|
}
|
|
|
|
await reset();
|
|
|
|
await util.httpPOST(global.config.modep.host, global.config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle);
|
|
|
|
cache.setCurrentPedalboard(pedalboard);
|
|
|
|
return await parseCurrentPedalboard(pedalboard);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
|
|
|
|
2022-03-08 00:51:58 +01:00
|
|
|
function hasControlMidiBindings(control) {
|
2022-03-25 01:10:50 +01:00
|
|
|
return control?.midi !== undefined;
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 01:10:50 +01:00
|
|
|
function isBypassActive(control) {
|
|
|
|
return (control.midi?.value === undefined && control.value >= 1) || control.midi?.value === 0;
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isPedalBypassed(pedal) {
|
2022-03-25 01:10:50 +01:00
|
|
|
return isBypassActive(getBypassControlFromPedal(pedal));
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getBypassControlFromPedal(pedal) {
|
|
|
|
if (pedal === undefined) {
|
|
|
|
throw new Error('could not get bypass for an undefined pedal');
|
|
|
|
}
|
|
|
|
if (pedal.controls === undefined || pedal.controls.length === 0) {
|
|
|
|
throw new Error('could not get bypass for pedal \'' + pedal.name + '\' with id \'' + pedal.id + '\', pedal has no controls');
|
|
|
|
}
|
|
|
|
let bypass;
|
|
|
|
for (let index = 0; index < pedal.controls.length; index++) {
|
|
|
|
const control = pedal.controls[index];
|
|
|
|
if (control.name === constants.CONTROL_BYPASS) {
|
|
|
|
bypass = pedal.controls[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bypass === undefined) {
|
|
|
|
throw new Error('could not find bypass control for pedal \'' + pedal.name + '\' with id \'' + pedal.id + '\'');
|
|
|
|
}
|
|
|
|
return bypass;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function toggleBypass(pedalId) {
|
|
|
|
let currentPedalboard = await getCurrentPedalboard();
|
|
|
|
let defaultPedalboard = await getDefaultPedalboard();
|
|
|
|
if (currentPedalboard.id === defaultPedalboard.id && pedalboardIdBypassOrigin === undefined) {
|
|
|
|
throw new Error('could not activate bypass, default pedalboard is currently already active');
|
|
|
|
}
|
|
|
|
if (pedalId === undefined) {
|
|
|
|
try {
|
|
|
|
let pedalboardId;
|
|
|
|
if (pedalboardIdBypassOrigin === undefined) {
|
|
|
|
pedalboardId = defaultPedalboard.id;
|
|
|
|
pedalboardIdBypassOrigin = currentPedalboard.id;
|
|
|
|
} else {
|
|
|
|
pedalboardId = pedalboardIdBypassOrigin;
|
|
|
|
pedalboardIdBypassOrigin = undefined;
|
|
|
|
}
|
|
|
|
await setPedalboardById(pedalboardId);
|
|
|
|
blinky.setBypass(pedalboardId === defaultPedalboard.id);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-03-24 16:30:00 +01:00
|
|
|
const pedal = await getCurrentPedalById(pedalId);
|
|
|
|
const bypass = getBypassControlFromPedal(pedal);
|
|
|
|
let value = 0;
|
2022-03-25 01:10:50 +01:00
|
|
|
if (isBypassActive(bypass)) {
|
2022-03-24 16:30:00 +01:00
|
|
|
value = 127;
|
2022-03-08 00:51:58 +01:00
|
|
|
}
|
|
|
|
try {
|
2022-03-25 12:30:17 +01:00
|
|
|
await setControl(bypass, value);
|
2022-03-10 00:32:38 +01:00
|
|
|
blinky.setPedalStatus(pedal);
|
2022-03-08 00:51:58 +01:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
module.exports = {
|
|
|
|
getBanks,
|
2022-02-07 23:50:16 +01:00
|
|
|
getBankById,
|
2022-02-07 15:41:27 +01:00
|
|
|
getPedalboards,
|
2022-02-07 23:50:16 +01:00
|
|
|
getPedalboardById,
|
2022-02-07 15:41:27 +01:00
|
|
|
getDefaultPedalboard,
|
|
|
|
getCurrentPedalboard,
|
|
|
|
getCurrentPedals,
|
|
|
|
getCurrentPedalById,
|
|
|
|
getPedalControlById,
|
|
|
|
setPedalboard,
|
|
|
|
setPedalboardById,
|
2022-03-08 00:51:58 +01:00
|
|
|
setControlByRequest,
|
|
|
|
toggleBypass,
|
2022-03-25 01:10:50 +01:00
|
|
|
isBypassActive,
|
2022-03-08 00:51:58 +01:00
|
|
|
isPedalBypassed,
|
2022-03-25 01:10:50 +01:00
|
|
|
getBypassControlFromPedal,
|
|
|
|
hasControlMidiBindings
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|