1 /* 2 * Copyright (c) 2013 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_TRANSIENT_TRANSIENT_SUPPRESSOR_IMPL_H_ 12 #define MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_SUPPRESSOR_IMPL_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <memory> 18 19 #include "modules/audio_processing/transient/transient_suppressor.h" 20 #include "rtc_base/gtest_prod_util.h" 21 22 namespace webrtc { 23 24 class TransientDetector; 25 26 // Detects transients in an audio stream and suppress them using a simple 27 // restoration algorithm that attenuates unexpected spikes in the spectrum. 28 class TransientSuppressorImpl : public TransientSuppressor { 29 public: 30 TransientSuppressorImpl(); 31 ~TransientSuppressorImpl() override; 32 33 int Initialize(int sample_rate_hz, 34 int detector_rate_hz, 35 int num_channels) override; 36 37 // Processes a |data| chunk, and returns it with keystrokes suppressed from 38 // it. The float format is assumed to be int16 ranged. If there are more than 39 // one channel, the chunks are concatenated one after the other in |data|. 40 // |data_length| must be equal to |data_length_|. 41 // |num_channels| must be equal to |num_channels_|. 42 // A sub-band, ideally the higher, can be used as |detection_data|. If it is 43 // NULL, |data| is used for the detection too. The |detection_data| is always 44 // assumed mono. 45 // If a reference signal (e.g. keyboard microphone) is available, it can be 46 // passed in as |reference_data|. It is assumed mono and must have the same 47 // length as |data|. NULL is accepted if unavailable. 48 // This suppressor performs better if voice information is available. 49 // |voice_probability| is the probability of voice being present in this chunk 50 // of audio. If voice information is not available, |voice_probability| must 51 // always be set to 1. 52 // |key_pressed| determines if a key was pressed on this audio chunk. 53 // Returns 0 on success and -1 otherwise. 54 int Suppress(float* data, 55 size_t data_length, 56 int num_channels, 57 const float* detection_data, 58 size_t detection_length, 59 const float* reference_data, 60 size_t reference_length, 61 float voice_probability, 62 bool key_pressed) override; 63 64 private: 65 FRIEND_TEST_ALL_PREFIXES(TransientSuppressorImplTest, 66 TypingDetectionLogicWorksAsExpectedForMono); 67 void Suppress(float* in_ptr, float* spectral_mean, float* out_ptr); 68 69 void UpdateKeypress(bool key_pressed); 70 void UpdateRestoration(float voice_probability); 71 72 void UpdateBuffers(float* data); 73 74 void HardRestoration(float* spectral_mean); 75 void SoftRestoration(float* spectral_mean); 76 77 std::unique_ptr<TransientDetector> detector_; 78 79 size_t data_length_; 80 size_t detection_length_; 81 size_t analysis_length_; 82 size_t buffer_delay_; 83 size_t complex_analysis_length_; 84 int num_channels_; 85 // Input buffer where the original samples are stored. 86 std::unique_ptr<float[]> in_buffer_; 87 std::unique_ptr<float[]> detection_buffer_; 88 // Output buffer where the restored samples are stored. 89 std::unique_ptr<float[]> out_buffer_; 90 91 // Arrays for fft. 92 std::unique_ptr<size_t[]> ip_; 93 std::unique_ptr<float[]> wfft_; 94 95 std::unique_ptr<float[]> spectral_mean_; 96 97 // Stores the data for the fft. 98 std::unique_ptr<float[]> fft_buffer_; 99 100 std::unique_ptr<float[]> magnitudes_; 101 102 const float* window_; 103 104 std::unique_ptr<float[]> mean_factor_; 105 106 float detector_smoothed_; 107 108 int keypress_counter_; 109 int chunks_since_keypress_; 110 bool detection_enabled_; 111 bool suppression_enabled_; 112 113 bool use_hard_restoration_; 114 int chunks_since_voice_change_; 115 116 uint32_t seed_; 117 118 bool using_reference_; 119 }; 120 121 } // namespace webrtc 122 123 #endif // MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_SUPPRESSOR_IMPL_H_ 124