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 
11 #ifndef MODULES_AUDIO_PROCESSING_CAPTURE_LEVELS_ADJUSTER_AUDIO_SAMPLES_SCALER_H_
12 #define MODULES_AUDIO_PROCESSING_CAPTURE_LEVELS_ADJUSTER_AUDIO_SAMPLES_SCALER_H_
13 
14 #include <stddef.h>
15 
16 #include "modules/audio_processing/audio_buffer.h"
17 
18 namespace webrtc {
19 
20 // Handles and applies a gain to the samples in an audio buffer.
21 // The gain is applied for each sample and any changes in the gain take effect
22 // gradually (in a linear manner) over one frame.
23 class AudioSamplesScaler {
24  public:
25   // C-tor. The supplied `initial_gain` is used immediately at the first call to
26   // Process(), i.e., in contrast to the gain supplied by SetGain(...) there is
27   // no gradual change to the `initial_gain`.
28   explicit AudioSamplesScaler(float initial_gain);
29   AudioSamplesScaler(const AudioSamplesScaler&) = delete;
30   AudioSamplesScaler& operator=(const AudioSamplesScaler&) = delete;
31 
32   // Applies the specified gain to the audio in `audio_buffer`.
33   void Process(AudioBuffer& audio_buffer);
34 
35   // Sets the gain to apply to each sample.
SetGain(float gain)36   void SetGain(float gain) { target_gain_ = gain; }
37 
38  private:
39   float previous_gain_ = 1.f;
40   float target_gain_ = 1.f;
41   int samples_per_channel_ = -1;
42   float one_by_samples_per_channel_ = -1.f;
43 };
44 }  // namespace webrtc
45 
46 #endif  // MODULES_AUDIO_PROCESSING_CAPTURE_LEVELS_ADJUSTER_AUDIO_SAMPLES_SCALER_H_
47