You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 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. #include "modules/audio_processing/gain_controller2.h"
  11. #include "common_audio/include/audio_util.h"
  12. #include "modules/audio_processing/audio_buffer.h"
  13. #include "modules/audio_processing/include/audio_frame_view.h"
  14. #include "modules/audio_processing/logging/apm_data_dumper.h"
  15. #include "rtc_base/atomic_ops.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/strings/string_builder.h"
  18. namespace webrtc {
  19. int GainController2::instance_count_ = 0;
  20. GainController2::GainController2()
  21. : data_dumper_(
  22. new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
  23. gain_applier_(/*hard_clip_samples=*/false,
  24. /*initial_gain_factor=*/0.f),
  25. adaptive_agc_(new AdaptiveAgc(data_dumper_.get())),
  26. limiter_(static_cast<size_t>(48000), data_dumper_.get(), "Agc2") {}
  27. GainController2::~GainController2() = default;
  28. void GainController2::Initialize(int sample_rate_hz) {
  29. RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz ||
  30. sample_rate_hz == AudioProcessing::kSampleRate16kHz ||
  31. sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
  32. sample_rate_hz == AudioProcessing::kSampleRate48kHz);
  33. limiter_.SetSampleRate(sample_rate_hz);
  34. data_dumper_->InitiateNewSetOfRecordings();
  35. data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz);
  36. }
  37. void GainController2::Process(AudioBuffer* audio) {
  38. AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
  39. audio->num_frames());
  40. // Apply fixed gain first, then the adaptive one.
  41. gain_applier_.ApplyGain(float_frame);
  42. if (config_.adaptive_digital.enabled) {
  43. adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel());
  44. }
  45. limiter_.Process(float_frame);
  46. }
  47. void GainController2::NotifyAnalogLevel(int level) {
  48. if (analog_level_ != level && config_.adaptive_digital.enabled) {
  49. adaptive_agc_->Reset();
  50. }
  51. analog_level_ = level;
  52. }
  53. void GainController2::ApplyConfig(
  54. const AudioProcessing::Config::GainController2& config) {
  55. RTC_DCHECK(Validate(config))
  56. << " the invalid config was " << ToString(config);
  57. config_ = config;
  58. if (config.fixed_digital.gain_db != config_.fixed_digital.gain_db) {
  59. // Reset the limiter to quickly react on abrupt level changes caused by
  60. // large changes of the fixed gain.
  61. limiter_.Reset();
  62. }
  63. gain_applier_.SetGainFactor(DbToRatio(config_.fixed_digital.gain_db));
  64. adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get(), config_));
  65. }
  66. bool GainController2::Validate(
  67. const AudioProcessing::Config::GainController2& config) {
  68. return config.fixed_digital.gain_db >= 0.f &&
  69. config.fixed_digital.gain_db < 50.f &&
  70. config.adaptive_digital.extra_saturation_margin_db >= 0.f &&
  71. config.adaptive_digital.extra_saturation_margin_db <= 100.f;
  72. }
  73. std::string GainController2::ToString(
  74. const AudioProcessing::Config::GainController2& config) {
  75. rtc::StringBuilder ss;
  76. std::string adaptive_digital_level_estimator;
  77. using LevelEstimatorType =
  78. AudioProcessing::Config::GainController2::LevelEstimator;
  79. switch (config.adaptive_digital.level_estimator) {
  80. case LevelEstimatorType::kRms:
  81. adaptive_digital_level_estimator = "RMS";
  82. break;
  83. case LevelEstimatorType::kPeak:
  84. adaptive_digital_level_estimator = "peak";
  85. break;
  86. }
  87. // clang-format off
  88. // clang formatting doesn't respect custom nested style.
  89. ss << "{"
  90. << "enabled: " << (config.enabled ? "true" : "false") << ", "
  91. << "fixed_digital: {gain_db: " << config.fixed_digital.gain_db << "}, "
  92. << "adaptive_digital: {"
  93. << "enabled: "
  94. << (config.adaptive_digital.enabled ? "true" : "false") << ", "
  95. << "level_estimator: " << adaptive_digital_level_estimator << ", "
  96. << "extra_saturation_margin_db:"
  97. << config.adaptive_digital.extra_saturation_margin_db << "}"
  98. << "}";
  99. // clang-format on
  100. return ss.Release();
  101. }
  102. } // namespace webrtc