1
1
'use strict' ;
2
2
3
- define ( function ( require ) {
3
+ define ( function ( require ) {
4
4
var p5sound = require ( 'master' ) ;
5
5
6
6
/**
@@ -93,7 +93,7 @@ define(function (require) {
93
93
this . input = this . analyser = p5sound . audiocontext . createAnalyser ( ) ;
94
94
95
95
Object . defineProperties ( this , {
96
- ' bins' : {
96
+ bins : {
97
97
get : function ( ) {
98
98
return this . analyser . fftSize / 2 ;
99
99
} ,
@@ -103,7 +103,7 @@ define(function (require) {
103
103
configurable : true ,
104
104
enumerable : true
105
105
} ,
106
- ' smoothing' : {
106
+ smoothing : {
107
107
get : function ( ) {
108
108
return this . analyser . smoothingTimeConstant ;
109
109
} ,
@@ -193,14 +193,13 @@ define(function (require) {
193
193
} else {
194
194
timeToInt ( this , this . timeDomain ) ;
195
195
this . analyser . getByteTimeDomainData ( this . timeDomain ) ;
196
- var normalArray = new Array ( ) ;
196
+ var normalArray = new Array ( ) ;
197
197
for ( var j = 0 ; j < this . timeDomain . length ; j ++ ) {
198
198
var scaled = p5 . prototype . map ( this . timeDomain [ j ] , 0 , 255 , - 1 , 1 ) ;
199
199
normalArray . push ( scaled ) ;
200
200
}
201
201
return normalArray ;
202
202
}
203
-
204
203
} ;
205
204
206
205
/**
@@ -291,17 +290,16 @@ define(function (require) {
291
290
} else {
292
291
freqToInt ( this , this . freqDomain ) ;
293
292
this . analyser . getByteFrequencyData ( this . freqDomain ) ;
294
- var normalArray = Array . apply ( [ ] , this . freqDomain ) ;
293
+ var normalArray = Array . apply ( [ ] , this . freqDomain ) ;
295
294
normalArray . length === this . analyser . fftSize ;
296
295
normalArray . constructor === Array ;
297
296
return normalArray ;
298
297
}
299
-
300
298
} ;
301
299
302
300
/**
303
301
* Returns the amount of energy (volume) at a specific
304
- * <a href="en.wikipedia.org/wiki/Audio_frequency" target="_blank">
302
+ * <a href="https:// en.wikipedia.org/wiki/Audio_frequency" target="_blank">
305
303
* frequency</a>, or the average amount of energy between two
306
304
* frequencies. Accepts Number(s) corresponding
307
305
* to frequency (in Hz), or a String corresponding to predefined
@@ -328,7 +326,7 @@ define(function (require) {
328
326
*
329
327
*/
330
328
p5 . FFT . prototype . getEnergy = function ( frequency1 , frequency2 ) {
331
- var nyquist = p5sound . audiocontext . sampleRate / 2 ;
329
+ var nyquist = p5sound . audiocontext . sampleRate / 2 ;
332
330
333
331
if ( frequency1 === 'bass' ) {
334
332
frequency1 = this . bass [ 0 ] ;
@@ -349,37 +347,32 @@ define(function (require) {
349
347
350
348
if ( typeof frequency1 !== 'number' ) {
351
349
throw 'invalid input for getEnergy()' ;
352
- }
353
-
354
- // if only one parameter:
355
- else if ( ! frequency2 ) {
356
- var index = Math . round ( frequency1 / nyquist * this . freqDomain . length ) ;
350
+ } else if ( ! frequency2 ) {
351
+ // if only one parameter:
352
+ var index = Math . round ( frequency1 / nyquist * this . freqDomain . length ) ;
357
353
return this . freqDomain [ index ] ;
358
- }
359
-
360
- // if two parameters:
361
- else if ( frequency1 && frequency2 ) {
354
+ } else if ( frequency1 && frequency2 ) {
355
+ // if two parameters:
362
356
// if second is higher than first
363
357
if ( frequency1 > frequency2 ) {
364
358
var swap = frequency2 ;
365
359
frequency2 = frequency1 ;
366
360
frequency1 = swap ;
367
361
}
368
- var lowIndex = Math . round ( frequency1 / nyquist * this . freqDomain . length ) ;
369
- var highIndex = Math . round ( frequency2 / nyquist * this . freqDomain . length ) ;
362
+ var lowIndex = Math . round ( frequency1 / nyquist * this . freqDomain . length ) ;
363
+ var highIndex = Math . round ( frequency2 / nyquist * this . freqDomain . length ) ;
370
364
371
365
var total = 0 ;
372
366
var numFrequencies = 0 ;
373
367
// add up all of the values for the frequencies
374
- for ( var i = lowIndex ; i <= highIndex ; i ++ ) {
368
+ for ( var i = lowIndex ; i <= highIndex ; i ++ ) {
375
369
total += this . freqDomain [ i ] ;
376
370
numFrequencies += 1 ;
377
371
}
378
372
// divide by total number of frequencies
379
- var toReturn = total / numFrequencies ;
373
+ var toReturn = total / numFrequencies ;
380
374
return toReturn ;
381
- }
382
- else {
375
+ } else {
383
376
throw 'invalid input for getEnergy()' ;
384
377
}
385
378
} ;
@@ -458,24 +451,23 @@ define(function (require) {
458
451
* </code></div>
459
452
*/
460
453
p5 . FFT . prototype . getCentroid = function ( ) {
461
- var nyquist = p5sound . audiocontext . sampleRate / 2 ;
454
+ var nyquist = p5sound . audiocontext . sampleRate / 2 ;
462
455
var cumulative_sum = 0 ;
463
456
var centroid_normalization = 0 ;
464
457
465
- for ( var i = 0 ; i < this . freqDomain . length ; i ++ )
466
- {
458
+ for ( var i = 0 ; i < this . freqDomain . length ; i ++ ) {
467
459
cumulative_sum += i * this . freqDomain [ i ] ;
468
460
centroid_normalization += this . freqDomain [ i ] ;
469
461
}
470
462
471
463
var mean_freq_index = 0 ;
472
464
473
- if ( centroid_normalization !== 0 )
474
- {
465
+ if ( centroid_normalization !== 0 ) {
475
466
mean_freq_index = cumulative_sum / centroid_normalization ;
476
467
}
477
468
478
- var spec_centroid_freq = mean_freq_index * ( nyquist / this . freqDomain . length ) ;
469
+ var spec_centroid_freq =
470
+ mean_freq_index * ( nyquist / this . freqDomain . length ) ;
479
471
return spec_centroid_freq ;
480
472
} ;
481
473
@@ -526,10 +518,10 @@ define(function (require) {
526
518
var groupIndex = 0 ;
527
519
528
520
for ( var specIndex = 0 ; specIndex < spectrumLength ; specIndex ++ ) {
529
-
530
- linearAverages [ groupIndex ] = linearAverages [ groupIndex ] !== undefined
531
- ? ( linearAverages [ groupIndex ] + spectrum [ specIndex ] ) / 2
532
- : spectrum [ specIndex ] ;
521
+ linearAverages [ groupIndex ] =
522
+ linearAverages [ groupIndex ] !== undefined
523
+ ? ( linearAverages [ groupIndex ] + spectrum [ specIndex ] ) / 2
524
+ : spectrum [ specIndex ] ;
533
525
534
526
// Increase the group index when the last element of the group is processed
535
527
if ( specIndex % spectrumStep === spectrumStep - 1 ) {
@@ -563,16 +555,19 @@ define(function (require) {
563
555
var octaveIndex = 0 ;
564
556
565
557
for ( var specIndex = 0 ; specIndex < spectrumLength ; specIndex ++ ) {
566
- var specIndexFrequency = Math . round ( specIndex * nyquist / this . freqDomain . length ) ;
558
+ var specIndexFrequency = Math . round (
559
+ specIndex * nyquist / this . freqDomain . length
560
+ ) ;
567
561
568
562
// Increase the group index if the current frequency exceeds the limits of the band
569
563
if ( specIndexFrequency > octaveBands [ octaveIndex ] . hi ) {
570
564
octaveIndex ++ ;
571
565
}
572
566
573
- logAverages [ octaveIndex ] = logAverages [ octaveIndex ] !== undefined
574
- ? ( logAverages [ octaveIndex ] + spectrum [ specIndex ] ) / 2
575
- : spectrum [ specIndex ] ;
567
+ logAverages [ octaveIndex ] =
568
+ logAverages [ octaveIndex ] !== undefined
569
+ ? ( logAverages [ octaveIndex ] + spectrum [ specIndex ] ) / 2
570
+ : spectrum [ specIndex ] ;
576
571
}
577
572
578
573
return logAverages ;
@@ -592,24 +587,23 @@ define(function (require) {
592
587
* @return {Array } octaveBands Array of octave band objects with their bounds
593
588
*/
594
589
p5 . FFT . prototype . getOctaveBands = function ( N , fCtr0 ) {
595
- var N = N || 3 ; // Default to 1/3 Octave Bands
596
- var fCtr0 = fCtr0 || 15.625 ; // Minimum central frequency, defaults to 15.625Hz
590
+ var N = N || 3 ; // Default to 1/3 Octave Bands
591
+ var fCtr0 = fCtr0 || 15.625 ; // Minimum central frequency, defaults to 15.625Hz
597
592
598
593
var octaveBands = [ ] ;
599
594
var lastFrequencyBand = {
600
- lo : fCtr0 / Math . pow ( 2 , 1 / ( 2 * N ) ) ,
595
+ lo : fCtr0 / Math . pow ( 2 , 1 / ( 2 * N ) ) ,
601
596
ctr : fCtr0 ,
602
- hi : fCtr0 * Math . pow ( 2 , 1 / ( 2 * N ) ) ,
597
+ hi : fCtr0 * Math . pow ( 2 , 1 / ( 2 * N ) )
603
598
} ;
604
599
octaveBands . push ( lastFrequencyBand ) ;
605
600
606
601
var nyquist = p5sound . audiocontext . sampleRate / 2 ;
607
602
while ( lastFrequencyBand . hi < nyquist ) {
608
-
609
603
var newFrequencyBand = { } ;
610
604
newFrequencyBand . lo = lastFrequencyBand . hi ;
611
605
newFrequencyBand . ctr = lastFrequencyBand . ctr * Math . pow ( 2 , 1 / N ) ;
612
- newFrequencyBand . hi = newFrequencyBand . ctr * Math . pow ( 2 , 1 / ( 2 * N ) ) ;
606
+ newFrequencyBand . hi = newFrequencyBand . ctr * Math . pow ( 2 , 1 / ( 2 * N ) ) ;
613
607
614
608
octaveBands . push ( newFrequencyBand ) ;
615
609
lastFrequencyBand = newFrequencyBand ;
@@ -619,26 +613,24 @@ define(function (require) {
619
613
} ;
620
614
621
615
// helper methods to convert type from float (dB) to int (0-255)
622
- var freqToFloat = function ( fft ) {
616
+ var freqToFloat = function ( fft ) {
623
617
if ( fft . freqDomain instanceof Float32Array === false ) {
624
618
fft . freqDomain = new Float32Array ( fft . analyser . frequencyBinCount ) ;
625
619
}
626
620
} ;
627
- var freqToInt = function ( fft ) {
621
+ var freqToInt = function ( fft ) {
628
622
if ( fft . freqDomain instanceof Uint8Array === false ) {
629
623
fft . freqDomain = new Uint8Array ( fft . analyser . frequencyBinCount ) ;
630
624
}
631
625
} ;
632
- var timeToFloat = function ( fft ) {
626
+ var timeToFloat = function ( fft ) {
633
627
if ( fft . timeDomain instanceof Float32Array === false ) {
634
628
fft . timeDomain = new Float32Array ( fft . analyser . frequencyBinCount ) ;
635
629
}
636
630
} ;
637
- var timeToInt = function ( fft ) {
631
+ var timeToInt = function ( fft ) {
638
632
if ( fft . timeDomain instanceof Uint8Array === false ) {
639
633
fft . timeDomain = new Uint8Array ( fft . analyser . frequencyBinCount ) ;
640
634
}
641
635
} ;
642
-
643
-
644
636
} ) ;
0 commit comments