xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/fixed_digital_level_estimator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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_AGC2_FIXED_DIGITAL_LEVEL_ESTIMATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_FIXED_DIGITAL_LEVEL_ESTIMATOR_H_
13 
14 #include <array>
15 #include <vector>
16 
17 #include "modules/audio_processing/agc2/agc2_common.h"
18 #include "modules/audio_processing/include/audio_frame_view.h"
19 
20 namespace webrtc {
21 
22 class ApmDataDumper;
23 // Produces a smooth signal level estimate from an input audio
24 // stream. The estimate smoothing is done through exponential
25 // filtering.
26 class FixedDigitalLevelEstimator {
27  public:
28   // Sample rates are allowed if the number of samples in a frame
29   // (sample_rate_hz * kFrameDurationMs / 1000) is divisible by
30   // kSubFramesInSample. For kFrameDurationMs=10 and
31   // kSubFramesInSample=20, this means that sample_rate_hz has to be
32   // divisible by 2000.
33   FixedDigitalLevelEstimator(int sample_rate_hz,
34                              ApmDataDumper* apm_data_dumper);
35 
36   FixedDigitalLevelEstimator(const FixedDigitalLevelEstimator&) = delete;
37   FixedDigitalLevelEstimator& operator=(const FixedDigitalLevelEstimator&) =
38       delete;
39 
40   // The input is assumed to be in FloatS16 format. Scaled input will
41   // produce similarly scaled output. A frame of with kFrameDurationMs
42   // ms of audio produces a level estimates in the same scale. The
43   // level estimate contains kSubFramesInFrame values.
44   std::array<float, kSubFramesInFrame> ComputeLevel(
45       const AudioFrameView<const float>& float_frame);
46 
47   // Rate may be changed at any time (but not concurrently) from the
48   // value passed to the constructor. The class is not thread safe.
49   void SetSampleRate(int sample_rate_hz);
50 
51   // Resets the level estimator internal state.
52   void Reset();
53 
LastAudioLevel()54   float LastAudioLevel() const { return filter_state_level_; }
55 
56  private:
57   void CheckParameterCombination();
58 
59   ApmDataDumper* const apm_data_dumper_ = nullptr;
60   float filter_state_level_;
61   int samples_in_frame_;
62   int samples_in_sub_frame_;
63 };
64 }  // namespace webrtc
65 
66 #endif  // MODULES_AUDIO_PROCESSING_AGC2_FIXED_DIGITAL_LEVEL_ESTIMATOR_H_
67