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 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 11 | // |
| 12 | // Specifies core class for intelligbility enhancement. |
| 13 | // |
| 14 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 15 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |
| 16 | #define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |
| 17 | |
| 18 | #include <complex> |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 19 | #include <vector> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 20 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 21 | #include "webrtc/base/scoped_ptr.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 22 | #include "webrtc/common_audio/lapped_transform.h" |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 23 | #include "webrtc/common_audio/channel_buffer.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 24 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 25 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 26 | namespace webrtc { |
| 27 | |
| 28 | // Speech intelligibility enhancement module. Reads render and capture |
| 29 | // audio streams and modifies the render stream with a set of gains per |
| 30 | // frequency bin to enhance speech against the noise background. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 31 | // Note: assumes speech and noise streams are already separated. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 32 | class IntelligibilityEnhancer { |
| 33 | public: |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 34 | struct Config { |
| 35 | // |var_*| are parameters for the VarianceArray constructor for the |
| 36 | // clear speech stream. |
| 37 | // TODO(bercic): the |var_*|, |*_rate| and |gain_limit| parameters should |
| 38 | // probably go away once fine tuning is done. |
| 39 | Config() |
| 40 | : sample_rate_hz(16000), |
| 41 | num_capture_channels(1), |
| 42 | num_render_channels(1), |
| 43 | var_type(intelligibility::VarianceArray::kStepDecaying), |
| 44 | var_decay_rate(0.9f), |
| 45 | var_window_size(10), |
| 46 | analysis_rate(800), |
| 47 | gain_change_limit(0.1f), |
| 48 | rho(0.02f) {} |
| 49 | int sample_rate_hz; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 50 | size_t num_capture_channels; |
| 51 | size_t num_render_channels; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 52 | intelligibility::VarianceArray::StepType var_type; |
| 53 | float var_decay_rate; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 54 | size_t var_window_size; |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 55 | int analysis_rate; |
| 56 | float gain_change_limit; |
| 57 | float rho; |
| 58 | }; |
| 59 | |
| 60 | explicit IntelligibilityEnhancer(const Config& config); |
| 61 | IntelligibilityEnhancer(); // Initialize with default config. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 62 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 63 | // Reads and processes chunk of noise stream in time domain. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 64 | void AnalyzeCaptureAudio(float* const* audio, |
| 65 | int sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 66 | size_t num_channels); |
ekm | b7553df | 2015-06-16 18:57:32 -0700 | [diff] [blame] | 67 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 68 | // Reads chunk of speech in time domain and updates with modified signal. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 69 | void ProcessRenderAudio(float* const* audio, |
| 70 | int sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 71 | size_t num_channels); |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 72 | bool active() const; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 73 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 74 | private: |
| 75 | enum AudioSource { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 76 | kRenderStream = 0, // Clear speech stream. |
| 77 | kCaptureStream, // Noise stream. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 80 | // Provides access point to the frequency domain. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 81 | class TransformCallback : public LappedTransform::Callback { |
| 82 | public: |
| 83 | TransformCallback(IntelligibilityEnhancer* parent, AudioSource source); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 84 | |
| 85 | // All in frequency domain, receives input |in_block|, applies |
| 86 | // intelligibility enhancement, and writes result to |out_block|. |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 87 | void ProcessAudioBlock(const std::complex<float>* const* in_block, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 88 | size_t in_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 89 | size_t frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 90 | size_t out_channels, |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 91 | std::complex<float>* const* out_block) override; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | IntelligibilityEnhancer* parent_; |
| 95 | AudioSource source_; |
| 96 | }; |
| 97 | friend class TransformCallback; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 98 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation); |
| 99 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 100 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 101 | // Sends streams to ProcessClearBlock or ProcessNoiseBlock based on source. |
| 102 | void DispatchAudio(AudioSource source, |
| 103 | const std::complex<float>* in_block, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 104 | std::complex<float>* out_block); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 105 | |
| 106 | // Updates variance computation and analysis with |in_block_|, |
| 107 | // and writes modified speech to |out_block|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 108 | void ProcessClearBlock(const std::complex<float>* in_block, |
| 109 | std::complex<float>* out_block); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 110 | |
| 111 | // Computes and sets modified gains. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 112 | void AnalyzeClearBlock(float power_target); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 113 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 114 | // Bisection search for optimal |lambda|. |
| 115 | void SolveForLambda(float power_target, float power_bot, float power_top); |
| 116 | |
| 117 | // Transforms freq gains to ERB gains. |
| 118 | void UpdateErbGains(); |
| 119 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 120 | // Updates variance calculation for noise input with |in_block|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 121 | void ProcessNoiseBlock(const std::complex<float>* in_block, |
| 122 | std::complex<float>* out_block); |
| 123 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 124 | // Returns number of ERB filters. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 125 | static size_t GetBankSize(int sample_rate, size_t erb_resolution); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 126 | |
| 127 | // Initializes ERB filterbank. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 128 | void CreateErbBank(); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 129 | |
| 130 | // Analytically solves quadratic for optimal gains given |lambda|. |
| 131 | // Negative gains are set to 0. Stores the results in |sols|. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 132 | void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 133 | |
| 134 | // Computes variance across ERB filters from freq variance |var|. |
| 135 | // Stores in |result|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 136 | void FilterVariance(const float* var, float* result); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 137 | |
| 138 | // Returns dot product of vectors specified by size |length| arrays |a|,|b|. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 139 | static float DotProduct(const float* a, const float* b, size_t length); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 140 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 141 | const size_t freqs_; // Num frequencies in frequency domain. |
| 142 | const size_t window_size_; // Window size in samples; also the block size. |
| 143 | const size_t chunk_length_; // Chunk size in samples. |
| 144 | const size_t bank_size_; // Num ERB filters. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 145 | const int sample_rate_hz_; |
| 146 | const int erb_resolution_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 147 | const size_t num_capture_channels_; |
| 148 | const size_t num_render_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 149 | const int analysis_rate_; // Num blocks before gains recalculated. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 150 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 151 | const bool active_; // Whether render gains are being updated. |
| 152 | // TODO(ekm): Add logic for updating |active_|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 153 | |
| 154 | intelligibility::VarianceArray clear_variance_; |
| 155 | intelligibility::VarianceArray noise_variance_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 156 | rtc::scoped_ptr<float[]> filtered_clear_var_; |
| 157 | rtc::scoped_ptr<float[]> filtered_noise_var_; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 158 | std::vector<std::vector<float>> filter_bank_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 159 | rtc::scoped_ptr<float[]> center_freqs_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 160 | size_t start_freq_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 161 | rtc::scoped_ptr<float[]> rho_; // Production and interpretation SNR. |
| 162 | // for each ERB band. |
| 163 | rtc::scoped_ptr<float[]> gains_eq_; // Pre-filter modified gains. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 164 | intelligibility::GainApplier gain_applier_; |
| 165 | |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 166 | // Destination buffers used to reassemble blocked chunks before overwriting |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 167 | // the original input array with modifications. |
ekmeyerson | 60d9b33 | 2015-08-14 10:35:55 -0700 | [diff] [blame] | 168 | ChannelBuffer<float> temp_render_out_buffer_; |
| 169 | ChannelBuffer<float> temp_capture_out_buffer_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 170 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 171 | rtc::scoped_ptr<float[]> kbd_window_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 172 | TransformCallback render_callback_; |
| 173 | TransformCallback capture_callback_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 174 | rtc::scoped_ptr<LappedTransform> render_mangler_; |
| 175 | rtc::scoped_ptr<LappedTransform> capture_mangler_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 176 | int block_count_; |
| 177 | int analysis_step_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } // namespace webrtc |
| 181 | |
| 182 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |