blob: 224b886abd1af01b4e28e9bd2190433ac64ffab6 [file] [log] [blame]
peahca4cac72016-06-29 15:26:12 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_
12#define MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_
peahca4cac72016-06-29 15:26:12 -070013
14#include <memory>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#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"
peahca4cac72016-06-29 15:26:12 -070026
27namespace webrtc {
28
29class ApmDataDumper;
30class AudioBuffer;
31
32class 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
peahc19f3122016-10-07 14:54:10 -070041 // 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);
peah88ac8532016-09-12 16:47:25 -070045 // 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
peahca4cac72016-06-29 15:26:12 -070051 private:
52 class Metrics {
53 public:
54 Metrics() { Initialize(AudioProcessing::kSampleRate48kHz); }
55 void Initialize(int sample_rate_hz);
peah3026ee82016-08-26 11:15:47 -070056 void Update(float long_term_peak_level,
57 float noise_level,
58 float gain,
59 float frame_peak_level);
peahca4cac72016-06-29 15:26:12 -070060
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_;
peahc19f3122016-10-07 14:54:10 -070087 bool gain_jumpstart_ = false;
88 AudioProcessing::Config::LevelController config_;
peahca4cac72016-06-29 15:26:12 -070089
90 RTC_DISALLOW_COPY_AND_ASSIGN(LevelController);
91};
92
93} // namespace webrtc
94
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020095#endif // MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_