-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
643 lines (600 loc) · 27.6 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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<!DOCTYPE html>
<html>
<head>
<title> Synthesizing, Looking and Listening to Clicks</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/wavefile"></script>
<script src="https://cdn.jsdelivr.net/npm/wave-resampler"></script>
<script src="FFT.js"></script>
<!-- <script src="filterCodes.js"></script>
<script src="plots.js"></script>
-->
<style>
.psd_spec-container {
display: flex;
}
#PSD_plot {
flex: 1;
height: 700px;
}
#spectrogram_plot {
flex: 4;
height: 700px;
}
</style>
</head>
<body>
<label for="amplitude">Amplitude:</label>
<input type="number" id="amplitude" value="1"/>
<div id="center" style="background-color: #ffffff; width:100%;">
<label>Click period, T (microSec): </label>
<input id="T_click" style="width:5%;" value="300" type="text" onChange="runPipeline(0)"/>
</div>
<div id="center" style="background-color: #ffffff; width:100%;">
<label>Damping time constant, tau (microSec): </label>
<input id="tau" style="width:5%;" value="800" type="text" onChange="runPipeline(0)"/>
</div>
<div id="center" style="background-color: #ffffff; width:100%;">
<label>Time (microSec): </label>
<input id="T_window" style="width:20%;" value="300" type="text" onChange="runPipeline(0)"/>
</div>
<div id="center" style="background-color: #ffffff; ">
<label>Function x(t): </label>
<input id="Click_func" style="width:60%;" value="(exp(-t/tau)**2)*cos(2*PI*t/T)*sin(2*PI*t/T)" type="text"
onChange="runPipeline(0)"/>
</div>
<div id="center" style="background-color: #ffffff; ">
<label>Click separation (microSec): </label>
<input id="click_sep" style="width:5%;" value="2000" type="text" onChange="runPipeline(0)"/>
</div>
<input id="sinAmpMod" type="checkbox" onchange="runPipeline(1)">
<label>Sin modulation over 2 sec call</label>
<div id="center" style="background-color: #ffffff; width:100%;">
<select id="modulation" onchange="runPipeline(1)">
<option value="Nomod">No Call Modulation</option>
<option value="Upsweep">Call: upsweep</option>
<option value="Downsweep">Call: downsweep</option>
<option value="Excitement">Call: Excitement</option>
</select>
<label> Mod Start (Sec): </label>
<input id="modStart" style="width:2%;" value="0.5" type="text" onChange="runPipeline(1)"/>
<label> Mod Parameter: </label>
<input id="modParm" style="width:2%;" value="1.5" type="text" onChange="runPipeline(1)"/>
</div>
<div id="center" style="background-color: #ffffff; ">
<label id="label2">DEBUG</label>
<select id="DEBUG" onchange="runDEBUG()">
` <option value="none">None</option>
<option value="squareWav">Square Wave</option>
<option value="sawtoothWav">Sawtooth Wave</option>
<option value="orcaCall">Orca Call</option>`
</select>
</div>
<div id="clickPlot"></div>
<label id="label1"> Desired audio samplerate: </label>
<input id="Audio samplerate" style="width:3%;" value="48000" type="text" onchange="runPipeline(2)">
<input id="low_pass" type="checkbox" onchange="runPipeline(2)">
<label>Low Pass Filter</label>
<input id="cutoff" style="width:3%;" value="24000" type="text" onchange="runPipeline(2)">
<label>Plot t1 (millisec)</label>
<input id="t1" style="width:3%;" value="00" type="text" onchange="runPipeline(3)">
<label>Plot t2 (millisec)</label>
<input id="t2" style="width:3%;" value="20" type="text" onchange="runPipeline(3)">
<div>
<label>Message: </label>
<p id="msg"></p>
</div>
<div id="CallPlot"></div>
<div id="resampledCallPlot"></div>
<label id="label2"> Number of ms per PSD slice </label>
<input id="dt" style="width:5%;" value="50" type="text" onchange="runPipeline(4)">
<label id="label3"> Time (s) for PSD plot </label>
<input id="t_PSD" style="width:5%;" value="0.5" type="text" onchange="runPipeline(4)">
<button id="playResample">Play 2 sec of Resampled Clicks</button>
<div id="center" style="background-color: #00b5e2; width:100%;">
<select id="FFTwindow" onchange="Plot_Spectogram()">
<option value="None">FFT Window: None</option>
<option value="Cosine">FFT Window: Cosine</option>
<option value="Hanning">FFT Window: Hanning</option>
<option value="Hamming">FFT Window: Hamming</option>
<option value="Blackman">FFT Window: Blackman</option>
</select>
</div>
<input id="db_psd" type="checkbox" value="x" checked onchange="runPipeline(4)">
<label>dB PSD </label>
<input id="log_freq" type="checkbox" value="y" checked onchange="runPipeline(4)">
<label>log Frequency</label>
<div class="psd_spec-container">
<div id="PSD_plot"></div>
<div id="spectrogram_plot"></div>
</div>
<!-- <script src="script_0.js"></script>-->
<script>
/////////// GLOBAL PARAMETER(s)
// length of audio sample is 2 seconds
window.global_taudio = 2;
window.global_resampled = false; //document.getElementById("low_pass").checked
// set DEBUG default to 'none'
document.getElementById("DEBUG").value = 'none';
////// Construct the full (maybe modulated) call time series at the high sample rate (1e6) length = global_taudio
window.global_tCall = new Array(global_taudio * 1e6).fill(0);
window.global_yAmpCall = new Array(global_taudio * 1e6).fill(0);
var msg = document.getElementById('msg');
runPipeline(0);
// Event listener for the Play Resampled Clicks button
document.getElementById('playResample').addEventListener('click', playResampledClicks);
/////////////////////////////////////////////////////////////////////////
function runPipeline(iStart){
if (iStart === 0) {
msg.innerHTML = "Calculating clicks ...";
buildClicks();
plotClicks();
msg.innerHTML = "";
}
if (iStart <= 1){
msg.innerHTML = "Calculating call ...";
buildCall();
// plotCall();
msg.innerHTML = "";
}
if (iStart <= 2){
msg.innerHTML = "Resampling call ...";
window.global_audioSamplerate = parseInt(document.getElementById('Audio samplerate').value);
resampleCall();
msg.innerHTML = "";
}
if (iStart <= 3){
msg.innerHTML = "Plotting call ...";
plotCall();
plotResampledCall();
msg.innerHTML = "";
}
if (iStart <= 4){
msg.innerHTML = "Plotting spectrogram ...";
plotSpectrogram();
msg.innerHTML = "";
}
}
function buildClicks() {
var amplitude = parseFloat(document.getElementById('amplitude').value);
const funcString = document.getElementById('Click_func').value;
var T = parseFloat(document.getElementById('T_click').value);
var tau = parseFloat(document.getElementById('tau').value);
var T_window = parseFloat(document.getElementById('T_window').value);
var click_sep = parseFloat(document.getElementById('click_sep').value);
var customFunction = new Function('T', 'tau', 'exp', 'cos', 'sin', 'PI', 't', `return ${funcString}`);
var tmus = new Array(T).fill(0);
var yAmp = new Array(T).fill(0);
window.global_tmus = tmus
window.global_yAmp = yAmp
with (Math) {
for (var n = 0; n < T_window; n += 1) {
tmus[n] = n
yAmp[n] = amplitude * customFunction(T*2, tau, exp, cos, sin, PI, n);
}
// console.log(yAmp)
}
var tmus2 = new Array(2 * click_sep).fill(0);
var yAmp2 = new Array(2 * click_sep).fill(0);
window.global_tmus2 = tmus2
window.global_yAmp2 = yAmp2 // make this global so can play as a sound
with (Math) {
for (var n = 0; n < 2 * click_sep; n += 1) {
tmus2[n] = n;
yAmp2[n] = amplitude * customFunction(T*2, tau, exp, cos, sin, PI, n)
if (n > click_sep) {
yAmp2[n] = yAmp2[n] + amplitude * customFunction(T*2, tau, exp, cos, sin, PI, n - click_sep);
}
}
}
}
function buildCall() {
//////// Now build the Call
////// Construct the full (maybe modulated) call time series at the high sample rate (1e6) length = global_taudio
var callType = document.getElementById('modulation').value;
var doSinAmpMod = document.getElementById("sinAmpMod").checked;
window.global_tCall = new Array(global_taudio * 1e6).fill(0); // the HF call has time in microseconds
window.global_yAmpCall = new Array(global_taudio * 1e6).fill(0);
var lenCall = 0.8*global_yAmpCall.length;
if (callType === 'Nomod'){
var idx = 0;
for (var n = 0; n < global_yAmpCall.length; n += 1) {
global_tCall[n] = n / 1e6; // time axis in seconds for full call
global_yAmpCall[n] = global_yAmp2[idx];
idx += 1;
if (idx == global_yAmp2.length) {
idx = 0;
}
}
}
var amplitude = parseFloat(document.getElementById('amplitude').value);
const funcString = document.getElementById('Click_func').value;
var T = parseFloat(document.getElementById('T_click').value);
var tau = parseFloat(document.getElementById('tau').value);
var T_window = parseFloat(document.getElementById('T_window').value);
var click_sep = parseFloat(document.getElementById('click_sep').value);
var customFunction = new Function('T', 'tau', 'exp', 'cos', 'sin', 'PI', 't', `return ${funcString}`);
if (callType === 'Upsweep'){ // after selected time, keep decreasing the time between click pairs
const modStart = document.getElementById('modStart').value;
const modParm = document.getElementById('modParm').value;
with (Math) {
var idx = 0;
var k = 0;
for (var j = 0; j < global_yAmpCall.length; j += floor(2 * click_sep)) {
for (var i = 0; i < 2 * click_sep; i += 1) {
global_tCall[k] = (k) / 1e6; // time axis in seconds for full call
global_yAmpCall[k] = amplitude * customFunction(T*2, tau, exp, cos, sin, PI, i);
if (i > click_sep) {
global_yAmpCall[k] = global_yAmpCall[k] + amplitude * customFunction(T*2, tau, exp, cos, sin, PI, floor(i - click_sep));
}
k += 1
}
console.log(j, k, click_sep);
click_sep = click_sep * 0.999;
if (click_sep < 100){
break;
}
}
}
}
if (doSinAmpMod) { // apply sin envelope to the call
for (n = 0; n < global_yAmpCall.length; n += 1) {
if ((n > 0.1 * global_yAmpCall.length) && (n < 0.9 * global_yAmpCall.length)) {
var amp = Math.sin(Math.PI * (n - 0.1 * global_yAmpCall.length) / (lenCall/2));
global_yAmpCall[n] *= amp+Math.random()/10;
} else {
global_yAmpCall[n] = Math.random()/10;
}
}
}
}
function resampleCall(){
if (global_resampled & document.getElementById("low_pass").checked) {
var newSamples = waveResampler.resample(lowpassFunction(global_yAmpCall), 1e6, global_audioSamplerate);
} else {
var newSamples = waveResampler.resample(global_yAmpCall, 1e6, global_audioSamplerate);
}
global_resampled = true
window.global_callResampled = newSamples;
window.global_tCallResampled = new Array(global_callResampled.length).fill(0);
for (var i=0; i<global_callResampled.length; i += 1){
window.global_tCallResampled[i] = i/global_audioSamplerate
}
}
function plotClicks() {
// Create a trace for the click function
var trace1 = {
x: global_tmus,
y: global_yAmp,
type: 'scatter'
};
var trace2 = {
x: global_tmus2,
y: global_yAmp2,
xaxis: 'x2',
yaxis: 'y2',
type: 'scatter'
};
// Define the layout of the plot
var layout = {
title: 'One Click (left) and Two successive Clicks (right)',
yaxis: {
title: 'Amplitude'
},
height: 300,
xaxis: {domain: [0, 0.3], title: "Time (microsec)"},
yaxis2: {anchor: 'x2'},
xaxis2: {domain: [0.33, 1], title: "Time (microsec)"}
};
data = [trace1, trace2];
// Plot the click function using Plotly
Plotly.newPlot('clickPlot', data, layout);
}
function plotCall(){
var t1 = Math.floor(parseInt(document.getElementById('t1').value)*1e6/1000);
var t2 = Math.floor(parseInt(document.getElementById('t2').value)*1e6/1000);
console.log(t1, t2, parseInt(document.getElementById('t1').value),parseInt(document.getElementById('t2').value));
if (t2<=t1){
msg.innerHTML = "************** t2 must be greater than t1 **********";
t2 = t1 + 100 * 1e6/1000 ;
document.getElementById('t2').value = Math.floor(t2/(1e6/1000));
}
var tAxis = global_tCall.slice(t1, t2);
var yCall = global_yAmpCall.slice(t1, t2);
var trace = {
x: tAxis,
y: yCall,
type: 'scatter',
mode: 'markers', // Set mode to 'markers' to display markers
marker: {
size: 2, // Size of markers
color: 'rgb(255, 0, 0)', // Color of markers
symbol: 'circle', // Marker symbol ('circle', 'square', 'cross', etc.)
// Other marker attributes can also be set here
}
};
var layout = {
title: "Amplitude vs Time for HF Call",
height: 300,
yaxis: {
title: 'Amplitude'
},
xaxis: {
title: 'Seconds'
}
};
data = [trace]
// Plot the resampled clicks using Plotly
Plotly.newPlot('CallPlot', data, layout);
}
function plotResampledCall(){
// msg.innerHTML = "ready to plot resampled clicks " + global_callResampled.length +" " + global_audioSamplerate
const samplerate = parseInt(document.getElementById('Audio samplerate').value);
const t1 = Math.floor(parseInt(document.getElementById('t1').value)*samplerate/1000);
const t2 = Math.floor(parseInt(document.getElementById('t2').value)*samplerate/1000);
var tAxis = global_tCallResampled.slice(t1, t2);
var yCallResampled = global_callResampled.slice(t1, t2);
var traceResampled = {
x: tAxis,
y: yCallResampled,
type: 'scatter',
mode: 'lines+markers', // Set mode to 'markers' to display markers
marker: {
size: 10, // Size of markers
color: 'rgb(255, 0, 0)', // Color of markers
symbol: 'circle', // Marker symbol ('circle', 'square', 'cross', etc.)
// Other marker attributes can also be set here
}
};
var layout = {
title: "Amplitude vs Time for Resampled Call",
height: 300,
yaxis: {
title: 'Amplitude'
},
xaxis: {
title: 'Seconds'
}
};
data = [traceResampled]
// Plot the resampled clicks using Plotly
Plotly.newPlot('resampledCallPlot', data, layout);
}
function plotSpectrogram() {
console.log("new call to plotSpectrogram");
var full_x = new Array(global_callResampled.length);
full_x = [...global_callResampled]; // this is a clone using "spread syntax"
N_perSlice = Math.floor(document.getElementById("dt").value * global_audioSamplerate / 1000);
//// time for PSD plot
t_PSD = document.getElementById("t_PSD").value;
var hop = Math.floor(N_perSlice/2) // advance psd's by hop for each successive psd
Nfft = 2048
X_abs_bin = new Array(Nfft).fill(0);
n_bins = Math.floor(global_callResampled.length/hop)
bin_PSD = Math.floor((t_PSD/global_taudio)*n_bins)
var imageSpectrogram = new Array(n_bins).fill(0);
var FFTwindow = document.getElementById("FFTwindow").value;
var max_x_abs = 0
for (var bin = 0; bin < n_bins; bin += 1) {
var x_bin = new Array(Nfft).fill(0);
for (var i = 0; i < Nfft; i += 1) {
if (FFTwindow == "Cosine") {
x_bin[i] = full_x[bin * hop + i] * Math.sin(Math.PI * i / Nfft);
} else if (FFTwindow == "Hanning") {
x_bin[i] = full_x[bin * hop + i] * 0.5 * (1 - Math.cos(2 * Math.PI * i / (Nfft - 1)));
} else if (FFTwindow == "Hamming") {
x_bin[i] = full_x[bin * hop + i] * (0.53836 - .46164 * Math.cos(2 * Math.PI * i / (Nfft - 1)));
} else if (FFTwindow == "Blackman") {
x_bin[i] = full_x[bin * hop + i] * (0.42 - .5 * Math.cos(2 * Math.PI * i / (Nfft - 1)) + .08 * Math.cos(4 * Math.PI * i / (Nfft - 1)));
} else if (FFTwindow == "None") {
x_bin[i] = full_x[bin * hop + i];
}
}
fft = FFT(x_bin);
frec = new Array(Nfft / 2).fill(0);
var timeaxis = new Array(n_bins).fill(0);
my_X_abs = new Array(x_bin.length / 2).fill(0);
for (var i = 0; i < Nfft / 2; i += 1) {
if (document.getElementById("db_psd").checked) {
my_X_abs[i] = Math.log10(Math.sqrt(fft[i].re * fft[i].re + fft[i].im * fft[i].im));
} else {
my_X_abs[i] = Math.sqrt(fft[i].re * fft[i].re + fft[i].im * fft[i].im);
}
if (Math.abs(my_X_abs[i]) > max_x_abs){
max_x_abs = Math.abs(my_X_abs[i]);
}
frec[i] = i * global_audioSamplerate / Nfft;
// if (bin == 0) {
// console.log(i, frec[i], my_X_abs[i]);
// }
}
imageSpectrogram[bin] = my_X_abs ;
if (bin === bin_PSD){
var plot_PSD = my_X_abs
}
}
//console.log("in Spectrogram, maxes ", getMax(imageSpectrogram), max_x_abs)
// for (var bin = 0; bin < imageSpectrogram.length; bin += 1) {
// for (var i = 0; i < imageSpectrogram[bin].length; i += 1) {
// imageSpectrogram[bin][i] /= max_x_abs;
// }
// }
// for (var i = 0; i < imageSpectrogram[0].length; i += 1) {
// console.log(i, frec[i], imageSpectrogram[0][i])
// }
// console.log(getMax(imageSpectrogram))
var timeaxis = new Array(n_bins).fill(0);
for (var n = 0; n < n_bins; n += 1) {
timeaxis[n] = (n / n_bins) * global_taudio;
}
// Sample data for graph 1
var data1 = [{
x: frec,
y: plot_PSD,
type: 'scatter',
name: 'PSD'
}];
// Sample data for graph 2
var data2 = [{
x: timeaxis,
y: frec,
z: imageSpectrogram,
type: 'heatmap',
xtype: "array",
name: 'Spectrogram',
yaxis: 'y2'
}];
if (document.getElementById("log_freq").checked) {
// Layout for graph 1
var layout1 = {
title: 'PSD',
xaxis: {
title: 'Log Frequency',
type: 'log'
},
yaxis: {
title: 'PSD'
}
};
// Layout for graph 2
var layout2 = {
title: 'Spectrogram',
xaxis: {
title: 'Time (sed)'
},
yaxis2: {
title: 'Log Frequency (hz)',
// range: [100, 20000],
type: 'log',
overlaying: 'y',
side: 'left'
}
};
} else {
// Layout for graph 1
var layout1 = {
title: 'PSD',
xaxis: {
title: 'Frequency'
},
yaxis: {
title: 'PSD'
}
};
// Layout for graph 2
var layout2 = {
title: 'Spectrogram',
xaxis: {
title: 'Time (sec)'
},
yaxis2: {
title: 'Frequency (hz)',
// range: [100, 20000],
overlaying: 'y',
side: 'left'
}
};
}
// Plot graph 1
Plotly.newPlot('PSD_plot', data1, layout1);
// Plot graph 2
Plotly.newPlot('spectrogram_plot', data2, layout2);
var update = {
"transpose": true
};
Plotly.restyle('spectrogram_plot', update, 0);
};
function playResampledClicks(){ // Function to play audio from the array
msg.innerHTML = "Play resampled call"
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Create an empty AudioBuffer with 1 channel and desired length
var audioBuffer = audioContext.createBuffer(1, global_callResampled.length, global_audioSamplerate);
// Fill the AudioBuffer with data from ydata
var channelData = audioBuffer.getChannelData(0);
for (var i = 0; i < global_callResampled.length; i++) {
channelData[i] = global_callResampled[i];
}
// Create an AudioBufferSourceNode
var source = audioContext.createBufferSource();
source.buffer = audioBuffer;
// Connect the source to the destination (speakers)
source.connect(audioContext.destination);
// Start playing the audio
source.start();
setTimeout(clear, 2000)
};
function clear(){
msg.innerHTML = ""
}
function runDEBUG(){
dbg = document.getElementById('DEBUG').value;
if (dbg == "squareWav") {
// setup a 1 Mhz square wave and run through low pass and resample etc.
period = 1000/2; // microseconds
amp = parseFloat(document.getElementById('amplitude').value);
cnt = 0;
theAmp = amp;
for (i = 0; i < global_yAmpCall.length; i++) {
global_yAmpCall[i] = theAmp;
global_tCall[i] = i / 1e6; // time axis in seconds for full call
cnt += 1;
if (cnt === period) {
theAmp = -theAmp;
cnt = 0;
}
}
runPipeline(2)
}
if (dbg == "sawtoothWav"){
// setup a 1 Mhz square wave and run through low pass and resample etc.
period = 1000; // microseconds
amp = parseFloat(document.getElementById('amplitude').value);
cnt = 0;
theAmp = amp;
for (i=0; i<global_yAmpCall.length; i++){
global_yAmpCall[i] = theAmp * (cnt +1)/period;
global_tCall[i] = i / 1e6; // time axis in seconds for full call
cnt += 1;
if (cnt === period){
theAmp = -theAmp;
cnt = 0;
}
}
runPipeline(2)
}
if (dbg == "orcaCall"){
// load wav file into global_callResampled and set global_audioSamplerate
const wavUrl = 'S199-L-4.wav'; // replace with your WAV file URL
readWavFile(wavUrl);
runPipeline(4);
const debug = document.getElementById('DEBUG');
debug.value = 'none';
}
}
// Function to fetch and read a WAV file
async function readWavFile(url) {
try {
const response = await fetch(url);
const buffer = await response.arrayBuffer();
const dataView = new DataView(buffer);
global_callResampled = [];
// Extract sample rate from WAV header
const headerSize = 44;
const sampleRateOffset = 24;
global_audioSamplerate = dataView.getUint32(sampleRateOffset, true);
// Skip WAV header (44 bytes)
for (let i = headerSize; i < dataView.byteLength; i += 2) {
const value = dataView.getInt16(i, true); // little-endian
global_callResampled.push(value);
}
window.global_tCallResampled = new Array(global_callResampled.length).fill(0);
for (i=0; i<global_callResampled.length; i+=1){
global_tCallResampled[i] = i; // this is so can plot waveform
}
} catch (error) {
console.error('Error reading WAV file:', error);
}
}
</script>
</body>
</html>