peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #ifndef MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_ |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
| 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 17 | #include "api/optional.h" |
| 18 | #include "modules/audio_processing/include/audio_processing.h" |
| 19 | #include "modules/audio_processing/level_controller/gain_applier.h" |
| 20 | #include "modules/audio_processing/level_controller/gain_selector.h" |
| 21 | #include "modules/audio_processing/level_controller/noise_level_estimator.h" |
| 22 | #include "modules/audio_processing/level_controller/peak_level_estimator.h" |
| 23 | #include "modules/audio_processing/level_controller/saturating_gain_estimator.h" |
| 24 | #include "modules/audio_processing/level_controller/signal_classifier.h" |
| 25 | #include "rtc_base/constructormagic.h" |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class ApmDataDumper; |
| 30 | class AudioBuffer; |
| 31 | |
| 32 | class LevelController { |
| 33 | public: |
| 34 | LevelController(); |
| 35 | ~LevelController(); |
| 36 | |
| 37 | void Initialize(int sample_rate_hz); |
| 38 | void Process(AudioBuffer* audio); |
| 39 | float GetLastGain() { return last_gain_; } |
| 40 | |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 41 | // TODO(peah): This method is a temporary solution as the the aim is to |
| 42 | // instead apply the config inside the constructor. Therefore this is likely |
| 43 | // to change. |
| 44 | void ApplyConfig(const AudioProcessing::Config::LevelController& config); |
peah | 88ac853 | 2016-09-12 16:47:25 -0700 | [diff] [blame] | 45 | // Validates a config. |
| 46 | static bool Validate(const AudioProcessing::Config::LevelController& config); |
| 47 | // Dumps a config to a string. |
| 48 | static std::string ToString( |
| 49 | const AudioProcessing::Config::LevelController& config); |
| 50 | |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 51 | private: |
| 52 | class Metrics { |
| 53 | public: |
| 54 | Metrics() { Initialize(AudioProcessing::kSampleRate48kHz); } |
| 55 | void Initialize(int sample_rate_hz); |
peah | 3026ee8 | 2016-08-26 11:15:47 -0700 | [diff] [blame] | 56 | void Update(float long_term_peak_level, |
| 57 | float noise_level, |
| 58 | float gain, |
| 59 | float frame_peak_level); |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | void Reset(); |
| 63 | |
| 64 | size_t metrics_frame_counter_; |
| 65 | float gain_sum_; |
| 66 | float peak_level_sum_; |
| 67 | float noise_energy_sum_; |
| 68 | float max_gain_; |
| 69 | float max_peak_level_; |
| 70 | float max_noise_energy_; |
| 71 | float frame_length_; |
| 72 | }; |
| 73 | |
| 74 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 75 | GainSelector gain_selector_; |
| 76 | GainApplier gain_applier_; |
| 77 | SignalClassifier signal_classifier_; |
| 78 | NoiseLevelEstimator noise_level_estimator_; |
| 79 | PeakLevelEstimator peak_level_estimator_; |
| 80 | SaturatingGainEstimator saturating_gain_estimator_; |
| 81 | Metrics metrics_; |
| 82 | rtc::Optional<int> sample_rate_hz_; |
| 83 | static int instance_count_; |
| 84 | float dc_level_[2]; |
| 85 | float dc_forgetting_factor_; |
| 86 | float last_gain_; |
peah | c19f312 | 2016-10-07 14:54:10 -0700 | [diff] [blame] | 87 | bool gain_jumpstart_ = false; |
| 88 | AudioProcessing::Config::LevelController config_; |
peah | ca4cac7 | 2016-06-29 15:26:12 -0700 | [diff] [blame] | 89 | |
| 90 | RTC_DISALLOW_COPY_AND_ASSIGN(LevelController); |
| 91 | }; |
| 92 | |
| 93 | } // namespace webrtc |
| 94 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 95 | #endif // MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_ |