blob: e637469070eab6032a1f66e5fb5d9059c85ac1f2 [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
Alex Loiko5e784612018-11-01 14:51:56 +010027 SaturationProtector(ApmDataDumper* apm_data_dumper,
28 float extra_saturation_margin_db);
29
Alex Loiko1e48e802018-03-28 09:45:29 +020030 // Update and return margin estimate. This method should be called
31 // whenever a frame is reliably classified as 'speech'.
32 //
33 // Returned value is in DB scale.
34 void UpdateMargin(const VadWithLevel::LevelAndProbability& vad_data,
35 float last_speech_level_estimate_dbfs);
36
37 // Returns latest computed margin. Used in cases when speech is not
38 // detected.
39 float LastMargin() const;
Alex Loiko9917c4a2018-04-04 14:16:10 +020040
Alex Loiko4d011462018-07-02 17:00:31 +020041 // Resets the internal memory.
42 void Reset();
43
Alex Loiko9917c4a2018-04-04 14:16:10 +020044 void DebugDumpEstimate() const;
45
46 private:
47 // Computes a delayed envelope of peaks.
48 class PeakEnveloper {
49 public:
50 PeakEnveloper();
51 void Process(float frame_peak_dbfs);
52
53 float Query() const;
54
55 private:
56 size_t speech_time_in_estimate_ms_ = 0;
57 float current_superframe_peak_dbfs_ = -90.f;
58 size_t elements_in_buffer_ = 0;
59 std::array<float, kPeakEnveloperBufferSize> peak_delay_buffer_ = {};
60 };
61
62 ApmDataDumper* apm_data_dumper_;
63
Alex Loiko4bb1e4a2018-10-05 11:06:14 +020064 float last_margin_;
Alex Loiko9917c4a2018-04-04 14:16:10 +020065 PeakEnveloper peak_enveloper_;
Alex Loiko5e784612018-11-01 14:51:56 +010066 const float extra_saturation_margin_db_;
Alex Loiko1e48e802018-03-28 09:45:29 +020067};
68
69} // namespace webrtc
70
71#endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_