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 | |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 27 | SaturationProtector(ApmDataDumper* apm_data_dumper, |
| 28 | float extra_saturation_margin_db); |
| 29 | |
Alex Loiko | 1e48e80 | 2018-03-28 09:45:29 +0200 | [diff] [blame] | 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; |
Alex Loiko | 9917c4a | 2018-04-04 14:16:10 +0200 | [diff] [blame] | 40 | |
Alex Loiko | 4d01146 | 2018-07-02 17:00:31 +0200 | [diff] [blame] | 41 | // Resets the internal memory. |
| 42 | void Reset(); |
| 43 | |
Alex Loiko | 9917c4a | 2018-04-04 14:16:10 +0200 | [diff] [blame] | 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 | |
Alex Loiko | 4bb1e4a | 2018-10-05 11:06:14 +0200 | [diff] [blame] | 64 | float last_margin_; |
Alex Loiko | 9917c4a | 2018-04-04 14:16:10 +0200 | [diff] [blame] | 65 | PeakEnveloper peak_enveloper_; |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 66 | const float extra_saturation_margin_db_; |
Alex Loiko | 1e48e80 | 2018-03-28 09:45:29 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | } // namespace webrtc |
| 70 | |
| 71 | #endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_ |