ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |
| 13 | |
| 14 | #include <complex> |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 15 | #include <memory> |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 16 | #include <vector> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 17 | |
terelius | 85fa7d5 | 2016-03-24 01:51:52 -0700 | [diff] [blame] | 18 | #include "webrtc/base/swap_queue.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 19 | #include "webrtc/common_audio/lapped_transform.h" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 20 | #include "webrtc/common_audio/channel_buffer.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
peah | 737f4b8 | 2016-03-10 23:05:28 -0800 | [diff] [blame] | 22 | #include "webrtc/modules/audio_processing/render_queue_item_verifier.h" |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 23 | #include "webrtc/modules/audio_processing/vad/voice_activity_detector.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 24 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | |
| 27 | // Speech intelligibility enhancement module. Reads render and capture |
| 28 | // audio streams and modifies the render stream with a set of gains per |
| 29 | // frequency bin to enhance speech against the noise background. |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 30 | // Details of the model and algorithm can be found in the original paper: |
| 31 | // http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788 |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 32 | class IntelligibilityEnhancer : public LappedTransform::Callback { |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 33 | public: |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 34 | IntelligibilityEnhancer(int sample_rate_hz, |
| 35 | size_t num_render_channels, |
| 36 | size_t num_noise_bins); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 37 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 38 | // Sets the capture noise magnitude spectrum estimate. |
| 39 | void SetCaptureNoiseEstimate(std::vector<float> noise); |
ekm | b7553df | 2015-06-16 18:57:32 -0700 | [diff] [blame] | 40 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 41 | // Reads chunk of speech in time domain and updates with modified signal. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 42 | void ProcessRenderAudio(float* const* audio, |
| 43 | int sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 44 | size_t num_channels); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 45 | bool active() const; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 46 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 47 | protected: |
| 48 | // All in frequency domain, receives input |in_block|, applies |
| 49 | // intelligibility enhancement, and writes result to |out_block|. |
| 50 | void ProcessAudioBlock(const std::complex<float>* const* in_block, |
| 51 | size_t in_channels, |
| 52 | size_t frames, |
| 53 | size_t out_channels, |
| 54 | std::complex<float>* const* out_block) override; |
| 55 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 56 | private: |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 57 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation); |
| 58 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 59 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 60 | // Bisection search for optimal |lambda|. |
aluebs | f99af6b | 2016-02-24 17:25:42 -0800 | [diff] [blame] | 61 | void SolveForLambda(float power_target); |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 62 | |
| 63 | // Transforms freq gains to ERB gains. |
| 64 | void UpdateErbGains(); |
| 65 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 66 | // Returns number of ERB filters. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 67 | static size_t GetBankSize(int sample_rate, size_t erb_resolution); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 68 | |
| 69 | // Initializes ERB filterbank. |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 70 | std::vector<std::vector<float>> CreateErbBank(size_t num_freqs); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 71 | |
| 72 | // Analytically solves quadratic for optimal gains given |lambda|. |
| 73 | // Negative gains are set to 0. Stores the results in |sols|. |
Alejandro Luebs | 98c69a0 | 2016-03-29 12:43:32 -0700 | [diff] [blame^] | 74 | void SolveForGainsGivenLambda(double lambda, size_t start_freq, float* sols); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 75 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 76 | // Returns true if the audio is speech. |
| 77 | bool IsSpeech(const float* audio); |
| 78 | |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 79 | static const size_t kMaxNumNoiseEstimatesToBuffer = 5; |
| 80 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 81 | const size_t freqs_; // Num frequencies in frequency domain. |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 82 | const size_t num_noise_bins_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 83 | const size_t chunk_length_; // Chunk size in samples. |
| 84 | const size_t bank_size_; // Num ERB filters. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 85 | const int sample_rate_hz_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 86 | const size_t num_render_channels_; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 87 | |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 88 | intelligibility::PowerEstimator<std::complex<float>> clear_power_estimator_; |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 89 | intelligibility::PowerEstimator<float> noise_power_estimator_; |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 90 | std::vector<float> filtered_clear_pow_; |
| 91 | std::vector<float> filtered_noise_pow_; |
| 92 | std::vector<float> center_freqs_; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 93 | std::vector<std::vector<float>> capture_filter_bank_; |
| 94 | std::vector<std::vector<float>> render_filter_bank_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 95 | size_t start_freq_; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 96 | |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 97 | std::vector<float> gains_eq_; // Pre-filter modified gains. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 98 | intelligibility::GainApplier gain_applier_; |
| 99 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 100 | std::unique_ptr<LappedTransform> render_mangler_; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 101 | |
| 102 | VoiceActivityDetector vad_; |
| 103 | std::vector<int16_t> audio_s16_; |
| 104 | size_t chunks_since_voice_; |
| 105 | bool is_speech_; |
Alex Luebs | 57ae829 | 2016-03-09 16:24:34 +0100 | [diff] [blame] | 106 | |
| 107 | std::vector<float> noise_estimation_buffer_; |
| 108 | SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>> |
| 109 | noise_estimation_queue_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace webrtc |
| 113 | |
| 114 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |