Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add negative number validation to getEnergy() #730

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/fft.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class FFT {
* <a href="https://en.wikipedia.org/wiki/Audio_frequency" target="_blank">
* frequency</a>, or the average amount of energy between two
* frequencies. Accepts Number(s) corresponding
* to frequency (in Hz), or a "string" corresponding to predefined
* to frequency (in Hz) (frequency must be >= 0), or a "string" corresponding to predefined
* frequency ranges ("bass", "lowMid", "mid", "highMid", "treble").
* Returns a range between 0 (no energy/volume at that frequency) and
* 255 (maximum energy).
Expand All @@ -318,8 +318,8 @@ class FFT {
* will return average amount of
* energy that exists between the
* two frequencies.
* @return {Number} Energy Energy (volume/amplitude) from
* 0 and 255.
* @return {Number} Energy (volume/amplitude) from
* 0 and 255.
*
*/
getEnergy(frequency1, frequency2) {
Expand Down Expand Up @@ -350,7 +350,9 @@ class FFT {
var index = Math.round((frequency1 / nyquist) * this.freqDomain.length);
return this.freqDomain[index];
}

if (frequency1 < 0 || frequency2 < 0) {
throw 'invalid input for getEnergy(), frequency cannot be a negative number';
}
// if two parameters:
// if second is higher than first
if (frequency1 > frequency2) {
Expand Down