From 656ddfe28773afaa57baf6dc8797d5709853484f Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 09:27:33 -0800 Subject: [PATCH] audio level calculation in tabletUI.js --- scripts/system/tablet-ui/tabletUI.js | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/scripts/system/tablet-ui/tabletUI.js b/scripts/system/tablet-ui/tabletUI.js index 077485ea35..50c76d6b0f 100644 --- a/scripts/system/tablet-ui/tabletUI.js +++ b/scripts/system/tablet-ui/tabletUI.js @@ -67,4 +67,37 @@ Script.update.connect(updateShowTablet); // Script.setInterval(updateShowTablet, 1000); + // Initialise variables used to calculate audio level + var accumulatedLevel = 0.0; + // Note: Might have to tweak the following two based on the rate we're getting the data + var AVERAGING_RATIO = 0.05; + var AUDIO_LEVEL_UPDATE_INTERVAL_MS = 100; + + // Calculate audio level with the same scaling equation (log scale, exponentially averaged) in AvatarInputs and pal.js + function getAudioLevel() { + var LOUDNESS_FLOOR = 11.0; + var LOUDNESS_SCALE = 2.8 / 5.0; + var LOG2 = Math.log(2.0); + var audioLevel = 0.0; + accumulatedLevel = AVERAGING_RATIO * accumulatedLevel + (1 - AVERAGING_RATIO) * (MyAvatar.audioLoudness); + // Convert to log base 2 + var logLevel = Math.log(accumulatedLevel + 1) / LOG2; + + if (logLevel <= LOUDNESS_FLOOR) { + audioLevel = logLevel / LOUDNESS_FLOOR * LOUDNESS_SCALE; + } else { + audioLevel = (logLevel - (LOUDNESS_FLOOR - 1.0)) * LOUDNESS_SCALE; + } + if (audioLevel > 1.0) { + audioLevel = 1.0; + } + return audioLevel; + } + + Script.setInterval(function() { + var currentAudioLevel = getAudioLevel(); + // TODO: send the audio level to QML + + }, AUDIO_LEVEL_UPDATE_INTERVAL_MS); + }()); // END LOCAL_SCOPE