blob: 505ad15b295104302cc7c8d73e83b05692a4d13f [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
12#define MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
kwiberg88788ad2016-02-19 07:04:49 -080014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/agc/agc.h"
Alex Loikoc1676732018-07-02 12:05:28 +020017#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
Alex Loiko9489c3a2018-08-09 15:04:24 +020019#include "rtc_base/gtest_prod_util.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020
21namespace webrtc {
22
23class AudioFrame;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024class GainControl;
25
26// Callbacks that need to be injected into AgcManagerDirect to read and control
Alejandro Luebsd094c042015-09-29 15:43:42 -070027// the volume values. This is done to remove the VoiceEngine dependency in
28// AgcManagerDirect.
29// TODO(aluebs): Remove VolumeCallbacks.
pbos@webrtc.org788acd12014-12-15 09:41:24 +000030class 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 Luebsd094c042015-09-29 15:43:42 -070039// AGC.
pbos@webrtc.org788acd12014-12-15 09:41:24 +000040//
41// This class is not thread-safe.
Alejandro Luebsd094c042015-09-29 15:43:42 -070042class AgcManagerDirect final {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000043 public:
44 // AgcManagerDirect will configure GainControl internally. The user is
45 // responsible for processing the audio using it after the call to Process.
Bjorn Volckeradc46c42015-04-15 11:42:40 +020046 // 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.lundinbd681b92016-12-05 09:08:42 -080050 int startup_min_level,
Alex Loiko64cb83b2018-07-02 13:38:19 +020051 int clipped_level_min,
52 bool use_agc2_level_estimation,
Alex Loiko9489c3a2018-08-09 15:04:24 +020053 bool disable_digital_adaptive);
Alex Loiko64cb83b2018-07-02 13:38:19 +020054
pbos@webrtc.org788acd12014-12-15 09:41:24 +000055 ~AgcManagerDirect();
56
57 int Initialize();
Per Åhgren361d1c32019-11-06 22:17:14 +010058 void AnalyzePreProcess(const float* const* audio,
pbos@webrtc.org788acd12014-12-15 09:41:24 +000059 int num_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070060 size_t samples_per_channel);
Per Åhgren928146f2019-08-20 09:19:21 +020061 void Process(const float* audio, size_t length, int sample_rate_hz);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000062
Alejandro Luebsd094c042015-09-29 15:43:42 -070063 // 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 Loiko2ffafa82018-07-06 15:35:42 +020072 friend class AgcManagerDirectTest;
73
Alex Loiko9489c3a2018-08-09 15:04:24 +020074 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
75 DisableDigitalDisablesDigital);
henrikaebf45522019-11-04 13:59:21 +010076 FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
77 AgcMinMicLevelExperiment);
Alex Loiko9489c3a2018-08-09 15:04:24 +020078
Alex Loiko2ffafa82018-07-06 15:35:42 +020079 // 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
henrikaebf45522019-11-04 13:59:21 +010087 int min_mic_level() const { return min_mic_level_; }
88 int startup_min_level() const { return startup_min_level_; }
89
pbos@webrtc.org788acd12014-12-15 09:41:24 +000090 // 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.org788acd12014-12-15 09:41:24 +000099 int CheckVolumeAndReset();
100 void UpdateGain();
101 void UpdateCompressor();
102
Alex Loikoc1676732018-07-02 12:05:28 +0200103 std::unique_ptr<ApmDataDumper> data_dumper_;
104 static int instance_counter_;
105
kwiberg88788ad2016-02-19 07:04:49 -0800106 std::unique_ptr<Agc> agc_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000107 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_;
henrikaebf45522019-11-04 13:59:21 +0100120 const int min_mic_level_;
Alex Loiko9489c3a2018-08-09 15:04:24 +0200121 const bool disable_digital_adaptive_;
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200122 int startup_min_level_;
henrik.lundinbd681b92016-12-05 09:08:42 -0800123 const int clipped_level_min_;
Alex Loikof3122e02018-08-10 14:43:51 +0200124 int calls_since_last_gain_log_ = 0;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000125
Alejandro Luebsd094c042015-09-29 15:43:42 -0700126 RTC_DISALLOW_COPY_AND_ASSIGN(AgcManagerDirect);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000127};
128
129} // namespace webrtc
130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_