Alex Loiko | 1e48e80 | 2018-03-28 09:45:29 +0200 | [diff] [blame] | 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" |
Alex Loiko | db6af36 | 2018-06-20 14:14:18 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/agc2/vad_with_level.h" |
Alex Loiko | 1e48e80 | 2018-03-28 09:45:29 +0200 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | class ApmDataDumper; |
| 22 | |
| 23 | class 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 Loiko | 9917c4a | 2018-04-04 14:16:10 +0200 | [diff] [blame] | 37 | |
Alex Loiko | 4d01146 | 2018-07-02 17:00:31 +0200 | [diff] [blame] | 38 | // Resets the internal memory. |
| 39 | void Reset(); |
| 40 | |
Alex Loiko | 9917c4a | 2018-04-04 14:16:10 +0200 | [diff] [blame] | 41 | 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 Loiko | 1e48e80 | 2018-03-28 09:45:29 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace webrtc |
| 66 | |
| 67 | #endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_ |