From 497ee3a522eb6f9647964a016ae44421657f5459 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Mon, 19 Jun 2017 17:07:43 -0700 Subject: [PATCH] add Audio.inputLevel --- interface/src/scripting/Audio.cpp | 37 ++++++++++++++++++++-- interface/src/scripting/Audio.h | 7 ++++ libraries/audio-client/src/AudioClient.cpp | 2 ++ libraries/audio-client/src/AudioClient.h | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/interface/src/scripting/Audio.cpp b/interface/src/scripting/Audio.cpp index 0b99e60c89..4576190413 100644 --- a/interface/src/scripting/Audio.cpp +++ b/interface/src/scripting/Audio.cpp @@ -23,9 +23,33 @@ QString Audio::HMD { "VR" }; Setting::Handle enableNoiseReductionSetting { QStringList { Audio::AUDIO, "NoiseReduction" }, true }; +float Audio::loudnessToLevel(float loudness) { + const float LOG2 = log(2.0f); + const float METER_LOUDNESS_SCALE = 2.8f / 5.0f; + const float LOG2_LOUDNESS_FLOOR = 11.0f; + + float level = 0.0f; + + loudness += 1.0f; + float log2loudness = logf(loudness) / LOG2; + + if (log2loudness <= LOG2_LOUDNESS_FLOOR) { + level = (log2loudness / LOG2_LOUDNESS_FLOOR) * METER_LOUDNESS_SCALE; + } else { + level = (log2loudness - (LOG2_LOUDNESS_FLOOR - 1.0f)) * METER_LOUDNESS_SCALE; + } + + if (level > 1.0f) { + level = 1.0; + } + + return level; +} + Audio::Audio() : _devices(_contextIsHMD) { - auto client = DependencyManager::get(); - connect(client.data(), &AudioClient::muteToggled, this, &Audio::onMutedChanged); + auto client = DependencyManager::get().data(); + connect(client, &AudioClient::muteToggled, this, &Audio::onMutedChanged); + connect(client, &AudioClient::inputLoudnessChanged, this, &Audio::onInputLoudnessChanged); connect(this, &Audio::contextChanged, &_devices, &AudioDevices::onContextChanged); connect(&_devices._inputs, &AudioDeviceList::deviceChanged, this, &Audio::onInputChanged); enableNoiseReduction(enableNoiseReductionSetting.get()); @@ -88,6 +112,15 @@ void Audio::onInputChanged() { } } +void Audio::onInputLoudnessChanged(float loudness) { + float level = loudnessToLevel(loudness); + + if (_inputLevel != level) { + _inputLevel = level; + emit inputLevelChanged(_inputLevel); + } +} + QString Audio::getContext() const { return _contextIsHMD ? Audio::HMD : Audio::DESKTOP; } diff --git a/interface/src/scripting/Audio.h b/interface/src/scripting/Audio.h index d438df41ca..953727ede8 100644 --- a/interface/src/scripting/Audio.h +++ b/interface/src/scripting/Audio.h @@ -26,6 +26,7 @@ class Audio : public AudioScriptingInterface { Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged) Q_PROPERTY(bool noiseReduction READ noiseReductionEnabled WRITE enableNoiseReduction NOTIFY noiseReductionChanged) Q_PROPERTY(float inputVolume READ getInputVolume WRITE setInputVolume NOTIFY inputVolumeChanged) + Q_PROPERTY(float inputLevel READ getInputLevel NOTIFY inputLevelChanged) Q_PROPERTY(QString context READ getContext NOTIFY contextChanged) Q_PROPERTY(AudioDevices* devices READ getDevices NOTIFY nop) @@ -34,11 +35,14 @@ public: static QString HMD; static QString DESKTOP; + static float loudnessToLevel(float loudness); + virtual ~Audio() {} bool isMuted() const { return _isMuted; } bool noiseReductionEnabled() const { return _enableNoiseReduction; } float getInputVolume() const { return _inputVolume; } + float getInputLevel() const { return _inputLevel; } QString getContext() const; void setMuted(bool muted); @@ -54,12 +58,14 @@ signals: void mutedChanged(bool isMuted); void noiseReductionChanged(bool isEnabled); void inputVolumeChanged(float volume); + void inputLevelChanged(float level); void contextChanged(const QString& context); public slots: void onMutedChanged(); void onContextChanged(); void onInputChanged(); + void onInputLoudnessChanged(float loudness); protected: // Audio must live on a separate thread from AudioClient to avoid deadlocks @@ -68,6 +74,7 @@ protected: private: float _inputVolume { 1.0f }; + float _inputLevel { 0.0f }; bool _isMuted { false }; bool _enableNoiseReduction; bool _contextIsHMD { false }; diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index f32d37562a..fc54a04a5e 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -1023,6 +1023,8 @@ void AudioClient::handleAudioInput(QByteArray& audioBuffer) { emit inputReceived(audioBuffer); } + emit inputLoudnessChanged(_lastInputLoudness); + // state machine to detect gate opening and closing bool audioGateOpen = (_lastInputLoudness != 0.0f); bool openedInLastBlock = !_audioGateOpen && audioGateOpen; // the gate just opened diff --git a/libraries/audio-client/src/AudioClient.h b/libraries/audio-client/src/AudioClient.h index 01241938d9..54ce3aa6c2 100644 --- a/libraries/audio-client/src/AudioClient.h +++ b/libraries/audio-client/src/AudioClient.h @@ -210,6 +210,7 @@ signals: bool muteToggled(); void mutedByMixer(); void inputReceived(const QByteArray& inputSamples); + void inputLoudnessChanged(float loudness); void outputBytesToNetwork(int numBytes); void inputBytesFromNetwork(int numBytes); void noiseGateOpened();