From 2ba8dab95e10596ac88539a74e47024836138019 Mon Sep 17 00:00:00 2001 From: Luciano Iam Date: Tue, 14 Apr 2020 09:53:25 +0200 Subject: WebSockets: update JS client and demo avoid hardcoded node name strings in mixer-demo improve mixer-demo design split ardour.js client into control and metadata mixins rename JS client class Ardour to ArdourClient JS client improve mixin implementation JS client improve manifest parser NO-OP whitespace and comments NO-OP update websurface manifest versions add Ardour icon in mixer-demo improve mixer-demo scrolling on touch screens --- share/web_surfaces/builtin/mixer-demo/css/main.css | 50 +++++++--- .../builtin/mixer-demo/img/ardour-icon.svg | 21 +++++ share/web_surfaces/builtin/mixer-demo/index.html | 9 +- share/web_surfaces/builtin/mixer-demo/js/main.js | 44 ++++----- share/web_surfaces/builtin/mixer-demo/manifest.xml | 2 +- share/web_surfaces/builtin/transport/manifest.xml | 2 +- share/web_surfaces/index/main.js | 4 +- share/web_surfaces/shared/ardour.js | 102 ++++----------------- share/web_surfaces/shared/callback.js | 2 + share/web_surfaces/shared/control.js | 73 +++++++++++++++ share/web_surfaces/shared/metadata.js | 51 +++++++++++ 11 files changed, 237 insertions(+), 123 deletions(-) create mode 100644 share/web_surfaces/builtin/mixer-demo/img/ardour-icon.svg create mode 100644 share/web_surfaces/shared/control.js create mode 100644 share/web_surfaces/shared/metadata.js diff --git a/share/web_surfaces/builtin/mixer-demo/css/main.css b/share/web_surfaces/builtin/mixer-demo/css/main.css index 5477f9840e..0fd812c39b 100644 --- a/share/web_surfaces/builtin/mixer-demo/css/main.css +++ b/share/web_surfaces/builtin/mixer-demo/css/main.css @@ -7,6 +7,8 @@ body { color: rgb(248,248,242); font-family: Helvetica, Arial, sans-serif; height: 100%; + width: 100%; + position: fixed; margin: 0; } @@ -20,26 +22,46 @@ div { height: 100%; } -#strips { +#surface { flex: 1; - overflow: scroll; - overflow-x: hidden; + display: flex; + flex-direction: column; box-shadow: 0px 0px 10px #000; } -#manifest { +#top { + display: flex; + align-items: center; padding: 0.25em 0.5em; - opacity: 0.5; - background: rgba(0,0,0,0.4); + box-shadow: 0px 0px 10px #000; +} + +#top > img { + height: 24px; +} + +#top > span { + opacity: 0.75; + margin-left: 12px; } #log { - height: 6em; + height: 7em; overflow: scroll; overflow-x: hidden; background: rgba(0,0,0,0.4); } +#strips { + flex: 1; + display: flex; + flex-direction: column; + align-items: center; + overflow: scroll; + overflow-x: hidden; + background: rgba(0,0,0,0.2); +} + #log pre { margin: 0; font-family: Menlo, monospace; @@ -64,10 +86,12 @@ div { } .strip { - margin: 5%; - padding: 2.5% 5%; + margin: 20px; + padding: 20px 40px; background: rgba(0,0,0,0.1); border-radius: 5px; + width: 100%; + max-width: 768px; } .slider-meter { @@ -75,12 +99,12 @@ div { } .strip-slider { - margin-top: 2.5%; + margin-top: 20px; } .plugin { - margin: 5%; - padding: 2.5% 5%; + margin: 40px 0; + padding: 20px 40px; background: rgba(0,0,0,0.05); border-radius: 5px; } @@ -90,7 +114,7 @@ div { } .plugin-param { - margin: 5%; + margin: 40px; } .plugin-param.boolean { diff --git a/share/web_surfaces/builtin/mixer-demo/img/ardour-icon.svg b/share/web_surfaces/builtin/mixer-demo/img/ardour-icon.svg new file mode 100644 index 0000000000..b824d7df55 --- /dev/null +++ b/share/web_surfaces/builtin/mixer-demo/img/ardour-icon.svg @@ -0,0 +1,21 @@ + + + + + diff --git a/share/web_surfaces/builtin/mixer-demo/index.html b/share/web_surfaces/builtin/mixer-demo/index.html index a94dd17f61..d1c4308616 100644 --- a/share/web_surfaces/builtin/mixer-demo/index.html +++ b/share/web_surfaces/builtin/mixer-demo/index.html @@ -8,8 +8,13 @@
-
-
+
+
+ + +
+
+
diff --git a/share/web_surfaces/builtin/mixer-demo/js/main.js b/share/web_surfaces/builtin/mixer-demo/js/main.js index a2780565b6..64d5fca33f 100644 --- a/share/web_surfaces/builtin/mixer-demo/js/main.js +++ b/share/web_surfaces/builtin/mixer-demo/js/main.js @@ -21,7 +21,7 @@ // tightly to the message stream import { ANode, Message } from '/shared/message.js'; -import { Ardour } from '/shared/ardour.js'; +import { ArdourClient } from '/shared/ardour.js'; import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, StripPanSlider, StripGainSlider, StripMeter } from './widget.js'; @@ -32,7 +32,7 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, const FEEDBACK_NODES = [ANode.STRIP_GAIN, ANode.STRIP_PAN, ANode.STRIP_METER, ANode.STRIP_PLUGIN_ENABLE, ANode.STRIP_PLUGIN_PARAM_VALUE]; - const ardour = new Ardour(location.host); + const ardour = new ArdourClient(location.host); const widgets = {}; main(); @@ -40,7 +40,7 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, function main () { ardour.getSurfaceManifest().then((manifest) => { const div = document.getElementById('manifest'); - div.innerHTML = `${manifest.name} v${manifest.version}`; + div.innerHTML = `${manifest.name.toUpperCase()} v${manifest.version} — ${manifest.description}`; }); ardour.addCallback({ @@ -75,7 +75,7 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, createElem(``, div); // meter - const meter = new StripMeter('strip_meter', addr); + const meter = new StripMeter(ANode.STRIP_METER, addr); meter.el.classList.add('slider-meter'); meter.attach(div); register(meter); @@ -83,14 +83,14 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, // gain let holder = createElem(`
`, div); createElem(``, holder); - const gain = new StripGainSlider('strip_gain', addr); + const gain = new StripGainSlider(ANode.STRIP_GAIN, addr); gain.attach(holder, (val) => send(gain)); register(gain); // pan holder = createElem(`
`, div); createElem(``, holder); - const pan = new StripPanSlider('strip_pan', addr); + const pan = new StripPanSlider(ANode.STRIP_PAN, addr); pan.attach(holder, (val) => send(pan)); register(pan); } @@ -100,33 +100,33 @@ import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, const id = `plugin-${addr[0]}-${addr[1]}`; const div = createElem(`
`, strip); createElem(``, div); - const enable = new Switch('strip_plugin_enable', addr); + const enable = new Switch(ANode.STRIP_PLUGIN_ENABLE, addr); enable.el.classList.add('plugin-enable'); enable.attach(div, (val) => send(enable)); register(enable); } - function createStripPluginParam (addr, name, data_type, min, max, is_log) { - let param, clazz; - - if (data_type == 'b') { - clazz = 'boolean'; - param = new Switch('strip_plugin_param_value', addr); - } else if (data_type == 'i') { - clazz = 'discrete'; - param = new DiscreteSlider('strip_plugin_param_value', addr, min, max); - } else if (data_type == 'd') { - clazz = 'continuous'; - if (is_log) { - param = new LogarithmicSlider('strip_plugin_param_value', addr, min, max); + function createStripPluginParam (addr, name, dataType, min, max, isLog) { + let param, cssClass; + + if (dataType == 'b') { + cssClass = 'boolean'; + param = new Switch(ANode.STRIP_PLUGIN_PARAM_VALUE, addr); + } else if (dataType == 'i') { + cssClass = 'discrete'; + param = new DiscreteSlider(ANode.STRIP_PLUGIN_PARAM_VALUE, addr, min, max); + } else if (dataType == 'd') { + cssClass = 'continuous'; + if (isLog) { + param = new LogarithmicSlider(ANode.STRIP_PLUGIN_PARAM_VALUE, addr, min, max); } else { - param = new ContinuousSlider('strip_plugin_param_value', addr, min, max); + param = new ContinuousSlider(ANode.STRIP_PLUGIN_PARAM_VALUE, addr, min, max); } } const plugin = document.getElementById(`plugin-${addr[0]}-${addr[1]}`); const id = `param-${addr[0]}-${addr[1]}-${addr[2]}`; - const div = createElem(`
`, plugin); + const div = createElem(`
`, plugin); createElem(``, div); param.attach(div, (val) => send(param)); diff --git a/share/web_surfaces/builtin/mixer-demo/manifest.xml b/share/web_surfaces/builtin/mixer-demo/manifest.xml index f0c1b5ddc3..bfa80a21b9 100644 --- a/share/web_surfaces/builtin/mixer-demo/manifest.xml +++ b/share/web_surfaces/builtin/mixer-demo/manifest.xml @@ -2,5 +2,5 @@ - + diff --git a/share/web_surfaces/builtin/transport/manifest.xml b/share/web_surfaces/builtin/transport/manifest.xml index 2f9174a152..aef70440cc 100644 --- a/share/web_surfaces/builtin/transport/manifest.xml +++ b/share/web_surfaces/builtin/transport/manifest.xml @@ -2,5 +2,5 @@ - + diff --git a/share/web_surfaces/index/main.js b/share/web_surfaces/index/main.js index 2dcb59e654..3594268991 100644 --- a/share/web_surfaces/index/main.js +++ b/share/web_surfaces/index/main.js @@ -16,13 +16,13 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -import { Ardour } from '/shared/ardour.js'; +import { ArdourClient } from '/shared/ardour.js'; (() => { async function main () { try { - const surfaces = await new Ardour().getAvailableSurfaces(); + const surfaces = await new ArdourClient().getAvailableSurfaces(); printSurfaces(surfaces); } catch (err) { printError(`Error loading surfaces list: ${err.message}`); diff --git a/share/web_surfaces/shared/ardour.js b/share/web_surfaces/shared/ardour.js index 77e6bf49b9..a7e6978ba2 100644 --- a/share/web_surfaces/shared/ardour.js +++ b/share/web_surfaces/shared/ardour.js @@ -16,10 +16,14 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -import { ANode, Message } from './message.js'; +import { MetadataMixin } from './metadata.js'; +import { ControlMixin } from './control.js'; +import { Message } from './message.js'; import { MessageChannel } from './channel.js'; -export class Ardour { +// See *Mixin for the available APIs + +class BaseArdourClient { constructor () { this._callbacks = []; @@ -56,90 +60,11 @@ export class Ardour { this._channel.send(msg); } - // Surface metadata API over HTTP - - async getAvailableSurfaces () { - const response = await fetch('/surfaces.json'); - - if (response.status == 200) { - return await response.json(); - } else { - throw this._fetchResponseStatusError(response.status); - } - } - - 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'), - version: xmlDoc.getElementsByTagName('Version')[0].getAttribute('value') - } - } else { - throw this._fetchResponseStatusError(response.status); - } - } - - // Surface control API over WebSockets - // clients need to call open() before calling these methods - - async getTempo () { - return (await this._sendAndReceive(ANode.TEMPO))[0]; - } - - async getStripGain (stripId) { - return (await this._sendAndReceive(ANode.STRIP_GAIN, [stripId]))[0]; - } - - async getStripPan (stripId) { - return (await this._sendAndReceive(ANode.STRIP_PAN, [stripId]))[0]; - } - - async getStripMute (stripId) { - return (await this._sendAndReceive(ANode.STRIP_MUTE, [stripId]))[0]; - } - - async getStripPluginEnable (stripId, pluginId) { - return (await this._sendAndReceive(ANode.STRIP_PLUGIN_ENABLE, [stripId, pluginId]))[0]; - } - - async getStripPluginParamValue (stripId, pluginId, paramId) { - return (await this._sendAndReceive(ANode.STRIP_PLUGIN_PARAM_VALUE, [stripId, pluginId, paramId]))[0]; - } - - setTempo (bpm) { - this._send(ANode.TEMPO, [], [bpm]); - } - - setStripGain (stripId, db) { - this._send(ANode.STRIP_GAIN, [stripId], [db]); - } - - setStripPan (stripId, value) { - this._send(ANode.STRIP_PAN, [stripId], [value]); - } - - setStripMute (stripId, value) { - this._send(ANode.STRIP_MUTE, [stripId], [value]); - } - - setStripPluginEnable (stripId, pluginId, value) { - this._send(ANode.STRIP_PLUGIN_ENABLE, [stripId, pluginId], [value]); - } - - setStripPluginParamValue (stripId, pluginId, paramId, value) { - this._send(ANode.STRIP_PLUGIN_PARAM_VALUE, [stripId, pluginId, paramId], [value]); - } - // Private methods _send (node, addr, val) { const msg = new Message(node, addr, val); - this._channel.send(msg); + this.send(msg); return msg; } @@ -178,3 +103,16 @@ export class Ardour { } } + +export class ArdourClient extends mixin(BaseArdourClient, ControlMixin, MetadataMixin) {} + +function mixin (dstClass, ...classes) { + for (const srcClass of classes) { + for (const methName of Object.getOwnPropertyNames(srcClass.prototype)) { + if (methName != 'constructor') { + dstClass.prototype[methName] = srcClass.prototype[methName]; + } + } + } + return dstClass; +} diff --git a/share/web_surfaces/shared/callback.js b/share/web_surfaces/shared/callback.js index 1328f49ffb..a510b118cf 100644 --- a/share/web_surfaces/shared/callback.js +++ b/share/web_surfaces/shared/callback.js @@ -16,6 +16,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + // Example empty callback + export class ArdourCallback { onTempo (bpm) {} diff --git a/share/web_surfaces/shared/control.js b/share/web_surfaces/shared/control.js new file mode 100644 index 0000000000..19116bf4c7 --- /dev/null +++ b/share/web_surfaces/shared/control.js @@ -0,0 +1,73 @@ +/* + * Copyright © 2020 Luciano Iam + * + * 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. + */ + +import { ANode } from './message.js'; + +// Surface control API over WebSockets + +export class ControlMixin { + + async getTempo () { + return (await this._sendAndReceive(ANode.TEMPO))[0]; + } + + async getStripGain (stripId) { + return (await this._sendAndReceive(ANode.STRIP_GAIN, [stripId]))[0]; + } + + async getStripPan (stripId) { + return (await this._sendAndReceive(ANode.STRIP_PAN, [stripId]))[0]; + } + + async getStripMute (stripId) { + return (await this._sendAndReceive(ANode.STRIP_MUTE, [stripId]))[0]; + } + + async getStripPluginEnable (stripId, pluginId) { + return (await this._sendAndReceive(ANode.STRIP_PLUGIN_ENABLE, [stripId, pluginId]))[0]; + } + + async getStripPluginParamValue (stripId, pluginId, paramId) { + return (await this._sendAndReceive(ANode.STRIP_PLUGIN_PARAM_VALUE, [stripId, pluginId, paramId]))[0]; + } + + setTempo (bpm) { + this._send(ANode.TEMPO, [], [bpm]); + } + + setStripGain (stripId, db) { + this._send(ANode.STRIP_GAIN, [stripId], [db]); + } + + setStripPan (stripId, value) { + this._send(ANode.STRIP_PAN, [stripId], [value]); + } + + setStripMute (stripId, value) { + this._send(ANode.STRIP_MUTE, [stripId], [value]); + } + + setStripPluginEnable (stripId, pluginId, value) { + this._send(ANode.STRIP_PLUGIN_ENABLE, [stripId, pluginId], [value]); + } + + setStripPluginParamValue (stripId, pluginId, paramId, value) { + this._send(ANode.STRIP_PLUGIN_PARAM_VALUE, [stripId, pluginId, paramId], [value]); + } + +} diff --git a/share/web_surfaces/shared/metadata.js b/share/web_surfaces/shared/metadata.js new file mode 100644 index 0000000000..888476c681 --- /dev/null +++ b/share/web_surfaces/shared/metadata.js @@ -0,0 +1,51 @@ +/* + * Copyright © 2020 Luciano Iam + * + * 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. + */ + +// Surface metadata API over HTTP + +export class MetadataMixin { + + async getAvailableSurfaces () { + const response = await fetch('/surfaces.json'); + + if (response.status == 200) { + return await response.json(); + } else { + throw this._fetchResponseStatusError(response.status); + } + } + + async getSurfaceManifest () { + const response = await fetch('manifest.xml'); + + if (response.status == 200) { + const manifest = {}; + const xmlText = await response.text(); + const xmlDoc = new DOMParser().parseFromString(xmlText, 'text/xml'); + + for (const child of xmlDoc.children[0].children) { + manifest[child.tagName.toLowerCase()] = child.getAttribute('value'); + } + + return manifest; + } else { + throw this._fetchResponseStatusError(response.status); + } + } + +} -- cgit v1.2.3