-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
532 lines (456 loc) · 20.7 KB
/
index.html
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<!DOCTYPE html>
<html lang="en">
<head>
<title>FilterFun</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Play around with analog filters">
<meta name="keywords" content="">
<link rel="stylesheet" href="filterfun.css">
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div id="ui_div">
<div id="curves_div">
<div id="circ_div">
<canvas width=256 height=128 id="in_osc"></canvas>
<img id="circ_img" src="" title="">
<canvas width=256 height=128 id="out_osc"></canvas>
</div>
<br>
<p id="lp_formula" class="circ_formula">f<sub>cutoff</sub>=1/(2πRC)</p>
<p id="hp_formula" class="circ_formula">f<sub>cutoff</sub>=1/(2πRC)</p>
<p id="bp_formula" class="circ_formula">f<sub>center</sub>=(f<sub>lp</sub>+f<sub>hp</sub>)/2<br>Q=R*sqrt(C/L)</p>
<p id="bs_formula" class="circ_formula">f<sub>center</sub>=(f<sub>lp</sub>+f<sub>hp</sub>)/2<br>Q=(1/R)*sqrt(C/L)</p>
<p id="ls_formula" class="circ_formula"></p>
<p id="hs_formula" class="circ_formula"></p>
<p>Center- or Cutoff-Frequency: <span id="cutoff_freq_text"></span></p>
<br>
<table id="circ_params">
<tr><td><label for="r_val">Resistance:</label> </td><td><span id="r_val_text">?</span><td class="circ_param_slider"><input type="range" min="1" value="50" max="1000" step="0.1" onchange="drawCurves();" onpointerdown="mousedowns.r=true;drawCurves();" onpointerup="mousedowns.r=false;" id="r_val" autocomplete="off"></td></tr>
<tr><td><label for="c_val">Capacitance:</label> </td><td><span id="c_val_text">?</span></td></td><td class="circ_param_slider"><input type="range" min="0.000000001" value="0.000001" max="0.00001" step="0.000000001" onchange="drawCurves();" onpointerdown="mousedowns.c=true;drawCurves();" onpointerup="mousedowns.c=false;" id="c_val" autocomplete="off"></td></tr>
<tr><td><label for="l_val">Inductance:</label> </td></td><td><span id="l_val_text">?</span></td><td class="circ_param_slider"><input type="range" min="0.000000001" value="0.000001" max="0.00001" step="0.000000001" onchange="drawCurves();" onpointerdown="mousedowns.l=true;drawCurves();" onpointerup="mousedowns.l=false;" id="l_val" autocomplete="off"></tr>
</table>
<br>
<table>
<tr><td></td><td><span class="scale_left">0Hz</span><span class="scale_line"> </span><span class="scale_right"><span id="max_freq_text">?</span>Hz</span></td></tr>
<tr><td onclick="helpme('in_curve');" class="helptext">Input:</td><td><canvas id="in_fft" width=256 height=64></canvas></td></tr>
<tr><td onclick="helpme('filter_curve');" class="helptext">Filter:</td><td><canvas id="filter_curve" width=256 height=64></canvas></td></tr>
<tr><td onclick="helpme('out_curve');" class="helptext">Output:</td><td><canvas id="out_fft" width=256 height=64></canvas></td></tr>
</table>
</div>
<div id="control_panel">
<h1>FilterFun</h1>
<hr>
<div class="fullscreen_block"> <button id="start_button" onclick="ac.resume(); this.parentElement.remove();">CLICK TO START</button> <noscript><br>JS Required</noscript></div>
<p id="fps_counter">FPS: ??</p>
<hr>
<label for="filter_circ">Circuit: </label><select onchange="selCirc();" id="filter_circ" name="filter_circ" type="select" autocomplete="off">
<option value="lp">Low-pass</option>
<option value="hp">High-pass</option>
<option value="bp">Band-pass</option>
<option value="bs">Band-stop/Notch</option>
<!--<option value="ls">Low-shelf</option>
<option value="hs">High-shelf</option>-->
</select>
<!--<div id="circ_params">
<span class="option_line"><label for="r_val">Resistance: </label><input type="range" min="1" value="50" max="1000" step="0.1" onchange="drawCurves();" id="r_val" autocomplete="off"> <span id="r_val_text">?</span></span>
<br>
<span class="option_line"><label for="c_val">Capacitance: </label><input type="range" min="0.000000001" value="0.000001" max="0.00001" step="0.000000001" onchange="drawCurves();" id="c_val" autocomplete="off"> <span id="c_val_text">?</span></span>
<br>
<span class="option_line"><label for="l_val">Inductance: </label><input type="range" min="0.000000001" value="0.000001" max="0.00001" step="0.000000001" onchange="drawCurves();" id="l_val" autocomplete="off"> <span id="l_val_text">?</span></span>
<br>
</div>-->
<hr>
<span class="option_line"><label for="scope_scale">Scope X Scale: </label><input type="range" min="1" value="16" max="32" step="1" id="scope_scale" autocomplete="off"></span>
<br>
<span class="option_line"><label for="osc_trig">Trigger Scopes: </label><input type="checkbox" id="osc_trig" checked="true" autocomplete="off"></span>
<br>
<span class="option_line"><label for="osc_trig_side">Trigger Scopes on Output Wave: </label><input type="checkbox" id="osc_trig_side" autocomplete="off"></span>
<hr>
<span class="option_line"><label for="use_log_scale">Use log2 X scale: </label><input type="checkbox" id="use_log_scale" onchange="drawCurves()" autocomplete="off"></span>
<!--<span class="option_line"><label for="use_db_scale">Use dB filter curve Y scale: </label><input type="checkbox" id="use_db_scale" onchange="drawCurves()" autocomplete="off"></span>-->
<br>
<span class="option_line"><label for="fft_smoothing">FFT Smoothing: </label><input type="range" min="0.1" value="0.1" max="9" step="0.1" onchange="fft_smoothing_text.innerText=(audioAnalyzer.smoothingTimeConstant=audioAnalyzerOut.smoothingTimeConstant=Math.pow(this.value,2)/100).toFixed(2);" id="fft_smoothing" autocomplete="off"> <span id="fft_smoothing_text">?</span></span>
<hr>
<span class="option_line"><label for="audio_gain">Gain: </label><input type="range" min="0.1" value="1" max="4" step="0.1" onchange="audio_gain_text.innerText=audioGain.gain.value=this.value;" id="audio_gain" autocomplete="off"> <span id="audio_gain_text">1</span></span>
<br>
<span class="option_line"><label for="playback_vol">Playback Volume: </label><input type="range" min="0" value="1" max="2" step="0.1" onchange="playback_vol_text.innerText=audioGainO.gain.value=this.value;" id="playback_vol" autocomplete="off"> <span id="playback_vol_text">1</span></span>
<hr>
<label for="input_mode">Mode: </label><select onchange="selSource();" id="input_mode" name="input_mode" type="select" autocomplete="off">
<option value="file">File</option>
<option value="mic">Mic</option>
<option value="osc">Oscillators</option>
</select>
<br>
<div class="control_mode" id="file_control">
<input type="file" onchange="fileSel(this);" style="max-width: 20vw;" id="file_sel"><br>
<audio id="file_player" controls loop></audio><br>
<!--<span class="option_line"><label for="file_speed">Speed: </label><input type="range" min="0.01" value="1" max="2" step="0.1" id="file_speed" onchange="file_speed_text.innerText = (file_player.playbackRate=this.value) + 'x';"> <span id="file_speed_text">1x</span><button onclick="file_speed.value=1">R</button></span>-->
<p>
Sample Files:<br>
<ul>
<li>White Noise</a><wbr>
<button onclick="loadURL('sample_audio/white5s.flac')">PLAY</button></li>
<li>Pink Noise</a><wbr>
<button onclick="loadURL('sample_audio/pink5s.flac')">PLAY</button></li>
<li>Brown Noise</a><wbr>
<button onclick="loadURL('sample_audio/brown5s.flac')">PLAY</button></li>
<li>ROZKOL - Blood in a Cocktail Glass</a><wbr>
<a href="https://freemusicarchive.org/music/ROZKOL/Remnants_I_FMA_Exclusive/01_-_Blood_In_A_Cocktail_Glass/">[CC-BY]</a><br>
<button onclick="loadURL('sample_audio/rozkol_biacg.mp3')">PLAY</button></li>
</ul>
</p>
</div>
<div class="control_mode" id="mic_control">
</div>
</input>
</div>
</div>
<script>
const PI = Math.PI;
window.addEventListener("error", (e) => {
console.error(e.error);
let errorstr = "Uh oh. An error ocurred!\n"
errorstr += e.message;
errorstr += "\n\nIt ocurred in " + e.filename + "\non line " + e.lineno + ", column " + e.colno;
alert(errorstr);
});
const helptexts = {
'in_curve': "This graph shows the amplitude of frequencies in the input signal.",
'filter_curve': "This curve shows the filter response. \n\n\nThe white trace shows the behavior of the filter in real life. \n\nThe cyan trace (if present) shows how the audio filter is simulated, because JavaScript's WebAudio API can't do that type of filter perfectly.\n\nThe red vertical line shows the cutoff frequency.",
'out_curve': "This graph shows the amplitude of frequencies of the signal after it has been filtered."
};
function helpme(tag) {
alert(helptexts[tag]);
}
//canvas
const cnv_in = document.getElementById('in_fft');
const ctx_in = cnv_in.getContext('2d');
const cnv_filt = document.getElementById('filter_curve');
const ctx_filt = cnv_filt.getContext('2d');
const cnv_out = document.getElementById('out_fft');
const ctx_out = cnv_out.getContext('2d');
const cnv_osc_in = document.getElementById('in_osc');
const ctx_osc_in = cnv_osc_in.getContext('2d');
const cnv_osc_out = document.getElementById('out_osc');
const ctx_osc_out = cnv_osc_out.getContext('2d');
cnv_osc_in.imageSmoothingEnabled = ctx_osc_out.imageSmoothingEnabled = false;
ctx_osc_in.lineWidth=ctx_osc_out.lineWidth=ctx_in.lineWidth=ctx_filt.lineWidth=ctx_out.lineWidth = 1;
ctx_osc_in.strokeStyle=ctx_osc_out.strokeStyle=ctx_in.strokeStyle=ctx_filt.strokeStyle=ctx_out.strokeStyle = 'white';
ctx_osc_in.fillStyle = ctx_osc_out.fillStyle = 'black';
//audio main
const ac = new (window.AudioContext || window.webkitAudioContext)();
const max_freq = ac.sampleRate/2/2;
max_freq_text.innerText=max_freq;
const audioGain = ac.createGain();
const audioAnalyzer = ac.createAnalyser();
audioAnalyzer.fftSize=32768;
audioAnalyzer.smoothingTimeConstant = 0.01;
audioGain.connect(audioAnalyzer);
const audioFilter = ac.createBiquadFilter();
audioAnalyzer.connect(audioFilter);
const audioAnalyzerOut = ac.createAnalyser();
audioAnalyzerOut.fftSize=32768;
audioAnalyzerOut.smoothingTimeConstant = 0.01;
audioFilter.connect(audioAnalyzerOut);
const audioGainOut = ac.createGain();
audioAnalyzerOut.connect(audioGainOut);
audioGainOut.connect(ac.destination);
//audio sources
//file
const audioPlayer = document.getElementById("file_player");
const audioFileSrc = ac.createMediaElementSource(audioPlayer);
function fileSel(e) {
file_sel.style.animation="fileblink alternate 1s infinite ease-in-out";
const fR = new FileReader();
fR.addEventListener('load', (event) => {
audioPlayer.src = event.target.result;
file_sel.style.animation='initial';
audioPlayer.play();
});
fR.readAsDataURL(e.files[0]);
}
function loadURL(audio_url) {
audioPlayer.src = audio_url;
audioPlayer.play();
}
//mic
var audioMic;
function getMic() {
try {audioMic.disconnect();} catch {}
navigator.mediaDevices.getUserMedia({audio:{
echoCancellation: false,
autoGainControl: false,
noiseSuppression: false,
channelCount: 2
}})
.then((stream) => {
audioMic = ac.createMediaStreamSource(stream);
audioMic.connect(audioGain);
})
.catch((err) => {
alert(err);
});
}
if (typeof file_sel.files[0] != 'undefined') if (file_sel.files[0].name.length > 0) fileSel(file_sel); //if a file is autocompleted into the file field, load it
//stuff
function mapfloat(x, in_min, in_max, out_min, out_max) {//arduino style map()
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
function selSource() {
audioFileSrc.disconnect();
try {audioMic.disconnect();} catch {}
file_control.style.display="none";
mic_control.style.display="none";
switch (input_mode.value) {
case "file":
file_control.style.display="inline-block";
audioFileSrc.connect(audioGain);
break;
case "mic":
mic_control.style.display="inline-block";
getMic();
break;
}
}
selSource();
function pythag_hyp(a,b) {return Math.sqrt(Math.pow(a,2) + Math.pow(b,2));}
function pythag(a,b) {return Math.sqrt(Math.pow(a,2) + Math.pow(b,2));}
//circ implementations
const filters = { //TODO: Make attenuation scales dB
'lp': {
mode: 'lowpass',
atten_f: function(f,r,c,l){
let Xc = 1/(2*PI*f*c);
let Z = pythag_hyp(r,Xc);
return (1/Z /*<-- I*/) * Xc; //falls from 1 to 0
},
cutoff: function(r,c,l){return 1/(2*PI*r*c);},
img: "circ_img/rc_lp.png"
},
'hp': {
mode: 'highpass',
atten_f: function(f,r,c,l){
let Xc = 1/(2*PI*f*c);
let Z = pythag(Xc,r);
return (1/Z /*<-- I*/) * r;
},
cutoff: function(r,c,l){return 1/(2*PI*r*c);},
img: "circ_img/rc_hp.png"
},
'lp_l': { //does not really work IRL
mode: 'lowpass',
atten_f: function(f,r,c,l){
let Xl = 2*PI*f*l;
let Z = pythag_hyp(r,Xl);
return (1/Z) * Xl; //falls from 1 to 0
},
cutoff: function(r,c,l){return r/(2*PI*l);},
img: "circ_img/rl_lp.png"
},
'hp_l': {
mode: 'highpass',
atten_f: function(f,r,c,l){
let Xl = 2*PI*f*l;
let Z = pythag(Xl,r);
return (1/Z /*<-- I*/) * r;
},
cutoff: function(r,c,l){return r/(2*PI*l);},
img: "circ_img/rl_hp.png"
},
'bp': {
mode: 'bandpass',
atten_f: function(f,r,c,l){return 0;},
cutoff: function(r,c,l){return Math.sqrt(filters.hp_l.cutoff(r,c,l)*filters.lp.cutoff(r,c,l));},
q_factor: function(r,c,l){return r*Math.sqrt(c/l);},
img: "circ_img/rcl_bp.png"
},
'bs': {
mode: 'notch',
atten_f: function(f,r,c,l){return 0;},
cutoff: function(r,c,l){return Math.sqrt(filters.lp_l.cutoff(r,c,l)*filters.hp.cutoff(r,c,l));},
q_factor: function(r,c,l){return (1/r)*Math.sqrt(l/c)},
img: "circ_img/rcl_n.png"
},
'ls': {
mode: 'lowshelf',
atten_f: function(f,r,c,l){return 0;},
cutoff: function(r,c,l){return 0;},
img: "circ_img/ls.png"
},
'hs': {
mode: 'highshelf',
atten_f: function(f,r,c,l){return 0;},
cutoff: function(r,c,l){return 0;},
img: "circ_img/hs.png"
}
};
var mousedowns = {'r':false,'c':false,'l':false,};
function drawCurves() {
r_val_text.innerText = (r_val.value * 1).toFixed(2) + "Ω";
c_val_text.innerText = (c_val.value * 1E6).toFixed(2) + "µF";
l_val_text.innerText = (l_val.value * 1E6).toFixed(2) + "µH";
audioFilter.Q.value = 0;
switch (filter_circ.value) {
case "hp_l":
l_val.disabled=false;
case "lp":
case "hp":
l_val.disabled=true;
c_val.max=l_val.max=0.00001;
c_val.min=l_val.min=0.000000001;
r_val.max=1000;
break;
case "bp":
case "bs":
l_val.disabled=false;
c_val.max=l_val.max=0.0001;
c_val.min=l_val.min=0.00001;
r_val.max=100;
audioFilter.Q.value = filters[filter_circ.value].q_factor(r_val.value,c_val.value,l_val.value);
break;
case "ls":
break;
case "hs":
break;
}
cutoff_freq_text.innerText = (audioFilter.frequency.value = filters[filter_circ.value].cutoff(r_val.value,c_val.value,l_val.value)).toFixed(2) + 'Hz';
ctx_filt.fillStyle = 'black';
ctx_filt.fillRect(0,0,cnv_in.width,cnv_in.height);
let scale_fac = (cnv_out.width * Math.log(2)) / Math.log(cnv_out.width); //log2(w)*x = w
ctx_filt.fillStyle = 'black';
let freqs = new Float32Array(cnv_filt.width);
let atten = new Float32Array(cnv_filt.width);
let phase = new Float32Array(cnv_filt.width);
let hz_per_px = max_freq / cnv_filt.width;
for (i=0;i<freqs.length;i++) {
freqs[i] = i * hz_per_px;
}
audioFilter.getFrequencyResponse(freqs,atten,phase);
ctx_filt.strokeStyle = 'cyan';
ctx_filt.beginPath();
for (let i = 0; i < cnv_filt.width; i++) {
let y = cnv_filt.height - (atten[i] * cnv_filt.height * 0.85);
if (i == 0) ctx_filt.moveTo(0,y);
else ctx_filt.lineTo(use_log_scale.checked ? Math.log2(i)*scale_fac : i, y);
}
ctx_filt.stroke();
ctx_filt.strokeStyle = 'white';
ctx_filt.beginPath();
for (let f = 0; f < cnv_filt.width; f++) {
let a = filters[filter_circ.value].atten_f(f*hz_per_px, r_val.value, c_val.value, l_val.value);
//if (use_db_scale.checked) a = -10 * Math.log10(1/a);
let y = cnv_filt.height - a * cnv_filt.height * 0.85;
if (f == 0) ctx_filt.moveTo(0,y);
else ctx_filt.lineTo(use_log_scale.checked ? Math.log2(f)*scale_fac : f ,y);
}
ctx_filt.stroke();
ctx_filt.strokeStyle = 'red';
ctx_filt.beginPath();
ctx_filt.moveTo(use_log_scale.checked ? Math.log2(audioFilter.frequency.value/hz_per_px)*scale_fac : audioFilter.frequency.value/hz_per_px,0);
ctx_filt.lineTo(use_log_scale.checked ? Math.log2(audioFilter.frequency.value/hz_per_px)*scale_fac : audioFilter.frequency.value/hz_per_px, cnv_filt.height);
ctx_filt.stroke();
ctx_filt.strokeStyle = 'white';
if (mousedowns.r || mousedowns.c || mousedowns.l) requestAnimationFrame(drawCurves);
}
function resChange() {
cnv_in.width = cnv_filt.width = cnv_out.width = curves_div.clientWidth - document.getElementsByClassName("helptext")[0].clientWidth*2;
cnv_in.height = cnv_filt.height = cnv_out.height = Math.floor(curves_div.clientHeight / 4);
/*cnv_in.width = cnv_filt.width = cnv_out.width = cnv_out.clientWidth;
cnv_in.height = cnv_filt.height = cnv_out.height = cnv_out.clientHeight;*/
/*circ_div.style.height=(cnv_osc_in.height=cnv_osc_out.height=circ_img.height=(curves_div.clientHeight/4)-((curves_div.clientHeight/4)%128))+"px";
cnv_osc_in.width=cnv_osc_out.width=cnv_osc_in.height*2;
osc_scale.max=*/
drawCurves();
}
resChange();
window.addEventListener('resize', resChange);
//sel curve
const circ_formulas = [lp_formula,hp_formula,bp_formula,bs_formula];
function selCirc() {
circ_formulas.forEach(x=>{x.style.display='none';});
document.getElementById(filter_circ.value + '_formula').style.display = 'initial';
//change filter
audioFilter.type = filters[filter_circ.value].mode;
//change img etc
circ_img.src = filters[filter_circ.value].img;
drawCurves();
}
selCirc();
//draw FFT
var fft_raf;
var frames_this_sec = 0;
function drawFFT() {
ctx_in.fillStyle = ctx_out.fillStyle = 'black';
ctx_in.fillRect(0,0,cnv_in.width,cnv_in.height);
ctx_out.fillRect(0,0,cnv_out.width,cnv_out.height);
ctx_osc_in.fillRect(0,0,cnv_osc_in.width,cnv_osc_in.height);
ctx_osc_out.fillRect(0,0,cnv_osc_out.width,cnv_osc_out.height);
ctx_in.strokeStyle = ctx_out.strokeStyle = 'white';
let scale_fac = cnv_out.width / (use_log_scale.checked ? Math.log2(audioAnalyzerOut.frequencyBinCount/2) : audioAnalyzerOut.frequencyBinCount/2);
let in_data = new Uint8Array(audioAnalyzer.frequencyBinCount);
audioAnalyzer.getByteFrequencyData(in_data);
let out_data = new Uint8Array(audioAnalyzerOut.frequencyBinCount);
audioAnalyzerOut.getByteFrequencyData(out_data);
let in_osc_data = new Uint8Array(audioAnalyzer.frequencyBinCount);
audioAnalyzer.getByteTimeDomainData(in_osc_data);
let out_osc_data = new Uint8Array(audioAnalyzerOut.frequencyBinCount);
audioAnalyzerOut.getByteTimeDomainData(out_osc_data);
ctx_in.beginPath();
for (let i=0; i<audioAnalyzer.frequencyBinCount; i++){
let y = mapfloat(in_data[i], 0, 255, cnv_in.height, 0);
if (i == 0) ctx_in.moveTo(0, y);
else ctx_in.lineTo((use_log_scale.checked ? Math.log2(i)*scale_fac : i*scale_fac), y);
}
ctx_in.stroke();
ctx_out.beginPath();
for (let i=0; i<audioAnalyzerOut.frequencyBinCount; i++){
let y = mapfloat(out_data[i], 0, 255, cnv_out.height, 0)
if (i == 0) ctx_out.moveTo(0, y);
else ctx_out.lineTo((use_log_scale.checked ? Math.log2(i)*scale_fac : i*scale_fac), y);
}
ctx_out.stroke();
//oscilloscopes
let osc_scale = scope_scale.value;
//let osc_scale = (audioAnalyzerOut.frequencyBinCount/cnv_osc_out.width)/2;
let max_x=0;
if (osc_trig.checked) { //if supposed to trigger
let max_y=0;
let t = osc_trig_side.checked;
for (let i=audioAnalyzer.frequencyBinCount-(cnv_osc_out.width*osc_scale)-1024 /*look only in the last 1024 samples*/; i<audioAnalyzer.frequencyBinCount-(cnv_osc_out.width*osc_scale); i++){ //find high spot
if((t ? out_osc_data[i] : in_osc_data[i]) >= max_y) {
max_y = t ? out_osc_data[i] : in_osc_data[i];
max_x=i;
}
}
max_x=Math.min(max_x, (audioAnalyzer.frequencyBinCount-1)-cnv_osc_in.width);
}
else max_x = audioAnalyzer.frequencyBinCount-(cnv_osc_out.width*osc_scale); //else just use the last samples in buffer
ctx_osc_in.beginPath();
for (let x=0; x<cnv_osc_in.width; x++){
let y = mapfloat(in_osc_data[(x*osc_scale)+max_x], 0, 255, cnv_osc_in.height-1, 0);
//if ((cnv_osc_in.width)-x < 16) y = ((y+128)*(((cnv_osc_in.width)-x+1)/16))-128; //"suck in effect"
if (x == 0) ctx_osc_in.moveTo(0, y);
else ctx_osc_in.lineTo(x, y);
}
ctx_osc_in.stroke();
ctx_osc_out.beginPath();
for (let x=0; x<cnv_osc_out.width; x++){
let y = mapfloat(out_osc_data[(x*osc_scale)+max_x], 0, 255, cnv_osc_out.height-1, 0);
//if (x < 16) y = ((y128)*(x+1)/16); //"expand out effect"
if (x == 0) ctx_osc_out.moveTo(0, y);
else ctx_osc_out.lineTo(x, y);
}
ctx_osc_out.stroke();
frames_this_sec++;
fft_raf = requestAnimationFrame(drawFFT);
}
drawFFT();
setInterval(function(){
fps_counter.innerText = "FPS: " + frames_this_sec;
fps_counter.style.color = "hsl(" + Math.min(Math.max(mapfloat(frames_this_sec, 20, 60, 0, 180), 0), 180) + ", 100%, 50%)";
frames_this_sec=0;
},1000);
</script>
</body>