xref: /aosp_15_r20/external/webrtc/modules/audio_processing/vad/voice_activity_detector.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2015 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 #ifndef MODULES_AUDIO_PROCESSING_VAD_VOICE_ACTIVITY_DETECTOR_H_
12 #define MODULES_AUDIO_PROCESSING_VAD_VOICE_ACTIVITY_DETECTOR_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <memory>
18 #include <vector>
19 
20 #include "common_audio/resampler/include/resampler.h"
21 #include "modules/audio_processing/vad/common.h"
22 #include "modules/audio_processing/vad/pitch_based_vad.h"
23 #include "modules/audio_processing/vad/standalone_vad.h"
24 #include "modules/audio_processing/vad/vad_audio_proc.h"
25 
26 namespace webrtc {
27 
28 // A Voice Activity Detector (VAD) that combines the voice probability from the
29 // StandaloneVad and PitchBasedVad to get a more robust estimation.
30 class VoiceActivityDetector {
31  public:
32   VoiceActivityDetector();
33   ~VoiceActivityDetector();
34 
35   // Processes each audio chunk and estimates the voice probability.
36   // TODO(bugs.webrtc.org/7494): Switch to rtc::ArrayView and remove
37   // `sample_rate_hz`.
38   void ProcessChunk(const int16_t* audio, size_t length, int sample_rate_hz);
39 
40   // Returns a vector of voice probabilities for each chunk. It can be empty for
41   // some chunks, but it catches up afterwards returning multiple values at
42   // once.
chunkwise_voice_probabilities()43   const std::vector<double>& chunkwise_voice_probabilities() const {
44     return chunkwise_voice_probabilities_;
45   }
46 
47   // Returns a vector of RMS values for each chunk. It has the same length as
48   // chunkwise_voice_probabilities().
chunkwise_rms()49   const std::vector<double>& chunkwise_rms() const { return chunkwise_rms_; }
50 
51   // Returns the last voice probability, regardless of the internal
52   // implementation, although it has a few chunks of delay.
last_voice_probability()53   float last_voice_probability() const { return last_voice_probability_; }
54 
55  private:
56   // TODO(aluebs): Change these to float.
57   std::vector<double> chunkwise_voice_probabilities_;
58   std::vector<double> chunkwise_rms_;
59 
60   float last_voice_probability_;
61 
62   Resampler resampler_;
63   VadAudioProc audio_processing_;
64 
65   std::unique_ptr<StandaloneVad> standalone_vad_;
66   PitchBasedVad pitch_based_vad_;
67 
68   int16_t resampled_[kLength10Ms];
69   AudioFeatures features_;
70 };
71 
72 }  // namespace webrtc
73 
74 #endif  // MODULES_AUDIO_PROCESSING_VAD_VOICE_ACTIVITY_DETECTOR_H_
75