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" |
| 23 | #include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 24 | |
| 25 | struct WebRtcVadInst; |
| 26 | typedef struct WebRtcVadInst VadInst; |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | // Speech intelligibility enhancement module. Reads render and capture |
| 31 | // audio streams and modifies the render stream with a set of gains per |
| 32 | // frequency bin to enhance speech against the noise background. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 33 | // Note: assumes speech and noise streams are already separated. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 34 | class IntelligibilityEnhancer { |
| 35 | public: |
| 36 | // Construct a new instance with the given filter bank resolution, |
| 37 | // sampling rate, number of channels and analysis rates. |
| 38 | // |analysis_rate| sets the number of input blocks (containing speech!) |
| 39 | // to elapse before a new gain computation is made. |variance_rate| specifies |
| 40 | // the number of gain recomputations after which the variances are reset. |
| 41 | // |cv_*| are parameters for the VarianceArray constructor for the |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 42 | // clear speech stream. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 43 | // TODO(bercic): the |cv_*|, |*_rate| and |gain_limit| parameters should |
| 44 | // probably go away once fine tuning is done. They override the internal |
| 45 | // constants in the class (kGainChangeLimit, kAnalyzeRate, kVarianceRate). |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 46 | IntelligibilityEnhancer(int erb_resolution, |
| 47 | int sample_rate_hz, |
| 48 | int channels, |
| 49 | int cv_type, |
| 50 | float cv_alpha, |
| 51 | int cv_win, |
| 52 | int analysis_rate, |
| 53 | int variance_rate, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 54 | float gain_limit); |
| 55 | ~IntelligibilityEnhancer(); |
| 56 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 57 | // Reads and processes chunk of noise stream in time domain. |
aluebs | c555b99 | 2015-06-16 20:26:16 -0700 | [diff] [blame] | 58 | void ProcessCaptureAudio(float* const* audio); |
ekm | b7553df | 2015-06-16 18:57:32 -0700 | [diff] [blame] | 59 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 60 | // Reads chunk of speech in time domain and updates with modified signal. |
| 61 | void ProcessRenderAudio(float* const* audio); |
| 62 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 63 | private: |
| 64 | enum AudioSource { |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 65 | kRenderStream = 0, // Clear speech stream. |
| 66 | kCaptureStream, // Noise stream. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 69 | // Provides access point to the frequency domain. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 70 | class TransformCallback : public LappedTransform::Callback { |
| 71 | public: |
| 72 | TransformCallback(IntelligibilityEnhancer* parent, AudioSource source); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 73 | |
| 74 | // All in frequency domain, receives input |in_block|, applies |
| 75 | // intelligibility enhancement, and writes result to |out_block|. |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame^] | 76 | void ProcessAudioBlock(const std::complex<float>* const* in_block, |
| 77 | int in_channels, |
| 78 | int frames, |
| 79 | int out_channels, |
| 80 | std::complex<float>* const* out_block) override; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | IntelligibilityEnhancer* parent_; |
| 84 | AudioSource source_; |
| 85 | }; |
| 86 | friend class TransformCallback; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 87 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation); |
| 88 | FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 89 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 90 | // Sends streams to ProcessClearBlock or ProcessNoiseBlock based on source. |
| 91 | void DispatchAudio(AudioSource source, |
| 92 | const std::complex<float>* in_block, |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 93 | std::complex<float>* out_block); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 94 | |
| 95 | // Updates variance computation and analysis with |in_block_|, |
| 96 | // and writes modified speech to |out_block|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 97 | void ProcessClearBlock(const std::complex<float>* in_block, |
| 98 | std::complex<float>* out_block); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 99 | |
| 100 | // Computes and sets modified gains. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 101 | void AnalyzeClearBlock(float power_target); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 102 | |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 103 | // Bisection search for optimal |lambda|. |
| 104 | void SolveForLambda(float power_target, float power_bot, float power_top); |
| 105 | |
| 106 | // Transforms freq gains to ERB gains. |
| 107 | void UpdateErbGains(); |
| 108 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 109 | // Updates variance calculation for noise input with |in_block|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 110 | void ProcessNoiseBlock(const std::complex<float>* in_block, |
| 111 | std::complex<float>* out_block); |
| 112 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 113 | // Returns number of ERB filters. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 114 | static int GetBankSize(int sample_rate, int erb_resolution); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 115 | |
| 116 | // Initializes ERB filterbank. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 117 | void CreateErbBank(); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 118 | |
| 119 | // Analytically solves quadratic for optimal gains given |lambda|. |
| 120 | // Negative gains are set to 0. Stores the results in |sols|. |
| 121 | void SolveForGainsGivenLambda(float lambda, int start_freq, float* sols); |
| 122 | |
| 123 | // Computes variance across ERB filters from freq variance |var|. |
| 124 | // Stores in |result|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 125 | void FilterVariance(const float* var, float* result); |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 126 | |
| 127 | // Returns dot product of vectors specified by size |length| arrays |a|,|b|. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 128 | static float DotProduct(const float* a, const float* b, int length); |
| 129 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 130 | const int freqs_; // Num frequencies in frequency domain. |
| 131 | const int window_size_; // Window size in samples; also the block size. |
| 132 | const int chunk_length_; // Chunk size in samples. |
| 133 | const int bank_size_; // Num ERB filters. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 134 | const int sample_rate_hz_; |
| 135 | const int erb_resolution_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 136 | const int channels_; // Num channels. |
| 137 | const int analysis_rate_; // Num blocks before gains recalculated. |
| 138 | const int variance_rate_; // Num recalculations before history is cleared. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 139 | |
| 140 | intelligibility::VarianceArray clear_variance_; |
| 141 | intelligibility::VarianceArray noise_variance_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 142 | rtc::scoped_ptr<float[]> filtered_clear_var_; |
| 143 | rtc::scoped_ptr<float[]> filtered_noise_var_; |
ekm | 35b72fb | 2015-07-10 14:11:52 -0700 | [diff] [blame] | 144 | std::vector<std::vector<float>> filter_bank_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 145 | rtc::scoped_ptr<float[]> center_freqs_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 146 | int start_freq_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 147 | rtc::scoped_ptr<float[]> rho_; // Production and interpretation SNR. |
| 148 | // for each ERB band. |
| 149 | rtc::scoped_ptr<float[]> gains_eq_; // Pre-filter modified gains. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 150 | intelligibility::GainApplier gain_applier_; |
| 151 | |
| 152 | // Destination buffer used to reassemble blocked chunks before overwriting |
| 153 | // the original input array with modifications. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 154 | // TODO(ekmeyerson): Switch to using ChannelBuffer. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 155 | float** temp_out_buffer_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 156 | |
| 157 | rtc::scoped_ptr<float* []> input_audio_; |
| 158 | rtc::scoped_ptr<float[]> kbd_window_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 159 | TransformCallback render_callback_; |
| 160 | TransformCallback capture_callback_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 161 | rtc::scoped_ptr<LappedTransform> render_mangler_; |
| 162 | rtc::scoped_ptr<LappedTransform> capture_mangler_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 163 | int block_count_; |
| 164 | int analysis_step_; |
| 165 | |
| 166 | // TODO(bercic): Quick stopgap measure for voice detection in the clear |
| 167 | // and noise streams. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 168 | // Note: VAD currently does not affect anything in IntelligibilityEnhancer. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 169 | VadInst* vad_high_; |
| 170 | VadInst* vad_low_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 171 | rtc::scoped_ptr<int16_t[]> vad_tmp_buffer_; |
| 172 | bool has_voice_low_; // Whether voice detected in speech stream. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace webrtc |
| 176 | |
| 177 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_ |