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_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" 17 #include "modules/audio_processing/agc2/vad_with_level.h" 18 19 namespace webrtc { 20 21 class ApmDataDumper; 22 23 class SaturationProtector { 24 public: 25 explicit SaturationProtector(ApmDataDumper* apm_data_dumper); 26 27 SaturationProtector(ApmDataDumper* apm_data_dumper, 28 float extra_saturation_margin_db); 29 30 // 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; 40 41 // Resets the internal memory. 42 void Reset(); 43 44 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 64 float last_margin_; 65 PeakEnveloper peak_enveloper_; 66 const float extra_saturation_margin_db_; 67 }; 68 69 } // namespace webrtc 70 71 #endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_ 72