/* * Copyright (C) 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 { MessageChannel, Message } from '/shared/channel.js'; import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider, StripPanSlider, StripGainSlider, StripMeter } from './widget.js'; (() => { const MAX_LOG_LINES = 1000; const FEEDBACK_NODES = ['strip_gain', 'strip_pan', 'strip_meter', 'strip_plugin_enable', 'strip_plugin_param_value']; const channel = new MessageChannel(location.host); const widgets = {}; main(); function main () { channel.messageCallback = (msg) => { log(`↙ ${msg}`, 'message-in'); if (msg.node == 'strip_desc') { createStrip (msg.addr, ...msg.val); } else if (msg.node == 'strip_plugin_desc') { createStripPlugin (msg.addr, ...msg.val); } else if (msg.node == 'strip_plugin_param_desc') { createStripPluginParam (msg.addr, ...msg.val); } else if (FEEDBACK_NODES.includes(msg.node)) { if (widgets[[msg.node, msg.addr]]) { widgets[[msg.node, msg.addr]].value = msg.val[0]; } } }; channel.closeCallback = () => { log('Connection dropped', 'error'); }; channel.errorCallback = () => { log('Connection error', 'error'); }; channel.open(); } function createStrip (addr, name) { const id = `strip-${addr[0]}`; const strips = document.getElementById('strips'); const div = createElem(`
`, strips); createElem(``, div); // meter const meter = new StripMeter('strip_meter', addr); meter.el.classList.add('slider-meter'); meter.attach(div); register(meter); // gain let holder = createElem(`
`, div); createElem(``, holder); const gain = new StripGainSlider('strip_gain', addr); gain.attach(holder, (val) => send(gain)); register(gain); // pan holder = createElem(`
`, div); createElem(``, holder); const pan = new StripPanSlider('strip_pan', addr); pan.attach(holder, (val) => send(pan)); register(pan); } function createStripPlugin (addr, name) { const strip = document.getElementById(`strip-${addr[0]}`); const id = `plugin-${addr[0]}-${addr[1]}`; const div = createElem(`
`, strip); createElem(``, div); const enable = new Switch('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); } else { param = new ContinuousSlider('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); createElem(``, div); param.attach(div, (val) => send(param)); param.el.name = id; register(param); } function send (widget) { const msg = new Message(widget.node, widget.addr, [widget.value]); log(`↗ ${msg}`, 'message-out'); channel.send(msg); } function createElem (html, parent) { const t = document.createElement('template'); t.innerHTML = html; const elem = t.content.firstChild; if (parent) { parent.appendChild(elem); } return elem; } function register (widget) { widgets[widget.hash] = widget; } function log (message, className) { const output = document.getElementById('log'); if (output.childElementCount > MAX_LOG_LINES) { output.removeChild(output.childNodes[0]); } const pre = document.createElement('pre'); pre.innerHTML = message; pre.className = className; output.appendChild(pre); output.scrollTop = output.scrollHeight; } })();