2018-03-27 13:38:36 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "modules/audio_processing/agc2/adaptive_agc.h"
|
|
|
|
|
|
|
|
|
|
#include "common_audio/include/audio_util.h"
|
2020-11-27 16:02:38 +01:00
|
|
|
#include "modules/audio_processing/agc2/cpu_features.h"
|
2018-06-20 14:14:18 +02:00
|
|
|
#include "modules/audio_processing/agc2/vad_with_level.h"
|
2018-03-27 13:38:36 +02:00
|
|
|
#include "modules/audio_processing/logging/apm_data_dumper.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2020-09-30 22:54:00 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2018-03-27 13:38:36 +02:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2020-09-30 13:07:57 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2021-04-14 16:17:09 +02:00
|
|
|
using AdaptiveDigitalConfig =
|
|
|
|
|
AudioProcessing::Config::GainController2::AdaptiveDigital;
|
|
|
|
|
using NoiseEstimatorType =
|
|
|
|
|
AudioProcessing::Config::GainController2::NoiseEstimator;
|
|
|
|
|
|
2020-09-30 13:07:57 +02:00
|
|
|
void DumpDebugData(const AdaptiveDigitalGainApplier::FrameInfo& info,
|
|
|
|
|
ApmDataDumper& dumper) {
|
|
|
|
|
dumper.DumpRaw("agc2_vad_probability", info.vad_result.speech_probability);
|
|
|
|
|
dumper.DumpRaw("agc2_vad_rms_dbfs", info.vad_result.rms_dbfs);
|
|
|
|
|
dumper.DumpRaw("agc2_vad_peak_dbfs", info.vad_result.peak_dbfs);
|
|
|
|
|
dumper.DumpRaw("agc2_noise_estimate_dbfs", info.input_noise_level_dbfs);
|
|
|
|
|
dumper.DumpRaw("agc2_last_limiter_audio_level", info.limiter_envelope_dbfs);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 16:57:45 +02:00
|
|
|
constexpr int kGainApplierAdjacentSpeechFramesThreshold = 1;
|
2021-03-31 15:04:03 +02:00
|
|
|
constexpr float kMaxGainChangePerSecondDb = 3.0f;
|
|
|
|
|
constexpr float kMaxOutputNoiseLevelDbfs = -50.0f;
|
2020-10-01 16:57:45 +02:00
|
|
|
|
2021-01-05 10:28:24 +01:00
|
|
|
// Detects the available CPU features and applies any kill-switches.
|
|
|
|
|
AvailableCpuFeatures GetAllowedCpuFeatures(
|
2021-04-14 16:17:09 +02:00
|
|
|
const AdaptiveDigitalConfig& config) {
|
2020-11-27 16:02:38 +01:00
|
|
|
AvailableCpuFeatures features = GetAvailableCpuFeatures();
|
2021-01-05 10:28:24 +01:00
|
|
|
if (!config.sse2_allowed) {
|
|
|
|
|
features.sse2 = false;
|
|
|
|
|
}
|
|
|
|
|
if (!config.avx2_allowed) {
|
2020-11-27 16:02:38 +01:00
|
|
|
features.avx2 = false;
|
|
|
|
|
}
|
2021-01-05 10:28:24 +01:00
|
|
|
if (!config.neon_allowed) {
|
|
|
|
|
features.neon = false;
|
|
|
|
|
}
|
2020-11-27 16:02:38 +01:00
|
|
|
return features;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 16:17:09 +02:00
|
|
|
std::unique_ptr<NoiseLevelEstimator> CreateNoiseLevelEstimator(
|
|
|
|
|
NoiseEstimatorType estimator_type,
|
|
|
|
|
ApmDataDumper* apm_data_dumper) {
|
|
|
|
|
switch (estimator_type) {
|
|
|
|
|
case NoiseEstimatorType::kStationaryNoise:
|
|
|
|
|
return CreateStationaryNoiseEstimator(apm_data_dumper);
|
|
|
|
|
case NoiseEstimatorType::kNoiseFloor:
|
|
|
|
|
return CreateNoiseFloorEstimator(apm_data_dumper);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr NoiseEstimatorType kDefaultNoiseLevelEstimatorType =
|
|
|
|
|
NoiseEstimatorType::kNoiseFloor;
|
|
|
|
|
|
2020-09-30 13:07:57 +02:00
|
|
|
} // namespace
|
2018-03-27 13:38:36 +02:00
|
|
|
|
|
|
|
|
AdaptiveAgc::AdaptiveAgc(ApmDataDumper* apm_data_dumper)
|
|
|
|
|
: speech_level_estimator_(apm_data_dumper),
|
2020-10-01 16:57:45 +02:00
|
|
|
gain_applier_(apm_data_dumper,
|
|
|
|
|
kGainApplierAdjacentSpeechFramesThreshold,
|
2020-10-01 17:16:56 +02:00
|
|
|
kMaxGainChangePerSecondDb,
|
|
|
|
|
kMaxOutputNoiseLevelDbfs),
|
2018-04-04 15:05:57 +02:00
|
|
|
apm_data_dumper_(apm_data_dumper),
|
2021-04-14 16:17:09 +02:00
|
|
|
noise_level_estimator_(
|
|
|
|
|
CreateNoiseLevelEstimator(kDefaultNoiseLevelEstimatorType,
|
|
|
|
|
apm_data_dumper)) {
|
2018-03-27 13:38:36 +02:00
|
|
|
RTC_DCHECK(apm_data_dumper);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-01 14:51:56 +01:00
|
|
|
AdaptiveAgc::AdaptiveAgc(ApmDataDumper* apm_data_dumper,
|
2021-04-14 16:17:09 +02:00
|
|
|
const AdaptiveDigitalConfig& config)
|
2018-11-13 14:44:15 +01:00
|
|
|
: speech_level_estimator_(
|
|
|
|
|
apm_data_dumper,
|
2021-04-14 16:17:09 +02:00
|
|
|
config.level_estimator,
|
|
|
|
|
config.level_estimator_adjacent_speech_frames_threshold,
|
|
|
|
|
config.initial_saturation_margin_db,
|
|
|
|
|
config.extra_saturation_margin_db),
|
|
|
|
|
vad_(config.vad_reset_period_ms,
|
|
|
|
|
config.vad_probability_attack,
|
|
|
|
|
GetAllowedCpuFeatures(config)),
|
|
|
|
|
gain_applier_(apm_data_dumper,
|
|
|
|
|
config.gain_applier_adjacent_speech_frames_threshold,
|
|
|
|
|
config.max_gain_change_db_per_second,
|
|
|
|
|
config.max_output_noise_level_dbfs),
|
2018-11-01 14:51:56 +01:00
|
|
|
apm_data_dumper_(apm_data_dumper),
|
2021-04-14 16:17:09 +02:00
|
|
|
noise_level_estimator_(
|
|
|
|
|
CreateNoiseLevelEstimator(config.noise_estimator, apm_data_dumper)) {
|
2018-11-01 14:51:56 +01:00
|
|
|
RTC_DCHECK(apm_data_dumper);
|
2021-04-14 16:17:09 +02:00
|
|
|
if (!config.use_saturation_protector) {
|
2020-09-30 22:54:00 +02:00
|
|
|
RTC_LOG(LS_WARNING) << "The saturation protector cannot be disabled.";
|
|
|
|
|
}
|
2018-11-01 14:51:56 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-27 13:38:36 +02:00
|
|
|
AdaptiveAgc::~AdaptiveAgc() = default;
|
|
|
|
|
|
2020-09-30 13:07:57 +02:00
|
|
|
void AdaptiveAgc::Process(AudioFrameView<float> frame, float limiter_envelope) {
|
|
|
|
|
AdaptiveDigitalGainApplier::FrameInfo info;
|
|
|
|
|
info.vad_result = vad_.AnalyzeFrame(frame);
|
|
|
|
|
speech_level_estimator_.Update(info.vad_result);
|
|
|
|
|
info.input_level_dbfs = speech_level_estimator_.level_dbfs();
|
2021-04-07 14:57:40 +02:00
|
|
|
info.input_noise_level_dbfs = noise_level_estimator_->Analyze(frame);
|
2020-09-30 13:07:57 +02:00
|
|
|
info.limiter_envelope_dbfs =
|
2021-03-31 15:04:03 +02:00
|
|
|
limiter_envelope > 0 ? FloatS16ToDbfs(limiter_envelope) : -90.0f;
|
2020-09-30 13:07:57 +02:00
|
|
|
info.estimate_is_confident = speech_level_estimator_.IsConfident();
|
|
|
|
|
DumpDebugData(info, *apm_data_dumper_);
|
|
|
|
|
gain_applier_.Process(info, frame);
|
2018-03-27 13:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:32:12 +02:00
|
|
|
void AdaptiveAgc::Reset() {
|
|
|
|
|
speech_level_estimator_.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 13:38:36 +02:00
|
|
|
} // namespace webrtc
|