Skip to content

Commit 76bac0a

Browse files
authored
Merge pull request #730 from KevinGrajeda/error-fft-getenergy
add negative number validation to `getEnergy()`
2 parents de83cd8 + cf924cc commit 76bac0a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/fft.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class FFT {
298298
* <a href="https://en.wikipedia.org/wiki/Audio_frequency" target="_blank">
299299
* frequency</a>, or the average amount of energy between two
300300
* frequencies. Accepts Number(s) corresponding
301-
* to frequency (in Hz), or a "string" corresponding to predefined
301+
* to frequency (in Hz) (frequency must be >= 0), or a "string" corresponding to predefined
302302
* frequency ranges ("bass", "lowMid", "mid", "highMid", "treble").
303303
* Returns a range between 0 (no energy/volume at that frequency) and
304304
* 255 (maximum energy).
@@ -318,8 +318,8 @@ class FFT {
318318
* will return average amount of
319319
* energy that exists between the
320320
* two frequencies.
321-
* @return {Number} Energy Energy (volume/amplitude) from
322-
* 0 and 255.
321+
* @return {Number} Energy (volume/amplitude) from
322+
* 0 and 255.
323323
*
324324
*/
325325
getEnergy(frequency1, frequency2) {
@@ -350,7 +350,9 @@ class FFT {
350350
var index = Math.round((frequency1 / nyquist) * this.freqDomain.length);
351351
return this.freqDomain[index];
352352
}
353-
353+
if (frequency1 < 0 || frequency2 < 0) {
354+
throw 'invalid input for getEnergy(), frequency cannot be a negative number';
355+
}
354356
// if two parameters:
355357
// if second is higher than first
356358
if (frequency1 > frequency2) {

0 commit comments

Comments
 (0)