pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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_AGC_AGC_MANAGER_DIRECT_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_ |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 13 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/audio_processing/agc/agc.h" |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/constructor_magic.h" |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 19 | #include "rtc_base/gtest_prod_util.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | class AudioFrame; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 24 | class GainControl; |
| 25 | |
| 26 | // Callbacks that need to be injected into AgcManagerDirect to read and control |
Alejandro Luebs | d094c04 | 2015-09-29 15:43:42 -0700 | [diff] [blame] | 27 | // the volume values. This is done to remove the VoiceEngine dependency in |
| 28 | // AgcManagerDirect. |
| 29 | // TODO(aluebs): Remove VolumeCallbacks. |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 30 | class VolumeCallbacks { |
| 31 | public: |
| 32 | virtual ~VolumeCallbacks() {} |
| 33 | virtual void SetMicVolume(int volume) = 0; |
| 34 | virtual int GetMicVolume() = 0; |
| 35 | }; |
| 36 | |
| 37 | // Direct interface to use AGC to set volume and compression values. |
| 38 | // AudioProcessing uses this interface directly to integrate the callback-less |
Alejandro Luebs | d094c04 | 2015-09-29 15:43:42 -0700 | [diff] [blame] | 39 | // AGC. |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 40 | // |
| 41 | // This class is not thread-safe. |
Alejandro Luebs | d094c04 | 2015-09-29 15:43:42 -0700 | [diff] [blame] | 42 | class AgcManagerDirect final { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 43 | public: |
| 44 | // AgcManagerDirect will configure GainControl internally. The user is |
| 45 | // responsible for processing the audio using it after the call to Process. |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 46 | // The operating range of startup_min_level is [12, 255] and any input value |
| 47 | // outside that range will be clamped. |
| 48 | AgcManagerDirect(GainControl* gctrl, |
| 49 | VolumeCallbacks* volume_callbacks, |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 50 | int startup_min_level, |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 51 | int clipped_level_min, |
| 52 | bool use_agc2_level_estimation, |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 53 | bool disable_digital_adaptive); |
Alex Loiko | 64cb83b | 2018-07-02 13:38:19 +0200 | [diff] [blame] | 54 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 55 | ~AgcManagerDirect(); |
| 56 | |
| 57 | int Initialize(); |
Per Åhgren | 361d1c3 | 2019-11-06 22:17:14 +0100 | [diff] [blame] | 58 | void AnalyzePreProcess(const float* const* audio, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 59 | int num_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 60 | size_t samples_per_channel); |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 61 | void Process(const float* audio, size_t length, int sample_rate_hz); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 62 | |
Alejandro Luebs | d094c04 | 2015-09-29 15:43:42 -0700 | [diff] [blame] | 63 | // Call when the capture stream has been muted/unmuted. This causes the |
| 64 | // manager to disregard all incoming audio; chances are good it's background |
| 65 | // noise to which we'd like to avoid adapting. |
| 66 | void SetCaptureMuted(bool muted); |
| 67 | bool capture_muted() { return capture_muted_; } |
| 68 | |
| 69 | float voice_probability(); |
| 70 | |
| 71 | private: |
Alex Loiko | 2ffafa8 | 2018-07-06 15:35:42 +0200 | [diff] [blame] | 72 | friend class AgcManagerDirectTest; |
| 73 | |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 74 | FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest, |
| 75 | DisableDigitalDisablesDigital); |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 76 | FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest, |
| 77 | AgcMinMicLevelExperiment); |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 78 | |
Alex Loiko | 2ffafa8 | 2018-07-06 15:35:42 +0200 | [diff] [blame] | 79 | // Dependency injection for testing. Don't delete |agc| as the memory is owned |
| 80 | // by the manager. |
| 81 | AgcManagerDirect(Agc* agc, |
| 82 | GainControl* gctrl, |
| 83 | VolumeCallbacks* volume_callbacks, |
| 84 | int startup_min_level, |
| 85 | int clipped_level_min); |
| 86 | |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 87 | int min_mic_level() const { return min_mic_level_; } |
| 88 | int startup_min_level() const { return startup_min_level_; } |
| 89 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 90 | // Sets a new microphone level, after first checking that it hasn't been |
| 91 | // updated by the user, in which case no action is taken. |
| 92 | void SetLevel(int new_level); |
| 93 | |
| 94 | // Set the maximum level the AGC is allowed to apply. Also updates the |
| 95 | // maximum compression gain to compensate. The level must be at least |
| 96 | // |kClippedLevelMin|. |
| 97 | void SetMaxLevel(int level); |
| 98 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 99 | int CheckVolumeAndReset(); |
| 100 | void UpdateGain(); |
| 101 | void UpdateCompressor(); |
| 102 | |
Alex Loiko | c167673 | 2018-07-02 12:05:28 +0200 | [diff] [blame] | 103 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 104 | static int instance_counter_; |
| 105 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 106 | std::unique_ptr<Agc> agc_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 107 | GainControl* gctrl_; |
| 108 | VolumeCallbacks* volume_callbacks_; |
| 109 | |
| 110 | int frames_since_clipped_; |
| 111 | int level_; |
| 112 | int max_level_; |
| 113 | int max_compression_gain_; |
| 114 | int target_compression_; |
| 115 | int compression_; |
| 116 | float compression_accumulator_; |
| 117 | bool capture_muted_; |
| 118 | bool check_volume_on_next_process_; |
| 119 | bool startup_; |
henrika | ebf4552 | 2019-11-04 13:59:21 +0100 | [diff] [blame] | 120 | const int min_mic_level_; |
Alex Loiko | 9489c3a | 2018-08-09 15:04:24 +0200 | [diff] [blame] | 121 | const bool disable_digital_adaptive_; |
Bjorn Volcker | adc46c4 | 2015-04-15 11:42:40 +0200 | [diff] [blame] | 122 | int startup_min_level_; |
henrik.lundin | bd681b9 | 2016-12-05 09:08:42 -0800 | [diff] [blame] | 123 | const int clipped_level_min_; |
Alex Loiko | f3122e0 | 2018-08-10 14:43:51 +0200 | [diff] [blame] | 124 | int calls_since_last_gain_log_ = 0; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 125 | |
Alejandro Luebs | d094c04 | 2015-09-29 15:43:42 -0700 | [diff] [blame] | 126 | RTC_DISALLOW_COPY_AND_ASSIGN(AgcManagerDirect); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace webrtc |
| 130 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 131 | #endif // MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_ |