xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/vad_wrapper.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_wrapper.h"
12 
13 #include <array>
14 #include <utility>
15 
16 #include "api/array_view.h"
17 #include "common_audio/resampler/include/push_resampler.h"
18 #include "modules/audio_processing/agc2/agc2_common.h"
19 #include "modules/audio_processing/agc2/rnn_vad/common.h"
20 #include "modules/audio_processing/agc2/rnn_vad/features_extraction.h"
21 #include "modules/audio_processing/agc2/rnn_vad/rnn.h"
22 #include "rtc_base/checks.h"
23 
24 namespace webrtc {
25 namespace {
26 
27 constexpr int kNumFramesPerSecond = 100;
28 
29 class MonoVadImpl : public VoiceActivityDetectorWrapper::MonoVad {
30  public:
MonoVadImpl(const AvailableCpuFeatures & cpu_features)31   explicit MonoVadImpl(const AvailableCpuFeatures& cpu_features)
32       : features_extractor_(cpu_features), rnn_vad_(cpu_features) {}
33   MonoVadImpl(const MonoVadImpl&) = delete;
34   MonoVadImpl& operator=(const MonoVadImpl&) = delete;
35   ~MonoVadImpl() = default;
36 
SampleRateHz() const37   int SampleRateHz() const override { return rnn_vad::kSampleRate24kHz; }
Reset()38   void Reset() override { rnn_vad_.Reset(); }
Analyze(rtc::ArrayView<const float> frame)39   float Analyze(rtc::ArrayView<const float> frame) override {
40     RTC_DCHECK_EQ(frame.size(), rnn_vad::kFrameSize10ms24kHz);
41     std::array<float, rnn_vad::kFeatureVectorSize> feature_vector;
42     const bool is_silence = features_extractor_.CheckSilenceComputeFeatures(
43         /*samples=*/{frame.data(), rnn_vad::kFrameSize10ms24kHz},
44         feature_vector);
45     return rnn_vad_.ComputeVadProbability(feature_vector, is_silence);
46   }
47 
48  private:
49   rnn_vad::FeaturesExtractor features_extractor_;
50   rnn_vad::RnnVad rnn_vad_;
51 };
52 
53 }  // namespace
54 
VoiceActivityDetectorWrapper(int vad_reset_period_ms,const AvailableCpuFeatures & cpu_features,int sample_rate_hz)55 VoiceActivityDetectorWrapper::VoiceActivityDetectorWrapper(
56     int vad_reset_period_ms,
57     const AvailableCpuFeatures& cpu_features,
58     int sample_rate_hz)
59     : VoiceActivityDetectorWrapper(vad_reset_period_ms,
60                                    std::make_unique<MonoVadImpl>(cpu_features),
61                                    sample_rate_hz) {}
62 
VoiceActivityDetectorWrapper(int vad_reset_period_ms,std::unique_ptr<MonoVad> vad,int sample_rate_hz)63 VoiceActivityDetectorWrapper::VoiceActivityDetectorWrapper(
64     int vad_reset_period_ms,
65     std::unique_ptr<MonoVad> vad,
66     int sample_rate_hz)
67     : vad_reset_period_frames_(
68           rtc::CheckedDivExact(vad_reset_period_ms, kFrameDurationMs)),
69       time_to_vad_reset_(vad_reset_period_frames_),
70       vad_(std::move(vad)) {
71   RTC_DCHECK(vad_);
72   RTC_DCHECK_GT(vad_reset_period_frames_, 1);
73   resampled_buffer_.resize(
74       rtc::CheckedDivExact(vad_->SampleRateHz(), kNumFramesPerSecond));
75   Initialize(sample_rate_hz);
76 }
77 
78 VoiceActivityDetectorWrapper::~VoiceActivityDetectorWrapper() = default;
79 
Initialize(int sample_rate_hz)80 void VoiceActivityDetectorWrapper::Initialize(int sample_rate_hz) {
81   RTC_DCHECK_GT(sample_rate_hz, 0);
82   frame_size_ = rtc::CheckedDivExact(sample_rate_hz, kNumFramesPerSecond);
83   int status =
84       resampler_.InitializeIfNeeded(sample_rate_hz, vad_->SampleRateHz(),
85                                     /*num_channels=*/1);
86   constexpr int kStatusOk = 0;
87   RTC_DCHECK_EQ(status, kStatusOk);
88   vad_->Reset();
89 }
90 
Analyze(AudioFrameView<const float> frame)91 float VoiceActivityDetectorWrapper::Analyze(AudioFrameView<const float> frame) {
92   // Periodically reset the VAD.
93   time_to_vad_reset_--;
94   if (time_to_vad_reset_ <= 0) {
95     vad_->Reset();
96     time_to_vad_reset_ = vad_reset_period_frames_;
97   }
98   // Resample the first channel of `frame`.
99   RTC_DCHECK_EQ(frame.samples_per_channel(), frame_size_);
100   resampler_.Resample(frame.channel(0).data(), frame_size_,
101                       resampled_buffer_.data(), resampled_buffer_.size());
102 
103   return vad_->Analyze(resampled_buffer_);
104 }
105 
106 }  // namespace webrtc
107