From 656ddfe28773afaa57baf6dc8797d5709853484f Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 09:27:33 -0800 Subject: [PATCH 1/7] 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 From 7e1b06efdc53651580b3d4ff3d6b3f54b09ad8e1 Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 11:15:28 -0800 Subject: [PATCH 2/7] slot function in QML --- interface/resources/qml/hifi/tablet/Tablet.qml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/interface/resources/qml/hifi/tablet/Tablet.qml b/interface/resources/qml/hifi/tablet/Tablet.qml index 93d388b374..ce0e8b7cfe 100644 --- a/interface/resources/qml/hifi/tablet/Tablet.qml +++ b/interface/resources/qml/hifi/tablet/Tablet.qml @@ -5,11 +5,16 @@ Item { id: tablet objectName: "tablet" - property double miclevel: 0.8 + property double micLevel: 0.8 width: parent.width height: parent.height + // called by C++ code to keep mic level display bar UI updated + function setMicLevel(newMicLevel) { + tablet.micLevel = newMicLevel; + } + // used to look up a button by its uuid function findButtonIndex(uuid) { if (!uuid) { @@ -101,7 +106,7 @@ Item { } Rectangle { id: audioBarMask - width: parent.width * tablet.miclevel + width: parent.width * tablet.micLevel color: "#333333" radius: 5 anchors.bottom: parent.bottom @@ -205,7 +210,7 @@ Item { PropertyChanges { target: tablet - miclevel: 0 + micLevel: 0 } } ] From 68aacfe5448f8d32f029dce57243e9328503d727 Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 12:38:23 -0800 Subject: [PATCH 3/7] added "updateAudioBar" function signature in header file --- interface/resources/qml/hifi/tablet/Tablet.qml | 2 +- libraries/script-engine/src/TabletScriptingInterface.h | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/interface/resources/qml/hifi/tablet/Tablet.qml b/interface/resources/qml/hifi/tablet/Tablet.qml index ce0e8b7cfe..170ba1d26f 100644 --- a/interface/resources/qml/hifi/tablet/Tablet.qml +++ b/interface/resources/qml/hifi/tablet/Tablet.qml @@ -10,7 +10,7 @@ Item { width: parent.width height: parent.height - // called by C++ code to keep mic level display bar UI updated + // called by C++ code to keep audio bar updated function setMicLevel(newMicLevel) { tablet.micLevel = newMicLevel; } diff --git a/libraries/script-engine/src/TabletScriptingInterface.h b/libraries/script-engine/src/TabletScriptingInterface.h index 9739066c17..5ccd4689e3 100644 --- a/libraries/script-engine/src/TabletScriptingInterface.h +++ b/libraries/script-engine/src/TabletScriptingInterface.h @@ -84,6 +84,13 @@ public: */ Q_INVOKABLE void removeButton(QObject* tabletButtonProxy); + /**jsdoc + * @function TabletProxy#updateAudioBar + * Updates the audio bar in tablet to reflect latest mic level + * @param micLevel {double} mic level value between 0 and 1 + */ + Q_INVOKABLE void updateAudioBar(const double micLevel); + QString getName() const { return _name; } protected: From d8227a686cbbbbd89e49054a1a21710b196cd09f Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 12:51:26 -0800 Subject: [PATCH 4/7] fill in updateAudioBar function, invokes qml function --- libraries/script-engine/src/TabletScriptingInterface.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libraries/script-engine/src/TabletScriptingInterface.cpp b/libraries/script-engine/src/TabletScriptingInterface.cpp index bafa9ab4b4..73482218ea 100644 --- a/libraries/script-engine/src/TabletScriptingInterface.cpp +++ b/libraries/script-engine/src/TabletScriptingInterface.cpp @@ -141,6 +141,15 @@ void TabletProxy::removeButton(QObject* tabletButtonProxy) { } } +void TabletProxy::updateAudioBar(const double micLevel) { + auto tablet = getQmlTablet(); + if (!tablet) { + qCCritical(scriptengine) << "Could not find tablet in TabletRoot.qml"; + } else { + QMetaObject::invokeMethod(tablet, "setMicLevel", Qt::AutoConnection, Q_ARG(QVariant, QVariant::QVariant(micLevel))); + } +} + void TabletProxy::addButtonsToHomeScreen() { auto tablet = getQmlTablet(); if (!tablet) { From 4391e9eaeee10bf498955a4ddb2d464f8b153321 Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 12:57:08 -0800 Subject: [PATCH 5/7] send the mic level from js --- scripts/system/tablet-ui/tabletUI.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/scripts/system/tablet-ui/tabletUI.js b/scripts/system/tablet-ui/tabletUI.js index 50c76d6b0f..2d7b69ad38 100644 --- a/scripts/system/tablet-ui/tabletUI.js +++ b/scripts/system/tablet-ui/tabletUI.js @@ -71,33 +71,33 @@ 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; + var MIC_LEVEL_UPDATE_INTERVAL_MS = 100; - // Calculate audio level with the same scaling equation (log scale, exponentially averaged) in AvatarInputs and pal.js - function getAudioLevel() { + // Calculate microphone level with the same scaling equation (log scale, exponentially averaged) in AvatarInputs and pal.js + function getMicLevel() { var LOUDNESS_FLOOR = 11.0; var LOUDNESS_SCALE = 2.8 / 5.0; var LOG2 = Math.log(2.0); - var audioLevel = 0.0; + var micLevel = 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; + micLevel = logLevel / LOUDNESS_FLOOR * LOUDNESS_SCALE; } else { - audioLevel = (logLevel - (LOUDNESS_FLOOR - 1.0)) * LOUDNESS_SCALE; + micLevel = (logLevel - (LOUDNESS_FLOOR - 1.0)) * LOUDNESS_SCALE; } - if (audioLevel > 1.0) { - audioLevel = 1.0; + if (micLevel > 1.0) { + micLevel = 1.0; } - return audioLevel; + return micLevel; } Script.setInterval(function() { - var currentAudioLevel = getAudioLevel(); - // TODO: send the audio level to QML - - }, AUDIO_LEVEL_UPDATE_INTERVAL_MS); + var currentMicLevel = getMicLevel(); + var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); + tablet.updateAudioBar(currentMicLevel); + }, MIC_LEVEL_UPDATE_INTERVAL_MS); }()); // END LOCAL_SCOPE From 752051834da2581badb2ccfb8c69e09cb1e348e2 Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 13:30:19 -0800 Subject: [PATCH 6/7] function style casting --- libraries/script-engine/src/TabletScriptingInterface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/script-engine/src/TabletScriptingInterface.cpp b/libraries/script-engine/src/TabletScriptingInterface.cpp index 73482218ea..3831e6cf05 100644 --- a/libraries/script-engine/src/TabletScriptingInterface.cpp +++ b/libraries/script-engine/src/TabletScriptingInterface.cpp @@ -146,7 +146,7 @@ void TabletProxy::updateAudioBar(const double micLevel) { if (!tablet) { qCCritical(scriptengine) << "Could not find tablet in TabletRoot.qml"; } else { - QMetaObject::invokeMethod(tablet, "setMicLevel", Qt::AutoConnection, Q_ARG(QVariant, QVariant::QVariant(micLevel))); + QMetaObject::invokeMethod(tablet, "setMicLevel", Qt::AutoConnection, Q_ARG(QVariant, QVariant(micLevel))); } } From fc186a281f9f667037e01190711440071bdf9304 Mon Sep 17 00:00:00 2001 From: Faye Li Date: Tue, 3 Jan 2017 14:37:25 -0800 Subject: [PATCH 7/7] update audio bar only when tablet is shown --- scripts/system/tablet-ui/tabletUI.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/system/tablet-ui/tabletUI.js b/scripts/system/tablet-ui/tabletUI.js index 2d7b69ad38..f6fa931709 100644 --- a/scripts/system/tablet-ui/tabletUI.js +++ b/scripts/system/tablet-ui/tabletUI.js @@ -95,9 +95,11 @@ } Script.setInterval(function() { - var currentMicLevel = getMicLevel(); - var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); - tablet.updateAudioBar(currentMicLevel); + if (tabletShown) { + var currentMicLevel = getMicLevel(); + var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"); + tablet.updateAudioBar(currentMicLevel); + } }, MIC_LEVEL_UPDATE_INTERVAL_MS); }()); // END LOCAL_SCOPE