2020-04-11 11:30:15 -04:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 Luciano Iam <lucianito@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2020-04-12 08:59:29 -04:00
|
|
|
import { MessageChannel, Message, ANode } from './channel.js';
|
2020-04-11 11:30:15 -04:00
|
|
|
|
|
|
|
export class Ardour {
|
|
|
|
|
|
|
|
constructor () {
|
2020-04-12 05:29:01 -04:00
|
|
|
this.channel = new MessageChannel(location.host);
|
2020-04-12 08:44:58 -04:00
|
|
|
this.channel.messageCallback = (msg) => this._onChannelMessage(msg);
|
|
|
|
this.pendingRequest = null;
|
2020-04-11 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async open () {
|
|
|
|
await this.channel.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
close () {
|
|
|
|
this.channel.close();
|
|
|
|
}
|
|
|
|
|
2020-04-12 08:56:09 -04:00
|
|
|
messageCallback (msg) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
// Surface metadata API over HTTP
|
|
|
|
|
2020-04-11 11:30:15 -04:00
|
|
|
async getAvailableSurfaces () {
|
2020-04-12 04:37:09 -04:00
|
|
|
const response = await fetch('/surfaces.json');
|
2020-04-11 11:30:15 -04:00
|
|
|
|
|
|
|
if (response.status == 200) {
|
|
|
|
return await response.json();
|
|
|
|
} else {
|
2020-04-12 06:39:21 -04:00
|
|
|
throw this._fetchResponseStatusError(response.status);
|
2020-04-11 11:30:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 14:09:53 -04:00
|
|
|
async getSurfaceManifest () {
|
|
|
|
const response = await fetch('manifest.xml');
|
|
|
|
|
|
|
|
if (response.status == 200) {
|
|
|
|
const xmlText = await response.text();
|
|
|
|
const xmlDoc = new DOMParser().parseFromString(xmlText, 'text/xml');
|
|
|
|
return {
|
|
|
|
name: xmlDoc.getElementsByTagName('Name')[0].getAttribute('value'),
|
|
|
|
description: xmlDoc.getElementsByTagName('Description')[0].getAttribute('value')
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-12 06:39:21 -04:00
|
|
|
throw this._fetchResponseStatusError(response.status);
|
2020-04-11 14:09:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 09:11:54 -04:00
|
|
|
// Surface control API over WebSockets
|
2020-04-12 08:56:09 -04:00
|
|
|
// clients need to call open() before calling these methods
|
2020-04-12 08:44:58 -04:00
|
|
|
|
2020-04-12 06:39:21 -04:00
|
|
|
async getTempo () {
|
2020-04-12 08:59:29 -04:00
|
|
|
return (await this._sendAndReceive(ANode.TEMPO))[0];
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async getStripGain (stripId) {
|
2020-04-12 08:59:29 -04:00
|
|
|
return (await this._sendAndReceive(ANode.STRIP_GAIN, [stripId]))[0];
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async getStripPan (stripId) {
|
2020-04-12 08:59:29 -04:00
|
|
|
return (await this._sendAndReceive(ANode.STRIP_PAN, [stripId]))[0];
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async getStripMute (stripId) {
|
2020-04-12 08:59:29 -04:00
|
|
|
return (await this._sendAndReceive(ANode.STRIP_MUTE, [stripId]))[0];
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async getStripPluginEnable (stripId, pluginId) {
|
2020-04-12 08:59:29 -04:00
|
|
|
return (await this._sendAndReceive(ANode.STRIP_PLUGIN_ENABLE, [stripId, pluginId]))[0];
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async getStripPluginParamValue (stripId, pluginId, paramId) {
|
2020-04-12 08:59:29 -04:00
|
|
|
return (await this._sendAndReceive(ANode.STRIP_PLUGIN_PARAM_VALUE, [stripId, pluginId, paramId]))[0];
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
setTempo (bpm) {
|
2020-04-12 08:59:29 -04:00
|
|
|
this._send(ANode.TEMPO, [], [bpm]);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
setStripGain (stripId, db) {
|
2020-04-12 08:59:29 -04:00
|
|
|
this._send(ANode.STRIP_GAIN, [stripId], [db]);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
setStripPan (stripId, value) {
|
2020-04-12 08:59:29 -04:00
|
|
|
this._send(ANode.STRIP_PAN, [stripId], [value]);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
setStripMute (stripId, value) {
|
2020-04-12 08:59:29 -04:00
|
|
|
this._send(ANode.STRIP_MUTE, [stripId], [value]);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
setStripPluginEnable (stripId, pluginId, value) {
|
2020-04-12 08:59:29 -04:00
|
|
|
this._send(ANode.STRIP_PLUGIN_ENABLE, [stripId, pluginId], [value]);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
setStripPluginParamValue (stripId, pluginId, paramId, value) {
|
2020-04-12 08:59:29 -04:00
|
|
|
this._send(ANode.STRIP_PLUGIN_PARAM_VALUE, [stripId, pluginId, paramId], [value]);
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
// Private methods
|
|
|
|
|
|
|
|
_send (node, addr, val) {
|
|
|
|
const msg = new Message(node, addr, val);
|
|
|
|
this.channel.send(msg);
|
|
|
|
return msg;
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
async _sendAndReceive (node, addr, val) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const hash = this._send(node, addr, val).hash;
|
|
|
|
this.pendingRequest = {resolve: resolve, hash: hash};
|
|
|
|
});
|
|
|
|
}
|
2020-04-12 06:39:21 -04:00
|
|
|
|
2020-04-12 08:44:58 -04:00
|
|
|
_onChannelMessage (msg) {
|
|
|
|
if (this.pendingRequest && (this.pendingRequest.hash == msg.hash)) {
|
|
|
|
this.pendingRequest.resolve(msg.val);
|
|
|
|
this.pendingRequest = null;
|
2020-04-12 08:56:09 -04:00
|
|
|
} else {
|
|
|
|
this.messageCallback(msg);
|
2020-04-12 08:44:58 -04:00
|
|
|
}
|
2020-04-12 06:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_fetchResponseStatusError (status) {
|
|
|
|
return new Error(`HTTP response status ${status}`);
|
|
|
|
}
|
2020-04-11 11:30:15 -04:00
|
|
|
|
|
|
|
}
|