blob: 3f207da83016291c15f476bce989e45a4e9a5110 [file] [log] [blame]
Alex Loiko1e48e802018-03-28 09:45:29 +02001/*
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_SATURATION_PROTECTOR_H_
12#define MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_
13
14#include <array>
15
16#include "modules/audio_processing/agc2/agc2_common.h"
Alex Loikodb6af362018-06-20 14:14:18 +020017#include "modules/audio_processing/agc2/vad_with_level.h"
Alex Loiko1e48e802018-03-28 09:45:29 +020018
19namespace webrtc {
20
21class ApmDataDumper;
22
23class SaturationProtector {
24 public:
25 explicit SaturationProtector(ApmDataDumper* apm_data_dumper);
26
27 // Update and return margin estimate. This method should be called
28 // whenever a frame is reliably classified as 'speech'.
29 //
30 // Returned value is in DB scale.
31 void UpdateMargin(const VadWithLevel::LevelAndProbability& vad_data,
32 float last_speech_level_estimate_dbfs);
33
34 // Returns latest computed margin. Used in cases when speech is not
35 // detected.
36 float LastMargin() const;
Alex Loiko9917c4a2018-04-04 14:16:10 +020037
Alex Loiko4d011462018-07-02 17:00:31 +020038 // Resets the internal memory.
39 void Reset();
40
Alex Loiko9917c4a2018-04-04 14:16:10 +020041 void DebugDumpEstimate() const;
42
43 private:
44 // Computes a delayed envelope of peaks.
45 class PeakEnveloper {
46 public:
47 PeakEnveloper();
48 void Process(float frame_peak_dbfs);
49
50 float Query() const;
51
52 private:
53 size_t speech_time_in_estimate_ms_ = 0;
54 float current_superframe_peak_dbfs_ = -90.f;
55 size_t elements_in_buffer_ = 0;
56 std::array<float, kPeakEnveloperBufferSize> peak_delay_buffer_ = {};
57 };
58
59 ApmDataDumper* apm_data_dumper_;
60
61 float last_margin_ = kInitialSaturationMarginDb;
62 PeakEnveloper peak_enveloper_;
Alex Loiko1e48e802018-03-28 09:45:29 +020063};
64
65} // namespace webrtc
66
67#endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_