summaryrefslogtreecommitdiff
path: root/share/web_surfaces/builtin/mixer-demo/js/widget.js
blob: 335e3d8756fbee3608e4aa91c78a8d05293a57a6 (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
/*
 * 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.
 */

import { Message } from '/shared/message.js';

export class Widget {

    constructor (node, addr, html) {
        this.node = node;
        this.addr = addr;
        const template = document.createElement('template');
        template.innerHTML = html;
        this.el = template.content.firstChild;
    }

    attach (parent, callback) {
        parent.appendChild(this.el);

        if (callback) {
            this.callback = callback;
        }
    }

    callback (value) {
        // do nothing by default
    }

    get hash () {
        return Message.hash(this.node, this.addr);
    }

}

export class Switch extends Widget {

    constructor (node, addr) {
        super (node, addr, `<input type="checkbox" class="widget-switch">`);
        this.el.addEventListener('input', (ev) => this.callback(this.value));
    }

    get value () {
        return this.el.checked;
    }

    set value (val) {
        this.el.checked = val;
    }
     
}

export class Slider extends Widget {

    constructor (node, addr, min, max, step) {
        const html = `<input type="range" class="widget-slider"
            min="${min}" max="${max}" step="${step}">`;
        super(node, addr, html);
        this.min = min;
        this.max = max;
        this.el.addEventListener('input', (ev) => this.callback(this.value));
    }

    get value () {
        return parseFloat(this.el.value)
    }

    set value (val) {
        this.el.value = val;
    }

}

export class DiscreteSlider extends Slider {

    constructor (node, addr, min, max) {
        super(node, addr, min, max, 1);
    }

}

export class ContinuousSlider extends Slider {

    constructor (node, addr, min, max) {
        super(node, addr, min, max, 0.001);
    }

}

export class LogarithmicSlider extends ContinuousSlider {

    constructor (node, addr, min, max) {
        super(node, addr, 0, 1.0);
        this.minVal = Math.log(min);
        this.maxVal = Math.log(max);
        this.scale = this.maxVal - this.minVal;
    }

    get value () {
        return Math.exp(this.minVal + this.scale * super.value);
    }

    set value (val) {
        this.el.value = (Math.log(val) - this.minVal) / this.scale;
    }

}

export class StripPanSlider extends ContinuousSlider {

    constructor (node, addr) {
        super(node, addr, -1.0, 1.0);
    }

}

export class StripGainSlider extends ContinuousSlider {

    constructor (node, addr) {
        super(node, addr, 0, 1.0)
        this.minVal = -58.0;
        this.maxVal = 6.0;
        this.scale = (this.maxVal - this.minVal);
    }

    get value () {
        return this.maxVal + Math.log10(super.value) * this.scale;
    }

    set value (val) {
        this.el.value = Math.pow(10.0, (val - this.maxVal) / this.scale);
    }

}

export class StripMeter extends Widget {

    constructor (node, addr) {
        super(node, addr, `<label></label>`);
    }

    set value (val) {
        this.el.innerHTML = val == -Infinity ? '-∞' : `${Math.round(val)} dB`;
    }

}