Line data Source code
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 :
11 : #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_
12 : #define WEBRTC_MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_
13 :
14 : #include <memory>
15 : #include <vector>
16 :
17 : #include "webrtc/base/constructormagic.h"
18 : #include "webrtc/base/optional.h"
19 : #include "webrtc/modules/audio_processing/include/audio_processing.h"
20 : #include "webrtc/modules/audio_processing/level_controller/gain_applier.h"
21 : #include "webrtc/modules/audio_processing/level_controller/gain_selector.h"
22 : #include "webrtc/modules/audio_processing/level_controller/noise_level_estimator.h"
23 : #include "webrtc/modules/audio_processing/level_controller/peak_level_estimator.h"
24 : #include "webrtc/modules/audio_processing/level_controller/saturating_gain_estimator.h"
25 : #include "webrtc/modules/audio_processing/level_controller/signal_classifier.h"
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 :
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);
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 :
51 : private:
52 : class Metrics {
53 : public:
54 0 : Metrics() { Initialize(AudioProcessing::kSampleRate48kHz); }
55 : void Initialize(int sample_rate_hz);
56 : void Update(float long_term_peak_level,
57 : float noise_level,
58 : float gain,
59 : float frame_peak_level);
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_;
87 : bool gain_jumpstart_ = false;
88 : AudioProcessing::Config::LevelController config_;
89 :
90 : RTC_DISALLOW_COPY_AND_ASSIGN(LevelController);
91 : };
92 :
93 : } // namespace webrtc
94 :
95 : #endif // WEBRTC_MODULES_AUDIO_PROCESSING_LEVEL_CONTROLLER_LEVEL_CONTROLLER_H_
|