From c47d80574e61e49564ff281e38fc584ac65fc6ba Mon Sep 17 00:00:00 2001 From: Ken Cooke Date: Tue, 23 May 2017 15:32:41 -0700 Subject: [PATCH] Replace the other noise gate, in Agent.cpp --- assignment-client/src/Agent.cpp | 54 ++++++++++++++++++++++++--------- assignment-client/src/Agent.h | 5 +-- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index 5864cadc15..ef396a06b3 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -55,7 +55,8 @@ static const int RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES = 10; Agent::Agent(ReceivedMessage& message) : ThreadedAssignment(message), - _receivedAudioStream(RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES, RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES) + _receivedAudioStream(RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES, RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES), + _audioGate(AudioConstants::SAMPLE_RATE, AudioConstants::MONO) { _entityEditSender.setPacketsPerSecond(DEFAULT_ENTITY_PPS_PER_SCRIPT); DependencyManager::get()->setPacketSender(&_entityEditSender); @@ -397,16 +398,23 @@ void Agent::executeScript() { QByteArray audio(frame->data); if (_isNoiseGateEnabled) { - static int numSamples = AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL; - _noiseGate.gateSamples(reinterpret_cast(audio.data()), numSamples); + int16_t* samples = reinterpret_cast(audio.data()); + int numSamples = AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL; + _audioGate.render(samples, samples, numSamples); } computeLoudness(&audio, scriptedAvatar); - // the codec needs a flush frame before sending silent packets, so - // do not send one if the gate closed in this block (eventually this can be crossfaded). + // state machine to detect gate opening and closing + bool audioGateOpen = (scriptedAvatar->getAudioLoudness() != 0.0f); + bool openedInLastBlock = !_audioGateOpen && audioGateOpen; // the gate just opened + bool closedInLastBlock = _audioGateOpen && !audioGateOpen; // the gate just closed + _audioGateOpen = audioGateOpen; + + // the codec must be flushed to silence before sending silent packets, + // so delay the transition to silent packets by one packet after becoming silent. auto packetType = PacketType::MicrophoneAudioNoEcho; - if (scriptedAvatar->getAudioLoudness() == 0.0f && !_noiseGate.closedInLastBlock()) { + if (!audioGateOpen && !closedInLastBlock) { packetType = PacketType::SilentAudioFrame; } @@ -619,19 +627,35 @@ void Agent::encodeFrameOfZeros(QByteArray& encodedZeros) { } void Agent::computeLoudness(const QByteArray* decodedBuffer, QSharedPointer scriptableAvatar) { - float loudness = 0.0f; + //float loudness = 0.0f; + //if (decodedBuffer) { + // auto soundData = reinterpret_cast(decodedBuffer->constData()); + // int numFrames = decodedBuffer->size() / sizeof(int16_t); + // // now iterate and come up with average + // if (numFrames > 0) { + // for(int i = 0; i < numFrames; i++) { + // loudness += (float) std::abs(soundData[i]); + // } + // loudness /= numFrames; + // } + //} + //scriptableAvatar->setAudioLoudness(loudness); + + float lastInputLoudness = 0.0f; if (decodedBuffer) { - auto soundData = reinterpret_cast(decodedBuffer->constData()); - int numFrames = decodedBuffer->size() / sizeof(int16_t); - // now iterate and come up with average - if (numFrames > 0) { - for(int i = 0; i < numFrames; i++) { - loudness += (float) std::abs(soundData[i]); + auto samples = reinterpret_cast(decodedBuffer->constData()); + int numSamples = decodedBuffer->size() / AudioConstants::SAMPLE_SIZE; + + assert(numSamples < 65536); // int32_t loudness cannot overflow + if (numSamples > 0) { + int32_t loudness = 0; + for (int i = 0; i < numSamples; ++i) { + loudness += std::abs((int32_t)samples[i]); } - loudness /= numFrames; + lastInputLoudness = (float)loudness / numSamples; } } - scriptableAvatar->setAudioLoudness(loudness); + scriptableAvatar->setAudioLoudness(lastInputLoudness); } void Agent::processAgentAvatarAudio() { diff --git a/assignment-client/src/Agent.h b/assignment-client/src/Agent.h index 2a156aba18..edba366601 100644 --- a/assignment-client/src/Agent.h +++ b/assignment-client/src/Agent.h @@ -29,7 +29,7 @@ #include -#include "AudioNoiseGate.h" +#include "AudioGate.h" #include "MixedAudioStream.h" #include "avatars/ScriptableAvatar.h" @@ -111,7 +111,8 @@ private: QTimer* _avatarIdentityTimer = nullptr; QHash _outgoingScriptAudioSequenceNumbers; - AudioNoiseGate _noiseGate; + AudioGate _audioGate; + bool _audioGateOpen { false }; bool _isNoiseGateEnabled { false }; CodecPluginPointer _codec;