From dd0d9556ed53783e4f19a62387c026b7fb6b8767 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Fri, 23 Jun 2017 13:13:30 -0400 Subject: [PATCH] Explain loudness scaling, and clamp to 100 --- src/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index c5fc058..a0841d9 100644 --- a/src/index.js +++ b/src/index.js @@ -321,11 +321,14 @@ class AudioEngine { } this._lastValue = rms; - // scale it - // @todo figure out why this magic number is needed and remove it! + // Scale the measurement so it's more sensitive to quieter sounds rms *= 1.63; - // scale and round the output - return Math.round(Math.sqrt(rms) * 100); + rms = Math.sqrt(rms); + // Scale it up to 0-100 and round + rms = Math.round(rms * 100); + // Prevent it from going above 100 + rms = Math.min(rms, 100); + return rms; } // if there is no microphone input, return -1