summaryrefslogtreecommitdiff
path: root/share/web_surfaces/builtin/mixer-demo/js/main.js
blob: 64d5fca33fba73a9a707fff36d1ecafda4335a28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 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.
 */

 // This example does not call the API methods in ardour.js,
 // instead it interacts at a lower level by coupling the widgets
 // tightly to the message stream

import { ANode, Message } from '/shared/message.js';
import { ArdourClient } from '/shared/ardour.js';

import { Switch, DiscreteSlider, ContinuousSlider, LogarithmicSlider,
        StripPanSlider, StripGainSlider, StripMeter } from './widget.js';

(() => {

    const MAX_LOG_LINES = 1000;
    const FEEDBACK_NODES = [ANode.STRIP_GAIN, ANode.STRIP_PAN, ANode.STRIP_METER,
                            ANode.STRIP_PLUGIN_ENABLE, ANode.STRIP_PLUGIN_PARAM_VALUE];
    
    const ardour = new ArdourClient(location.host);
    const widgets = {};

    main();

    function main () {
        ardour.getSurfaceManifest().then((manifest) => {
            const div = document.getElementById('manifest');
            div.innerHTML = `${manifest.name.toUpperCase()} v${manifest.version} — ${manifest.description}`;
        });

        ardour.addCallback({
            onMessage: (msg) => {
                log(`↙ ${msg}`, 'message-in');

                if (msg.node == ANode.STRIP_DESC) {
                    createStrip (msg.addr, ...msg.val);
                } else if (msg.node == ANode.STRIP_PLUGIN_DESC) {
                    createStripPlugin (msg.addr, ...msg.val);
                } else if (msg.node == ANode.STRIP_PLUGIN_PARAM_DESC) {
                    createStripPluginParam (msg.addr, ...msg.val);
                } else if (FEEDBACK_NODES.includes(msg.node)) {
                    if (widgets[msg.hash]) {
                        widgets[msg.hash].value = msg.val[0];
                    }
                }
            },

            onError: () => {
                log('Client error', 'error');
            }
        });
        
        ardour.open();
    }

    function createStrip (addr, name) {
        const id = `strip-${addr[0]}`;
        const strips = document.getElementById('strips');
        const div = createElem(`<div class="strip" id="${id}"></div>`, strips);
        createElem(`<label class="comp-name" for="${id}">∿&emsp;&emsp;${name}</label>`, div);
        
        // meter
        const meter = new StripMeter(ANode.STRIP_METER, addr);
        meter.el.classList.add('slider-meter');
        meter.attach(div);
        register(meter);

        // gain
        let holder = createElem(`<div class="strip-slider"></div>`, div); 
        createElem(`<label>Gain</label>`, holder);
        const gain = new StripGainSlider(ANode.STRIP_GAIN, addr);
        gain.attach(holder, (val) => send(gain));
        register(gain);

        // pan
        holder = createElem(`<div class="strip-slider"></div>`, div); 
        createElem(`<label>Pan</label>`, holder);
        const pan = new StripPanSlider(ANode.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(`<div class="plugin" id="${id}"></div>`, strip);
        createElem(`<label class="comp-name">⨍&emsp;&emsp;${name}</label>`, div);
        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, 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(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(`<div class="plugin-param ${cssClass}" id="${id}"></div>`, plugin);
        createElem(`<label for="${id}">${name}</label>`, 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');
        ardour.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;
    }

})();