• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021 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 #ifndef MODULES_AUDIO_PROCESSING_CAPTURE_LEVELS_ADJUSTER_CAPTURE_LEVELS_ADJUSTER_H_
11 #define MODULES_AUDIO_PROCESSING_CAPTURE_LEVELS_ADJUSTER_CAPTURE_LEVELS_ADJUSTER_H_
12 
13 #include <stddef.h>
14 
15 #include "modules/audio_processing/audio_buffer.h"
16 #include "modules/audio_processing/capture_levels_adjuster/audio_samples_scaler.h"
17 
18 namespace webrtc {
19 
20 // Adjusts the level of the capture signal before and after all capture-side
21 // processing is done using a combination of explicitly specified gains
22 // and an emulated analog gain functionality where a specified analog level
23 // results in an additional gain. The pre-adjustment is achieved by combining
24 // the gain value `pre_gain` and the level `emulated_analog_mic_gain_level` to
25 // form a combined gain of `pre_gain`*`emulated_analog_mic_gain_level`/255 which
26 // is multiplied to each sample. The intention of the
27 // `emulated_analog_mic_gain_level` is to be controlled by the analog AGC
28 // functionality and to produce an emulated analog mic gain equal to
29 // `emulated_analog_mic_gain_level`/255. The post level adjustment is achieved
30 // by multiplying each sample with the value of `post_gain`. Any changes in the
31 // gains take are done smoothly over one frame and the scaled samples are
32 // clamped to fit into the allowed S16 sample range.
33 class CaptureLevelsAdjuster {
34  public:
35   // C-tor. The values for the level and the gains must fulfill
36   // 0 <= emulated_analog_mic_gain_level <= 255.
37   // 0.f <= pre_gain.
38   // 0.f <= post_gain.
39   CaptureLevelsAdjuster(bool emulated_analog_mic_gain_enabled,
40                         int emulated_analog_mic_gain_level,
41                         float pre_gain,
42                         float post_gain);
43   CaptureLevelsAdjuster(const CaptureLevelsAdjuster&) = delete;
44   CaptureLevelsAdjuster& operator=(const CaptureLevelsAdjuster&) = delete;
45 
46   // Adjusts the level of the signal. This should be called before any of the
47   // other processing is performed.
48   void ApplyPreLevelAdjustment(AudioBuffer& audio_buffer);
49 
50   // Adjusts the level of the signal. This should be called after all of the
51   // other processing have been performed.
52   void ApplyPostLevelAdjustment(AudioBuffer& audio_buffer);
53 
54   // Sets the gain to apply to each sample before any of the other processing is
55   // performed.
56   void SetPreGain(float pre_gain);
57 
58   // Returns the total pre-adjustment gain applied, comprising both the pre_gain
59   // as well as the gain from the emulated analog mic, to each sample before any
60   // of the other processing is performed.
GetPreAdjustmentGain()61   float GetPreAdjustmentGain() const { return pre_adjustment_gain_; }
62 
63   // Sets the gain to apply to each sample after all of the other processing
64   // have been performed.
65   void SetPostGain(float post_gain);
66 
67   // Sets the analog gain level to use for the emulated analog gain.
68   // `level` must be in the range [0...255].
69   void SetAnalogMicGainLevel(int level);
70 
71   // Returns the current analog gain level used for the emulated analog gain.
GetAnalogMicGainLevel()72   int GetAnalogMicGainLevel() const { return emulated_analog_mic_gain_level_; }
73 
74  private:
75   // Updates the value of `pre_adjustment_gain_` based on the supplied values
76   // for `pre_gain` and `emulated_analog_mic_gain_level_`.
77   void UpdatePreAdjustmentGain();
78 
79   const bool emulated_analog_mic_gain_enabled_;
80   int emulated_analog_mic_gain_level_;
81   float pre_gain_;
82   float pre_adjustment_gain_;
83   AudioSamplesScaler pre_scaler_;
84   AudioSamplesScaler post_scaler_;
85 };
86 }  // namespace webrtc
87 
88 #endif  // MODULES_AUDIO_PROCESSING_CAPTURE_LEVELS_ADJUSTER_CAPTURE_LEVELS_ADJUSTER_H_
89