xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc/agc.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc/agc.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <cmath>
14*d9f75844SAndroid Build Coastguard Worker #include <cstdlib>
15*d9f75844SAndroid Build Coastguard Worker #include <vector>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc/loudness_histogram.h"
18*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/agc/utility.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
22*d9f75844SAndroid Build Coastguard Worker namespace {
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker constexpr int kDefaultLevelDbfs = -18;
25*d9f75844SAndroid Build Coastguard Worker constexpr int kNumAnalysisFrames = 100;
26*d9f75844SAndroid Build Coastguard Worker constexpr double kActivityThreshold = 0.3;
27*d9f75844SAndroid Build Coastguard Worker constexpr int kNum10msFramesInOneSecond = 100;
28*d9f75844SAndroid Build Coastguard Worker constexpr int kMaxSampleRateHz = 384000;
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker }  // namespace
31*d9f75844SAndroid Build Coastguard Worker 
Agc()32*d9f75844SAndroid Build Coastguard Worker Agc::Agc()
33*d9f75844SAndroid Build Coastguard Worker     : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
34*d9f75844SAndroid Build Coastguard Worker       target_level_dbfs_(kDefaultLevelDbfs),
35*d9f75844SAndroid Build Coastguard Worker       histogram_(LoudnessHistogram::Create(kNumAnalysisFrames)),
36*d9f75844SAndroid Build Coastguard Worker       inactive_histogram_(LoudnessHistogram::Create()) {}
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker Agc::~Agc() = default;
39*d9f75844SAndroid Build Coastguard Worker 
Process(rtc::ArrayView<const int16_t> audio)40*d9f75844SAndroid Build Coastguard Worker void Agc::Process(rtc::ArrayView<const int16_t> audio) {
41*d9f75844SAndroid Build Coastguard Worker   const int sample_rate_hz = audio.size() * kNum10msFramesInOneSecond;
42*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(sample_rate_hz, kMaxSampleRateHz);
43*d9f75844SAndroid Build Coastguard Worker   vad_.ProcessChunk(audio.data(), audio.size(), sample_rate_hz);
44*d9f75844SAndroid Build Coastguard Worker   const std::vector<double>& rms = vad_.chunkwise_rms();
45*d9f75844SAndroid Build Coastguard Worker   const std::vector<double>& probabilities =
46*d9f75844SAndroid Build Coastguard Worker       vad_.chunkwise_voice_probabilities();
47*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(rms.size(), probabilities.size());
48*d9f75844SAndroid Build Coastguard Worker   for (size_t i = 0; i < rms.size(); ++i) {
49*d9f75844SAndroid Build Coastguard Worker     histogram_->Update(rms[i], probabilities[i]);
50*d9f75844SAndroid Build Coastguard Worker   }
51*d9f75844SAndroid Build Coastguard Worker }
52*d9f75844SAndroid Build Coastguard Worker 
GetRmsErrorDb(int * error)53*d9f75844SAndroid Build Coastguard Worker bool Agc::GetRmsErrorDb(int* error) {
54*d9f75844SAndroid Build Coastguard Worker   if (!error) {
55*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_NOTREACHED();
56*d9f75844SAndroid Build Coastguard Worker     return false;
57*d9f75844SAndroid Build Coastguard Worker   }
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   if (histogram_->num_updates() < kNumAnalysisFrames) {
60*d9f75844SAndroid Build Coastguard Worker     // We haven't yet received enough frames.
61*d9f75844SAndroid Build Coastguard Worker     return false;
62*d9f75844SAndroid Build Coastguard Worker   }
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker   if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
65*d9f75844SAndroid Build Coastguard Worker     // We are likely in an inactive segment.
66*d9f75844SAndroid Build Coastguard Worker     return false;
67*d9f75844SAndroid Build Coastguard Worker   }
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker   double loudness = Linear2Loudness(histogram_->CurrentRms());
70*d9f75844SAndroid Build Coastguard Worker   *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
71*d9f75844SAndroid Build Coastguard Worker   histogram_->Reset();
72*d9f75844SAndroid Build Coastguard Worker   return true;
73*d9f75844SAndroid Build Coastguard Worker }
74*d9f75844SAndroid Build Coastguard Worker 
Reset()75*d9f75844SAndroid Build Coastguard Worker void Agc::Reset() {
76*d9f75844SAndroid Build Coastguard Worker   histogram_->Reset();
77*d9f75844SAndroid Build Coastguard Worker }
78*d9f75844SAndroid Build Coastguard Worker 
set_target_level_dbfs(int level)79*d9f75844SAndroid Build Coastguard Worker int Agc::set_target_level_dbfs(int level) {
80*d9f75844SAndroid Build Coastguard Worker   // TODO(turajs): just some arbitrary sanity check. We can come up with better
81*d9f75844SAndroid Build Coastguard Worker   // limits. The upper limit should be chosen such that the risk of clipping is
82*d9f75844SAndroid Build Coastguard Worker   // low. The lower limit should not result in a too quiet signal.
83*d9f75844SAndroid Build Coastguard Worker   if (level >= 0 || level <= -100)
84*d9f75844SAndroid Build Coastguard Worker     return -1;
85*d9f75844SAndroid Build Coastguard Worker   target_level_dbfs_ = level;
86*d9f75844SAndroid Build Coastguard Worker   target_level_loudness_ = Dbfs2Loudness(level);
87*d9f75844SAndroid Build Coastguard Worker   return 0;
88*d9f75844SAndroid Build Coastguard Worker }
89*d9f75844SAndroid Build Coastguard Worker 
target_level_dbfs() const90*d9f75844SAndroid Build Coastguard Worker int Agc::target_level_dbfs() const {
91*d9f75844SAndroid Build Coastguard Worker   return target_level_dbfs_;
92*d9f75844SAndroid Build Coastguard Worker }
93*d9f75844SAndroid Build Coastguard Worker 
voice_probability() const94*d9f75844SAndroid Build Coastguard Worker float Agc::voice_probability() const {
95*d9f75844SAndroid Build Coastguard Worker   return vad_.last_voice_probability();
96*d9f75844SAndroid Build Coastguard Worker }
97*d9f75844SAndroid Build Coastguard Worker 
98*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
99