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 | |
| 18 | #include "webrtc/common_audio/lapped_transform.h" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 19 | #include "webrtc/common_audio/channel_buffer.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 21 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | // Speech intelligibility enhancement module. Reads render and capture |
| 25 | // audio streams and modifies the render stream with a set of gains per |
| 26 | // frequency bin to enhance speech against the noise background. |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 27 | // Details of the model and algorithm can be found in the original paper: |
| 28 | // http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788 |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 29 | class IntelligibilityEnhancer { |
| 30 | public: |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 31 | struct Config { |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 32 | // TODO(bercic): the |decay_rate|, |analysis_rate| and |gain_limit| |
| 33 | // parameters should probably go away once fine tuning is done. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 34 | Config() |
| 35 | : sample_rate_hz(16000), |
| 36 | num_capture_channels(1), |
| 37 | num_render_channels(1), |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 38 | decay_rate(0.9f), |
| 39 | analysis_rate(60), |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 40 | gain_change_limit(0.1f), |
| 41 | rho(0.02f) {} |
| 42 | int sample_rate_hz; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 43 | size_t num_capture_channels; |
| 44 | size_t num_render_channels; |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 45 | float decay_rate; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 46 | int analysis_rate; |
| 47 | float gain_change_limit; |
| 48 | float rho; |
| 49 | }; |
| 50 | |
| 51 | explicit IntelligibilityEnhancer(const Config& config); |
| 52 | IntelligibilityEnhancer(); // Initialize with default config. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 53 | |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 54 | // Sets the capture noise magnitude spectrum estimate. |
| 55 | void SetCaptureNoiseEstimate(std::vector<float> noise); |
ekm | b7553df | 2015-06-16 18:57:32 -0700 | [diff] [blame] | 56 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 57 | // Reads chunk of speech in time domain and updates with modified signal. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 58 | void ProcessRenderAudio(float* const* audio, |
| 59 | int sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 60 | size_t num_channels); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 61 | bool active() const; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 62 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 63 | private: |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 64 | // Provides access point to the frequency domain. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 65 | class TransformCallback : public LappedTransform::Callback { |
| 66 | public: |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 67 | TransformCallback(IntelligibilityEnhancer* parent); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 68 | |
| 69 | // All in frequency domain, receives input |in_block|, applies |
| 70 | // intelligibility enhancement, and writes result to |out_block|. |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 71 | void ProcessAudioBlock(const std::complex<float>* const* in_block, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 72 | size_t in_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 73 | size_t frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 74 | size_t out_channels, |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 75 | std::complex<float>* const* out_block) override; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | IntelligibilityEnhancer* parent_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 79 | }; |
| 80 | friend class TransformCallback; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 81 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation); |
| 82 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 83 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 84 | // Updates power computation and analysis with |in_block_|, |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 85 | // and writes modified speech to |out_block|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 86 | void ProcessClearBlock(const std::complex<float>* in_block, |
| 87 | std::complex<float>* out_block); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 88 | |
| 89 | // Computes and sets modified gains. |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 90 | void AnalyzeClearBlock(); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 91 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 92 | // Bisection search for optimal |lambda|. |
| 93 | void SolveForLambda(float power_target, float power_bot, float power_top); |
| 94 | |
| 95 | // Transforms freq gains to ERB gains. |
| 96 | void UpdateErbGains(); |
| 97 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 98 | // Returns number of ERB filters. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 99 | static size_t GetBankSize(int sample_rate, size_t erb_resolution); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 100 | |
| 101 | // Initializes ERB filterbank. |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 102 | std::vector<std::vector<float>> CreateErbBank(size_t num_freqs); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 103 | |
| 104 | // Analytically solves quadratic for optimal gains given |lambda|. |
| 105 | // Negative gains are set to 0. Stores the results in |sols|. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 106 | void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 107 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 108 | const size_t freqs_; // Num frequencies in frequency domain. |
| 109 | const size_t window_size_; // Window size in samples; also the block size. |
| 110 | const size_t chunk_length_; // Chunk size in samples. |
| 111 | const size_t bank_size_; // Num ERB filters. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 112 | const int sample_rate_hz_; |
| 113 | const int erb_resolution_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 114 | const size_t num_capture_channels_; |
| 115 | const size_t num_render_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 116 | const int analysis_rate_; // Num blocks before gains recalculated. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 117 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 118 | const bool active_; // Whether render gains are being updated. |
| 119 | // TODO(ekm): Add logic for updating |active_|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 120 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 121 | intelligibility::PowerEstimator clear_power_; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 122 | std::vector<float> noise_power_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame^] | 123 | std::unique_ptr<float[]> filtered_clear_pow_; |
| 124 | std::unique_ptr<float[]> filtered_noise_pow_; |
| 125 | std::unique_ptr<float[]> center_freqs_; |
aluebs | c466bad | 2016-02-10 12:03:00 -0800 | [diff] [blame] | 126 | std::vector<std::vector<float>> capture_filter_bank_; |
| 127 | std::vector<std::vector<float>> render_filter_bank_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 128 | size_t start_freq_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame^] | 129 | std::unique_ptr<float[]> rho_; // Production and interpretation SNR. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 130 | // for each ERB band. |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame^] | 131 | std::unique_ptr<float[]> gains_eq_; // Pre-filter modified gains. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 132 | intelligibility::GainApplier gain_applier_; |
| 133 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 134 | // Destination buffers used to reassemble blocked chunks before overwriting |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 135 | // the original input array with modifications. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 136 | ChannelBuffer<float> temp_render_out_buffer_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 137 | |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame^] | 138 | std::unique_ptr<float[]> kbd_window_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 139 | TransformCallback render_callback_; |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame^] | 140 | std::unique_ptr<LappedTransform> render_mangler_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 141 | int block_count_; |
| 142 | int analysis_step_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | } // namespace webrtc |
| 146 | |
| 147 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |