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