1 /*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_processing/agc2/vad_with_level.h"
12
13 #include <algorithm>
14 #include <array>
15 #include <cmath>
16
17 #include "api/array_view.h"
18 #include "common_audio/include/audio_util.h"
19 #include "common_audio/resampler/include/push_resampler.h"
20 #include "modules/audio_processing/agc2/agc2_common.h"
21 #include "modules/audio_processing/agc2/rnn_vad/common.h"
22 #include "modules/audio_processing/agc2/rnn_vad/features_extraction.h"
23 #include "modules/audio_processing/agc2/rnn_vad/rnn.h"
24 #include "rtc_base/checks.h"
25
26 namespace webrtc {
27 namespace {
28
29 using VoiceActivityDetector = VadLevelAnalyzer::VoiceActivityDetector;
30
31 // Default VAD that combines a resampler and the RNN VAD.
32 // Computes the speech probability on the first channel.
33 class Vad : public VoiceActivityDetector {
34 public:
35 Vad() = default;
36 Vad(const Vad&) = delete;
37 Vad& operator=(const Vad&) = delete;
38 ~Vad() = default;
39
ComputeProbability(AudioFrameView<const float> frame)40 float ComputeProbability(AudioFrameView<const float> frame) override {
41 // The source number of channels is 1, because we always use the 1st
42 // channel.
43 resampler_.InitializeIfNeeded(
44 /*sample_rate_hz=*/static_cast<int>(frame.samples_per_channel() * 100),
45 rnn_vad::kSampleRate24kHz,
46 /*num_channels=*/1);
47
48 std::array<float, rnn_vad::kFrameSize10ms24kHz> work_frame;
49 // Feed the 1st channel to the resampler.
50 resampler_.Resample(frame.channel(0).data(), frame.samples_per_channel(),
51 work_frame.data(), rnn_vad::kFrameSize10ms24kHz);
52
53 std::array<float, rnn_vad::kFeatureVectorSize> feature_vector;
54 const bool is_silence = features_extractor_.CheckSilenceComputeFeatures(
55 work_frame, feature_vector);
56 return rnn_vad_.ComputeVadProbability(feature_vector, is_silence);
57 }
58
59 private:
60 PushResampler<float> resampler_;
61 rnn_vad::FeaturesExtractor features_extractor_;
62 rnn_vad::RnnBasedVad rnn_vad_;
63 };
64
65 // Returns an updated version of `p_old` by using instant decay and the given
66 // `attack` on a new VAD probability value `p_new`.
SmoothedVadProbability(float p_old,float p_new,float attack)67 float SmoothedVadProbability(float p_old, float p_new, float attack) {
68 RTC_DCHECK_GT(attack, 0.f);
69 RTC_DCHECK_LE(attack, 1.f);
70 if (p_new < p_old || attack == 1.f) {
71 // Instant decay (or no smoothing).
72 return p_new;
73 } else {
74 // Attack phase.
75 return attack * p_new + (1.f - attack) * p_old;
76 }
77 }
78
79 } // namespace
80
VadLevelAnalyzer()81 VadLevelAnalyzer::VadLevelAnalyzer()
82 : VadLevelAnalyzer(kDefaultSmoothedVadProbabilityAttack,
83 std::make_unique<Vad>()) {}
84
VadLevelAnalyzer(float vad_probability_attack)85 VadLevelAnalyzer::VadLevelAnalyzer(float vad_probability_attack)
86 : VadLevelAnalyzer(vad_probability_attack, std::make_unique<Vad>()) {}
87
VadLevelAnalyzer(float vad_probability_attack,std::unique_ptr<VoiceActivityDetector> vad)88 VadLevelAnalyzer::VadLevelAnalyzer(float vad_probability_attack,
89 std::unique_ptr<VoiceActivityDetector> vad)
90 : vad_(std::move(vad)), vad_probability_attack_(vad_probability_attack) {
91 RTC_DCHECK(vad_);
92 }
93
94 VadLevelAnalyzer::~VadLevelAnalyzer() = default;
95
AnalyzeFrame(AudioFrameView<const float> frame)96 VadLevelAnalyzer::Result VadLevelAnalyzer::AnalyzeFrame(
97 AudioFrameView<const float> frame) {
98 // Compute levels.
99 float peak = 0.f;
100 float rms = 0.f;
101 for (const auto& x : frame.channel(0)) {
102 peak = std::max(std::fabs(x), peak);
103 rms += x * x;
104 }
105 // Compute smoothed speech probability.
106 vad_probability_ = SmoothedVadProbability(
107 /*p_old=*/vad_probability_, /*p_new=*/vad_->ComputeProbability(frame),
108 vad_probability_attack_);
109 return {vad_probability_,
110 FloatS16ToDbfs(std::sqrt(rms / frame.samples_per_channel())),
111 FloatS16ToDbfs(peak)};
112 }
113
114 } // namespace webrtc
115