Bump to WebRTC M120 release
Some API deprecation -- ExperimentalAgc and ExperimentalNs are gone. We're continuing to carry iSAC even though it's gone upstream, but maybe we'll want to drop that soon.
This commit is contained in:
@ -20,20 +20,23 @@ rtc_library("agc") {
|
||||
configs += [ "..:apm_debug_dump" ]
|
||||
deps = [
|
||||
":gain_control_interface",
|
||||
":gain_map",
|
||||
":level_estimation",
|
||||
"..:api",
|
||||
"..:apm_logging",
|
||||
"..:audio_buffer",
|
||||
"..:audio_frame_view",
|
||||
"../../../api:array_view",
|
||||
"../../../common_audio",
|
||||
"../../../common_audio:common_audio_c",
|
||||
"../../../rtc_base:checks",
|
||||
"../../../rtc_base:gtest_prod",
|
||||
"../../../rtc_base:logging",
|
||||
"../../../rtc_base:rtc_base_approved",
|
||||
"../../../rtc_base:safe_minmax",
|
||||
"../../../system_wrappers:field_trial",
|
||||
"../../../system_wrappers:metrics",
|
||||
"../agc2:level_estimation_agc",
|
||||
"../agc2:clipping_predictor",
|
||||
"../agc2:gain_map",
|
||||
"../agc2:input_volume_stats_reporter",
|
||||
"../vad",
|
||||
]
|
||||
absl_deps = [ "//third_party/abseil-cpp/absl/types:optional" ]
|
||||
@ -49,6 +52,7 @@ rtc_library("level_estimation") {
|
||||
"utility.h",
|
||||
]
|
||||
deps = [
|
||||
"../../../api:array_view",
|
||||
"../../../rtc_base:checks",
|
||||
"../vad",
|
||||
]
|
||||
@ -75,7 +79,6 @@ rtc_library("legacy_agc") {
|
||||
"../../../common_audio:common_audio_c",
|
||||
"../../../common_audio/third_party/ooura:fft_size_256",
|
||||
"../../../rtc_base:checks",
|
||||
"../../../rtc_base:rtc_base_approved",
|
||||
"../../../system_wrappers",
|
||||
]
|
||||
|
||||
@ -88,10 +91,6 @@ rtc_library("legacy_agc") {
|
||||
}
|
||||
}
|
||||
|
||||
rtc_source_set("gain_map") {
|
||||
sources = [ "gain_map_internal.h" ]
|
||||
}
|
||||
|
||||
if (rtc_include_tests) {
|
||||
rtc_library("agc_unittests") {
|
||||
testonly = true
|
||||
@ -107,10 +106,21 @@ if (rtc_include_tests) {
|
||||
":gain_control_interface",
|
||||
":level_estimation",
|
||||
"..:mocks",
|
||||
"../../../api:array_view",
|
||||
"../../../rtc_base:checks",
|
||||
"../../../rtc_base:random",
|
||||
"../../../rtc_base:safe_conversions",
|
||||
"../../../rtc_base:safe_minmax",
|
||||
"../../../rtc_base:stringutils",
|
||||
"../../../system_wrappers:metrics",
|
||||
"../../../test:field_trial",
|
||||
"../../../test:fileutils",
|
||||
"../../../test:test_support",
|
||||
"//testing/gtest",
|
||||
]
|
||||
absl_deps = [
|
||||
"//third_party/abseil-cpp/absl/strings",
|
||||
"//third_party/abseil-cpp/absl/types:optional",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -21,9 +21,11 @@
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
|
||||
const int kDefaultLevelDbfs = -18;
|
||||
const int kNumAnalysisFrames = 100;
|
||||
const double kActivityThreshold = 0.3;
|
||||
constexpr int kDefaultLevelDbfs = -18;
|
||||
constexpr int kNumAnalysisFrames = 100;
|
||||
constexpr double kActivityThreshold = 0.3;
|
||||
constexpr int kNum10msFramesInOneSecond = 100;
|
||||
constexpr int kMaxSampleRateHz = 384000;
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -35,8 +37,10 @@ Agc::Agc()
|
||||
|
||||
Agc::~Agc() = default;
|
||||
|
||||
void Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
|
||||
vad_.ProcessChunk(audio, length, sample_rate_hz);
|
||||
void Agc::Process(rtc::ArrayView<const int16_t> audio) {
|
||||
const int sample_rate_hz = audio.size() * kNum10msFramesInOneSecond;
|
||||
RTC_DCHECK_LE(sample_rate_hz, kMaxSampleRateHz);
|
||||
vad_.ProcessChunk(audio.data(), audio.size(), sample_rate_hz);
|
||||
const std::vector<double>& rms = vad_.chunkwise_rms();
|
||||
const std::vector<double>& probabilities =
|
||||
vad_.chunkwise_voice_probabilities();
|
||||
@ -48,7 +52,7 @@ void Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
|
||||
|
||||
bool Agc::GetRmsErrorDb(int* error) {
|
||||
if (!error) {
|
||||
RTC_NOTREACHED();
|
||||
RTC_DCHECK_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "api/array_view.h"
|
||||
#include "modules/audio_processing/vad/voice_activity_detector.h"
|
||||
|
||||
namespace webrtc {
|
||||
@ -24,13 +25,13 @@ class Agc {
|
||||
Agc();
|
||||
virtual ~Agc();
|
||||
|
||||
// |audio| must be mono; in a multi-channel stream, provide the first (usually
|
||||
// `audio` must be mono; in a multi-channel stream, provide the first (usually
|
||||
// left) channel.
|
||||
virtual void Process(const int16_t* audio, size_t length, int sample_rate_hz);
|
||||
virtual void Process(rtc::ArrayView<const int16_t> audio);
|
||||
|
||||
// Retrieves the difference between the target RMS level and the current
|
||||
// signal RMS level in dB. Returns true if an update is available and false
|
||||
// otherwise, in which case |error| should be ignored and no action taken.
|
||||
// otherwise, in which case `error` should be ignored and no action taken.
|
||||
virtual bool GetRmsErrorDb(int* error);
|
||||
virtual void Reset();
|
||||
|
||||
|
@ -13,11 +13,12 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "api/array_view.h"
|
||||
#include "common_audio/include/audio_util.h"
|
||||
#include "modules/audio_processing/agc/gain_control.h"
|
||||
#include "modules/audio_processing/agc/gain_map_internal.h"
|
||||
#include "modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h"
|
||||
#include "rtc_base/atomic_ops.h"
|
||||
#include "modules/audio_processing/agc2/gain_map_internal.h"
|
||||
#include "modules/audio_processing/agc2/input_volume_stats_reporter.h"
|
||||
#include "modules/audio_processing/include/audio_frame_view.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/numerics/safe_minmax.h"
|
||||
@ -28,69 +29,65 @@ namespace webrtc {
|
||||
|
||||
namespace {
|
||||
|
||||
// Amount the microphone level is lowered with every clipping event.
|
||||
const int kClippedLevelStep = 15;
|
||||
// Proportion of clipped samples required to declare a clipping event.
|
||||
const float kClippedRatioThreshold = 0.1f;
|
||||
// Time in frames to wait after a clipping event before checking again.
|
||||
const int kClippedWaitFrames = 300;
|
||||
|
||||
// Amount of error we tolerate in the microphone level (presumably due to OS
|
||||
// quantization) before we assume the user has manually adjusted the microphone.
|
||||
const int kLevelQuantizationSlack = 25;
|
||||
constexpr int kLevelQuantizationSlack = 25;
|
||||
|
||||
const int kDefaultCompressionGain = 7;
|
||||
const int kMaxCompressionGain = 12;
|
||||
const int kMinCompressionGain = 2;
|
||||
constexpr int kDefaultCompressionGain = 7;
|
||||
constexpr int kMaxCompressionGain = 12;
|
||||
constexpr int kMinCompressionGain = 2;
|
||||
// Controls the rate of compression changes towards the target.
|
||||
const float kCompressionGainStep = 0.05f;
|
||||
constexpr float kCompressionGainStep = 0.05f;
|
||||
|
||||
const int kMaxMicLevel = 255;
|
||||
constexpr int kMaxMicLevel = 255;
|
||||
static_assert(kGainMapSize > kMaxMicLevel, "gain map too small");
|
||||
const int kMinMicLevel = 12;
|
||||
constexpr int kMinMicLevel = 12;
|
||||
|
||||
// Prevent very large microphone level changes.
|
||||
const int kMaxResidualGainChange = 15;
|
||||
constexpr int kMaxResidualGainChange = 15;
|
||||
|
||||
// Maximum additional gain allowed to compensate for microphone level
|
||||
// restrictions from clipping events.
|
||||
const int kSurplusCompressionGain = 6;
|
||||
constexpr int kSurplusCompressionGain = 6;
|
||||
|
||||
// Returns whether a fall-back solution to choose the maximum level should be
|
||||
// chosen.
|
||||
bool UseMaxAnalogChannelLevel() {
|
||||
return field_trial::IsEnabled("WebRTC-UseMaxAnalogAgcChannelLevel");
|
||||
}
|
||||
// Target speech level (dBFs) and speech probability threshold used to compute
|
||||
// the RMS error override in `GetSpeechLevelErrorDb()`. These are only used for
|
||||
// computing the error override and they are not passed to `agc_`.
|
||||
// TODO(webrtc:7494): Move these to a config and pass in the ctor.
|
||||
constexpr float kOverrideTargetSpeechLevelDbfs = -18.0f;
|
||||
constexpr float kOverrideSpeechProbabilitySilenceThreshold = 0.5f;
|
||||
// The minimum number of frames between `UpdateGain()` calls.
|
||||
// TODO(webrtc:7494): Move this to a config and pass in the ctor with
|
||||
// kOverrideWaitFrames = 100. Default value zero needed for the unit tests.
|
||||
constexpr int kOverrideWaitFrames = 0;
|
||||
|
||||
// Returns kMinMicLevel if no field trial exists or if it has been disabled.
|
||||
// Returns a value between 0 and 255 depending on the field-trial string.
|
||||
// Example: 'WebRTC-Audio-AgcMinMicLevelExperiment/Enabled-80' => returns 80.
|
||||
int GetMinMicLevel() {
|
||||
RTC_LOG(LS_INFO) << "[agc] GetMinMicLevel";
|
||||
using AnalogAgcConfig =
|
||||
AudioProcessing::Config::GainController1::AnalogGainController;
|
||||
|
||||
// If the "WebRTC-Audio-2ndAgcMinMicLevelExperiment" field trial is specified,
|
||||
// parses it and returns a value between 0 and 255 depending on the field-trial
|
||||
// string. Returns an unspecified value if the field trial is not specified, if
|
||||
// disabled or if it cannot be parsed. Example:
|
||||
// 'WebRTC-Audio-2ndAgcMinMicLevelExperiment/Enabled-80' => returns 80.
|
||||
absl::optional<int> GetMinMicLevelOverride() {
|
||||
constexpr char kMinMicLevelFieldTrial[] =
|
||||
"WebRTC-Audio-AgcMinMicLevelExperiment";
|
||||
"WebRTC-Audio-2ndAgcMinMicLevelExperiment";
|
||||
if (!webrtc::field_trial::IsEnabled(kMinMicLevelFieldTrial)) {
|
||||
RTC_LOG(LS_INFO) << "[agc] Using default min mic level: " << kMinMicLevel;
|
||||
return kMinMicLevel;
|
||||
return absl::nullopt;
|
||||
}
|
||||
const auto field_trial_string =
|
||||
webrtc::field_trial::FindFullName(kMinMicLevelFieldTrial);
|
||||
int min_mic_level = -1;
|
||||
sscanf(field_trial_string.c_str(), "Enabled-%d", &min_mic_level);
|
||||
if (min_mic_level >= 0 && min_mic_level <= 255) {
|
||||
RTC_LOG(LS_INFO) << "[agc] Experimental min mic level: " << min_mic_level;
|
||||
return min_mic_level;
|
||||
} else {
|
||||
RTC_LOG(LS_WARNING) << "[agc] Invalid parameter for "
|
||||
<< kMinMicLevelFieldTrial << ", ignored.";
|
||||
return kMinMicLevel;
|
||||
return absl::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
int ClampLevel(int mic_level, int min_mic_level) {
|
||||
return rtc::SafeClamp(mic_level, min_mic_level, kMaxMicLevel);
|
||||
}
|
||||
|
||||
int LevelFromGainError(int gain_error, int level, int min_mic_level) {
|
||||
RTC_DCHECK_GE(level, 0);
|
||||
RTC_DCHECK_LE(level, kMaxMicLevel);
|
||||
@ -124,7 +121,7 @@ float ComputeClippedRatio(const float* const* audio,
|
||||
int num_clipped_in_ch = 0;
|
||||
for (size_t i = 0; i < samples_per_channel; ++i) {
|
||||
RTC_DCHECK(audio[ch]);
|
||||
if (audio[ch][i] >= 32767.f || audio[ch][i] <= -32768.f) {
|
||||
if (audio[ch][i] >= 32767.0f || audio[ch][i] <= -32768.0f) {
|
||||
++num_clipped_in_ch;
|
||||
}
|
||||
}
|
||||
@ -133,29 +130,49 @@ float ComputeClippedRatio(const float* const* audio,
|
||||
return static_cast<float>(num_clipped) / (samples_per_channel);
|
||||
}
|
||||
|
||||
void LogClippingMetrics(int clipping_rate) {
|
||||
RTC_LOG(LS_INFO) << "Input clipping rate: " << clipping_rate << "%";
|
||||
RTC_HISTOGRAM_COUNTS_LINEAR(/*name=*/"WebRTC.Audio.Agc.InputClippingRate",
|
||||
/*sample=*/clipping_rate, /*min=*/0, /*max=*/100,
|
||||
/*bucket_count=*/50);
|
||||
}
|
||||
|
||||
// Computes the speech level error in dB. `speech_level_dbfs` is required to be
|
||||
// in the range [-90.0f, 30.0f] and `speech_probability` in the range
|
||||
// [0.0f, 1.0f].
|
||||
int GetSpeechLevelErrorDb(float speech_level_dbfs, float speech_probability) {
|
||||
constexpr float kMinSpeechLevelDbfs = -90.0f;
|
||||
constexpr float kMaxSpeechLevelDbfs = 30.0f;
|
||||
RTC_DCHECK_GE(speech_level_dbfs, kMinSpeechLevelDbfs);
|
||||
RTC_DCHECK_LE(speech_level_dbfs, kMaxSpeechLevelDbfs);
|
||||
RTC_DCHECK_GE(speech_probability, 0.0f);
|
||||
RTC_DCHECK_LE(speech_probability, 1.0f);
|
||||
|
||||
if (speech_probability < kOverrideSpeechProbabilitySilenceThreshold) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const float speech_level = rtc::SafeClamp<float>(
|
||||
speech_level_dbfs, kMinSpeechLevelDbfs, kMaxSpeechLevelDbfs);
|
||||
|
||||
return std::round(kOverrideTargetSpeechLevelDbfs - speech_level);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
MonoAgc::MonoAgc(ApmDataDumper* data_dumper,
|
||||
int startup_min_level,
|
||||
int clipped_level_min,
|
||||
bool use_agc2_level_estimation,
|
||||
bool disable_digital_adaptive,
|
||||
int min_mic_level)
|
||||
: min_mic_level_(min_mic_level),
|
||||
disable_digital_adaptive_(disable_digital_adaptive),
|
||||
agc_(std::make_unique<Agc>()),
|
||||
max_level_(kMaxMicLevel),
|
||||
max_compression_gain_(kMaxCompressionGain),
|
||||
target_compression_(kDefaultCompressionGain),
|
||||
compression_(target_compression_),
|
||||
compression_accumulator_(compression_),
|
||||
startup_min_level_(ClampLevel(startup_min_level, min_mic_level_)),
|
||||
clipped_level_min_(clipped_level_min) {
|
||||
if (use_agc2_level_estimation) {
|
||||
agc_ = std::make_unique<AdaptiveModeLevelEstimatorAgc>(data_dumper);
|
||||
} else {
|
||||
agc_ = std::make_unique<Agc>();
|
||||
}
|
||||
}
|
||||
clipped_level_min_(clipped_level_min) {}
|
||||
|
||||
MonoAgc::~MonoAgc() = default;
|
||||
|
||||
@ -165,13 +182,14 @@ void MonoAgc::Initialize() {
|
||||
target_compression_ = disable_digital_adaptive_ ? 0 : kDefaultCompressionGain;
|
||||
compression_ = disable_digital_adaptive_ ? 0 : target_compression_;
|
||||
compression_accumulator_ = compression_;
|
||||
capture_muted_ = false;
|
||||
capture_output_used_ = true;
|
||||
check_volume_on_next_process_ = true;
|
||||
frames_since_update_gain_ = 0;
|
||||
is_first_frame_ = true;
|
||||
}
|
||||
|
||||
void MonoAgc::Process(const int16_t* audio,
|
||||
size_t samples_per_channel,
|
||||
int sample_rate_hz) {
|
||||
void MonoAgc::Process(rtc::ArrayView<const int16_t> audio,
|
||||
absl::optional<int> rms_error_override) {
|
||||
new_compression_to_set_ = absl::nullopt;
|
||||
|
||||
if (check_volume_on_next_process_) {
|
||||
@ -181,34 +199,60 @@ void MonoAgc::Process(const int16_t* audio,
|
||||
CheckVolumeAndReset();
|
||||
}
|
||||
|
||||
agc_->Process(audio, samples_per_channel, sample_rate_hz);
|
||||
agc_->Process(audio);
|
||||
|
||||
// Always check if `agc_` has a new error available. If yes, `agc_` gets
|
||||
// reset.
|
||||
// TODO(webrtc:7494) Replace the `agc_` call `GetRmsErrorDb()` with `Reset()`
|
||||
// if an error override is used.
|
||||
int rms_error = 0;
|
||||
bool update_gain = agc_->GetRmsErrorDb(&rms_error);
|
||||
if (rms_error_override.has_value()) {
|
||||
if (is_first_frame_ || frames_since_update_gain_ < kOverrideWaitFrames) {
|
||||
update_gain = false;
|
||||
} else {
|
||||
rms_error = *rms_error_override;
|
||||
update_gain = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (update_gain) {
|
||||
UpdateGain(rms_error);
|
||||
}
|
||||
|
||||
UpdateGain();
|
||||
if (!disable_digital_adaptive_) {
|
||||
UpdateCompressor();
|
||||
}
|
||||
|
||||
is_first_frame_ = false;
|
||||
if (frames_since_update_gain_ < kOverrideWaitFrames) {
|
||||
++frames_since_update_gain_;
|
||||
}
|
||||
}
|
||||
|
||||
void MonoAgc::HandleClipping() {
|
||||
void MonoAgc::HandleClipping(int clipped_level_step) {
|
||||
RTC_DCHECK_GT(clipped_level_step, 0);
|
||||
// Always decrease the maximum level, even if the current level is below
|
||||
// threshold.
|
||||
SetMaxLevel(std::max(clipped_level_min_, max_level_ - kClippedLevelStep));
|
||||
SetMaxLevel(std::max(clipped_level_min_, max_level_ - clipped_level_step));
|
||||
if (log_to_histograms_) {
|
||||
RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.AgcClippingAdjustmentAllowed",
|
||||
level_ - kClippedLevelStep >= clipped_level_min_);
|
||||
level_ - clipped_level_step >= clipped_level_min_);
|
||||
}
|
||||
if (level_ > clipped_level_min_) {
|
||||
// Don't try to adjust the level if we're already below the limit. As
|
||||
// a consequence, if the user has brought the level above the limit, we
|
||||
// will still not react until the postproc updates the level.
|
||||
SetLevel(std::max(clipped_level_min_, level_ - kClippedLevelStep));
|
||||
SetLevel(std::max(clipped_level_min_, level_ - clipped_level_step));
|
||||
// Reset the AGCs for all channels since the level has changed.
|
||||
agc_->Reset();
|
||||
frames_since_update_gain_ = 0;
|
||||
is_first_frame_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void MonoAgc::SetLevel(int new_level) {
|
||||
int voe_level = stream_analog_level_;
|
||||
int voe_level = recommended_input_volume_;
|
||||
if (voe_level == 0) {
|
||||
RTC_DLOG(LS_INFO)
|
||||
<< "[agc] VolumeCallbacks returned level=0, taking no action.";
|
||||
@ -220,6 +264,10 @@ void MonoAgc::SetLevel(int new_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Detect manual input volume adjustments by checking if the current level
|
||||
// `voe_level` is outside of the `[level_ - kLevelQuantizationSlack, level_ +
|
||||
// kLevelQuantizationSlack]` range where `level_` is the last input volume
|
||||
// known by this gain controller.
|
||||
if (voe_level > level_ + kLevelQuantizationSlack ||
|
||||
voe_level < level_ - kLevelQuantizationSlack) {
|
||||
RTC_DLOG(LS_INFO) << "[agc] Mic volume was manually adjusted. Updating "
|
||||
@ -234,7 +282,8 @@ void MonoAgc::SetLevel(int new_level) {
|
||||
// was manually adjusted. The compressor will still provide some of the
|
||||
// desired gain change.
|
||||
agc_->Reset();
|
||||
|
||||
frames_since_update_gain_ = 0;
|
||||
is_first_frame_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -243,7 +292,7 @@ void MonoAgc::SetLevel(int new_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
stream_analog_level_ = new_level;
|
||||
recommended_input_volume_ = new_level;
|
||||
RTC_DLOG(LS_INFO) << "[agc] voe_level=" << voe_level << ", level_=" << level_
|
||||
<< ", new_level=" << new_level;
|
||||
level_ = new_level;
|
||||
@ -252,7 +301,7 @@ void MonoAgc::SetLevel(int new_level) {
|
||||
void MonoAgc::SetMaxLevel(int level) {
|
||||
RTC_DCHECK_GE(level, clipped_level_min_);
|
||||
max_level_ = level;
|
||||
// Scale the |kSurplusCompressionGain| linearly across the restricted
|
||||
// Scale the `kSurplusCompressionGain` linearly across the restricted
|
||||
// level range.
|
||||
max_compression_gain_ =
|
||||
kMaxCompressionGain + std::floor((1.f * kMaxMicLevel - max_level_) /
|
||||
@ -263,23 +312,23 @@ void MonoAgc::SetMaxLevel(int level) {
|
||||
<< ", max_compression_gain_=" << max_compression_gain_;
|
||||
}
|
||||
|
||||
void MonoAgc::SetCaptureMuted(bool muted) {
|
||||
if (capture_muted_ == muted) {
|
||||
void MonoAgc::HandleCaptureOutputUsedChange(bool capture_output_used) {
|
||||
if (capture_output_used_ == capture_output_used) {
|
||||
return;
|
||||
}
|
||||
capture_muted_ = muted;
|
||||
capture_output_used_ = capture_output_used;
|
||||
|
||||
if (!muted) {
|
||||
// When we unmute, we should reset things to be safe.
|
||||
if (capture_output_used) {
|
||||
// When we start using the output, we should reset things to be safe.
|
||||
check_volume_on_next_process_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
int MonoAgc::CheckVolumeAndReset() {
|
||||
int level = stream_analog_level_;
|
||||
int level = recommended_input_volume_;
|
||||
// Reasons for taking action at startup:
|
||||
// 1) A person starting a call is expected to be heard.
|
||||
// 2) Independent of interpretation of |level| == 0 we should raise it so the
|
||||
// 2) Independent of interpretation of `level` == 0 we should raise it so the
|
||||
// AGC can do its job properly.
|
||||
if (level == 0 && !startup_) {
|
||||
RTC_DLOG(LS_INFO)
|
||||
@ -293,31 +342,33 @@ int MonoAgc::CheckVolumeAndReset() {
|
||||
}
|
||||
RTC_DLOG(LS_INFO) << "[agc] Initial GetMicVolume()=" << level;
|
||||
|
||||
int minLevel = startup_ ? startup_min_level_ : min_mic_level_;
|
||||
if (level < minLevel) {
|
||||
level = minLevel;
|
||||
if (level < min_mic_level_) {
|
||||
level = min_mic_level_;
|
||||
RTC_DLOG(LS_INFO) << "[agc] Initial volume too low, raising to " << level;
|
||||
stream_analog_level_ = level;
|
||||
recommended_input_volume_ = level;
|
||||
}
|
||||
agc_->Reset();
|
||||
level_ = level;
|
||||
startup_ = false;
|
||||
frames_since_update_gain_ = 0;
|
||||
is_first_frame_ = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Requests the RMS error from AGC and distributes the required gain change
|
||||
// between the digital compression stage and volume slider. We use the
|
||||
// compressor first, providing a slack region around the current slider
|
||||
// position to reduce movement.
|
||||
// Distributes the required gain change between the digital compression stage
|
||||
// and volume slider. We use the compressor first, providing a slack region
|
||||
// around the current slider position to reduce movement.
|
||||
//
|
||||
// If the slider needs to be moved, we check first if the user has adjusted
|
||||
// it, in which case we take no action and cache the updated level.
|
||||
void MonoAgc::UpdateGain() {
|
||||
int rms_error = 0;
|
||||
if (!agc_->GetRmsErrorDb(&rms_error)) {
|
||||
// No error update ready.
|
||||
return;
|
||||
}
|
||||
void MonoAgc::UpdateGain(int rms_error_db) {
|
||||
int rms_error = rms_error_db;
|
||||
|
||||
// Always reset the counter regardless of whether the gain is changed
|
||||
// or not. This matches with the bahvior of `agc_` where the histogram is
|
||||
// reset every time an RMS error is successfully read.
|
||||
frames_since_update_gain_ = 0;
|
||||
|
||||
// The compressor will always add at least kMinCompressionGain. In effect,
|
||||
// this adjusts our target gain upward by the same amount and rms_error
|
||||
// needs to reflect that.
|
||||
@ -357,22 +408,12 @@ void MonoAgc::UpdateGain() {
|
||||
int old_level = level_;
|
||||
SetLevel(LevelFromGainError(residual_gain, level_, min_mic_level_));
|
||||
if (old_level != level_) {
|
||||
// level_ was updated by SetLevel; log the new value.
|
||||
RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.AgcSetLevel", level_, 1,
|
||||
kMaxMicLevel, 50);
|
||||
// Reset the AGC since the level has changed.
|
||||
agc_->Reset();
|
||||
}
|
||||
}
|
||||
|
||||
void MonoAgc::UpdateCompressor() {
|
||||
calls_since_last_gain_log_++;
|
||||
if (calls_since_last_gain_log_ == 100) {
|
||||
calls_since_last_gain_log_ = 0;
|
||||
RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc.DigitalGainApplied",
|
||||
compression_, 0, kMaxCompressionGain,
|
||||
kMaxCompressionGain + 1);
|
||||
}
|
||||
if (compression_ == target_compression_) {
|
||||
return;
|
||||
}
|
||||
@ -397,57 +438,66 @@ void MonoAgc::UpdateCompressor() {
|
||||
|
||||
// Set the new compression gain.
|
||||
if (new_compression != compression_) {
|
||||
RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc.DigitalGainUpdated",
|
||||
new_compression, 0, kMaxCompressionGain,
|
||||
kMaxCompressionGain + 1);
|
||||
compression_ = new_compression;
|
||||
compression_accumulator_ = new_compression;
|
||||
new_compression_to_set_ = compression_;
|
||||
}
|
||||
}
|
||||
|
||||
int AgcManagerDirect::instance_counter_ = 0;
|
||||
std::atomic<int> AgcManagerDirect::instance_counter_(0);
|
||||
|
||||
AgcManagerDirect::AgcManagerDirect(Agc* agc,
|
||||
int startup_min_level,
|
||||
int clipped_level_min,
|
||||
int sample_rate_hz)
|
||||
: AgcManagerDirect(/*num_capture_channels*/ 1,
|
||||
startup_min_level,
|
||||
clipped_level_min,
|
||||
/*use_agc2_level_estimation*/ false,
|
||||
/*disable_digital_adaptive*/ false,
|
||||
sample_rate_hz) {
|
||||
AgcManagerDirect::AgcManagerDirect(
|
||||
const AudioProcessing::Config::GainController1::AnalogGainController&
|
||||
analog_config,
|
||||
Agc* agc)
|
||||
: AgcManagerDirect(/*num_capture_channels=*/1, analog_config) {
|
||||
RTC_DCHECK(channel_agcs_[0]);
|
||||
RTC_DCHECK(agc);
|
||||
channel_agcs_[0]->set_agc(agc);
|
||||
}
|
||||
|
||||
AgcManagerDirect::AgcManagerDirect(int num_capture_channels,
|
||||
int startup_min_level,
|
||||
int clipped_level_min,
|
||||
bool use_agc2_level_estimation,
|
||||
bool disable_digital_adaptive,
|
||||
int sample_rate_hz)
|
||||
: data_dumper_(
|
||||
new ApmDataDumper(rtc::AtomicOps::Increment(&instance_counter_))),
|
||||
use_min_channel_level_(!UseMaxAnalogChannelLevel()),
|
||||
sample_rate_hz_(sample_rate_hz),
|
||||
const AnalogAgcConfig& analog_config)
|
||||
: analog_controller_enabled_(analog_config.enabled),
|
||||
min_mic_level_override_(GetMinMicLevelOverride()),
|
||||
data_dumper_(new ApmDataDumper(instance_counter_.fetch_add(1) + 1)),
|
||||
num_capture_channels_(num_capture_channels),
|
||||
disable_digital_adaptive_(disable_digital_adaptive),
|
||||
frames_since_clipped_(kClippedWaitFrames),
|
||||
capture_muted_(false),
|
||||
disable_digital_adaptive_(!analog_config.enable_digital_adaptive),
|
||||
frames_since_clipped_(analog_config.clipped_wait_frames),
|
||||
capture_output_used_(true),
|
||||
clipped_level_step_(analog_config.clipped_level_step),
|
||||
clipped_ratio_threshold_(analog_config.clipped_ratio_threshold),
|
||||
clipped_wait_frames_(analog_config.clipped_wait_frames),
|
||||
channel_agcs_(num_capture_channels),
|
||||
new_compressions_to_set_(num_capture_channels) {
|
||||
const int min_mic_level = GetMinMicLevel();
|
||||
new_compressions_to_set_(num_capture_channels),
|
||||
clipping_predictor_(
|
||||
CreateClippingPredictor(num_capture_channels,
|
||||
analog_config.clipping_predictor)),
|
||||
use_clipping_predictor_step_(
|
||||
!!clipping_predictor_ &&
|
||||
analog_config.clipping_predictor.use_predicted_step),
|
||||
clipping_rate_log_(0.0f),
|
||||
clipping_rate_log_counter_(0) {
|
||||
RTC_LOG(LS_INFO) << "[agc] analog controller enabled: "
|
||||
<< (analog_controller_enabled_ ? "yes" : "no");
|
||||
const int min_mic_level = min_mic_level_override_.value_or(kMinMicLevel);
|
||||
RTC_LOG(LS_INFO) << "[agc] Min mic level: " << min_mic_level
|
||||
<< " (overridden: "
|
||||
<< (min_mic_level_override_.has_value() ? "yes" : "no")
|
||||
<< ")";
|
||||
for (size_t ch = 0; ch < channel_agcs_.size(); ++ch) {
|
||||
ApmDataDumper* data_dumper_ch = ch == 0 ? data_dumper_.get() : nullptr;
|
||||
|
||||
channel_agcs_[ch] = std::make_unique<MonoAgc>(
|
||||
data_dumper_ch, startup_min_level, clipped_level_min,
|
||||
use_agc2_level_estimation, disable_digital_adaptive_, min_mic_level);
|
||||
data_dumper_ch, analog_config.clipped_level_min,
|
||||
disable_digital_adaptive_, min_mic_level);
|
||||
}
|
||||
RTC_DCHECK_LT(0, channel_agcs_.size());
|
||||
RTC_DCHECK(!channel_agcs_.empty());
|
||||
RTC_DCHECK_GT(clipped_level_step_, 0);
|
||||
RTC_DCHECK_LE(clipped_level_step_, 255);
|
||||
RTC_DCHECK_GT(clipped_ratio_threshold_, 0.0f);
|
||||
RTC_DCHECK_LT(clipped_ratio_threshold_, 1.0f);
|
||||
RTC_DCHECK_GT(clipped_wait_frames_, 0);
|
||||
channel_agcs_[0]->ActivateLogging();
|
||||
}
|
||||
|
||||
@ -459,48 +509,47 @@ void AgcManagerDirect::Initialize() {
|
||||
for (size_t ch = 0; ch < channel_agcs_.size(); ++ch) {
|
||||
channel_agcs_[ch]->Initialize();
|
||||
}
|
||||
capture_muted_ = false;
|
||||
capture_output_used_ = true;
|
||||
|
||||
AggregateChannelLevels();
|
||||
clipping_rate_log_ = 0.0f;
|
||||
clipping_rate_log_counter_ = 0;
|
||||
}
|
||||
|
||||
void AgcManagerDirect::SetupDigitalGainControl(
|
||||
GainControl* gain_control) const {
|
||||
RTC_DCHECK(gain_control);
|
||||
if (gain_control->set_mode(GainControl::kFixedDigital) != 0) {
|
||||
GainControl& gain_control) const {
|
||||
if (gain_control.set_mode(GainControl::kFixedDigital) != 0) {
|
||||
RTC_LOG(LS_ERROR) << "set_mode(GainControl::kFixedDigital) failed.";
|
||||
}
|
||||
const int target_level_dbfs = disable_digital_adaptive_ ? 0 : 2;
|
||||
if (gain_control->set_target_level_dbfs(target_level_dbfs) != 0) {
|
||||
if (gain_control.set_target_level_dbfs(target_level_dbfs) != 0) {
|
||||
RTC_LOG(LS_ERROR) << "set_target_level_dbfs() failed.";
|
||||
}
|
||||
const int compression_gain_db =
|
||||
disable_digital_adaptive_ ? 0 : kDefaultCompressionGain;
|
||||
if (gain_control->set_compression_gain_db(compression_gain_db) != 0) {
|
||||
if (gain_control.set_compression_gain_db(compression_gain_db) != 0) {
|
||||
RTC_LOG(LS_ERROR) << "set_compression_gain_db() failed.";
|
||||
}
|
||||
const bool enable_limiter = !disable_digital_adaptive_;
|
||||
if (gain_control->enable_limiter(enable_limiter) != 0) {
|
||||
if (gain_control.enable_limiter(enable_limiter) != 0) {
|
||||
RTC_LOG(LS_ERROR) << "enable_limiter() failed.";
|
||||
}
|
||||
}
|
||||
|
||||
void AgcManagerDirect::AnalyzePreProcess(const AudioBuffer* audio) {
|
||||
void AgcManagerDirect::AnalyzePreProcess(const AudioBuffer& audio_buffer) {
|
||||
const float* const* audio = audio_buffer.channels_const();
|
||||
size_t samples_per_channel = audio_buffer.num_frames();
|
||||
RTC_DCHECK(audio);
|
||||
AnalyzePreProcess(audio->channels_const(), audio->num_frames());
|
||||
}
|
||||
|
||||
void AgcManagerDirect::AnalyzePreProcess(const float* const* audio,
|
||||
size_t samples_per_channel) {
|
||||
RTC_DCHECK(audio);
|
||||
AggregateChannelLevels();
|
||||
if (capture_muted_) {
|
||||
if (!capture_output_used_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (frames_since_clipped_ < kClippedWaitFrames) {
|
||||
++frames_since_clipped_;
|
||||
return;
|
||||
if (!!clipping_predictor_) {
|
||||
AudioFrameView<const float> frame = AudioFrameView<const float>(
|
||||
audio, num_capture_channels_, static_cast<int>(samples_per_channel));
|
||||
clipping_predictor_->Analyze(frame);
|
||||
}
|
||||
|
||||
// Check for clipped samples, as the AGC has difficulty detecting pitch
|
||||
@ -514,55 +563,108 @@ void AgcManagerDirect::AnalyzePreProcess(const float* const* audio,
|
||||
// gain is increased, through SetMaxLevel().
|
||||
float clipped_ratio =
|
||||
ComputeClippedRatio(audio, num_capture_channels_, samples_per_channel);
|
||||
clipping_rate_log_ = std::max(clipped_ratio, clipping_rate_log_);
|
||||
clipping_rate_log_counter_++;
|
||||
constexpr int kNumFramesIn30Seconds = 3000;
|
||||
if (clipping_rate_log_counter_ == kNumFramesIn30Seconds) {
|
||||
LogClippingMetrics(std::round(100.0f * clipping_rate_log_));
|
||||
clipping_rate_log_ = 0.0f;
|
||||
clipping_rate_log_counter_ = 0;
|
||||
}
|
||||
|
||||
if (clipped_ratio > kClippedRatioThreshold) {
|
||||
if (frames_since_clipped_ < clipped_wait_frames_) {
|
||||
++frames_since_clipped_;
|
||||
return;
|
||||
}
|
||||
|
||||
const bool clipping_detected = clipped_ratio > clipped_ratio_threshold_;
|
||||
bool clipping_predicted = false;
|
||||
int predicted_step = 0;
|
||||
if (!!clipping_predictor_) {
|
||||
for (int channel = 0; channel < num_capture_channels_; ++channel) {
|
||||
const auto step = clipping_predictor_->EstimateClippedLevelStep(
|
||||
channel, recommended_input_volume_, clipped_level_step_,
|
||||
channel_agcs_[channel]->min_mic_level(), kMaxMicLevel);
|
||||
if (step.has_value()) {
|
||||
predicted_step = std::max(predicted_step, step.value());
|
||||
clipping_predicted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (clipping_detected) {
|
||||
RTC_DLOG(LS_INFO) << "[agc] Clipping detected. clipped_ratio="
|
||||
<< clipped_ratio;
|
||||
}
|
||||
int step = clipped_level_step_;
|
||||
if (clipping_predicted) {
|
||||
predicted_step = std::max(predicted_step, clipped_level_step_);
|
||||
RTC_DLOG(LS_INFO) << "[agc] Clipping predicted. step=" << predicted_step;
|
||||
if (use_clipping_predictor_step_) {
|
||||
step = predicted_step;
|
||||
}
|
||||
}
|
||||
if (clipping_detected ||
|
||||
(clipping_predicted && use_clipping_predictor_step_)) {
|
||||
for (auto& state_ch : channel_agcs_) {
|
||||
state_ch->HandleClipping();
|
||||
state_ch->HandleClipping(step);
|
||||
}
|
||||
frames_since_clipped_ = 0;
|
||||
if (!!clipping_predictor_) {
|
||||
clipping_predictor_->Reset();
|
||||
}
|
||||
}
|
||||
AggregateChannelLevels();
|
||||
}
|
||||
|
||||
void AgcManagerDirect::Process(const AudioBuffer* audio) {
|
||||
AggregateChannelLevels();
|
||||
void AgcManagerDirect::Process(const AudioBuffer& audio_buffer) {
|
||||
Process(audio_buffer, /*speech_probability=*/absl::nullopt,
|
||||
/*speech_level_dbfs=*/absl::nullopt);
|
||||
}
|
||||
|
||||
if (capture_muted_) {
|
||||
void AgcManagerDirect::Process(const AudioBuffer& audio_buffer,
|
||||
absl::optional<float> speech_probability,
|
||||
absl::optional<float> speech_level_dbfs) {
|
||||
AggregateChannelLevels();
|
||||
const int volume_after_clipping_handling = recommended_input_volume_;
|
||||
|
||||
if (!capture_output_used_) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t num_frames_per_band = audio_buffer.num_frames_per_band();
|
||||
absl::optional<int> rms_error_override = absl::nullopt;
|
||||
if (speech_probability.has_value() && speech_level_dbfs.has_value()) {
|
||||
rms_error_override =
|
||||
GetSpeechLevelErrorDb(*speech_level_dbfs, *speech_probability);
|
||||
}
|
||||
for (size_t ch = 0; ch < channel_agcs_.size(); ++ch) {
|
||||
int16_t* audio_use = nullptr;
|
||||
std::array<int16_t, AudioBuffer::kMaxSampleRate / 100> audio_data;
|
||||
int num_frames_per_band;
|
||||
if (audio) {
|
||||
FloatS16ToS16(audio->split_bands_const_f(ch)[0],
|
||||
audio->num_frames_per_band(), audio_data.data());
|
||||
audio_use = audio_data.data();
|
||||
num_frames_per_band = audio->num_frames_per_band();
|
||||
} else {
|
||||
// Only used for testing.
|
||||
// TODO(peah): Change unittests to only allow on non-null audio input.
|
||||
num_frames_per_band = 320;
|
||||
}
|
||||
channel_agcs_[ch]->Process(audio_use, num_frames_per_band, sample_rate_hz_);
|
||||
int16_t* audio_use = audio_data.data();
|
||||
FloatS16ToS16(audio_buffer.split_bands_const_f(ch)[0], num_frames_per_band,
|
||||
audio_use);
|
||||
channel_agcs_[ch]->Process({audio_use, num_frames_per_band},
|
||||
rms_error_override);
|
||||
new_compressions_to_set_[ch] = channel_agcs_[ch]->new_compression();
|
||||
}
|
||||
|
||||
AggregateChannelLevels();
|
||||
if (volume_after_clipping_handling != recommended_input_volume_) {
|
||||
// The recommended input volume was adjusted in order to match the target
|
||||
// level.
|
||||
UpdateHistogramOnRecommendedInputVolumeChangeToMatchTarget(
|
||||
recommended_input_volume_);
|
||||
}
|
||||
}
|
||||
|
||||
absl::optional<int> AgcManagerDirect::GetDigitalComressionGain() {
|
||||
return new_compressions_to_set_[channel_controlling_gain_];
|
||||
}
|
||||
|
||||
void AgcManagerDirect::SetCaptureMuted(bool muted) {
|
||||
void AgcManagerDirect::HandleCaptureOutputUsedChange(bool capture_output_used) {
|
||||
for (size_t ch = 0; ch < channel_agcs_.size(); ++ch) {
|
||||
channel_agcs_[ch]->SetCaptureMuted(muted);
|
||||
channel_agcs_[ch]->HandleCaptureOutputUsedChange(capture_output_used);
|
||||
}
|
||||
capture_muted_ = muted;
|
||||
capture_output_used_ = capture_output_used;
|
||||
}
|
||||
|
||||
float AgcManagerDirect::voice_probability() const {
|
||||
@ -575,6 +677,10 @@ float AgcManagerDirect::voice_probability() const {
|
||||
}
|
||||
|
||||
void AgcManagerDirect::set_stream_analog_level(int level) {
|
||||
if (!analog_controller_enabled_) {
|
||||
recommended_input_volume_ = level;
|
||||
}
|
||||
|
||||
for (size_t ch = 0; ch < channel_agcs_.size(); ++ch) {
|
||||
channel_agcs_[ch]->set_stream_analog_level(level);
|
||||
}
|
||||
@ -583,25 +689,25 @@ void AgcManagerDirect::set_stream_analog_level(int level) {
|
||||
}
|
||||
|
||||
void AgcManagerDirect::AggregateChannelLevels() {
|
||||
stream_analog_level_ = channel_agcs_[0]->stream_analog_level();
|
||||
int new_recommended_input_volume =
|
||||
channel_agcs_[0]->recommended_analog_level();
|
||||
channel_controlling_gain_ = 0;
|
||||
if (use_min_channel_level_) {
|
||||
for (size_t ch = 1; ch < channel_agcs_.size(); ++ch) {
|
||||
int level = channel_agcs_[ch]->stream_analog_level();
|
||||
if (level < stream_analog_level_) {
|
||||
stream_analog_level_ = level;
|
||||
channel_controlling_gain_ = static_cast<int>(ch);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (size_t ch = 1; ch < channel_agcs_.size(); ++ch) {
|
||||
int level = channel_agcs_[ch]->stream_analog_level();
|
||||
if (level > stream_analog_level_) {
|
||||
stream_analog_level_ = level;
|
||||
channel_controlling_gain_ = static_cast<int>(ch);
|
||||
}
|
||||
for (size_t ch = 1; ch < channel_agcs_.size(); ++ch) {
|
||||
int level = channel_agcs_[ch]->recommended_analog_level();
|
||||
if (level < new_recommended_input_volume) {
|
||||
new_recommended_input_volume = level;
|
||||
channel_controlling_gain_ = static_cast<int>(ch);
|
||||
}
|
||||
}
|
||||
|
||||
if (min_mic_level_override_.has_value() && new_recommended_input_volume > 0) {
|
||||
new_recommended_input_volume =
|
||||
std::max(new_recommended_input_volume, *min_mic_level_override_);
|
||||
}
|
||||
|
||||
if (analog_controller_enabled_) {
|
||||
recommended_input_volume_ = new_recommended_input_volume;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -11,11 +11,15 @@
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
#include "absl/types/optional.h"
|
||||
#include "api/array_view.h"
|
||||
#include "modules/audio_processing/agc/agc.h"
|
||||
#include "modules/audio_processing/agc2/clipping_predictor.h"
|
||||
#include "modules/audio_processing/audio_buffer.h"
|
||||
#include "modules/audio_processing/include/audio_processing.h"
|
||||
#include "modules/audio_processing/logging/apm_data_dumper.h"
|
||||
#include "rtc_base/gtest_prod_util.h"
|
||||
|
||||
@ -24,89 +28,168 @@ namespace webrtc {
|
||||
class MonoAgc;
|
||||
class GainControl;
|
||||
|
||||
// Direct interface to use AGC to set volume and compression values.
|
||||
// AudioProcessing uses this interface directly to integrate the callback-less
|
||||
// AGC.
|
||||
//
|
||||
// Adaptive Gain Controller (AGC) that controls the input volume and a digital
|
||||
// gain. The input volume controller recommends what volume to use, handles
|
||||
// volume changes and clipping. In particular, it handles changes triggered by
|
||||
// the user (e.g., volume set to zero by a HW mute button). The digital
|
||||
// controller chooses and applies the digital compression gain.
|
||||
// This class is not thread-safe.
|
||||
// TODO(bugs.webrtc.org/7494): Use applied/recommended input volume naming
|
||||
// convention.
|
||||
class AgcManagerDirect final {
|
||||
public:
|
||||
// AgcManagerDirect will configure GainControl internally. The user is
|
||||
// responsible for processing the audio using it after the call to Process.
|
||||
// The operating range of startup_min_level is [12, 255] and any input value
|
||||
// outside that range will be clamped.
|
||||
AgcManagerDirect(int num_capture_channels,
|
||||
int startup_min_level,
|
||||
int clipped_level_min,
|
||||
bool use_agc2_level_estimation,
|
||||
bool disable_digital_adaptive,
|
||||
int sample_rate_hz);
|
||||
// Ctor. `num_capture_channels` specifies the number of channels for the audio
|
||||
// passed to `AnalyzePreProcess()` and `Process()`. Clamps
|
||||
// `analog_config.startup_min_level` in the [12, 255] range.
|
||||
AgcManagerDirect(
|
||||
int num_capture_channels,
|
||||
const AudioProcessing::Config::GainController1::AnalogGainController&
|
||||
analog_config);
|
||||
|
||||
~AgcManagerDirect();
|
||||
AgcManagerDirect(const AgcManagerDirect&) = delete;
|
||||
AgcManagerDirect& operator=(const AgcManagerDirect&) = delete;
|
||||
|
||||
void Initialize();
|
||||
void SetupDigitalGainControl(GainControl* gain_control) const;
|
||||
|
||||
void AnalyzePreProcess(const AudioBuffer* audio);
|
||||
void Process(const AudioBuffer* audio);
|
||||
// Configures `gain_control` to work as a fixed digital controller so that the
|
||||
// adaptive part is only handled by this gain controller. Must be called if
|
||||
// `gain_control` is also used to avoid the side-effects of running two AGCs.
|
||||
void SetupDigitalGainControl(GainControl& gain_control) const;
|
||||
|
||||
// Sets the applied input volume.
|
||||
void set_stream_analog_level(int level);
|
||||
|
||||
// TODO(bugs.webrtc.org/7494): Add argument for the applied input volume and
|
||||
// remove `set_stream_analog_level()`.
|
||||
// Analyzes `audio` before `Process()` is called so that the analysis can be
|
||||
// performed before external digital processing operations take place (e.g.,
|
||||
// echo cancellation). The analysis consists of input clipping detection and
|
||||
// prediction (if enabled). Must be called after `set_stream_analog_level()`.
|
||||
void AnalyzePreProcess(const AudioBuffer& audio_buffer);
|
||||
|
||||
// Processes `audio_buffer`. Chooses a digital compression gain and the new
|
||||
// input volume to recommend. Must be called after `AnalyzePreProcess()`. If
|
||||
// `speech_probability` (range [0.0f, 1.0f]) and `speech_level_dbfs` (range
|
||||
// [-90.f, 30.0f]) are given, uses them to override the estimated RMS error.
|
||||
// TODO(webrtc:7494): This signature is needed for testing purposes, unify
|
||||
// the signatures when the clean-up is done.
|
||||
void Process(const AudioBuffer& audio_buffer,
|
||||
absl::optional<float> speech_probability,
|
||||
absl::optional<float> speech_level_dbfs);
|
||||
|
||||
// Processes `audio_buffer`. Chooses a digital compression gain and the new
|
||||
// input volume to recommend. Must be called after `AnalyzePreProcess()`.
|
||||
void Process(const AudioBuffer& audio_buffer);
|
||||
|
||||
// TODO(bugs.webrtc.org/7494): Return recommended input volume and remove
|
||||
// `recommended_analog_level()`.
|
||||
// Returns the recommended input volume. If the input volume contoller is
|
||||
// disabled, returns the input volume set via the latest
|
||||
// `set_stream_analog_level()` call. Must be called after
|
||||
// `AnalyzePreProcess()` and `Process()`.
|
||||
int recommended_analog_level() const { return recommended_input_volume_; }
|
||||
|
||||
// Call when the capture stream output has been flagged to be used/not-used.
|
||||
// If unused, the manager disregards all incoming audio.
|
||||
void HandleCaptureOutputUsedChange(bool capture_output_used);
|
||||
|
||||
// Call when the capture stream has been muted/unmuted. This causes the
|
||||
// manager to disregard all incoming audio; chances are good it's background
|
||||
// noise to which we'd like to avoid adapting.
|
||||
void SetCaptureMuted(bool muted);
|
||||
float voice_probability() const;
|
||||
|
||||
int stream_analog_level() const { return stream_analog_level_; }
|
||||
void set_stream_analog_level(int level);
|
||||
int num_channels() const { return num_capture_channels_; }
|
||||
int sample_rate_hz() const { return sample_rate_hz_; }
|
||||
|
||||
// If available, returns a new compression gain for the digital gain control.
|
||||
// If available, returns the latest digital compression gain that has been
|
||||
// chosen.
|
||||
absl::optional<int> GetDigitalComressionGain();
|
||||
|
||||
// Returns true if clipping prediction is enabled.
|
||||
bool clipping_predictor_enabled() const { return !!clipping_predictor_; }
|
||||
|
||||
// Returns true if clipping prediction is used to adjust the input volume.
|
||||
bool use_clipping_predictor_step() const {
|
||||
return use_clipping_predictor_step_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class AgcManagerDirectTest;
|
||||
friend class AgcManagerDirectTestHelper;
|
||||
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
|
||||
DisableDigitalDisablesDigital);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
|
||||
AgcMinMicLevelExperiment);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest, DisableDigitalDisablesDigital);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest,
|
||||
AgcMinMicLevelExperimentDefault);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest,
|
||||
AgcMinMicLevelExperimentDisabled);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest,
|
||||
AgcMinMicLevelExperimentOutOfRangeAbove);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest,
|
||||
AgcMinMicLevelExperimentOutOfRangeBelow);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest,
|
||||
AgcMinMicLevelExperimentEnabled50);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectTest,
|
||||
AgcMinMicLevelExperimentEnabledAboveStartupLevel);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectParametrizedTest,
|
||||
ClippingParametersVerified);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectParametrizedTest,
|
||||
DisableClippingPredictorDoesNotLowerVolume);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectParametrizedTest,
|
||||
UsedClippingPredictionsProduceLowerAnalogLevels);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectParametrizedTest,
|
||||
UnusedClippingPredictionsProduceEqualAnalogLevels);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectParametrizedTest,
|
||||
EmptyRmsErrorOverrideHasNoEffect);
|
||||
FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectParametrizedTest,
|
||||
NonEmptyRmsErrorOverrideHasEffect);
|
||||
|
||||
// Dependency injection for testing. Don't delete |agc| as the memory is owned
|
||||
// by the manager.
|
||||
AgcManagerDirect(Agc* agc,
|
||||
int startup_min_level,
|
||||
int clipped_level_min,
|
||||
int sample_rate_hz);
|
||||
|
||||
void AnalyzePreProcess(const float* const* audio, size_t samples_per_channel);
|
||||
// Ctor that creates a single channel AGC and by injecting `agc`.
|
||||
// `agc` will be owned by this class; hence, do not delete it.
|
||||
AgcManagerDirect(
|
||||
const AudioProcessing::Config::GainController1::AnalogGainController&
|
||||
analog_config,
|
||||
Agc* agc);
|
||||
|
||||
void AggregateChannelLevels();
|
||||
|
||||
const bool analog_controller_enabled_;
|
||||
|
||||
const absl::optional<int> min_mic_level_override_;
|
||||
std::unique_ptr<ApmDataDumper> data_dumper_;
|
||||
static int instance_counter_;
|
||||
const bool use_min_channel_level_;
|
||||
const int sample_rate_hz_;
|
||||
static std::atomic<int> instance_counter_;
|
||||
const int num_capture_channels_;
|
||||
const bool disable_digital_adaptive_;
|
||||
|
||||
int frames_since_clipped_;
|
||||
int stream_analog_level_ = 0;
|
||||
bool capture_muted_;
|
||||
|
||||
// TODO(bugs.webrtc.org/7494): Create a separate member for the applied input
|
||||
// volume.
|
||||
// TODO(bugs.webrtc.org/7494): Once
|
||||
// `AudioProcessingImpl::recommended_stream_analog_level()` becomes a trivial
|
||||
// getter, leave uninitialized.
|
||||
// Recommended input volume. After `set_stream_analog_level()` is called it
|
||||
// holds the observed input volume. Possibly updated by `AnalyzePreProcess()`
|
||||
// and `Process()`; after these calls, holds the recommended input volume.
|
||||
int recommended_input_volume_ = 0;
|
||||
|
||||
bool capture_output_used_;
|
||||
int channel_controlling_gain_ = 0;
|
||||
|
||||
const int clipped_level_step_;
|
||||
const float clipped_ratio_threshold_;
|
||||
const int clipped_wait_frames_;
|
||||
|
||||
std::vector<std::unique_ptr<MonoAgc>> channel_agcs_;
|
||||
std::vector<absl::optional<int>> new_compressions_to_set_;
|
||||
|
||||
const std::unique_ptr<ClippingPredictor> clipping_predictor_;
|
||||
const bool use_clipping_predictor_step_;
|
||||
float clipping_rate_log_;
|
||||
int clipping_rate_log_counter_;
|
||||
};
|
||||
|
||||
// TODO(bugs.webrtc.org/7494): Use applied/recommended input volume naming
|
||||
// convention.
|
||||
class MonoAgc {
|
||||
public:
|
||||
MonoAgc(ApmDataDumper* data_dumper,
|
||||
int startup_min_level,
|
||||
int clipped_level_min,
|
||||
bool use_agc2_level_estimation,
|
||||
bool disable_digital_adaptive,
|
||||
int min_mic_level);
|
||||
~MonoAgc();
|
||||
@ -114,16 +197,27 @@ class MonoAgc {
|
||||
MonoAgc& operator=(const MonoAgc&) = delete;
|
||||
|
||||
void Initialize();
|
||||
void SetCaptureMuted(bool muted);
|
||||
void HandleCaptureOutputUsedChange(bool capture_output_used);
|
||||
|
||||
void HandleClipping();
|
||||
// Sets the current input volume.
|
||||
void set_stream_analog_level(int level) { recommended_input_volume_ = level; }
|
||||
|
||||
void Process(const int16_t* audio,
|
||||
size_t samples_per_channel,
|
||||
int sample_rate_hz);
|
||||
// Lowers the recommended input volume in response to clipping based on the
|
||||
// suggested reduction `clipped_level_step`. Must be called after
|
||||
// `set_stream_analog_level()`.
|
||||
void HandleClipping(int clipped_level_step);
|
||||
|
||||
// Analyzes `audio`, requests the RMS error from AGC, updates the recommended
|
||||
// input volume based on the estimated speech level and, if enabled, updates
|
||||
// the (digital) compression gain to be applied by `agc_`. Must be called
|
||||
// after `HandleClipping()`. If `rms_error_override` has a value, RMS error
|
||||
// from AGC is overridden by it.
|
||||
void Process(rtc::ArrayView<const int16_t> audio,
|
||||
absl::optional<int> rms_error_override);
|
||||
|
||||
// Returns the recommended input volume. Must be called after `Process()`.
|
||||
int recommended_analog_level() const { return recommended_input_volume_; }
|
||||
|
||||
void set_stream_analog_level(int level) { stream_analog_level_ = level; }
|
||||
int stream_analog_level() const { return stream_analog_level_; }
|
||||
float voice_probability() const { return agc_->voice_probability(); }
|
||||
void ActivateLogging() { log_to_histograms_ = true; }
|
||||
absl::optional<int> new_compression() const {
|
||||
@ -133,20 +227,19 @@ class MonoAgc {
|
||||
// Only used for testing.
|
||||
void set_agc(Agc* agc) { agc_.reset(agc); }
|
||||
int min_mic_level() const { return min_mic_level_; }
|
||||
int startup_min_level() const { return startup_min_level_; }
|
||||
|
||||
private:
|
||||
// Sets a new microphone level, after first checking that it hasn't been
|
||||
// updated by the user, in which case no action is taken.
|
||||
// Sets a new input volume, after first checking that it hasn't been updated
|
||||
// by the user, in which case no action is taken.
|
||||
void SetLevel(int new_level);
|
||||
|
||||
// Set the maximum level the AGC is allowed to apply. Also updates the
|
||||
// maximum compression gain to compensate. The level must be at least
|
||||
// |kClippedLevelMin|.
|
||||
// Set the maximum input volume the AGC is allowed to apply. Also updates the
|
||||
// maximum compression gain to compensate. The volume must be at least
|
||||
// `kClippedLevelMin`.
|
||||
void SetMaxLevel(int level);
|
||||
|
||||
int CheckVolumeAndReset();
|
||||
void UpdateGain();
|
||||
void UpdateGain(int rms_error_db);
|
||||
void UpdateCompressor();
|
||||
|
||||
const int min_mic_level_;
|
||||
@ -158,15 +251,26 @@ class MonoAgc {
|
||||
int target_compression_;
|
||||
int compression_;
|
||||
float compression_accumulator_;
|
||||
bool capture_muted_ = false;
|
||||
bool capture_output_used_ = true;
|
||||
bool check_volume_on_next_process_ = true;
|
||||
bool startup_ = true;
|
||||
int startup_min_level_;
|
||||
int calls_since_last_gain_log_ = 0;
|
||||
int stream_analog_level_ = 0;
|
||||
|
||||
// TODO(bugs.webrtc.org/7494): Create a separate member for the applied
|
||||
// input volume.
|
||||
// Recommended input volume. After `set_stream_analog_level()` is
|
||||
// called, it holds the observed applied input volume. Possibly updated by
|
||||
// `HandleClipping()` and `Process()`; after these calls, holds the
|
||||
// recommended input volume.
|
||||
int recommended_input_volume_ = 0;
|
||||
|
||||
absl::optional<int> new_compression_to_set_;
|
||||
bool log_to_histograms_ = false;
|
||||
const int clipped_level_min_;
|
||||
|
||||
// Frames since the last `UpdateGain()` call.
|
||||
int frames_since_update_gain_ = 0;
|
||||
// Set to true for the first frame after startup and reset, otherwise false.
|
||||
bool is_first_frame_ = true;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
@ -20,12 +20,12 @@ namespace webrtc {
|
||||
// Recommended to be enabled on the client-side.
|
||||
class GainControl {
|
||||
public:
|
||||
// When an analog mode is set, this must be called prior to |ProcessStream()|
|
||||
// When an analog mode is set, this must be called prior to `ProcessStream()`
|
||||
// to pass the current analog level from the audio HAL. Must be within the
|
||||
// range provided to |set_analog_level_limits()|.
|
||||
// range provided to `set_analog_level_limits()`.
|
||||
virtual int set_stream_analog_level(int level) = 0;
|
||||
|
||||
// When an analog mode is set, this should be called after |ProcessStream()|
|
||||
// When an analog mode is set, this should be called after `ProcessStream()`
|
||||
// to obtain the recommended new analog level for the audio HAL. It is the
|
||||
// users responsibility to apply this level.
|
||||
virtual int stream_analog_level() const = 0;
|
||||
@ -33,7 +33,7 @@ class GainControl {
|
||||
enum Mode {
|
||||
// Adaptive mode intended for use if an analog volume control is available
|
||||
// on the capture device. It will require the user to provide coupling
|
||||
// between the OS mixer controls and AGC through the |stream_analog_level()|
|
||||
// between the OS mixer controls and AGC through the `stream_analog_level()`
|
||||
// functions.
|
||||
//
|
||||
// It consists of an analog gain prescription for the audio device and a
|
||||
@ -61,7 +61,7 @@ class GainControl {
|
||||
virtual int set_mode(Mode mode) = 0;
|
||||
virtual Mode mode() const = 0;
|
||||
|
||||
// Sets the target peak |level| (or envelope) of the AGC in dBFs (decibels
|
||||
// Sets the target peak `level` (or envelope) of the AGC in dBFs (decibels
|
||||
// from digital full-scale). The convention is to use positive values. For
|
||||
// instance, passing in a value of 3 corresponds to -3 dBFs, or a target
|
||||
// level 3 dB below full-scale. Limited to [0, 31].
|
||||
@ -71,7 +71,7 @@ class GainControl {
|
||||
virtual int set_target_level_dbfs(int level) = 0;
|
||||
virtual int target_level_dbfs() const = 0;
|
||||
|
||||
// Sets the maximum |gain| the digital compression stage may apply, in dB. A
|
||||
// Sets the maximum `gain` the digital compression stage may apply, in dB. A
|
||||
// higher number corresponds to greater compression, while a value of 0 will
|
||||
// leave the signal uncompressed. Limited to [0, 90].
|
||||
virtual int set_compression_gain_db(int gain) = 0;
|
||||
@ -83,7 +83,7 @@ class GainControl {
|
||||
virtual int enable_limiter(bool enable) = 0;
|
||||
virtual bool is_limiter_enabled() const = 0;
|
||||
|
||||
// Sets the |minimum| and |maximum| analog levels of the audio capture device.
|
||||
// Sets the `minimum` and `maximum` analog levels of the audio capture device.
|
||||
// Must be set if and only if an analog mode is used. Limited to [0, 65535].
|
||||
virtual int set_analog_level_limits(int minimum, int maximum) = 0;
|
||||
virtual int analog_level_minimum() const = 0;
|
||||
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AGC_GAIN_MAP_INTERNAL_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AGC_GAIN_MAP_INTERNAL_H_
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
static const int kGainMapSize = 256;
|
||||
// Uses parameters: si = 2, sf = 0.25, D = 8/256
|
||||
static const int kGainMap[kGainMapSize] = {
|
||||
-56, -54, -52, -50, -48, -47, -45, -43, -42, -40, -38, -37, -35, -34, -33,
|
||||
-31, -30, -29, -27, -26, -25, -24, -23, -22, -20, -19, -18, -17, -16, -15,
|
||||
-14, -14, -13, -12, -11, -10, -9, -8, -8, -7, -6, -5, -5, -4, -3,
|
||||
-2, -2, -1, 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6,
|
||||
6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
|
||||
13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 18, 19,
|
||||
19, 19, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24,
|
||||
24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 28,
|
||||
29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 32, 32, 32, 32, 33,
|
||||
33, 33, 33, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36, 36, 37,
|
||||
37, 37, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 41,
|
||||
41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 44, 44, 44, 44, 45,
|
||||
45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, 48, 48, 48, 48,
|
||||
49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52,
|
||||
52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 55, 56, 56,
|
||||
56, 56, 57, 57, 57, 57, 58, 58, 58, 58, 59, 59, 59, 59, 60,
|
||||
60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63, 63,
|
||||
64};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // MODULES_AUDIO_PROCESSING_AGC_GAIN_MAP_INTERNAL_H_
|
@ -160,7 +160,7 @@ int WebRtcAgc_AddMic(void* state,
|
||||
|
||||
/* apply slowly varying digital gain */
|
||||
if (stt->micVol > stt->maxAnalog) {
|
||||
/* |maxLevel| is strictly >= |micVol|, so this condition should be
|
||||
/* `maxLevel` is strictly >= `micVol`, so this condition should be
|
||||
* satisfied here, ensuring there is no divide-by-zero. */
|
||||
RTC_DCHECK_GT(stt->maxLevel, stt->maxAnalog);
|
||||
|
||||
|
@ -11,7 +11,6 @@
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
|
||||
|
||||
|
||||
#include "modules/audio_processing/agc/legacy/digital_agc.h"
|
||||
#include "modules/audio_processing/agc/legacy/gain_control.h"
|
||||
|
||||
@ -63,7 +62,7 @@ typedef struct {
|
||||
int32_t upperSecondaryLimit; // = kRxxBufferLen * 2677832; -17 dBfs
|
||||
int32_t lowerSecondaryLimit; // = kRxxBufferLen * 267783; -27 dBfs
|
||||
uint16_t targetIdx; // Table index for corresponding target level
|
||||
int16_t analogTarget; // Digital reference level in ENV scale
|
||||
int16_t analogTarget; // Digital reference level in ENV scale
|
||||
|
||||
// Analog AGC specific variables
|
||||
int32_t filterState[8]; // For downsampling wb to nb
|
||||
@ -74,8 +73,8 @@ typedef struct {
|
||||
int32_t Rxx160_LPw32; // Low pass filtered frame energies
|
||||
int32_t Rxx16_LPw32Max; // Keeps track of largest energy subframe
|
||||
int32_t Rxx16_vectorw32[kRxxBufferLen]; // Array with subframe energies
|
||||
int32_t Rxx16w32_array[2][5]; // Energy values of microphone signal
|
||||
int32_t env[2][10]; // Envelope values of subframes
|
||||
int32_t Rxx16w32_array[2][5]; // Energy values of microphone signal
|
||||
int32_t env[2][10]; // Envelope values of subframes
|
||||
|
||||
int16_t Rxx16pos; // Current position in the Rxx16_vectorw32
|
||||
int16_t envSum; // Filtered scaled envelope in subframes
|
||||
|
@ -79,10 +79,9 @@ int32_t WebRtcAgc_CalculateGainTable(int32_t* gainTable, // Q16
|
||||
uint16_t constMaxGain;
|
||||
uint16_t tmpU16, intPart, fracPart;
|
||||
const int16_t kCompRatio = 3;
|
||||
const int16_t kSoftLimiterLeft = 1;
|
||||
int16_t limiterOffset = 0; // Limiter offset
|
||||
int16_t limiterIdx, limiterLvlX;
|
||||
int16_t constLinApprox, zeroGainLvl, maxGain, diffGain;
|
||||
int16_t constLinApprox, maxGain, diffGain;
|
||||
int16_t i, tmp16, tmp16no1;
|
||||
int zeros, zerosScale;
|
||||
|
||||
@ -98,17 +97,11 @@ int32_t WebRtcAgc_CalculateGainTable(int32_t* gainTable, // Q16
|
||||
WebRtcSpl_DivW32W16ResW16(tmp32no1 + (kCompRatio >> 1), kCompRatio);
|
||||
maxGain = WEBRTC_SPL_MAX(tmp16no1, (analogTarget - targetLevelDbfs));
|
||||
tmp32no1 = maxGain * kCompRatio;
|
||||
zeroGainLvl = digCompGaindB;
|
||||
zeroGainLvl -= WebRtcSpl_DivW32W16ResW16(tmp32no1 + ((kCompRatio - 1) >> 1),
|
||||
kCompRatio - 1);
|
||||
if ((digCompGaindB <= analogTarget) && (limiterEnable)) {
|
||||
zeroGainLvl += (analogTarget - digCompGaindB + kSoftLimiterLeft);
|
||||
limiterOffset = 0;
|
||||
}
|
||||
|
||||
// Calculate the difference between maximum gain and gain at 0dB0v:
|
||||
// diffGain = maxGain + (compRatio-1)*zeroGainLvl/compRatio
|
||||
// = (compRatio-1)*digCompGaindB/compRatio
|
||||
// Calculate the difference between maximum gain and gain at 0dB0v
|
||||
tmp32no1 = digCompGaindB * (kCompRatio - 1);
|
||||
diffGain =
|
||||
WebRtcSpl_DivW32W16ResW16(tmp32no1 + (kCompRatio >> 1), kCompRatio);
|
||||
@ -191,9 +184,9 @@ int32_t WebRtcAgc_CalculateGainTable(int32_t* gainTable, // Q16
|
||||
numFIX -= (int32_t)logApprox * diffGain; // Q14
|
||||
|
||||
// Calculate ratio
|
||||
// Shift |numFIX| as much as possible.
|
||||
// Ensure we avoid wrap-around in |den| as well.
|
||||
if (numFIX > (den >> 8) || -numFIX > (den >> 8)) { // |den| is Q8.
|
||||
// Shift `numFIX` as much as possible.
|
||||
// Ensure we avoid wrap-around in `den` as well.
|
||||
if (numFIX > (den >> 8) || -numFIX > (den >> 8)) { // `den` is Q8.
|
||||
zeros = WebRtcSpl_NormW32(numFIX);
|
||||
} else {
|
||||
zeros = WebRtcSpl_NormW32(den) + 8;
|
||||
@ -294,15 +287,12 @@ int32_t WebRtcAgc_ComputeDigitalGains(DigitalAgc* stt,
|
||||
int16_t gate, gain_adj;
|
||||
int16_t k;
|
||||
size_t n, L;
|
||||
int16_t L2; // samples/subframe
|
||||
|
||||
// determine number of samples per ms
|
||||
if (FS == 8000) {
|
||||
L = 8;
|
||||
L2 = 3;
|
||||
} else if (FS == 16000 || FS == 32000 || FS == 48000) {
|
||||
L = 16;
|
||||
L2 = 4;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
@ -11,6 +11,9 @@
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
enum {
|
||||
|
@ -114,7 +114,7 @@ void LoudnessHistogram::RemoveOldestEntryAndUpdate() {
|
||||
|
||||
void LoudnessHistogram::RemoveTransient() {
|
||||
// Don't expect to be here if high-activity region is longer than
|
||||
// |kTransientWidthThreshold| or there has not been any transient.
|
||||
// `kTransientWidthThreshold` or there has not been any transient.
|
||||
RTC_DCHECK_LE(len_high_activity_, kTransientWidthThreshold);
|
||||
int index =
|
||||
(buffer_index_ > 0) ? (buffer_index_ - 1) : len_circular_buffer_ - 1;
|
||||
|
@ -25,7 +25,7 @@ class LoudnessHistogram {
|
||||
static LoudnessHistogram* Create();
|
||||
|
||||
// Create a sliding LoudnessHistogram, i.e. the histogram represents the last
|
||||
// |window_size| samples.
|
||||
// `window_size` samples.
|
||||
static LoudnessHistogram* Create(int window_size);
|
||||
~LoudnessHistogram();
|
||||
|
||||
@ -49,7 +49,7 @@ class LoudnessHistogram {
|
||||
LoudnessHistogram();
|
||||
explicit LoudnessHistogram(int window);
|
||||
|
||||
// Find the histogram bin associated with the given |rms|.
|
||||
// Find the histogram bin associated with the given `rms`.
|
||||
int GetBinIndex(double rms);
|
||||
|
||||
void RemoveOldestEntryAndUpdate();
|
||||
@ -63,10 +63,10 @@ class LoudnessHistogram {
|
||||
// Number of times the histogram is updated
|
||||
int num_updates_;
|
||||
// Audio content, this should be equal to the sum of the components of
|
||||
// |bin_count_q10_|.
|
||||
// `bin_count_q10_`.
|
||||
int64_t audio_content_q10_;
|
||||
|
||||
// LoudnessHistogram of input RMS in Q10 with |kHistSize_| bins. In each
|
||||
// LoudnessHistogram of input RMS in Q10 with `kHistSize_` bins. In each
|
||||
// 'Update(),' we increment the associated histogram-bin with the given
|
||||
// probability. The increment is implemented in Q10 to avoid rounding errors.
|
||||
int64_t bin_count_q10_[kHistSize];
|
||||
|
@ -11,6 +11,7 @@
|
||||
#ifndef MODULES_AUDIO_PROCESSING_AGC_MOCK_AGC_H_
|
||||
#define MODULES_AUDIO_PROCESSING_AGC_MOCK_AGC_H_
|
||||
|
||||
#include "api/array_view.h"
|
||||
#include "modules/audio_processing/agc/agc.h"
|
||||
#include "test/gmock.h"
|
||||
|
||||
@ -19,10 +20,7 @@ namespace webrtc {
|
||||
class MockAgc : public Agc {
|
||||
public:
|
||||
virtual ~MockAgc() {}
|
||||
MOCK_METHOD(void,
|
||||
Process,
|
||||
(const int16_t* audio, size_t length, int sample_rate_hz),
|
||||
(override));
|
||||
MOCK_METHOD(void, Process, (rtc::ArrayView<const int16_t> audio), (override));
|
||||
MOCK_METHOD(bool, GetRmsErrorDb, (int* error), (override));
|
||||
MOCK_METHOD(void, Reset, (), (override));
|
||||
MOCK_METHOD(int, set_target_level_dbfs, (int level), (override));
|
||||
|
Reference in New Issue
Block a user