-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiotools.js
812 lines (724 loc) · 25.6 KB
/
audiotools.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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* Audio tools by DrSnuggles
License : WTFPL 2.0, Beerware Revision 42
*/
"use strict";
var ATools = (function (my) {
//
// Init
//
var my = { // public available settings
ana: [], // Analyser Nodes
pos: 0
},
fft = 2048, // fft Size
ctx, // Audio context
octx, // new try with another offline context
source, // SourceBufferNode
splitter, // Splitter
isLoading = false, // avoid multiple starts, still possible via DND ;)
// new for waveform
raf = requestAnimationFrame(renderLoop), // ReqAnimFrame
sbuf, // decodec SourceBufferArray
canvas,
cctx, // canvas 2D context
width = screen.width, // waveform
height = screen.height/2,// waveform
wave, // this is a save of generated waveform
halfRange = false, // if true only upper half (0.5..1.0) of waveform is displayed
startOffset = 0, // where are we pos in audio
startTime = 0,
duration = 0,
channels,
sampleRate,
impulseResponseBuffer,
integratedLoudness,
waveDrS,
loudness,
psr,
tpeak,
pos,
posF,
max_true_peak,
numWorkers = 0,
readyWorkers = 0,
workerProgress = [0, 0, 0],
waveType = 0, // DrS, Loudness, PSR
data = [], // really needed????? does getChannelData take long????
dataTP = [],
dataFS = [],
fAna = {}, // file Analyser, looks like i need something like that
// finally display console logs?
debug = false;
//
// Private
//
function log(out) {
if (debug) console.log("ATools:", out);
if (document.getElementById("stat")) document.getElementById("stat").innerText = out;
};
function renderLoop() {
raf = requestAnimationFrame(renderLoop);
// info (memory+clock)
var mem = my.formatBytes(performance.memory.usedJSHeapSize);// +' / '+ my.formatBytes(performance.memory.totalJSHeapSize) +' / '+ my.formatBytes(performance.memory.jsHeapSizeLimit);
var now = new Date();
now = now.toLocaleTimeString();
info.innerHTML = now +'<br/>'+ mem;
// not running --> exit
if (typeof wave === "undefined") return;
// clear old
cctx.clearRect(0, 0, width, height);
// draw bg = previously created wave
cctx.putImageData(wave, 0, 0, 0, 0, wave.width, wave.height);
// draw position
if (source.buffer) {
pos = source.context.currentTime - startTime + startOffset;
my.pos = Math.floor(pos * sampleRate);
posF = ((pos) / duration);
cctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
cctx.fillRect(0, 0, posF*width, height);
// audio info (samplerate pos/len)
if (pos > duration) pos = duration;
ainfo.innerHTML = formatTime(pos) +' / '+ formatTime(duration) +'<br/>'+ my.ana.length +'ch @'+fAna.srate/1000 +'kHz -> '+source.buffer.sampleRate/1000 +'kHz';
}
};
function formatTime(s) {
return new Date(1000 * s).toISOString().substr(11, 8) + (s % 1).toFixed(2).substr(1);
};
function loadConvolver() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'audio/3sec-1-mono_44100.wav', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
ctx.decodeAudioData(xhr.response, function(buf) {
impulseResponseBuffer = buf;
}, function(e){
log("Convolver problem");
});
}
xhr.send();
};
function deepAnal(buf) {
log("deepAnal");
// the resampled area
channels = buf.numberOfChannels;
sampleRate = buf.sampleRate; // this is the already resampled rate !!!
// save resampled buffer per channel
data = [];
for (let i = 0; i < channels; i++) {
data.push( buf.getChannelData(i) );
}
duration = data[0].length / sampleRate;
readyWorkers = numWorkers = 0;
calc_IL(buf);
calc_loudness(buf);
// own offline context with original data not resampled
octx = new OfflineAudioContext(channels, duration * fAna.srate, fAna.srate);
var source = octx.createBufferSource();
source.buffer = buf;
source.connect(octx.destination);
source.start();
octx.startRendering().then(function(renderedBuffer) {
log('OCTX Rendering completed successfully');
//log(renderedBuffer);
data = [];
dataFS = [];
for (let i = 0; i < channels; i++) {
data.push( renderedBuffer.getChannelData(i) );
dataFS.push([]);
}
//duration = data[0].length / srate;
//var millis = Math.ceil(duration * 1000);
var framesize = fAna.srate / 100; // 44100 / 100 = 441
//console.log(data[0].length, framesize, data[0].length/framesize);
var framecount = 0;
var framesum = new Array(channels).fill(0);
var res = []; // result array for the drawwave peak diagram
var peak;
var chPeaks = new Array(channels).fill(0);
var chMin = new Array(channels).fill(+Infinity);
var chMax = new Array(channels).fill(-Infinity);
//console.log(dataFS);
for (var i = 0; i < data[0].length; i++) {
peak = 0;
for (var j = 0; j < channels; j++) {
var val = Math.abs(data[j][i]);
framesum[j] += val;
if (val >= 1.0) chPeaks[j] += 1;
if (val < chMin[j]) chMin[j] = val;
if (val > chMax[j]) chMax[j] = val;
peak = Math.max(peak, val);
}
res.push( peak );
framecount++;
if (framecount > framesize) {
//console.log(framecount, i, framesum[0]/framesize, framesum[1]/framesize);
for (var j = 0; j < channels; j++) {
//console.log(j, framesum[j]/framesize);
dataFS[j].push(framesum[j]/framesize);
framesum[j] = 0;
}
framecount = 0;
}
}
//console.log(i, i/framesize);
for (var i = 0; i < channels; i++) {
console.info("Channel "+ i +" found Min="+ chMin[i] +" = "+ absoluteValueToDBFS(chMin[i]).toFixed(2) +"dB Max="+ chMax[i] + " = "+ absoluteValueToDBFS(chMax[i]).toFixed(2) +"dB #Peaks="+ chPeaks[i]);
}
// make wave data smaller
// give each pixel 2 datas
var group = Math.floor( res.length / width );
log("group width: "+ group);
var res2 = [];
for (let i = 0; i < width; i++) {
var maxi = 0;
for (let j = group*i; j < group*(i+1); j++) {
if (res[j] > maxi) maxi = res[j];
}
res2.push(maxi);
}
waveDrS = res2;
drawWave(res2);
playAudio(sbuf, 0);
});
// try peak first, we will need it
// worker are not effective since we want to wait to have all that values
// will resample up to max but 4x is not guaranteed
// and we have to do this for each channel
//calc_tpeak_local(); // since this oversamples to max 192000 it takes most time
//calc_tpeak(buf);
// should also be a worker
// since we are here more interested in finding peaks i decided to make one single waveform
// with max of absolute values of all channels. never seen before :)
// i have values here which are out of bounds / of spec
// https://stackoverflow.com/questions/51252732/javascript-getchanneldata-some-out-of-bounds
// not 100% sure but think this is caused by some 32bit float conversion ? also not sure what browsers are doing. clamp??
// i will keep them and get used to values up to +6dbFS
// https://stackoverflow.com/questions/58136614/how-to-get-channel-data-directly-from-arraybuffer-without-web-audio-api
// OK, getChannelData respectively decodeAudio provides me with already resampled data (depends on my Win settings)
// thats not exactely what i wanted....
// all sample rate conversion is then not ideal since it doubles resampleing and failures
// shows how to fetch just a range of data: https://stackoverflow.com/questions/33428990/determining-the-sample-rate-of-a-large-audio-file-in-javascript
// https://github.com/WebAudio/web-audio-api/issues/30
// YES YES YES.. i have filesize, channels and duration -> samplerate for WAVs only
// for MP3, FLAC, OGG maybe i will find in TAGs
};
function calc_IL(buf) {
/* INTEGRATED LOUDNESS */
//get an audioBuffer, in which EBU-S values are stored
//do not resample
var targetSampleRate = sampleRate;
var OAC_IL = new OfflineAudioContext(channels, duration * targetSampleRate, targetSampleRate);
var source = OAC_IL.createBufferSource();
source.buffer = buf;
var splitter = OAC_IL.createChannelSplitter(channels);
var merger = OAC_IL.createChannelMerger(channels);
//first stage shelving filter
var highshelf_filter = OAC_IL.createBiquadFilter();
highshelf_filter.type = "highshelf";
highshelf_filter.Q.value = 1;
highshelf_filter.frequency.value = 1500;
highshelf_filter.gain.value = 4;
// second stage highpass filter
var highpass_filter = OAC_IL.createBiquadFilter();
highpass_filter.frequency.value = 76;
highpass_filter.Q.value = 1;
highpass_filter.type = "highpass";
//SQUARING EVERY CHANNEL
var square_gain = OAC_IL.createGain();
square_gain.gain.value = 0;
//CONNECTING EBU GRAPH
source
.connect(highshelf_filter)
.connect(highpass_filter)
.connect(square_gain);
highpass_filter.connect(square_gain.gain);
square_gain.connect(OAC_IL.destination);
source.start();
OAC_IL.startRendering().then(function(renderedBuffer) {
log('IL Rendering completed successfully');
var signal_filtered_squared = [];
for (let i = 0; i < channels; i++) {
signal_filtered_squared.push( renderedBuffer.getChannelData(i) );
}
var il_worker = new Worker("workers/integrated-loudness.js");
//compute integrated loudness
il_worker.postMessage({
buffers: signal_filtered_squared,
duration: duration
});
numWorkers++;
il_worker.onmessage = function(e) {
var data = e.data;
if (data.type == "finished"){
log("ILW finished! Integrated loudness: " + data.integratedLoudness + " LUFS");
integratedLoudness = data.integratedLoudness;
readyWorkers++;
}
if (data.type == "progress"){
workerProgress[0] = data.progress;
logProgress();
}
}
});
}; // worker version
function calc_loudness(buf) {
//do not resample
var targetSampleRate = sampleRate;
var OAC = new OfflineAudioContext(channels, duration * targetSampleRate, targetSampleRate);
var source = OAC.createBufferSource();
source.buffer = buf;
var ebu_splitter = OAC.createChannelSplitter(2);
//first stage shelving filter
var highshelf_filter_L = OAC.createBiquadFilter();
highshelf_filter_L.type = "highshelf";
highshelf_filter_L.Q.value = 1;
highshelf_filter_L.frequency.value = 1500;
highshelf_filter_L.gain.value = 4;
var highshelf_filter_R = OAC.createBiquadFilter();
highshelf_filter_R.type = "highshelf";
highshelf_filter_R.Q.value = 1;
highshelf_filter_R.frequency.value = 1500; //deduced with IIRFilter.getFrequencyResponse
highshelf_filter_R.gain.value = 4;
// second stage highpass filter
var highpass_filter_L = OAC.createBiquadFilter();
highpass_filter_L.frequency.value = 76;
highpass_filter_L.Q.value = 1;
highpass_filter_L.type = "highpass";
var highpass_filter_R = OAC.createBiquadFilter();
highpass_filter_R.frequency.value = 76;
highpass_filter_R.Q.value = 1;
highpass_filter_R.type = "highpass";
//SQUARING EVERY CHANNEL
var ebu_square_gain_L = OAC.createGain();
ebu_square_gain_L.gain.value = 0;
var ebu_square_gain_R = OAC.createGain();
ebu_square_gain_R.gain.value = 0;
var ebu_convolver_L = OAC.createConvolver();
ebu_convolver_L.normalize = false;
var ebu_convolver_R = OAC.createConvolver();
ebu_convolver_R.normalize = false;
ebu_convolver_L.buffer = impulseResponseBuffer;
ebu_convolver_R.buffer = impulseResponseBuffer;
var ebu_mean_gain_L = OAC.createGain();
ebu_mean_gain_L.gain.value = 1/(OAC.sampleRate * 3);
var ebu_mean_gain_R = OAC.createGain();
ebu_mean_gain_R.gain.value = 1/(OAC.sampleRate * 3);
var ebu_channel_summing_gain = OAC.createGain();
var ebu_s_analyzer = OAC.createAnalyser();
ebu_s_analyzer.fftSize = 2048;
//CONNECTING EBU GRAPH
source.connect(ebu_splitter);
ebu_splitter.connect(highshelf_filter_L, 0, 0);
ebu_splitter.connect(highshelf_filter_R, 1, 0);
highshelf_filter_L.connect(highpass_filter_L);
highshelf_filter_R.connect(highpass_filter_R);
highpass_filter_L.connect(ebu_square_gain_L);
highpass_filter_L.connect(ebu_square_gain_L.gain);
highpass_filter_R.connect(ebu_square_gain_R);
highpass_filter_R.connect(ebu_square_gain_R.gain);
ebu_square_gain_L.connect(ebu_convolver_L).connect(ebu_mean_gain_L);
ebu_square_gain_R.connect(ebu_convolver_R).connect(ebu_mean_gain_R);
ebu_mean_gain_L.connect(ebu_channel_summing_gain);
ebu_mean_gain_R.connect(ebu_channel_summing_gain);
ebu_channel_summing_gain.connect(OAC.destination);
source.start();
OAC.startRendering().then(function(renderedBuffer) {
log('Loudness Rendering completed successfully');
var ebu_buffer = renderedBuffer.getChannelData(0);
var worker = new Worker("workers/loudness.js");
worker.postMessage({
ebu_buffer: ebu_buffer,
untouched_buffers: data,
width: width
});
numWorkers++;
log('Data to analyse posted to worker');
worker.onmessage = function(e) {
var data = e.data;
if (data.type == "finished"){
log('Message received from loudness worker');
loudness = data.loudness;
psr = data.psr;
//console.log(psr);
//drawLoudnessDiagram(loudness);
//drawPSRDiagram(psr);
readyWorkers++;
}
if (data.type == "progress"){
workerProgress[1] = data.progress;
logProgress();
}
};
});
}; // worker version
function calc_tpeak(buf) {
//True-peak context
//oversample to 192000 Hz
var targetSampleRate = 192000;
var OAC_TP = new OfflineAudioContext(channels, duration * targetSampleRate, targetSampleRate);
var source = OAC_TP.createBufferSource();
source.buffer = buf;
source.start();
var gain = OAC_TP.createGain();
gain.gain.value = 0.5;
var lp_filter = OAC_TP.createBiquadFilter()
lp_filter.type = "lowpass";
lp_filter.frequency.value = 20000;
source.connect(gain).connect(lp_filter).connect(OAC_TP.destination);
OAC_TP.startRendering().then(function(renderedBuffer) {
log('OAC_TP rendering completed successfully');
var buffers = [renderedBuffer.getChannelData(0)];
if (renderedBuffer.channelCount > 1){
buffers.push(renderedBuffer.getChannelData(1));
}
var worker = new Worker("workers/true-peak.js");
worker.postMessage({
buffers: buffers,
width: width
});
numWorkers++;
log('True peak: Data to analyse posted to worker');
worker.onmessage = function(e) {
var data = e.data;
if (data.type == "finished"){
log('True peak: Worker has finished');
log("Maximum detected value: " + data.max + " dBTP");
max_true_peak = data.max;
tpeak = data.true_peak;
}
if (data.type == "progress"){
workerProgress[2] = data.progress;
logProgress();
}
};
});
}; // worker version
function calc_tpeak_local() {
//True-peak context
//oversample to 192000 Hz
//^^ thats max but 4x is not guaranteed
var targetSampleRate = sampleRate;// 192000;
var OAC_TP = new OfflineAudioContext(channels, duration * targetSampleRate, targetSampleRate);
var source = OAC_TP.createBufferSource();
source.buffer = sbuf;
source.start();
var gain = OAC_TP.createGain();
gain.gain.value = 0.5;
var lp_filter = OAC_TP.createBiquadFilter()
lp_filter.type = "lowpass";
lp_filter.frequency.value = 20000;
source.connect(gain).connect(lp_filter).connect(OAC_TP.destination);
OAC_TP.startRendering().then(function(renderedBuffer) {
log('OAC_TP rendering completed successfully');
var data_tp = [];
for (let c = 0; c < channels; c++) {
data_tp.push( renderedBuffer.getChannelData(c) );
}
var restoreGain = 20 * Math.log10(1/0.5); // compensate Gain = 50% with ~+6dB
dataTP = []; // output
// let's calculate a true peak value for each channel !!
for (var c = 0; c < channels; c++) {
log("Start analysing channel "+ c);
dataTP.push( [] );
// and for each sample
for (var i = 0; i < data_tp[0].length; i++) {
var value_db = absoluteValueToDBFS(Math.abs(data_tp[c][i])) + restoreGain; //restore original gain
dataTP[c][i] = value_db;
if (isNaN(value_db)) {
log("Sample "+ i +" channel "+ c + " = "+ value_db + " data: "+ data[c][i] + " DBFS "+ absoluteValueToDBFS(data[c][i]));
} else {
if (i % (10*sampleRate) == 0)
log("Channel "+ c +" Progress "+ (i/data_tp[0].length*100).toFixed(3) +"%");
}
}
}
log("Calc finished");
//log(dataTP);
//playAudio(sbuf, 0);
});
};
function absoluteValueToDBFS(value){
var ret = 20 * Math.log10(value);
ret = Math.max(ret, -180);
return ret;
}
function drawWave(buf) {
log("drawWave");
//screen.availWidth * 2;
// now draw them all, quite too much... i know
// to have better resolution in peak are i dismiss lower half of val range
cctx.lineWidth = 1;
cctx.strokeStyle = 'rgba(255, 255, 255, 1.0)';
cctx.clearRect(0, 0, width, height);
cctx.beginPath();
cctx.moveTo(0, (1-buf[0])* height);
for (var i = 1; i < buf.length; i++) {
if (halfRange) {
cctx.lineTo(i/buf.length*width, (1-(buf[i]-0.5)*2)*height); // half range
} else {
cctx.lineTo(i/buf.length*width, (1-buf[i])*height); // full range
}
}
cctx.stroke();
// now save this for later reuse
waveType = 0;
wave = cctx.getImageData(0, 0, width, height);
};
function drawLoudnessDiagram(vals) {
cctx.clearRect(0, 0, width, height);
//calculate RMS for every pixel
cctx.lineWidth = 1;
cctx.strokeStyle = 'rgba(255, 255, 255, 1.0)';
for (var i = 0; i < width; i++){
cctx.beginPath();
//typical crest factors are -20 to -3 dbFS
var lineHeight = height * ((vals[i] + 30) / 30);
cctx.moveTo(i, height);
cctx.lineTo(i, height - lineHeight);
cctx.stroke();
}
// now save this for later reuse
waveType = 1;
wave = cctx.getImageData(0, 0, width, height);
};
function drawPSRDiagram(vals) {
cctx.clearRect(0, 0, width, height);
//calculate RMS for every pixel
for (var i = 0; i < width; i++){
cctx.beginPath();
cctx.lineWidth = 1;
var psr_value = vals[i];
cctx.strokeStyle = getPSRColor(psr_value);
//typical values in pop music are 20 to 3 LU
var lineHeight = height * ((psr_value - 2) / 17);
cctx.moveTo(i, height);
cctx.lineTo(i, height - lineHeight);
cctx.stroke();
}
// now save this for later reuse
waveType = 2;
wave = cctx.getImageData(0, 0, width, height);
};
function getPSRColor(psr_value) {
var color;
if (psr_value < 5){
color = '#000000'; //black
} else if (psr_value < 6){
color = '#770000'; //dark red
} else if (psr_value < 7){
color = '#ff0000'; //red
} else if (psr_value < 7.5){
color = '#ff4500'; //orangered
} else if (psr_value < 8){
color = '#ffa500'; //orange
} else if (psr_value < 8.5){
color = '#ffc500'; //brighter orange
} else if (psr_value < 9.5){
color = '#ffff00'; //yellow
} else if (psr_value < 11){
color = '#b4ff00'; //yellow green
} else {
color = '#00ff00'; //lime green
}
return color;
};
function logProgress() {
/*
var total = 0;
for (let i = 0; i < workerProgress.length; i++) {
total += workerProgress[i];
}
total = total / workerProgress.length;
log("Workers Progress: "+ total.toFixed(1) +"%");
*/
};
function getLoudness() {
return Math.round(10 * (loudness[Math.round(posF * loudness.length)])) / 10;
};
function getPSR() {
return Math.round(10 * psr[Math.round(posF * psr.length)]) / 10;
};
function getDBTP(c) {
//if (typeof tpeak == "undefined" || posF >= 1) return -200;
//return Math.round(10 * tpeak[Math.round(posF * tpeak.length)]) / 10; // old method via worker
if (dataTP.length === 0) return -120;
var x = Math.floor(dataTP[0].length/data[0].length * pos * sampleRate) - 1;
return dataTP[c][x];
};
function getDBFS(c) {
// idea is to have a db value for each 1/100 second
var x = Math.floor(pos * 100) - 1;
if (dataFS.length === 0 || typeof dataFS[c] == "undefined" ) return 1/1000000000; // -180db
//console.log(dataFS[c][x], typeof dataFS[c][x]);
return dataFS[c][x];
};
function jumpTo(e) { // LMB on waveform
pos = Math.floor(e.offsetX / canvas.clientWidth * duration);
try {
my.stopAudio();
} catch(e){
// was stopped
}
// have to reinit audio
splitAudio(sbuf, pos);
};
function changeWaveType(e) { // RMB on waveform
e.preventDefault();
waveType = (waveType+1) % 3;
switch (waveType) {
case 0:
my.drawWave(waveDrS);
break;
case 1:
my.drawLoudnessDiagram(loudness);
break;
case 2:
my.drawPSRDiagram(psr);
break;
default:
}
};
function splitAudio(buf, pos) {
log("Loaded audio will be played back with "+ buf.numberOfChannels +" channels @"+ buf.sampleRate/1000.0 +"kHz");
log("Destination supports up to "+ ctx.destination.maxChannelCount +" channels");
var jumped = true;
if (sbuf !== buf) {
log("Buffer changed");
sbuf = buf;
deepAnal(buf);
jumped = false;
}
// Routing: Source --> Splitter --> Analyser
// Source --> Destination
// Source
source = ctx.createBufferSource();
// Splitter
splitter = ctx.createChannelSplitter( buf.numberOfChannels );
source.connect(splitter); // Input --> Splitter
// Analyzers
my.ana = []; // clear old analyzers
for (var i = 0; i < buf.numberOfChannels; i++) {
my.ana.push( ctx.createAnalyser() );
my.ana[i].fftSize = fft;
my.ana[i].smoothingTimeConstant = 0.0;
splitter.connect(my.ana[i], i, 0); // Route each single channel from Splitter --> Analyzer
}
source.connect(ctx.destination); // we also want to hear audio
if (jumped) playAudio(buf, pos);
};
function playAudio(buf, pos) {
source.buffer = buf;
//duration = source.buffer.duration;
startTime = ctx.currentTime;
startOffset = pos;
source.start(pos, startOffset % duration);
log("Audio playback started");
isLoading = false; // as late as possible
};
/*
function swap16(val) {
return ((val & 0xFF) << 8)
| ((val >> 8) & 0xFF);
};
function swap32(val) {
return ((val & 0xFF) << 24)
| ((val & 0xFF00) << 8)
| ((val >> 8) & 0xFF00)
| ((val >> 24) & 0xFF);
};
*/
//
// Public
//
my.absoluteValueToDBFS = absoluteValueToDBFS;
my.drawLoudnessDiagram = drawLoudnessDiagram; // needed in RMB event
my.drawPSRDiagram = drawPSRDiagram; // needed in RMB event
my.drawWave = drawWave; // needed in RMB event
my.getDBTP = getDBTP;
my.getDBFS = getDBFS;
my.log = log;
my.loadAudio = function(url) {
log("loadAudio: "+ url);
if (isLoading) return false; // user already started to load, maybe i will abort old load... ToDo
isLoading = true; // as soon as possible
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
log("Audio loaded");
my.decodeAudio(xhr.response);
}
xhr.onprogress = function(e) {
if (e.lengthComputable) {
var perc = e.loaded / e.total * 100;
log("Progress: "+ perc.toFixed(1) + "%");
} else {
log("Progress: "+ my.formatBytes(e.loaded));
}
}
xhr.send();
};
my.formatBytes = function(bytes) {
// b, kB, MB, GB
var kilobytes = bytes/1024;
var megabytes = kilobytes/1024;
var gigabytes = megabytes/1024;
if (gigabytes>1) return gigabytes.toFixed(2) +' GB';
if (megabytes>1) return megabytes.toFixed(2) +' MB';
if (kilobytes>1) return kilobytes.toFixed(2) +' kB';
return bytes +' b';
};
my.decodeAudio = function(buf) {
log("decodeAudio");
my.stopAudio();
// have plain file reader buffer and will try to identify
fAna = ATools.getSamplerate(buf);
log("Found "+ fAna.fType +" with "+ fAna.numChannels +" Channels @ "+ (fAna.srate/1000).toFixed(1)+"kHz with "+ fAna.bitsPerSample + " bits and bitrate of "+ fAna.bitrate);
ctx.decodeAudioData(buf, function(buf) {
splitAudio(buf, 0); // start from beginning
}, function(e){
//console.error(e);
log("Unable to decode audio");
});
};
my.stopAudio = function() {
if (!source) return; // nothing to stop
log("stopAudio");
// stop playback
source.buffer = null;
source.stop();
startOffset += ctx.currentTime - startTime;
// disconnect everything
try {
source.disconnect(splitter);
for (var i = 0; i < my.ana.length; i++) {
splitter.disconnect(my.ana[i], i, 0);
}
} catch(e) {
// mostly because audio already ended
log("Audio was already disconnected");
}
};
my.setFFTsize = function(newSize) {
fft = newSize;
for (var i = 0; i < my.ana.length; i++) {
my.ana[i].fftSize = fft;
}
log("FFT size set to: "+ fft);
};
my.init = function() {
ctx = new AudioContext(); // even that is not late enough
loadConvolver();
canvas = waveform;
cctx = canvas.getContext('2d');
cctx.imageSmoothingEnabled = false;
canvas.width = width;
canvas.height = height;
canvas.onclick = jumpTo;
canvas.oncontextmenu = changeWaveType;
};
//
// Exit
//
return my;
}(ATools || {}));