2022-02-07 15:41:27 +01:00
|
|
|
const config = require('../config.json');
|
|
|
|
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-02-07 15:41:27 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
util.httpGET(config.modep.host, config.modep.port, '/reset')
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBanks() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 14:07:59 +01:00
|
|
|
var banks = cache.getBanks();
|
2022-02-07 23:50:16 +01:00
|
|
|
if (banks != undefined) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return resolve(banks);
|
|
|
|
}
|
2022-02-11 01:13:51 +01:00
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
// FAKE DATA
|
2022-02-07 16:56:18 +01:00
|
|
|
// var 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 (var index = 0; index < fake.length; index++) {
|
|
|
|
// fake.id = index;
|
|
|
|
// }
|
|
|
|
// banks = util.sortById(fake);
|
|
|
|
// return resolve(fake);
|
2022-02-07 15:41:27 +01:00
|
|
|
|
|
|
|
util.httpGET(config.modep.host, config.modep.port, '/banks')
|
2022-02-07 16:56:18 +01:00
|
|
|
.then(function (banks) {
|
|
|
|
for (var index = 0; index < banks.length; index++) {
|
|
|
|
var bank = banks[index];
|
|
|
|
bank.id = index;
|
|
|
|
}
|
|
|
|
banks = util.sortById(banks);
|
2022-02-08 14:07:59 +01:00
|
|
|
cache.setBanks(banks);
|
2022-02-07 16:56:18 +01:00
|
|
|
return resolve(banks);
|
|
|
|
})
|
2022-02-07 15:41:27 +01:00
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 23:50:16 +01:00
|
|
|
function getBankById(bankId) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
getBanks()
|
|
|
|
.then(function (banks) {
|
|
|
|
for (var index = 0; index < banks.length; index++) {
|
|
|
|
if (banks[index].id != bankId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return resolve(banks[index]);
|
|
|
|
}
|
|
|
|
return reject('error: could not find bank by id \'' + bankId + '\'');
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
function getPedalboards() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 14:07:59 +01:00
|
|
|
var pedalboards = cache.getPedalboards();
|
2022-02-07 23:50:16 +01:00
|
|
|
if (pedalboards != undefined) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return resolve(pedalboards);
|
|
|
|
}
|
|
|
|
// FAKE DATA
|
2022-02-07 16:56:18 +01:00
|
|
|
// var 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 }];
|
|
|
|
// var id = 1;
|
|
|
|
// for (var index = 0; index < fake.length; index++) {
|
|
|
|
// var 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);
|
2022-02-07 15:41:27 +01:00
|
|
|
|
|
|
|
util.httpGET(config.modep.host, config.modep.port, '/pedalboard/list')
|
2022-02-08 14:07:59 +01:00
|
|
|
.then(function (pedalboards) {
|
2022-02-07 16:56:18 +01:00
|
|
|
var id = 1;
|
2022-02-08 14:07:59 +01:00
|
|
|
for (var index = 0; index < pedalboards.length; index++) {
|
|
|
|
var pedalboard = pedalboards[index];
|
2022-02-07 16:56:18 +01:00
|
|
|
if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) {
|
|
|
|
pedalboard.id = 0;
|
|
|
|
defaultPedalboard = pedalboard;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pedalboard.id = id;
|
|
|
|
id++;
|
|
|
|
}
|
2022-02-08 14:07:59 +01:00
|
|
|
pedalboards = util.sortById(pedalboards);
|
|
|
|
cache.setPedalboards(pedalboards);
|
2022-02-07 16:56:18 +01:00
|
|
|
return resolve(pedalboards);
|
2022-02-07 15:41:27 +01:00
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultPedalboard() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 14:07:59 +01:00
|
|
|
var defaultPedalboard = cache.getDefaultPedalboard();
|
2022-02-07 23:50:16 +01:00
|
|
|
if (defaultPedalboard != undefined) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return resolve(defaultPedalboard);
|
|
|
|
}
|
|
|
|
getPedalboardByBundle(constants.PEDALBOARD_DEFAULT)
|
2022-02-08 14:07:59 +01:00
|
|
|
.then(function (defaultPedalboard) {
|
|
|
|
cache.setDefaultPedalboard(defaultPedalboard);
|
|
|
|
return resolve(defaultPedalboard);
|
|
|
|
})
|
2022-02-07 15:41:27 +01:00
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentPedalboard() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 14:07:59 +01:00
|
|
|
var currentPedalboard = cache.getCurrentPedalboard();
|
2022-02-07 23:50:16 +01:00
|
|
|
if (currentPedalboard != undefined && currentPedalboard.id != undefined) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return resolve(currentPedalboard);
|
|
|
|
}
|
|
|
|
// FAKE DATA
|
2022-02-07 16:56:18 +01:00
|
|
|
// var fake = '/var/modep/pedalboards/FUZZ.pedalboard';
|
|
|
|
// getPedalboardByBundle(fake)
|
|
|
|
// .then(function (pedalboard) {
|
|
|
|
// currentPedalboard = pedalboard;
|
|
|
|
// return resolve(pedalboard);
|
|
|
|
// })
|
|
|
|
// .catch(reject);
|
2022-02-07 15:41:27 +01:00
|
|
|
|
|
|
|
// PRODUCTION
|
2022-02-07 16:56:18 +01:00
|
|
|
util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current')
|
|
|
|
.then(getPedalboardByBundle)
|
2022-02-08 14:07:59 +01:00
|
|
|
.then(function (currentPedalboard) {
|
|
|
|
cache.setCurrentPedalboard(currentPedalboard);
|
2022-02-07 23:50:16 +01:00
|
|
|
return resolve(currentPedalboard)
|
|
|
|
})
|
2022-02-07 16:56:18 +01:00
|
|
|
.catch(reject);
|
2022-02-07 15:41:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 23:50:16 +01:00
|
|
|
function getPedalboardById(pedalboardId) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
getPedalboards()
|
|
|
|
.then(function (pedalboards) {
|
|
|
|
for (var index = 0; index < pedalboards.length; index++) {
|
|
|
|
if (pedalboards[index].id != pedalboardId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return resolve(pedalboards[index]);
|
|
|
|
}
|
2022-02-07 23:50:16 +01:00
|
|
|
return reject('error: could not find pedalboard by id \'' + pedalboardId + '\'');
|
2022-02-07 15:41:27 +01:00
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPedalboardByBundle(pedalboardBundle) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-07 23:50:16 +01:00
|
|
|
if (pedalboardBundle == undefined) {
|
|
|
|
getDefaultPedalboard()
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-07 15:41:27 +01:00
|
|
|
getPedalboards()
|
|
|
|
.then(function (pedalboards) {
|
|
|
|
for (var index = 0; index < pedalboards.length; index++) {
|
|
|
|
if (pedalboards[index].bundle != pedalboardBundle) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return resolve(pedalboards[index]);
|
|
|
|
}
|
|
|
|
return reject('error: could not find pedalboard by bundle \'' + pedalboardBundle + '\'');
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPedalControlById(pedalId, controlId) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
getCurrentPedalById(pedalId)
|
|
|
|
.then(function (pedal) {
|
|
|
|
for (var index = 0; index < pedal.controls.length; index++) {
|
|
|
|
if (pedal.controls[index].id != controlId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return resolve(pedal.controls[index]);
|
|
|
|
}
|
|
|
|
return reject('error: could not find control for pedal \'' + pedalId + '\' by id \'' + controlId + '\'');
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentPedalById(id) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
getCurrentPedals()
|
2022-02-07 16:56:18 +01:00
|
|
|
.then(function (currentPedals) {
|
2022-02-07 15:41:27 +01:00
|
|
|
for (var index = 0; index < currentPedals.length; index++) {
|
|
|
|
if (currentPedals[index].id != id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return resolve(currentPedals[index]);
|
|
|
|
}
|
|
|
|
return reject('error: could not find current pedal by id \'' + id + '\'');
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentPedals() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 14:07:59 +01:00
|
|
|
var currentPedals = cache.getCurrentPedals();
|
2022-02-07 15:41:27 +01:00
|
|
|
if (currentPedals) {
|
|
|
|
return resolve(currentPedals);
|
|
|
|
}
|
|
|
|
getCurrentPedalboard()
|
|
|
|
.then(parseCurrentPedalboard)
|
2022-02-08 14:07:59 +01:00
|
|
|
.then(function (pedals) {
|
|
|
|
cache.setCurrentPedals(pedals);
|
|
|
|
return resolve(pedals);
|
|
|
|
})
|
2022-02-07 15:41:27 +01:00
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 16:56:18 +01:00
|
|
|
function parseCurrentPedalboard(currentPedalboard) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-07 16:56:18 +01:00
|
|
|
getDefaultPedalboard()
|
|
|
|
.then(function (defaultPedalboard) {
|
2022-02-11 01:13:51 +01:00
|
|
|
test = defaultPedalboard;
|
2022-02-08 13:46:02 +01:00
|
|
|
currentPedals = [];
|
2022-02-07 16:56:18 +01:00
|
|
|
if (defaultPedalboard.id == currentPedalboard.id) {
|
2022-02-08 13:46:02 +01:00
|
|
|
return resolve(currentPedals);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-02-07 16:56:18 +01:00
|
|
|
var startTime = new Date();
|
|
|
|
logger.debug('parsing current pedalboard...');
|
|
|
|
if (!currentPedalboard.uri) {
|
|
|
|
reject('error: could not determine current pedalboard config file');
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-02-07 16:56:18 +01:00
|
|
|
// FAKE DATA
|
2022-02-10 20:52:02 +01:00
|
|
|
var file = path.resolve(path.dirname(__dirname) + '/dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1));
|
2022-02-07 16:56:18 +01:00
|
|
|
// var file = path.resolve(currentPedalboard.uri.replace('file://', ''));
|
|
|
|
fs.readFile(file, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return reject('error: could not parse current pedalboard file \'' + file + '\' >>> ' + err);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-02-07 16:56:18 +01:00
|
|
|
var json = ttl2jsonld(data.toString())['@graph'];
|
|
|
|
var id = 0;
|
|
|
|
for (var index = 0; index < json.length; index++) {
|
|
|
|
var tmp = json[index];
|
|
|
|
if (!tmp['lv2:prototype']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var name = tmp['@id'];
|
|
|
|
currentPedals.push({ id: id, name: name, controls: [] });
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
for (var index = 0; index < json.length; index++) {
|
|
|
|
var tmp = json[index];
|
|
|
|
var name = tmp['@id'];
|
|
|
|
var value = tmp['ingen:value'];
|
|
|
|
if (value == undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var pedal = undefined;
|
|
|
|
for (var 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 == ':bypass') {
|
|
|
|
value = value;
|
|
|
|
} else {
|
|
|
|
value = value['@value'];
|
|
|
|
}
|
|
|
|
var control = { id: id, name: name, value: value };
|
|
|
|
pedal.controls.push(control);
|
|
|
|
id++;
|
|
|
|
var 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);
|
|
|
|
});
|
|
|
|
}).catch(reject);
|
2022-02-07 15:41:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-08 13:46:02 +01:00
|
|
|
function setControlValue(pedalId, requestParams) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 13:46:02 +01:00
|
|
|
var controlId = requestParams.get('id');
|
|
|
|
if (controlId == undefined) {
|
|
|
|
reject('error: could not handle POST - missing parameter \'id\'', 400);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-02-08 13:46:02 +01:00
|
|
|
var value = parseInt(requestParams.get('value'));
|
|
|
|
if (value == undefined) {
|
|
|
|
reject('error: could not handle POST - missing parameter \'value\'', 400);
|
|
|
|
} else if (Number.isNaN(value)) {
|
|
|
|
reject('error: parameter \'value\' is not a number', 400);
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|
2022-02-08 13:46:02 +01:00
|
|
|
getPedalControlById(pedalId, controlId)
|
|
|
|
.then(function (control) {
|
|
|
|
if (!control || !control.midi) {
|
|
|
|
return reject('error: control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings');
|
|
|
|
}
|
|
|
|
if (value > 127) {
|
|
|
|
value = 127;
|
|
|
|
} else if (value < 0) {
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
value = '00' + util.toHex(value) + '0' + util.toHex(control.midi.controller) + 'b' + util.toHex(control.midi.channel);
|
|
|
|
var cmd = 'oscsend';
|
|
|
|
var args = [config.osc.host, config.osc.port, config.osc.address, 'm', value];
|
2022-02-11 01:40:47 +01:00
|
|
|
commands.execute(cmd, args);
|
2022-02-08 13:46:02 +01:00
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:41:27 +01:00
|
|
|
function setPedalboardById(pedalboardId) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-08 11:11:49 +01:00
|
|
|
if (pedalboardId == undefined) {
|
2022-02-07 15:41:27 +01:00
|
|
|
return reject('error: no pedalboard id given');
|
|
|
|
}
|
|
|
|
getPedalboardById(pedalboardId)
|
|
|
|
.then(setPedalboard)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPedalboard(pedalboard) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (!pedalboard || !pedalboard.bundle) {
|
|
|
|
return reject('error: no bundle set for pedalboard');
|
|
|
|
}
|
2022-02-11 01:13:51 +01:00
|
|
|
var tmpPedalboard;
|
2022-02-07 23:50:16 +01:00
|
|
|
getCurrentPedalboard()
|
|
|
|
.then(function (currentPedalboard) {
|
|
|
|
if (pedalboard.id == currentPedalboard.id) {
|
2022-02-08 11:11:49 +01:00
|
|
|
return Promise.reject('pedalboard \'' + pedalboard.id + '\' is already active');
|
2022-02-07 23:50:16 +01:00
|
|
|
}
|
2022-02-11 01:13:51 +01:00
|
|
|
tmpPedalboard = currentPedalboard;
|
2022-02-07 23:50:16 +01:00
|
|
|
})
|
2022-02-08 11:11:49 +01:00
|
|
|
.then(function () {
|
|
|
|
return reset()
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)
|
|
|
|
})
|
2022-02-07 23:50:16 +01:00
|
|
|
.then(function () {
|
2022-02-11 01:13:51 +01:00
|
|
|
parseCurrentPedalboard(tmpPedalboard);
|
2022-02-07 23:50:16 +01:00
|
|
|
})
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
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-02-08 13:46:02 +01:00
|
|
|
setControlValue,
|
2022-02-07 15:41:27 +01:00
|
|
|
}
|