-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
387 lines (366 loc) · 14.8 KB
/
audio.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const global_area = document.getElementById('audio') // get the targeted unit converter console
const workspace = global_area.getElementsByClassName('workspace')
{ // tags for quick search / select
const module_tag = "声学 电声 音频 声音 扩声 音响 音箱 功放".split(' ')
const tag_area = global_area.getElementsByClassName('tag-area')[0]
module_tag.forEach(i => {
const tag = document.createElement('label')
tag.className = 'tag'
tag.textContent = i
tag_area.append(tag)
})
}
{
class value_keeper { // instances of this class may be used for undo and redo functions in the future
static WAVE_FORM = { sine: 0, triangular: 1, square: 2, }
wave_form
UP
UE
dBu
dBV
IP
IE
Z
S
mW
dBm
dBW
static {
Object.freeze(this.WAVE_FORM)
}
constructor(v) {
if (v !== undefined) { // then clone from v
}
}
convert_V() {
const lgV = Math.log10(this.UE)
this.dBV = 20 * lgV
this.dBu = this.dBV - 20 * Math.log10(Math.sqrt(1e-3 * 600))
}
convert_W() {
this.mW = this.S * 1e3
this.dBW = 10 * Math.log10(this.S)
this.dBm = this.dBW + 30
}
calc_peak() {
const w = value_keeper.WAVE_FORM
switch (this.wave_form) {
default: // case w.sine:
this.UP = this.UE * Math.sqrt(2)
this.IP = this.IE * Math.sqrt(2)
break
case w.triangular:
this.UP = this.UE * Math.sqrt(3)
this.IP = this.IE * Math.sqrt(3)
break
case w.square:
this.UP = this.UE
this.IP = this.IE
break
}
}
convert() {
this.convert_V()
this.convert_W()
this.calc_peak()
}
}
const current_value_keeper = new value_keeper
const scope = workspace[0] // get the targeted part (scope) in the unit converter console
const input = Array.from(scope.getElementsByTagName('input')) // get the inputs
const output_area = scope.getElementsByClassName('output-area')[0] // get the message output area
const radio_button = {}, number_input = {}
{
// The purpose of dual indices (compared with just number index): Lessen the modification of code when more inputs are inserted
const r = radio_button, n = number_input
let p = input.slice(0, 3) // radio button precursor
for (let i = 0; i < p.length; ++i) { r[i] = p[i], r[p[i].name] = {} } // index type 1 of 2: number; get ready for index type 2
for (let i = 0; i < p.length; ++i) { r[p[i].name][p[i].value] = p[i] } // index type 2 of 2: input.name and input.value
p = input.slice(3) // number input precursor
for (let i = 0; i < p.length; ++i) { n[i] = n[p[i].name] = p[i] } // 2 types of indices: number and string
// add event handlers for the current value keeper
const c = current_value_keeper
const m = new Map // Element-Handler Map: Add event listeners. Canonical units: V, A, Ω, W (i.e., VA)
m.set(n['voltage'], () => { // Here we primarily make Z fixed when U has changed
c.IE = c.UE / c.Z
c.S = c.UE * c.IE
c.convert()
})
m.set(n['current'], () => { // Here we primarily make Z fixed when I has changed
c.UE = c.IE * c.Z
c.S = c.UE * c.IE
c.convert()
})
m.set(n['impedance'], () => { // Here we primarily make U fixed when Z has changed
c.IE = c.UE / c.Z
c.S = c.UE * c.IE
c.convert()
})
m.set(n['power'], () => { // Here we primarily make Z fixed and let U, I able to change when S has changed
c.UE = Math.sqrt(c.S * c.Z)
c.IE = c.UE / c.Z
c.convert()
})
m.set(n['dBV'], () => {
c.UE = Math.pow(10, c.dBV / 20)
c.IE = c.UE / c.Z
c.S = c.UE * c.IE
c.convert()
})
m.set(n['dBu'], () => {
c.UE = Math.pow(10, (c.dBu + 20 * Math.log10(Math.sqrt(0.001 * 600))) / 20)
c.IE = c.UE / c.Z
c.S = c.UE * c.IE
c.convert()
})
m.set(n['dBW'], () => {
c.S = Math.pow(10, c.dBW / 10)
c.UE = Math.sqrt(c.S * c.Z)
c.IE = c.UE / c.Z
c.convert()
})
m.set(n['dBm'], () => {
c.S = Math.pow(10, (c.dBm - 30) / 10)
c.UE = Math.sqrt(c.S * c.Z)
c.IE = c.UE / c.Z
c.convert()
})
m.set(n['mW'], () => {
c.S = c.mW / 1e3
c.UE = Math.sqrt(c.S * c.Z)
c.IE = c.UE / c.Z
c.convert()
})
m.set(n['peak-voltage'], () => {
const w = value_keeper.WAVE_FORM
switch (c.wave_form) {
default: // case w.sine:
c.UE = c.UP / Math.sqrt(2)
break
case w.triangular:
c.UE = c.UP / Math.sqrt(3)
break
case w.square:
c.UE = c.UP
break
}
c.IE = c.UE / c.Z
c.IP = c.UP / c.Z
c.S = c.UE * c.IE
c.convert_V()
c.convert_W()
})
m.set(n['peak-current'], () => {
const w = value_keeper.WAVE_FORM
switch (c.wave_form) {
default: // case w.sine:
c.IE = c.IP / Math.sqrt(2)
break
case w.triangular:
c.IE = c.IP / Math.sqrt(3)
break
case w.square:
c.IE = c.IP
break
}
c.UE = c.IE * c.Z
c.UP = c.IP * c.Z
c.S = c.UE * c.IE
c.convert_V()
c.convert_W()
})
const read = () => {
const W = value_keeper.WAVE_FORM
const w = radio_button['wave-form']
c.wave_form = w['sine'].checked ? W.sine : w['triangular'].checked ? W.triangular : W.square
c.UP = parseFloat(n['peak-voltage'].value), c.UE = parseFloat(n['voltage'].value)
c.dBu = parseFloat(n['dBu'].value), c.dBV = parseFloat(n['dBV'].value)
c.IP = parseFloat(n['peak-current'].value), c.IE = parseFloat(n['current'].value)
c.Z = parseFloat(n['impedance'].value)
c.S = parseFloat(n['power'].value)
c.mW = parseFloat(n['mW'].value), c.dBm = parseFloat(n['dBm'].value), c.dBW = parseFloat(n['dBW'].value)
}
const write = (skipped_inputs = new Set) => {
const v = [ c.UP, c.UE, c.dBu, c.dBV, c.IP, c.IE, c.Z, c.S, c.mW, c.dBm, c.dBW, ]
output_area.innerHTML = ''
for (let i = 0; i < Object.keys(n).length / 2; ++i) {
if (!skipped_inputs.has(n[i])) {
if (!isNaN(v[i])) n[i].value = v[i]
else output_area.innerHTML += `${n[i].name} is NaN.<br>`
}
}
}
for (const [ e, h ] of m) {
e.addEventListener('input', (event) => {
read()
h()
write(new Set([ event.target ]))
})
}
m.clear()
m.set(r['wave-form']['sine'], () => {
c.wave_form = value_keeper.WAVE_FORM.sine
n['peak-voltage'].value = c.UP = c.UE * Math.sqrt(2)
n['peak-current'].value = c.IP = c.IE * Math.sqrt(2)
})
m.set(r['wave-form']['triangular'], () => {
c.wave_form = value_keeper.WAVE_FORM.triangular
n['peak-voltage'].value = c.UP = c.UE * Math.sqrt(3)
n['peak-current'].value = c.IP = c.IE * Math.sqrt(3)
})
m.set(r['wave-form']['square'], () => {
c.wave_form = value_keeper.WAVE_FORM.square
n['peak-voltage'].value = c.UP = c.UE
n['peak-current'].value = c.IP = c.IE
})
for (const [ e, h ] of m) {
e.addEventListener('click', (event) => { h() })
}
// fire the corresponding event handlers and show an example of this unit converter utility
n['voltage'].dispatchEvent(new Event('input', { bubbles: true, cancelable: true, }))
}
}
{
class value_keeper { // instances of this class may be used for undo and redo functions in the future
sensitivity
power
SPL
constructor(v) {
if (v !== undefined) { // then clone from v
}
}
}
const current_value_keeper = new value_keeper
const scope = workspace[1] // get the targeted part (scope) in the unit converter console
const input = Array.from(scope.getElementsByTagName('input')) // get the inputs
const output_area = scope.getElementsByClassName('output-area')[0] // get the message output area
const radio_button = {}, number_input = {}, associated_unit_label = {}
{
// The purpose of dual indices (compared with just number index): Lessen the modification of code when more inputs are inserted
const r = radio_button, n = number_input, a = associated_unit_label
let lr = 0, ln = 0
input.forEach(i => {
switch (i.type) {
case 'radio':
r[lr++] = i, r[i.name] = {} // index type 1 of 2: number; get ready for index type 2
break
default:
n[ln++] = n[i.name] = i // 2 types of indices: number and string
break
}
})
for (let i = 0; i < lr; ++i) { r[r[i].name][r[i].value] = r[i] } // index type 2 of 2: input.name and input.value
a[0] = a['power'] = n['power'].parentNode.nextElementSibling.getElementsByTagName('label')[0]
a[1] = a['SPL'] = n['SPL'].parentNode.nextElementSibling.getElementsByTagName('label')[0]
// add event handlers for the current value keeper
const c = current_value_keeper
const m = new Map // Element-Handler Map: Add event listeners.
m.set(n['sensitivity'], () => { // Here we primarily make Power fixed when Sensitivity has changed
c.SPL = c.sensitivity + 10 * Math.log10(c.power)
})
m.set(n['power'], () => { // Here we primarily make Sensitivity fixed when Power has changed
c.SPL = c.sensitivity + 10 * Math.log10(c.power)
})
m.set(n['SPL'], () => { // Here we primarily make Sensitivity fixed when SPL has changed
c.power = Math.pow(10, (c.SPL - c.sensitivity) / 10)
})
const read = () => {
c.sensitivity = parseFloat(n['sensitivity'].value)
c.power = parseFloat(n['power'].value)
c.SPL = parseFloat(n['SPL'].value)
}
const write = (skipped_inputs = new Set) => {
const v = [ c.sensitivity, c.power, c.SPL ]
output_area.innerHTML = ''
for (let i = 0; i < Object.keys(n).length / 2; ++i) {
if (!skipped_inputs.has(n[i])) {
if (!isNaN(v[i])) n[i].value = v[i]
else output_area.innerHTML += `${n[i].name} is NaN.<br>`
}
}
}
for (const [ e, h ] of m) {
e.addEventListener('input', (event) => {
read()
h()
write(new Set([ event.target ]))
})
}
m.clear()
m.set(r['unit']['dB/W@1m'], () => {
a['power'].innerHTML = 'W'
a['SPL'].innerHTML = 'dB@1m'
})
m.set(r['unit']['dB/mW'], () => {
a['power'].innerHTML = 'mW'
a['SPL'].innerHTML = 'dB'
})
for (const [ e, h ] of m) {
e.addEventListener('click', (event) => { h() })
}
// fire the corresponding event handlers and show an example of this unit converter utility
n['power'].dispatchEvent(new Event('input', { bubbles: true, cancelable: true, }))
}
}
{
class value_keeper { // instances of this class may be used for undo and redo functions in the future
sound_intensity
sound_pressure
SPL
constructor(v) {
if (v !== undefined) { // then clone from v
}
}
}
const current_value_keeper = new value_keeper
const scope = workspace[2] // get the targeted part (scope) in the unit converter console
const input = Array.from(scope.getElementsByTagName('input')) // get the inputs
const output_area = scope.getElementsByClassName('output-area')[0] // get the message output area
const number_input = {}
{
// The purpose of dual indices (compared with just number index): Lessen the modification of code when more inputs are inserted
const n = number_input
for (let i = 0; i < input.length; ++i) n[i] = n[input[i].name] = input[i]
// add event handlers for the current value keeper
const c = current_value_keeper
const m = new Map // Element-Handler Map: Add event listeners.
const Ir = 1e-12
const Pr = 20e-6
m.set(n['sound-intensity'], () => {
c.SPL = 10 * Math.log10(c.sound_intensity / Ir)
c.sound_pressure = Pr * Math.pow(10, c.SPL / 20)
})
m.set(n['sound-pressure'], () => {
c.SPL = 20 * Math.log10(c.sound_pressure / Pr)
c.sound_intensity = Ir * Math.pow(10, c.SPL / 10)
})
m.set(n['SPL'], () => {
c.sound_intensity = Ir * Math.pow(10, c.SPL / 10)
c.sound_pressure = Pr * Math.pow(10, c.SPL / 20)
})
const read = () => {
c.sound_intensity = parseFloat(n['sound-intensity'].value)
c.sound_pressure = parseFloat(n['sound-pressure'].value)
c.SPL = parseFloat(n['SPL'].value)
}
const write = (skipped_inputs = new Set) => {
const v = [ c.sound_intensity, c.sound_pressure, c.SPL ]
output_area.innerHTML = ''
for (let i = 0; i < Object.keys(n).length / 2; ++i) {
if (!skipped_inputs.has(n[i])) {
if (!isNaN(v[i])) n[i].value = v[i]
else output_area.innerHTML += `${n[i].name} is NaN.<br>`
}
}
}
for (const [ e, h ] of m) {
e.addEventListener('input', (event) => {
read()
h()
write(new Set([ event.target ]))
})
}
// fire the corresponding event handlers and show an example of this unit converter utility
n['SPL'].dispatchEvent(new Event('input', { bubbles: true, cancelable: true, }))
}
}