-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmicrophone.js
1542 lines (1276 loc) · 49.3 KB
/
microphone.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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* =============================================================================
This code is released under the MIT Open Source License.
Copyright (c) 2013 Patrick Yukman and Max Deibel
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File: microphone.js
Authors: Patrick Yukman and Max Deibel
Description: See README for usage.
============================================================================= */
// =============================================================================
// Constructor/class for Microphone
// =============================================================================
// The microphone class allows us to define a set of private variables that
// have global scope within the microphone object's set of functions. We
// encapsulate a number of internal helper functions as well, but allow
// outside access to a function foo with the command:
//
// this.foo = function(param) { ...
//
// This function can then be accessed (after a microphone object "mic" is
// initialized) by calling:
//
// mic.foo(param);
function Microphone() {
var that = this;
var initialized;
var context;
var inputHardware; // aka, whatever the user has for a microphone
var SAMPLE_RATE;
var timeData;
var procNode;
var BUFFER_LEN;
var MIN_SUPPORTED_FREQ;
var MAX_PEAK_SEARCH;
var fft;
var spectrum;
var MY_FFT_SIZE;
var FFT_FREQ_RES;
var processing;
var recording;
var recordingLength;
var leftChannel;
var rightChannel;
var notes; // A JSON look-up table to get notes from frequencies
// -----------------------------------------------------------------------------
// initialize function. Properly initializes the parameters of a Microphone
// object, defines the frequency-->note lookup table, and calls getUserMedia
// to request browser-level access to a user's microphone. In general, do not
// change any part of this initialize function without a compelling reason.
this.initialize = function() {
// Set parameters
initialized = false;
context = null;
inputHardware = null; // Microphone
SAMPLE_RATE = 44100;
timeData = null;
procNode = null;
BUFFER_LEN = 1024; // Keep a power of 2, but can change to
// provide more data, increased resolution
MIN_SUPPORTED_FREQ = 60;
MAX_PEAK_SEARCH = (SAMPLE_RATE/MIN_SUPPORTED_FREQ);
fft = null;
spectrum = null;
MY_FFT_SIZE = BUFFER_LEN;
FFT_FREQ_RES = (SAMPLE_RATE/2)/(MY_FFT_SIZE/2);
processing = false;
recording = false;
recordingLength = 0;
leftChannel = [];
rightChannel = [];
notes = {"A#1" : 58.2705, "B1" : 61.7354, "C2" : 65.4064,
"C#2" : 69.2957, "D2" : 73.4162, "D#2" : 77.7817, "E2" : 82.4069,
"F2" : 87.3071, "F#2" : 92.4986, "G2" : 97.9989, "G#2" : 103.826,
"A2" : 110, "A#2" : 116.542, "B2" : 123.471, "C3" : 130.813,
"C#3" : 138.591, "D3" : 146.832, "D#3" : 155.563, "E3" : 164.814,
"F3" : 174.614, "F#3" : 184.997, "G3" : 195.998, "G#3" : 207.652,
"A3" : 220, "A#3" : 233.082, "B3" : 246.942, "C4" : 261.626,
"C#4" : 277.183, "D4" : 293.665, "D#4" : 311.127, "E4" : 329.628,
"F4" : 349.228, "F#4" : 369.994, "G4" : 391.995, "G#4" : 415.305,
"A4" : 440, "A#4" : 466.164, "B4" : 493.883, "C5" : 523.251,
"C#5" : 554.365, "D5" : 587.330, "D#5" : 622.254, "E5" : 659.255,
"F5" : 698.456, "F#5" : 739.989, "G5" : 783.991, "G#5" : 830.609,
"A5" : 880, "A#5" : 932.328, "B5" : 987.767, "C6" : 1046.5,
"C#6" : 1108.73, "D6" : 1174.66, "D#6" : 1244.51, "E6" : 1318.51,
"F6" : 1396.91, "F#6" : 1479.98, "G6" : 1567.98, "G#6" : 1661.22,
"A6" : 1760, "A#6" : 1864.66, "B6" : 1975.53, "C7" : 2093,
"C#7" : 2217.46, "D7" : 2349.32, "D#7" : 2489.02, "E7" : 2637.02,
"F7" : 2793.83, "F#7" : 2959.96, "G7" : 3135.96, "G#7" : 3322.44,
"A7" : 3520, "A#7" : 3729.31, "B7" : 3951.07, "C8" : 4186.01};
// Make a note that the microphone is about to be accessed
console.log('Beginning!');
// Normalize the various vendor prefixed versions of getUserMedia
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
// Check that browser supports getUserMedia
if (navigator.getUserMedia) {
// Request the microphone
navigator.getUserMedia({audio:true}, gotStream, noStream);
}
else {
alert('Sorry, your browser does not support getUserMedia');
}
}
// -----------------------------------------------------------------------------
// gotStream function. This function is the success callback for getUserMedia
// and initializes the Web Audio API / DSP.JS structures that allow us to
// manipulate the data streaming in off the microphone.
function gotStream(stream) {
console.log('gotStream called');
// Create the audio context
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();
// Set up variables to perform FFT
timeData = [];
fft = new FFT(MY_FFT_SIZE, SAMPLE_RATE);
// Set up a processing node that will allow us to pass mic input off to
// the DSP library for frequency domain analysis
procNode = context.createJavaScriptNode(BUFFER_LEN, 1, 1);
procNode.onaudioprocess = function(e) {
timeData = e.inputBuffer.getChannelData(0);
if (recording) {
leftChannel.push(new Float32Array(timeData));
rightChannel.push(new Float32Array(timeData));
recordingLength += BUFFER_LEN;
}
}
// Create an audio source node from the microphone input to eventually
// feed into the processing node
inputHardware = context.createMediaStreamSource(stream);
procNode.connect(context.destination); // Node must have a destination
// to work. Weird.
console.log('gotStream finished')
initialized = true;
}
// -----------------------------------------------------------------------------
// noStream function. This function is the failure callback for getUserMedia
// and alerts the user if their browser doesn't support getUserMedia.
function noStream(e) {
alert('Error capturing audio.');
}
// -----------------------------------------------------------------------------
// isInitialized function. This function simply returns whether or not the
// microphone object has been fully initialized (indicated by the var
// 'initialized' being equal to true. Returns a boolean value.
this.isInitialized = function() {
if (initialized) {
return true;
}
else {
return false;
}
}
// -----------------------------------------------------------------------------
// startListening function. Connects the microphone input to a processing node
// for future operations. Throws an error if the microphone hasn't been
// initialized before this function is called -- in other words, if a user
// tries to get mic data before allowing the browser permission to collect it.
this.startListening = function() {
if (!initialized) {
throw "Not initialized";
}
else {
console.log('Now listening');
if (!processing) {
processing = true;
// connect mic input so we can process it whenever we want
inputHardware.connect(procNode);
}
}
}
// -----------------------------------------------------------------------------
// stopListening function. Disconnects the microphone input. Can be called or
// tied to a button to save on processing.
this.stopListening = function() {
console.log('Done listening');
if (processing && !recording) {
processing = false;
// Stop processing audio stream
inputHardware.disconnect();
}
}
// -----------------------------------------------------------------------------
// startRecording function. Begins gathering microphone input and storing it in
// a WAV file.
this.startRecording = function() {
if (!initialized || !processing) {
throw "Microphone not initialized / not processing"
}
else {
console.log('Now recording');
if (!recording) {
recording = true;
}
}
}
// -----------------------------------------------------------------------------
// stopRecording function. Stops packaging incoming microphone data into WAV
// file.
this.stopRecording = function() {
console.log('Done recording');
if (recording) {
recording = false;
writeToWav();
}
}
// =============================================================================
// General Purpose Functions
// =============================================================================
// -----------------------------------------------------------------------------
// matchNote function. Input: frequency, in Hertz. Output: closest note
// value to that frequency. This function iterates over the JSON lookup table
// to find the nearest note to the input frequency and returns the note as a
// string.
function matchNote(freq) {
var closest = "A#1"; // Default closest note
var closestFreq = 58.2705;
for (var key in notes) { // Iterates through note look-up table
// If the current note in the table is closer to the given
// frequency than the current "closest" note, replace the
// "closest" note.
if (Math.abs(notes[key] - freq) <= Math.abs(notes[closest] -
freq)) {
closest = key;
closestFreq = notes[key];
}
// Stop searching once the current note in the table is of higher
// frequency than the given frequency.
if (notes[key] > freq) {
break;
}
}
return [closest, closestFreq];
}
// -----------------------------------------------------------------------------
// getMaxInputAmplitude function. Input: none. Output: the amplitude of the
// microphone signal, expressed in deciBels (scaled from -120). Gives an idea
// of the volume of the sound picked up by the microphone.
this.getMaxInputAmplitude = function() {
var minDb = -120;
var minMag = Math.pow(10.0, minDb / 20.0);
var m = timeData[0];
for (var i = 0; i < timeData.length; i++) {
if (timeData[i] > m) {
m = timeData[i];
}
}
return Math.round(20.0*Math.log(Math.max(m, minMag)));
}
// -----------------------------------------------------------------------------
// getFreq function. Input: the method number (default is 1 to use
// autocorrelation, 2 to use FFT). Output: the detected frequency calculated via
// the selected method.
this.getFreq = function(method) {
if (!processing) {
throw "Cannot compute frequency from null input";
}
if (method == 1) {
return computeFreqFromAutocorr();
}
else if (method == 2) {
return computeFreqFromFFT();
}
}
// -----------------------------------------------------------------------------
// getNote function. Input: the method number (default is 1 to use
// autocorrelation, 2 to use FFT). Output: the detected note calculated via
// the selected method.
this.getNote = function(method) {
if (!processing) {
throw "Cannot compute frequency from null input";
}
if (method == 1) {
return getNoteFromAutocorr();
}
else if (method == 2) {
return getNoteFromFFT();
}
}
// -----------------------------------------------------------------------------
// getNoteCents function. Input: the method number (default is 1 to use
// autocorrelation, 2 to use FFT). Output: the detected note cents offset
// calculated via the selected method.
this.getNoteCents = function(method) {
if (!processing) {
throw "Cannot compute frequency from null input";
}
if (method == 1) {
return getNoteCentsFromAutocorr();
}
else if (method == 2) {
return getNoteCentsFromFFT();
}
}
// =============================================================================
// Autocorrelation Functions
// =============================================================================
// -----------------------------------------------------------------------------
// autocorrelate function. For each index in an array of length BUFFER_LEN,
// adds the data element at that index and the next index together, then stores
// it in a separate sums array.
function autocorrelate(data) {
var sums = new Array(BUFFER_LEN);
var i, j;
for (i = 0; i < BUFFER_LEN; i++) {
sums[i] = 0;
for (j = 0; j < BUFFER_LEN - i; j++) {
sums[i] += data[j] * data[j+i];
}
}
return sums;
}
// -----------------------------------------------------------------------------
// getPeakPeriodicityIndex function. After finding the second zero crossing
// in the passed sums array, finds the max peak that occcurs after that crossing
function getPeakPeriodicityIndex(sums) {
// Find second zero crossing, start searching at that point
for (i = 0; sums[i] >= 0 && i < BUFFER_LEN; i++) {}
for (i = i; sums[i] < 0 && i < BUFFER_LEN; i++) {}
var m = sums[i], maxIndex = i;
for (i = i; i < MAX_PEAK_SEARCH; i++) {
if (sums[i] > m) {
m = sums[i];
maxIndex = i;
}
}
return maxIndex;
}
// -----------------------------------------------------------------------------
// computeFreqFromAutocorr function. Gets the max peak index, and then
// calculates the frequency by dividing the sample rate by that index.
function computeFreqFromAutocorr() {
var sums = autocorrelate(timeData);
var maxIndex = getPeakPeriodicityIndex(sums);
return Math.round(SAMPLE_RATE / maxIndex);
}
// -----------------------------------------------------------------------------
// getNoteFromAutocorr function. Computes the current frequency with
// computeFreqFromAutoCorr, then determines the current note by feeding the
// current frequency to matchNote.
function getNoteFromAutocorr() {
var currFreq = computeFreqFromAutocorr();
var noteInfo = matchNote(currFreq);
return noteInfo[0];
}
// -----------------------------------------------------------------------------
// getNoteCentsFromAutocorr function. Computes the current frequency with
// computeFreqFromAutocorr, then determines the current note by feeding the
// current frequency to matchNote, and finally computes the cents offset from
// the current note.
function getNoteCentsFromAutocorr() {
var currFreq = computeFreqFromAutocorr();
var noteInfo = matchNote(currFreq);
var noteFreq = noteInfo[1];
var cents = 1200*(Math.log(currFreq/Math.round(noteFreq))/Math.log(2));
return [noteInfo[0], Math.round(cents)];
}
// =============================================================================
// FFT Functions
// =============================================================================
// -----------------------------------------------------------------------------
// computeFreqFromFFT function. Input: none. Output: frequency of the sound
// picked up by the microphone, computed via FFT. Automatically grabs the
// current microphone data from the timeData global variable and uses the FFT
// defined in DSP.JS. Interpolates the FFT power spectrum to more accurately
// guess the actual value of the peak frequency of the signal.
function computeFreqFromFFT() {
fft.forward(timeData); // See added dsp library for additional info
spectrum = fft.spectrum;
// Get index of maximum in spectrum array
var i = 0, m = spectrum[0], maxIndex = 0;
while (++i < spectrum.length) {
if (spectrum[i] > m) {
maxIndex = i;
m = spectrum[i];
}
}
// Make a best guess at the frequency of the signal
interpolatedBin = jainsMethodInterpolate(spectrum, maxIndex);
return Math.round(interpolatedBin*FFT_FREQ_RES);
}
// -----------------------------------------------------------------------------
// jainsMethodInterpolate function. Input: array of spectrum power values
// returned from FFT; index of bin in spectrum array with max power value.
// Output: a fractional bin number indicating the interpolated location of
// the actual signal peak frequency. Uses neighbouring indices to the index of
// greatest magnitude to create a more accurate estimate of the frequency.
// Simply multiply the returned fractional bin index by the FFT spectrum
// frequency resolution to get the estimate of the actual peak frequency.
function jainsMethodInterpolate(spctrm, maxIndex) {
var m1, m2, m3, n, o;
var m1 = Math.abs(spctrm[maxIndex - 1]);
var m2 = Math.abs(spctrm[maxIndex]);
var m3 = Math.abs(spctrm[maxIndex + 1]);
if (m1 > m3) {
a = m2 / m1;
d = a / (1 + a);
return maxIndex - 1 + d;
}
else {
a = m3 / m2;
d = a / (1 + a);
return maxIndex + d;
}
}
// -----------------------------------------------------------------------------
// getNoteFromFFT function. Computes the current frequency with
// computeFreqFromFFT, then determines the current note by feeding the current
// frequency to matchNote
function getNoteFromFFT() {
var currFreq = computeFreqFromFFT();
var noteInfo = matchNote(currFreq);
return noteInfo[0];
}
// -----------------------------------------------------------------------------
// getNoteCentsFromFFT function. Computes the current frequency with
// computeFreqFromFFT, then determines the current note by feeding the current
// frequency to matchNote, and finally computes the cents offset from the
// current note
function getNoteCentsFromFFT() {
var currFreq = computeFreqFromFFT()
var noteInfo = matchNote(currFreq);
var noteFreq = noteInfo[1];
var cents = 1200*(Math.log(currFreq/Math.round(noteFreq))/Math.log(2));
return [noteInfo[0], Math.round(cents)];
}
// =============================================================================
// Recording Functions
// =============================================================================
// -----------------------------------------------------------------------------
// writeToWav function. Writes our recording data to a .WAV file.
function writeToWav() {
// we flat the left and right channels down
var leftBuffer = mergeBuffers (leftChannel, recordingLength);
var rightBuffer = mergeBuffers (rightChannel, recordingLength);
// we interleave both channels together
var interleaved = interleave (leftBuffer, rightBuffer);
// we create our wav file
var buffer = new ArrayBuffer(44 + interleaved.length * 2);
var view = new DataView(buffer);
// RIFF chunk descriptor
writeUTFBytes(view, 0, 'RIFF');
view.setUint32(4, 44 + interleaved.length * 2, true);
writeUTFBytes(view, 8, 'WAVE');
// FMT sub-chunk
writeUTFBytes(view, 12, 'fmt ');
view.setUint32(16, 16, true);
view.setUint16(20, 1, true);
// stereo (2 channels)
view.setUint16(22, 2, true);
view.setUint32(24, SAMPLE_RATE, true);
view.setUint32(28, SAMPLE_RATE * 4, true);
view.setUint16(32, 4, true);
view.setUint16(34, 16, true);
// data sub-chunk
writeUTFBytes(view, 36, 'data');
view.setUint32(40, interleaved.length * 2, true);
// write the PCM samples
var lng = interleaved.length;
var index = 44;
var volume = 1;
for (var i = 0; i < lng; i++){
view.setInt16(index, interleaved[i] * 0x7FFF, true);
index += 2;
}
// our final binary blob
var blob = new Blob ( [ view ], { type : 'audio/wav' } );
// let's save it locally
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var link = window.document.createElement('a');
link.href = url;
link.download = 'recording.wav';
var click = document.createEvent("Event");
click.initEvent("click", true, true);
link.dispatchEvent(click);
}
// -----------------------------------------------------------------------------
// interleave function. Takes the left and right channels and combines them
// into one array, alternating between the two channels to copy the values over.
function interleave(leftChannel, rightChannel) {
var length = leftChannel.length + rightChannel.length;
var result = new Float32Array(length);
var inputIndex = 0;
for (var index = 0; index < length; ){
result[index++] = leftChannel[inputIndex];
result[index++] = rightChannel[inputIndex];
inputIndex++;
}
return result;
}
// -----------------------------------------------------------------------------
// mergeBuffers function. Takes each of the individual channel buffers and
// combines them sequentially into one final result array, which has length
// equal to the given length of the recording.
function mergeBuffers(channelBuffer, recordingLength) {
var result = new Float32Array(recordingLength);
var offset = 0;
var lng = channelBuffer.length;
for (var i = 0; i < lng; i++){
var buffer = channelBuffer[i];
result.set(buffer, offset);
offset += buffer.length;
}
return result;
}
// -----------------------------------------------------------------------------
// writeUTFBytes function. Helper function for writeToWav.
function writeUTFBytes(view, offset, string) {
var lng = string.length;
for (var i = 0; i < lng; i++){
view.setUint8(offset + i, string.charCodeAt(i));
}
}
// =============================================================================
// Test and Debugging Functions
// =============================================================================
// -----------------------------------------------------------------------------
// logData function. Logs time domain data, then frequency domain data for
// external analysis. Used as a debugging / quantitative analysis tool. If
// needed, tie it to a button so problems can be logged in real time.
this.logData = function() {
var t, f;
t = timeData;
fft.forward(t);
f = fft.spectrum;
var i;
var textToWrite = "";
textToWrite += "------- Time Domain Data -------\n";
for (i = 0; i < t.length; i++) {
textToWrite += t[i] + "\n";
}
textToWrite += "\n------- Frequency Domain Data -------\n";
for (i = 0; i < f.length; i++) {
textToWrite += f[i] + "\n";
}
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = "jslog.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else {
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
// Helper for Firefox
function destroyClickedElement(event) {
document.body.removeChild(event.target);
}
}
// ========================================================================== //
////////////////////////////////////////////////////////////////////////////////
// ========================================================================== //
// //
// HERE BE DRAGONS //
// PROCEED AT YOUR OWN RISK //
// //
// ========================================================================== //
////////////////////////////////////////////////////////////////////////////////
// ========================================================================== //
// DSP Library
/*
* DSP.js - a comprehensive digital signal processing library for javascript
*
* Created by Corban Brook <[email protected]> on 2010-01-01.
* Copyright 2010 Corban Brook. All rights reserved.
*
*/
/*Copyright (c) 2010 Corban Brook
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS //
////////////////////////////////////////////////////////////////////////////////
/**
* DSP is an object which contains general purpose utility functions and constants
*/
var DSP = {
// Channels
LEFT: 0,
RIGHT: 1,
MIX: 2,
// Waveforms
SINE: 1,
TRIANGLE: 2,
SAW: 3,
SQUARE: 4,
// Filters
LOWPASS: 0,
HIGHPASS: 1,
BANDPASS: 2,
NOTCH: 3,
// Window functions
BARTLETT: 1,
BARTLETTHANN: 2,
BLACKMAN: 3,
COSINE: 4,
GAUSS: 5,
HAMMING: 6,
HANN: 7,
LANCZOS: 8,
RECTANGULAR: 9,
TRIANGULAR: 10,
// Loop modes
OFF: 0,
FW: 1,
BW: 2,
FWBW: 3,
// Math
TWO_PI: 2*Math.PI
};
// Setup arrays for platforms which do not support byte arrays
function setupTypedArray(name, fallback) {
// check if TypedArray exists
// typeof on Minefield and Chrome return function, typeof on Webkit returns object.
if (typeof this[name] !== "function" && typeof this[name] !== "object") {
// nope.. check if WebGLArray exists
if (typeof this[fallback] === "function" && typeof this[fallback] !== "object") {
this[name] = this[fallback];
} else {
// nope.. set as Native JS array
this[name] = function(obj) {
if (obj instanceof Array) {
return obj;
} else if (typeof obj === "number") {
return new Array(obj);
}
};
}
}
}
setupTypedArray("Float32Array", "WebGLFloatArray");
setupTypedArray("Int32Array", "WebGLIntArray");
setupTypedArray("Uint16Array", "WebGLUnsignedShortArray");
setupTypedArray("Uint8Array", "WebGLUnsignedByteArray");
////////////////////////////////////////////////////////////////////////////////
// DSP UTILITY FUNCTIONS //
////////////////////////////////////////////////////////////////////////////////
/**
* Inverts the phase of a signal
*
* @param {Array} buffer A sample buffer
*
* @returns The inverted sample buffer
*/
DSP.invert = function(buffer) {
for (var i = 0, len = buffer.length; i < len; i++) {
buffer[i] *= -1;
}
return buffer;
};
/**
* Converts split-stereo (dual mono) sample buffers into a stereo interleaved sample buffer
*
* @param {Array} left A sample buffer
* @param {Array} right A sample buffer
*
* @returns The stereo interleaved buffer
*/
DSP.interleave = function(left, right) {
if (left.length !== right.length) {
throw "Can not interleave. Channel lengths differ.";
}
var stereoInterleaved = new Float32Array(left.length * 2);
for (var i = 0, len = left.length; i < len; i++) {
stereoInterleaved[2*i] = left[i];
stereoInterleaved[2*i+1] = right[i];
}
return stereoInterleaved;
};
/**
* Converts a stereo-interleaved sample buffer into split-stereo (dual mono) sample buffers
*
* @param {Array} buffer A stereo-interleaved sample buffer
*
* @returns an Array containing left and right channels
*/
DSP.deinterleave = (function() {
var left, right, mix, deinterleaveChannel = [];
deinterleaveChannel[DSP.MIX] = function(buffer) {
for (var i = 0, len = buffer.length/2; i < len; i++) {
mix[i] = (buffer[2*i] + buffer[2*i+1]) / 2;
}
return mix;
};
deinterleaveChannel[DSP.LEFT] = function(buffer) {
for (var i = 0, len = buffer.length/2; i < len; i++) {
left[i] = buffer[2*i];
}
return left;
};
deinterleaveChannel[DSP.RIGHT] = function(buffer) {
for (var i = 0, len = buffer.length/2; i < len; i++) {
right[i] = buffer[2*i+1];
}
return right;
};
return function(channel, buffer) {
left = left || new Float32Array(buffer.length/2);
right = right || new Float32Array(buffer.length/2);
mix = mix || new Float32Array(buffer.length/2);
if (buffer.length/2 !== left.length) {
left = new Float32Array(buffer.length/2);
right = new Float32Array(buffer.length/2);
mix = new Float32Array(buffer.length/2);
}
return deinterleaveChannel[channel](buffer);
};
}());
/**
* Separates a channel from a stereo-interleaved sample buffer
*
* @param {Array} buffer A stereo-interleaved sample buffer
* @param {Number} channel A channel constant (LEFT, RIGHT, MIX)
*
* @returns an Array containing a signal mono sample buffer
*/
DSP.getChannel = DSP.deinterleave;
/**
* Helper method (for Reverb) to mix two (interleaved) samplebuffers. It's possible
* to negate the second buffer while mixing and to perform a volume correction
* on the final signal.
*
* @param {Array} sampleBuffer1 Array containing Float values or a Float32Array
* @param {Array} sampleBuffer2 Array containing Float values or a Float32Array
* @param {Boolean} negate When true inverts/flips the audio signal
* @param {Number} volumeCorrection When you add multiple sample buffers, use this to tame your signal ;)
*
* @returns A new Float32Array interleaved buffer.
*/
DSP.mixSampleBuffers = function(sampleBuffer1, sampleBuffer2, negate, volumeCorrection){
var outputSamples = new Float32Array(sampleBuffer1);
for(var i = 0; i<sampleBuffer1.length; i++){
outputSamples[i] += (negate ? -sampleBuffer2[i] : sampleBuffer2[i]) / volumeCorrection;
}
return outputSamples;
};
// Biquad filter types
DSP.LPF = 0; // H(s) = 1 / (s^2 + s/Q + 1)
DSP.HPF = 1; // H(s) = s^2 / (s^2 + s/Q + 1)
DSP.BPF_CONSTANT_SKIRT = 2; // H(s) = s / (s^2 + s/Q + 1) (constant skirt gain, peak gain = Q)
DSP.BPF_CONSTANT_PEAK = 3; // H(s) = (s/Q) / (s^2 + s/Q + 1) (constant 0 dB peak gain)
DSP.NOTCH = 4; // H(s) = (s^2 + 1) / (s^2 + s/Q + 1)
DSP.APF = 5; // H(s) = (s^2 - s/Q + 1) / (s^2 + s/Q + 1)
DSP.PEAKING_EQ = 6; // H(s) = (s^2 + s*(A/Q) + 1) / (s^2 + s/(A*Q) + 1)
DSP.LOW_SHELF = 7; // H(s) = A * (s^2 + (sqrt(A)/Q)*s + A)/(A*s^2 + (sqrt(A)/Q)*s + 1)
DSP.HIGH_SHELF = 8; // H(s) = A * (A*s^2 + (sqrt(A)/Q)*s + 1)/(s^2 + (sqrt(A)/Q)*s + A)
// Biquad filter parameter types
DSP.Q = 1;
DSP.BW = 2; // SHARED with BACKWARDS LOOP MODE
DSP.S = 3;
// Find RMS of signal
DSP.RMS = function(buffer) {
var total = 0;
for (var i = 0, n = buffer.length; i < n; i++) {
total += buffer[i] * buffer[i];
}
return Math.sqrt(total / n);
};
// Find Peak of signal
DSP.Peak = function(buffer) {
var peak = 0;
for (var i = 0, n = buffer.length; i < n; i++) {
peak = (Math.abs(buffer[i]) > peak) ? Math.abs(buffer[i]) : peak;
}
return peak;
};
// Fourier Transform Module used by DFT, FFT, RFFT
function FourierTransform(bufferSize, sampleRate) {
this.bufferSize = bufferSize;
this.sampleRate = sampleRate;
this.bandwidth = 2 / bufferSize * sampleRate / 2;
this.spectrum = new Float32Array(bufferSize/2);
this.real = new Float32Array(bufferSize);
this.imag = new Float32Array(bufferSize);
this.peakBand = 0;
this.peak = 0;
/**
* Calculates the *middle* frequency of an FFT band.
*
* @param {Number} index The index of the FFT band.
*
* @returns The middle frequency in Hz.
*/
this.getBandFrequency = function(index) {
return this.bandwidth * index + this.bandwidth / 2;
};
this.calculateSpectrum = function() {
var spectrum = this.spectrum,
real = this.real,
imag = this.imag,
bSi = 2 / this.bufferSize,
sqrt = Math.sqrt,
rval,
ival,
mag;
for (var i = 0, N = bufferSize/2; i < N; i++) {
rval = real[i];
ival = imag[i];
mag = bSi * sqrt(rval * rval + ival * ival);
if (mag > this.peak) {
this.peakBand = i;
this.peak = mag;
}
spectrum[i] = mag;
}