henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/expand.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <string.h> // memset |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 15 | |
| 16 | #include <algorithm> // min, max |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 17 | #include <limits> // numeric_limits<T> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 20 | #include "modules/audio_coding/neteq/background_noise.h" |
| 21 | #include "modules/audio_coding/neteq/cross_correlation.h" |
| 22 | #include "modules/audio_coding/neteq/dsp_helper.h" |
| 23 | #include "modules/audio_coding/neteq/random_vector.h" |
| 24 | #include "modules/audio_coding/neteq/statistics_calculator.h" |
| 25 | #include "modules/audio_coding/neteq/sync_buffer.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 26 | #include "rtc_base/numerics/safe_conversions.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 30 | Expand::Expand(BackgroundNoise* background_noise, |
| 31 | SyncBuffer* sync_buffer, |
| 32 | RandomVector* random_vector, |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 33 | StatisticsCalculator* statistics, |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 34 | int fs, |
| 35 | size_t num_channels) |
| 36 | : random_vector_(random_vector), |
| 37 | sync_buffer_(sync_buffer), |
| 38 | first_expand_(true), |
| 39 | fs_hz_(fs), |
| 40 | num_channels_(num_channels), |
| 41 | consecutive_expands_(0), |
| 42 | background_noise_(background_noise), |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 43 | statistics_(statistics), |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 44 | overlap_length_(5 * fs / 8000), |
| 45 | lag_index_direction_(0), |
| 46 | current_lag_index_(0), |
| 47 | stop_muting_(false), |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 48 | expand_duration_samples_(0), |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 49 | channel_parameters_(new ChannelParameters[num_channels_]) { |
| 50 | assert(fs == 8000 || fs == 16000 || fs == 32000 || fs == 48000); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | assert(fs <= static_cast<int>(kMaxSampleRate)); // Should not be possible. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 52 | assert(num_channels_ > 0); |
| 53 | memset(expand_lags_, 0, sizeof(expand_lags_)); |
| 54 | Reset(); |
| 55 | } |
| 56 | |
| 57 | Expand::~Expand() = default; |
| 58 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 59 | void Expand::Reset() { |
| 60 | first_expand_ = true; |
| 61 | consecutive_expands_ = 0; |
| 62 | max_lag_ = 0; |
| 63 | for (size_t ix = 0; ix < num_channels_; ++ix) { |
| 64 | channel_parameters_[ix].expand_vector0.Clear(); |
| 65 | channel_parameters_[ix].expand_vector1.Clear(); |
| 66 | } |
| 67 | } |
| 68 | |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 69 | int Expand::Process(AudioMultiVector* output) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | int16_t random_vector[kMaxSampleRate / 8000 * 120 + 30]; |
| 71 | int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125]; |
| 72 | static const int kTempDataSize = 3600; |
| 73 | int16_t temp_data[kTempDataSize]; // TODO(hlundin) Remove this. |
| 74 | int16_t* voiced_vector_storage = temp_data; |
| 75 | int16_t* voiced_vector = &voiced_vector_storage[overlap_length_]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 76 | static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | int16_t unvoiced_array_memory[kNoiseLpcOrder + kMaxSampleRate / 8000 * 125]; |
| 78 | int16_t* unvoiced_vector = unvoiced_array_memory + kUnvoicedLpcOrder; |
| 79 | int16_t* noise_vector = unvoiced_array_memory + kNoiseLpcOrder; |
| 80 | |
| 81 | int fs_mult = fs_hz_ / 8000; |
| 82 | |
| 83 | if (first_expand_) { |
| 84 | // Perform initial setup if this is the first expansion since last reset. |
| 85 | AnalyzeSignal(random_vector); |
| 86 | first_expand_ = false; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 87 | expand_duration_samples_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 88 | } else { |
| 89 | // This is not the first expansion, parameters are already estimated. |
| 90 | // Extract a noise segment. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 91 | size_t rand_length = max_lag_; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 92 | // This only applies to SWB where length could be larger than 256. |
| 93 | assert(rand_length <= kMaxSampleRate / 8000 * 120 + 30); |
| 94 | GenerateRandomVector(2, rand_length, random_vector); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 95 | } |
| 96 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 97 | // Generate signal. |
| 98 | UpdateLagIndex(); |
| 99 | |
| 100 | // Voiced part. |
| 101 | // Generate a weighted vector with the current lag. |
| 102 | size_t expansion_vector_length = max_lag_ + overlap_length_; |
| 103 | size_t current_lag = expand_lags_[current_lag_index_]; |
| 104 | // Copy lag+overlap data. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 105 | size_t expansion_vector_position = |
| 106 | expansion_vector_length - current_lag - overlap_length_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | size_t temp_length = current_lag + overlap_length_; |
| 108 | for (size_t channel_ix = 0; channel_ix < num_channels_; ++channel_ix) { |
| 109 | ChannelParameters& parameters = channel_parameters_[channel_ix]; |
| 110 | if (current_lag_index_ == 0) { |
| 111 | // Use only expand_vector0. |
| 112 | assert(expansion_vector_position + temp_length <= |
| 113 | parameters.expand_vector0.Size()); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 114 | parameters.expand_vector0.CopyTo(temp_length, expansion_vector_position, |
| 115 | voiced_vector_storage); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | } else if (current_lag_index_ == 1) { |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 117 | std::unique_ptr<int16_t[]> temp_0(new int16_t[temp_length]); |
| 118 | parameters.expand_vector0.CopyTo(temp_length, expansion_vector_position, |
| 119 | temp_0.get()); |
| 120 | std::unique_ptr<int16_t[]> temp_1(new int16_t[temp_length]); |
| 121 | parameters.expand_vector1.CopyTo(temp_length, expansion_vector_position, |
| 122 | temp_1.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 123 | // Mix 3/4 of expand_vector0 with 1/4 of expand_vector1. |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 124 | WebRtcSpl_ScaleAndAddVectorsWithRound(temp_0.get(), 3, temp_1.get(), 1, 2, |
| 125 | voiced_vector_storage, temp_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 126 | } else if (current_lag_index_ == 2) { |
| 127 | // Mix 1/2 of expand_vector0 with 1/2 of expand_vector1. |
| 128 | assert(expansion_vector_position + temp_length <= |
| 129 | parameters.expand_vector0.Size()); |
| 130 | assert(expansion_vector_position + temp_length <= |
| 131 | parameters.expand_vector1.Size()); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 132 | |
| 133 | std::unique_ptr<int16_t[]> temp_0(new int16_t[temp_length]); |
| 134 | parameters.expand_vector0.CopyTo(temp_length, expansion_vector_position, |
| 135 | temp_0.get()); |
| 136 | std::unique_ptr<int16_t[]> temp_1(new int16_t[temp_length]); |
| 137 | parameters.expand_vector1.CopyTo(temp_length, expansion_vector_position, |
| 138 | temp_1.get()); |
| 139 | WebRtcSpl_ScaleAndAddVectorsWithRound(temp_0.get(), 1, temp_1.get(), 1, 1, |
| 140 | voiced_vector_storage, temp_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Get tapering window parameters. Values are in Q15. |
| 144 | int16_t muting_window, muting_window_increment; |
| 145 | int16_t unmuting_window, unmuting_window_increment; |
| 146 | if (fs_hz_ == 8000) { |
| 147 | muting_window = DspHelper::kMuteFactorStart8kHz; |
| 148 | muting_window_increment = DspHelper::kMuteFactorIncrement8kHz; |
| 149 | unmuting_window = DspHelper::kUnmuteFactorStart8kHz; |
| 150 | unmuting_window_increment = DspHelper::kUnmuteFactorIncrement8kHz; |
| 151 | } else if (fs_hz_ == 16000) { |
| 152 | muting_window = DspHelper::kMuteFactorStart16kHz; |
| 153 | muting_window_increment = DspHelper::kMuteFactorIncrement16kHz; |
| 154 | unmuting_window = DspHelper::kUnmuteFactorStart16kHz; |
| 155 | unmuting_window_increment = DspHelper::kUnmuteFactorIncrement16kHz; |
| 156 | } else if (fs_hz_ == 32000) { |
| 157 | muting_window = DspHelper::kMuteFactorStart32kHz; |
| 158 | muting_window_increment = DspHelper::kMuteFactorIncrement32kHz; |
| 159 | unmuting_window = DspHelper::kUnmuteFactorStart32kHz; |
| 160 | unmuting_window_increment = DspHelper::kUnmuteFactorIncrement32kHz; |
| 161 | } else { // fs_ == 48000 |
| 162 | muting_window = DspHelper::kMuteFactorStart48kHz; |
| 163 | muting_window_increment = DspHelper::kMuteFactorIncrement48kHz; |
| 164 | unmuting_window = DspHelper::kUnmuteFactorStart48kHz; |
| 165 | unmuting_window_increment = DspHelper::kUnmuteFactorIncrement48kHz; |
| 166 | } |
| 167 | |
| 168 | // Smooth the expanded if it has not been muted to a low amplitude and |
| 169 | // |current_voice_mix_factor| is larger than 0.5. |
| 170 | if ((parameters.mute_factor > 819) && |
| 171 | (parameters.current_voice_mix_factor > 8192)) { |
| 172 | size_t start_ix = sync_buffer_->Size() - overlap_length_; |
| 173 | for (size_t i = 0; i < overlap_length_; i++) { |
| 174 | // Do overlap add between new vector and overlap. |
| 175 | (*sync_buffer_)[channel_ix][start_ix + i] = |
| 176 | (((*sync_buffer_)[channel_ix][start_ix + i] * muting_window) + |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 177 | (((parameters.mute_factor * voiced_vector_storage[i]) >> 14) * |
| 178 | unmuting_window) + |
| 179 | 16384) >> |
| 180 | 15; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 181 | muting_window += muting_window_increment; |
| 182 | unmuting_window += unmuting_window_increment; |
| 183 | } |
| 184 | } else if (parameters.mute_factor == 0) { |
| 185 | // The expanded signal will consist of only comfort noise if |
| 186 | // mute_factor = 0. Set the output length to 15 ms for best noise |
| 187 | // production. |
| 188 | // TODO(hlundin): This has been disabled since the length of |
| 189 | // parameters.expand_vector0 and parameters.expand_vector1 no longer |
| 190 | // match with expand_lags_, causing invalid reads and writes. Is it a good |
| 191 | // idea to enable this again, and solve the vector size problem? |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 192 | // max_lag_ = fs_mult * 120; |
| 193 | // expand_lags_[0] = fs_mult * 120; |
| 194 | // expand_lags_[1] = fs_mult * 120; |
| 195 | // expand_lags_[2] = fs_mult * 120; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Unvoiced part. |
| 199 | // Filter |scaled_random_vector| through |ar_filter_|. |
| 200 | memcpy(unvoiced_vector - kUnvoicedLpcOrder, parameters.ar_filter_state, |
| 201 | sizeof(int16_t) * kUnvoicedLpcOrder); |
| 202 | int32_t add_constant = 0; |
| 203 | if (parameters.ar_gain_scale > 0) { |
| 204 | add_constant = 1 << (parameters.ar_gain_scale - 1); |
| 205 | } |
| 206 | WebRtcSpl_AffineTransformVector(scaled_random_vector, random_vector, |
| 207 | parameters.ar_gain, add_constant, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 208 | parameters.ar_gain_scale, current_lag); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 209 | WebRtcSpl_FilterARFastQ12(scaled_random_vector, unvoiced_vector, |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 210 | parameters.ar_filter, kUnvoicedLpcOrder + 1, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 211 | current_lag); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 212 | memcpy(parameters.ar_filter_state, |
| 213 | &(unvoiced_vector[current_lag - kUnvoicedLpcOrder]), |
| 214 | sizeof(int16_t) * kUnvoicedLpcOrder); |
| 215 | |
| 216 | // Combine voiced and unvoiced contributions. |
| 217 | |
| 218 | // Set a suitable cross-fading slope. |
| 219 | // For lag = |
| 220 | // <= 31 * fs_mult => go from 1 to 0 in about 8 ms; |
| 221 | // (>= 31 .. <= 63) * fs_mult => go from 1 to 0 in about 16 ms; |
| 222 | // >= 64 * fs_mult => go from 1 to 0 in about 32 ms. |
| 223 | // temp_shift = getbits(max_lag_) - 5. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 224 | int temp_shift = |
kwiberg | d3edd77 | 2017-03-01 18:52:48 -0800 | [diff] [blame] | 225 | (31 - WebRtcSpl_NormW32(rtc::dchecked_cast<int32_t>(max_lag_))) - 5; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 226 | int16_t mix_factor_increment = 256 >> temp_shift; |
| 227 | if (stop_muting_) { |
| 228 | mix_factor_increment = 0; |
| 229 | } |
| 230 | |
| 231 | // Create combined signal by shifting in more and more of unvoiced part. |
| 232 | temp_shift = 8 - temp_shift; // = getbits(mix_factor_increment). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 233 | size_t temp_length = |
| 234 | (parameters.current_voice_mix_factor - parameters.voice_mix_factor) >> |
| 235 | temp_shift; |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 236 | temp_length = std::min(temp_length, current_lag); |
| 237 | DspHelper::CrossFade(voiced_vector, unvoiced_vector, temp_length, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | ¶meters.current_voice_mix_factor, |
| 239 | mix_factor_increment, temp_data); |
| 240 | |
| 241 | // End of cross-fading period was reached before end of expanded signal |
| 242 | // path. Mix the rest with a fixed mixing factor. |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 243 | if (temp_length < current_lag) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 244 | if (mix_factor_increment != 0) { |
| 245 | parameters.current_voice_mix_factor = parameters.voice_mix_factor; |
| 246 | } |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 247 | int16_t temp_scale = 16384 - parameters.current_voice_mix_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 248 | WebRtcSpl_ScaleAndAddVectorsWithRound( |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 249 | voiced_vector + temp_length, parameters.current_voice_mix_factor, |
| 250 | unvoiced_vector + temp_length, temp_scale, 14, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 251 | temp_data + temp_length, current_lag - temp_length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Select muting slope depending on how many consecutive expands we have |
| 255 | // done. |
| 256 | if (consecutive_expands_ == 3) { |
| 257 | // Let the mute factor decrease from 1.0 to 0.95 in 6.25 ms. |
| 258 | // mute_slope = 0.0010 / fs_mult in Q20. |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 259 | parameters.mute_slope = std::max(parameters.mute_slope, 1049 / fs_mult); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 260 | } |
| 261 | if (consecutive_expands_ == 7) { |
| 262 | // Let the mute factor decrease from 1.0 to 0.90 in 6.25 ms. |
| 263 | // mute_slope = 0.0020 / fs_mult in Q20. |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 264 | parameters.mute_slope = std::max(parameters.mute_slope, 2097 / fs_mult); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // Mute segment according to slope value. |
| 268 | if ((consecutive_expands_ != 0) || !parameters.onset) { |
| 269 | // Mute to the previous level, then continue with the muting. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 270 | WebRtcSpl_AffineTransformVector( |
| 271 | temp_data, temp_data, parameters.mute_factor, 8192, 14, current_lag); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 272 | |
| 273 | if (!stop_muting_) { |
| 274 | DspHelper::MuteSignal(temp_data, parameters.mute_slope, current_lag); |
| 275 | |
| 276 | // Shift by 6 to go from Q20 to Q14. |
| 277 | // TODO(hlundin): Adding 8192 before shifting 6 steps seems wrong. |
| 278 | // Legacy. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 279 | int16_t gain = static_cast<int16_t>( |
| 280 | 16384 - (((current_lag * parameters.mute_slope) + 8192) >> 6)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 281 | gain = ((gain * parameters.mute_factor) + 8192) >> 14; |
| 282 | |
| 283 | // Guard against getting stuck with very small (but sometimes audible) |
| 284 | // gain. |
| 285 | if ((consecutive_expands_ > 3) && (gain >= parameters.mute_factor)) { |
| 286 | parameters.mute_factor = 0; |
| 287 | } else { |
| 288 | parameters.mute_factor = gain; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // Background noise part. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 294 | GenerateBackgroundNoise( |
| 295 | random_vector, channel_ix, channel_parameters_[channel_ix].mute_slope, |
| 296 | TooManyExpands(), current_lag, unvoiced_array_memory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 297 | |
| 298 | // Add background noise to the combined voiced-unvoiced signal. |
| 299 | for (size_t i = 0; i < current_lag; i++) { |
| 300 | temp_data[i] = temp_data[i] + noise_vector[i]; |
| 301 | } |
| 302 | if (channel_ix == 0) { |
| 303 | output->AssertSize(current_lag); |
| 304 | } else { |
| 305 | assert(output->Size() == current_lag); |
| 306 | } |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 307 | (*output)[channel_ix].OverwriteAt(temp_data, current_lag, 0); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Increase call number and cap it. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 311 | consecutive_expands_ = consecutive_expands_ >= kMaxConsecutiveExpands |
| 312 | ? kMaxConsecutiveExpands |
| 313 | : consecutive_expands_ + 1; |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 314 | expand_duration_samples_ += output->Size(); |
| 315 | // Clamp the duration counter at 2 seconds. |
kwiberg | d3edd77 | 2017-03-01 18:52:48 -0800 | [diff] [blame] | 316 | expand_duration_samples_ = std::min(expand_duration_samples_, |
| 317 | rtc::dchecked_cast<size_t>(fs_hz_ * 2)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | void Expand::SetParametersForNormalAfterExpand() { |
| 322 | current_lag_index_ = 0; |
| 323 | lag_index_direction_ = 0; |
| 324 | stop_muting_ = true; // Do not mute signal any more. |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 325 | statistics_->LogDelayedPacketOutageEvent( |
kwiberg | d3edd77 | 2017-03-01 18:52:48 -0800 | [diff] [blame] | 326 | rtc::dchecked_cast<int>(expand_duration_samples_) / (fs_hz_ / 1000)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void Expand::SetParametersForMergeAfterExpand() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 330 | current_lag_index_ = -1; /* out of the 3 possible ones */ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 331 | lag_index_direction_ = 1; /* make sure we get the "optimal" lag */ |
| 332 | stop_muting_ = true; |
| 333 | } |
| 334 | |
henrik.lundin | f3995f7 | 2016-05-10 05:54:35 -0700 | [diff] [blame] | 335 | bool Expand::Muted() const { |
| 336 | if (first_expand_ || stop_muting_) |
| 337 | return false; |
| 338 | RTC_DCHECK(channel_parameters_); |
| 339 | for (size_t ch = 0; ch < num_channels_; ++ch) { |
| 340 | if (channel_parameters_[ch].mute_factor != 0) |
| 341 | return false; |
| 342 | } |
| 343 | return true; |
| 344 | } |
| 345 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 346 | size_t Expand::overlap_length() const { |
| 347 | return overlap_length_; |
| 348 | } |
| 349 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 350 | void Expand::InitializeForAnExpandPeriod() { |
| 351 | lag_index_direction_ = 1; |
| 352 | current_lag_index_ = -1; |
| 353 | stop_muting_ = false; |
| 354 | random_vector_->set_seed_increment(1); |
| 355 | consecutive_expands_ = 0; |
| 356 | for (size_t ix = 0; ix < num_channels_; ++ix) { |
| 357 | channel_parameters_[ix].current_voice_mix_factor = 16384; // 1.0 in Q14. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 358 | channel_parameters_[ix].mute_factor = 16384; // 1.0 in Q14. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 359 | // Start with 0 gain for background noise. |
| 360 | background_noise_->SetMuteFactor(ix, 0); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | bool Expand::TooManyExpands() { |
| 365 | return consecutive_expands_ >= kMaxConsecutiveExpands; |
| 366 | } |
| 367 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 368 | void Expand::AnalyzeSignal(int16_t* random_vector) { |
| 369 | int32_t auto_correlation[kUnvoicedLpcOrder + 1]; |
| 370 | int16_t reflection_coeff[kUnvoicedLpcOrder]; |
| 371 | int16_t correlation_vector[kMaxSampleRate / 8000 * 102]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 372 | size_t best_correlation_index[kNumCorrelationCandidates]; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 373 | int16_t best_correlation[kNumCorrelationCandidates]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 374 | size_t best_distortion_index[kNumCorrelationCandidates]; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 375 | int16_t best_distortion[kNumCorrelationCandidates]; |
| 376 | int32_t correlation_vector2[(99 * kMaxSampleRate / 8000) + 1]; |
| 377 | int32_t best_distortion_w32[kNumCorrelationCandidates]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 378 | static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 379 | int16_t unvoiced_array_memory[kNoiseLpcOrder + kMaxSampleRate / 8000 * 125]; |
| 380 | int16_t* unvoiced_vector = unvoiced_array_memory + kUnvoicedLpcOrder; |
| 381 | |
| 382 | int fs_mult = fs_hz_ / 8000; |
| 383 | |
| 384 | // Pre-calculate common multiplications with fs_mult. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 385 | size_t fs_mult_4 = static_cast<size_t>(fs_mult * 4); |
| 386 | size_t fs_mult_20 = static_cast<size_t>(fs_mult * 20); |
| 387 | size_t fs_mult_120 = static_cast<size_t>(fs_mult * 120); |
| 388 | size_t fs_mult_dist_len = fs_mult * kDistortionLength; |
| 389 | size_t fs_mult_lpc_analysis_len = fs_mult * kLpcAnalysisLength; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 390 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 391 | const size_t signal_length = static_cast<size_t>(256 * fs_mult); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 392 | |
| 393 | const size_t audio_history_position = sync_buffer_->Size() - signal_length; |
| 394 | std::unique_ptr<int16_t[]> audio_history(new int16_t[signal_length]); |
| 395 | (*sync_buffer_)[0].CopyTo(signal_length, audio_history_position, |
| 396 | audio_history.get()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 397 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 398 | // Initialize. |
| 399 | InitializeForAnExpandPeriod(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 400 | |
| 401 | // Calculate correlation in downsampled domain (4 kHz sample rate). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 402 | size_t correlation_length = 51; // TODO(hlundin): Legacy bit-exactness. |
turaj@webrtc.org | 58cd316 | 2013-10-31 15:15:55 +0000 | [diff] [blame] | 403 | // If it is decided to break bit-exactness |correlation_length| should be |
| 404 | // initialized to the return value of Correlation(). |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 405 | Correlation(audio_history.get(), signal_length, correlation_vector); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 406 | |
| 407 | // Find peaks in correlation vector. |
| 408 | DspHelper::PeakDetection(correlation_vector, correlation_length, |
| 409 | kNumCorrelationCandidates, fs_mult, |
| 410 | best_correlation_index, best_correlation); |
| 411 | |
| 412 | // Adjust peak locations; cross-correlation lags start at 2.5 ms |
| 413 | // (20 * fs_mult samples). |
| 414 | best_correlation_index[0] += fs_mult_20; |
| 415 | best_correlation_index[1] += fs_mult_20; |
| 416 | best_correlation_index[2] += fs_mult_20; |
| 417 | |
| 418 | // Calculate distortion around the |kNumCorrelationCandidates| best lags. |
| 419 | int distortion_scale = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 420 | for (size_t i = 0; i < kNumCorrelationCandidates; i++) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 421 | size_t min_index = |
| 422 | std::max(fs_mult_20, best_correlation_index[i] - fs_mult_4); |
| 423 | size_t max_index = |
| 424 | std::min(fs_mult_120 - 1, best_correlation_index[i] + fs_mult_4); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 425 | best_distortion_index[i] = DspHelper::MinDistortion( |
| 426 | &(audio_history[signal_length - fs_mult_dist_len]), min_index, |
| 427 | max_index, fs_mult_dist_len, &best_distortion_w32[i]); |
| 428 | distortion_scale = std::max(16 - WebRtcSpl_NormW32(best_distortion_w32[i]), |
| 429 | distortion_scale); |
| 430 | } |
| 431 | // Shift the distortion values to fit in 16 bits. |
| 432 | WebRtcSpl_VectorBitShiftW32ToW16(best_distortion, kNumCorrelationCandidates, |
| 433 | best_distortion_w32, distortion_scale); |
| 434 | |
| 435 | // Find the maximizing index |i| of the cost function |
| 436 | // f[i] = best_correlation[i] / best_distortion[i]. |
turaj@webrtc.org | 58cd316 | 2013-10-31 15:15:55 +0000 | [diff] [blame] | 437 | int32_t best_ratio = std::numeric_limits<int32_t>::min(); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 438 | size_t best_index = std::numeric_limits<size_t>::max(); |
| 439 | for (size_t i = 0; i < kNumCorrelationCandidates; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 440 | int32_t ratio; |
| 441 | if (best_distortion[i] > 0) { |
ivoc | 4843dd1 | 2017-01-09 08:31:42 -0800 | [diff] [blame] | 442 | ratio = (best_correlation[i] * (1 << 16)) / best_distortion[i]; |
turaj@webrtc.org | 7126b38 | 2013-07-31 16:05:09 +0000 | [diff] [blame] | 443 | } else if (best_correlation[i] == 0) { |
| 444 | ratio = 0; // No correlation set result to zero. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 445 | } else { |
turaj@webrtc.org | 7126b38 | 2013-07-31 16:05:09 +0000 | [diff] [blame] | 446 | ratio = std::numeric_limits<int32_t>::max(); // Denominator is zero. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 447 | } |
| 448 | if (ratio > best_ratio) { |
| 449 | best_index = i; |
| 450 | best_ratio = ratio; |
| 451 | } |
| 452 | } |
| 453 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 454 | size_t distortion_lag = best_distortion_index[best_index]; |
| 455 | size_t correlation_lag = best_correlation_index[best_index]; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 456 | max_lag_ = std::max(distortion_lag, correlation_lag); |
| 457 | |
| 458 | // Calculate the exact best correlation in the range between |
| 459 | // |correlation_lag| and |distortion_lag|. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 460 | correlation_length = std::max(std::min(distortion_lag + 10, fs_mult_120), |
| 461 | static_cast<size_t>(60 * fs_mult)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 462 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 463 | size_t start_index = std::min(distortion_lag, correlation_lag); |
| 464 | size_t correlation_lags = static_cast<size_t>( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 465 | WEBRTC_SPL_ABS_W16((distortion_lag - correlation_lag)) + 1); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 466 | assert(correlation_lags <= static_cast<size_t>(99 * fs_mult + 1)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 467 | |
| 468 | for (size_t channel_ix = 0; channel_ix < num_channels_; ++channel_ix) { |
| 469 | ChannelParameters& parameters = channel_parameters_[channel_ix]; |
minyue | 8c22962 | 2016-04-28 02:16:48 -0700 | [diff] [blame] | 470 | // Calculate suitable scaling. |
| 471 | int16_t signal_max = WebRtcSpl_MaxAbsValueW16( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 472 | &audio_history[signal_length - correlation_length - start_index - |
| 473 | correlation_lags], |
| 474 | correlation_length + start_index + correlation_lags - 1); |
| 475 | int correlation_scale = |
| 476 | (31 - WebRtcSpl_NormW32(signal_max * signal_max)) + |
minyue | 8c22962 | 2016-04-28 02:16:48 -0700 | [diff] [blame] | 477 | (31 - WebRtcSpl_NormW32(static_cast<int32_t>(correlation_length))) - 31; |
| 478 | correlation_scale = std::max(0, correlation_scale); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 479 | |
| 480 | // Calculate the correlation, store in |correlation_vector2|. |
minyue | 8c22962 | 2016-04-28 02:16:48 -0700 | [diff] [blame] | 481 | WebRtcSpl_CrossCorrelation( |
| 482 | correlation_vector2, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 483 | &(audio_history[signal_length - correlation_length]), |
| 484 | &(audio_history[signal_length - correlation_length - start_index]), |
minyue | 8c22962 | 2016-04-28 02:16:48 -0700 | [diff] [blame] | 485 | correlation_length, correlation_lags, correlation_scale, -1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 486 | |
| 487 | // Find maximizing index. |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 488 | best_index = WebRtcSpl_MaxIndexW32(correlation_vector2, correlation_lags); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 489 | int32_t max_correlation = correlation_vector2[best_index]; |
| 490 | // Compensate index with start offset. |
| 491 | best_index = best_index + start_index; |
| 492 | |
| 493 | // Calculate energies. |
| 494 | int32_t energy1 = WebRtcSpl_DotProductWithScale( |
| 495 | &(audio_history[signal_length - correlation_length]), |
| 496 | &(audio_history[signal_length - correlation_length]), |
| 497 | correlation_length, correlation_scale); |
| 498 | int32_t energy2 = WebRtcSpl_DotProductWithScale( |
| 499 | &(audio_history[signal_length - correlation_length - best_index]), |
| 500 | &(audio_history[signal_length - correlation_length - best_index]), |
| 501 | correlation_length, correlation_scale); |
| 502 | |
| 503 | // Calculate the correlation coefficient between the two portions of the |
| 504 | // signal. |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 505 | int32_t corr_coefficient; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 506 | if ((energy1 > 0) && (energy2 > 0)) { |
| 507 | int energy1_scale = std::max(16 - WebRtcSpl_NormW32(energy1), 0); |
| 508 | int energy2_scale = std::max(16 - WebRtcSpl_NormW32(energy2), 0); |
| 509 | // Make sure total scaling is even (to simplify scale factor after sqrt). |
| 510 | if ((energy1_scale + energy2_scale) & 1) { |
| 511 | // If sum is odd, add 1 to make it even. |
| 512 | energy1_scale += 1; |
| 513 | } |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 514 | int32_t scaled_energy1 = energy1 >> energy1_scale; |
| 515 | int32_t scaled_energy2 = energy2 >> energy2_scale; |
| 516 | int16_t sqrt_energy_product = static_cast<int16_t>( |
| 517 | WebRtcSpl_SqrtFloor(scaled_energy1 * scaled_energy2)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 518 | // Calculate max_correlation / sqrt(energy1 * energy2) in Q14. |
| 519 | int cc_shift = 14 - (energy1_scale + energy2_scale) / 2; |
| 520 | max_correlation = WEBRTC_SPL_SHIFT_W32(max_correlation, cc_shift); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 521 | corr_coefficient = |
| 522 | WebRtcSpl_DivW32W16(max_correlation, sqrt_energy_product); |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 523 | // Cap at 1.0 in Q14. |
| 524 | corr_coefficient = std::min(16384, corr_coefficient); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 525 | } else { |
| 526 | corr_coefficient = 0; |
| 527 | } |
| 528 | |
| 529 | // Extract the two vectors expand_vector0 and expand_vector1 from |
| 530 | // |audio_history|. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 531 | size_t expansion_length = max_lag_ + overlap_length_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 532 | const int16_t* vector1 = &(audio_history[signal_length - expansion_length]); |
| 533 | const int16_t* vector2 = vector1 - distortion_lag; |
| 534 | // Normalize the second vector to the same energy as the first. |
| 535 | energy1 = WebRtcSpl_DotProductWithScale(vector1, vector1, expansion_length, |
| 536 | correlation_scale); |
| 537 | energy2 = WebRtcSpl_DotProductWithScale(vector2, vector2, expansion_length, |
| 538 | correlation_scale); |
| 539 | // Confirm that amplitude ratio sqrt(energy1 / energy2) is within 0.5 - 2.0, |
Henrik Lundin | e84e96e | 2016-01-12 16:36:13 +0100 | [diff] [blame] | 540 | // i.e., energy1 / energy2 is within 0.25 - 4. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 541 | int16_t amplitude_ratio; |
| 542 | if ((energy1 / 4 < energy2) && (energy1 > energy2 / 4)) { |
| 543 | // Energy constraint fulfilled. Use both vectors and scale them |
| 544 | // accordingly. |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 545 | int32_t scaled_energy2 = std::max(16 - WebRtcSpl_NormW32(energy2), 0); |
| 546 | int32_t scaled_energy1 = scaled_energy2 - 13; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 547 | // Calculate scaled_energy1 / scaled_energy2 in Q13. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 548 | int32_t energy_ratio = |
| 549 | WebRtcSpl_DivW32W16(WEBRTC_SPL_SHIFT_W32(energy1, -scaled_energy1), |
| 550 | static_cast<int16_t>(energy2 >> scaled_energy2)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 551 | // Calculate sqrt ratio in Q13 (sqrt of en1/en2 in Q26). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 552 | amplitude_ratio = |
| 553 | static_cast<int16_t>(WebRtcSpl_SqrtFloor(energy_ratio << 13)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 554 | // Copy the two vectors and give them the same energy. |
| 555 | parameters.expand_vector0.Clear(); |
| 556 | parameters.expand_vector0.PushBack(vector1, expansion_length); |
| 557 | parameters.expand_vector1.Clear(); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 558 | if (parameters.expand_vector1.Size() < expansion_length) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 559 | parameters.expand_vector1.Extend(expansion_length - |
| 560 | parameters.expand_vector1.Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 561 | } |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 562 | std::unique_ptr<int16_t[]> temp_1(new int16_t[expansion_length]); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 563 | WebRtcSpl_AffineTransformVector( |
| 564 | temp_1.get(), const_cast<int16_t*>(vector2), amplitude_ratio, 4096, |
| 565 | 13, expansion_length); |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 566 | parameters.expand_vector1.OverwriteAt(temp_1.get(), expansion_length, 0); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 567 | } else { |
| 568 | // Energy change constraint not fulfilled. Only use last vector. |
| 569 | parameters.expand_vector0.Clear(); |
| 570 | parameters.expand_vector0.PushBack(vector1, expansion_length); |
| 571 | // Copy from expand_vector0 to expand_vector1. |
henrik.lundin@webrtc.org | f6ab6f8 | 2014-09-04 10:58:43 +0000 | [diff] [blame] | 572 | parameters.expand_vector0.CopyTo(¶meters.expand_vector1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 573 | // Set the energy_ratio since it is used by muting slope. |
| 574 | if ((energy1 / 4 < energy2) || (energy2 == 0)) { |
| 575 | amplitude_ratio = 4096; // 0.5 in Q13. |
| 576 | } else { |
| 577 | amplitude_ratio = 16384; // 2.0 in Q13. |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | // Set the 3 lag values. |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 582 | if (distortion_lag == correlation_lag) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 583 | expand_lags_[0] = distortion_lag; |
| 584 | expand_lags_[1] = distortion_lag; |
| 585 | expand_lags_[2] = distortion_lag; |
| 586 | } else { |
| 587 | // |distortion_lag| and |correlation_lag| are not equal; use different |
| 588 | // combinations of the two. |
| 589 | // First lag is |distortion_lag| only. |
| 590 | expand_lags_[0] = distortion_lag; |
| 591 | // Second lag is the average of the two. |
| 592 | expand_lags_[1] = (distortion_lag + correlation_lag) / 2; |
| 593 | // Third lag is the average again, but rounding towards |correlation_lag|. |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 594 | if (distortion_lag > correlation_lag) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 595 | expand_lags_[2] = (distortion_lag + correlation_lag - 1) / 2; |
| 596 | } else { |
| 597 | expand_lags_[2] = (distortion_lag + correlation_lag + 1) / 2; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // Calculate the LPC and the gain of the filters. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 602 | |
| 603 | // Calculate kUnvoicedLpcOrder + 1 lags of the auto-correlation function. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 604 | size_t temp_index = |
| 605 | signal_length - fs_mult_lpc_analysis_len - kUnvoicedLpcOrder; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 606 | // Copy signal to temporary vector to be able to pad with leading zeros. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 607 | int16_t* temp_signal = |
| 608 | new int16_t[fs_mult_lpc_analysis_len + kUnvoicedLpcOrder]; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 609 | memset(temp_signal, 0, |
| 610 | sizeof(int16_t) * (fs_mult_lpc_analysis_len + kUnvoicedLpcOrder)); |
| 611 | memcpy(&temp_signal[kUnvoicedLpcOrder], |
| 612 | &audio_history[temp_index + kUnvoicedLpcOrder], |
| 613 | sizeof(int16_t) * fs_mult_lpc_analysis_len); |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 614 | CrossCorrelationWithAutoShift( |
| 615 | &temp_signal[kUnvoicedLpcOrder], &temp_signal[kUnvoicedLpcOrder], |
| 616 | fs_mult_lpc_analysis_len, kUnvoicedLpcOrder + 1, -1, auto_correlation); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 617 | delete[] temp_signal; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 618 | |
| 619 | // Verify that variance is positive. |
| 620 | if (auto_correlation[0] > 0) { |
| 621 | // Estimate AR filter parameters using Levinson-Durbin algorithm; |
| 622 | // kUnvoicedLpcOrder + 1 filter coefficients. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 623 | int16_t stability = |
| 624 | WebRtcSpl_LevinsonDurbin(auto_correlation, parameters.ar_filter, |
| 625 | reflection_coeff, kUnvoicedLpcOrder); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 626 | |
| 627 | // Keep filter parameters only if filter is stable. |
| 628 | if (stability != 1) { |
| 629 | // Set first coefficient to 4096 (1.0 in Q12). |
| 630 | parameters.ar_filter[0] = 4096; |
| 631 | // Set remaining |kUnvoicedLpcOrder| coefficients to zero. |
| 632 | WebRtcSpl_MemSetW16(parameters.ar_filter + 1, 0, kUnvoicedLpcOrder); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if (channel_ix == 0) { |
| 637 | // Extract a noise segment. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 638 | size_t noise_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 639 | if (distortion_lag < 40) { |
| 640 | noise_length = 2 * distortion_lag + 30; |
| 641 | } else { |
| 642 | noise_length = distortion_lag + 30; |
| 643 | } |
| 644 | if (noise_length <= RandomVector::kRandomTableSize) { |
| 645 | memcpy(random_vector, RandomVector::kRandomTable, |
| 646 | sizeof(int16_t) * noise_length); |
| 647 | } else { |
| 648 | // Only applies to SWB where length could be larger than |
| 649 | // |kRandomTableSize|. |
| 650 | memcpy(random_vector, RandomVector::kRandomTable, |
| 651 | sizeof(int16_t) * RandomVector::kRandomTableSize); |
| 652 | assert(noise_length <= kMaxSampleRate / 8000 * 120 + 30); |
| 653 | random_vector_->IncreaseSeedIncrement(2); |
| 654 | random_vector_->Generate( |
| 655 | noise_length - RandomVector::kRandomTableSize, |
| 656 | &random_vector[RandomVector::kRandomTableSize]); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // Set up state vector and calculate scale factor for unvoiced filtering. |
| 661 | memcpy(parameters.ar_filter_state, |
| 662 | &(audio_history[signal_length - kUnvoicedLpcOrder]), |
| 663 | sizeof(int16_t) * kUnvoicedLpcOrder); |
| 664 | memcpy(unvoiced_vector - kUnvoicedLpcOrder, |
| 665 | &(audio_history[signal_length - 128 - kUnvoicedLpcOrder]), |
| 666 | sizeof(int16_t) * kUnvoicedLpcOrder); |
bjornv@webrtc.org | c14e357 | 2015-01-12 05:50:52 +0000 | [diff] [blame] | 667 | WebRtcSpl_FilterMAFastQ12(&audio_history[signal_length - 128], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 668 | unvoiced_vector, parameters.ar_filter, |
| 669 | kUnvoicedLpcOrder + 1, 128); |
ivoc | ffecbbf | 2016-12-16 05:51:49 -0800 | [diff] [blame] | 670 | const int unvoiced_max_abs = [&] { |
| 671 | const int16_t max_abs = WebRtcSpl_MaxAbsValueW16(unvoiced_vector, 128); |
| 672 | // Since WebRtcSpl_MaxAbsValueW16 returns 2^15 - 1 when the input contains |
| 673 | // -2^15, we have to conservatively bump the return value by 1 |
| 674 | // if it is 2^15 - 1. |
| 675 | return max_abs == WEBRTC_SPL_WORD16_MAX ? max_abs + 1 : max_abs; |
| 676 | }(); |
| 677 | // Pick the smallest n such that 2^n > unvoiced_max_abs; then the maximum |
| 678 | // value of the dot product is less than 2^7 * 2^(2*n) = 2^(2*n + 7), so to |
| 679 | // prevent overflows we want 2n + 7 <= 31, which means we should shift by |
| 680 | // 2n + 7 - 31 bits, if this value is greater than zero. |
| 681 | int unvoiced_prescale = |
| 682 | std::max(0, 2 * WebRtcSpl_GetSizeInBits(unvoiced_max_abs) - 24); |
| 683 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 684 | int32_t unvoiced_energy = WebRtcSpl_DotProductWithScale( |
| 685 | unvoiced_vector, unvoiced_vector, 128, unvoiced_prescale); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 686 | |
| 687 | // Normalize |unvoiced_energy| to 28 or 29 bits to preserve sqrt() accuracy. |
| 688 | int16_t unvoiced_scale = WebRtcSpl_NormW32(unvoiced_energy) - 3; |
| 689 | // Make sure we do an odd number of shifts since we already have 7 shifts |
| 690 | // from dividing with 128 earlier. This will make the total scale factor |
| 691 | // even, which is suitable for the sqrt. |
| 692 | unvoiced_scale += ((unvoiced_scale & 0x1) ^ 0x1); |
| 693 | unvoiced_energy = WEBRTC_SPL_SHIFT_W32(unvoiced_energy, unvoiced_scale); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 694 | int16_t unvoiced_gain = |
| 695 | static_cast<int16_t>(WebRtcSpl_SqrtFloor(unvoiced_energy)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 696 | parameters.ar_gain_scale = |
| 697 | 13 + (unvoiced_scale + 7 - unvoiced_prescale) / 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 698 | parameters.ar_gain = unvoiced_gain; |
| 699 | |
| 700 | // Calculate voice_mix_factor from corr_coefficient. |
| 701 | // Let x = corr_coefficient. Then, we compute: |
| 702 | // if (x > 0.48) |
| 703 | // voice_mix_factor = (-5179 + 19931x - 16422x^2 + 5776x^3) / 4096; |
| 704 | // else |
| 705 | // voice_mix_factor = 0; |
| 706 | if (corr_coefficient > 7875) { |
| 707 | int16_t x1, x2, x3; |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 708 | // |corr_coefficient| is in Q14. |
| 709 | x1 = static_cast<int16_t>(corr_coefficient); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 710 | x2 = (x1 * x1) >> 14; // Shift 14 to keep result in Q14. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 711 | x3 = (x1 * x2) >> 14; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 712 | static const int kCoefficients[4] = {-5179, 19931, -16422, 5776}; |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 713 | int32_t temp_sum = kCoefficients[0] * 16384; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 714 | temp_sum += kCoefficients[1] * x1; |
| 715 | temp_sum += kCoefficients[2] * x2; |
| 716 | temp_sum += kCoefficients[3] * x3; |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 717 | parameters.voice_mix_factor = |
| 718 | static_cast<int16_t>(std::min(temp_sum / 4096, 16384)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 719 | parameters.voice_mix_factor = |
| 720 | std::max(parameters.voice_mix_factor, static_cast<int16_t>(0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 721 | } else { |
| 722 | parameters.voice_mix_factor = 0; |
| 723 | } |
| 724 | |
| 725 | // Calculate muting slope. Reuse value from earlier scaling of |
| 726 | // |expand_vector0| and |expand_vector1|. |
| 727 | int16_t slope = amplitude_ratio; |
| 728 | if (slope > 12288) { |
| 729 | // slope > 1.5. |
| 730 | // Calculate (1 - (1 / slope)) / distortion_lag = |
| 731 | // (slope - 1) / (distortion_lag * slope). |
| 732 | // |slope| is in Q13, so 1 corresponds to 8192. Shift up to Q25 before |
| 733 | // the division. |
| 734 | // Shift the denominator from Q13 to Q5 before the division. The result of |
| 735 | // the division will then be in Q20. |
Henrik Lundin | 9024da8 | 2018-05-21 13:41:16 +0200 | [diff] [blame] | 736 | int16_t denom = |
| 737 | rtc::saturated_cast<int16_t>((distortion_lag * slope) >> 8); |
| 738 | int temp_ratio = WebRtcSpl_DivW32W16((slope - 8192) << 12, denom); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 739 | if (slope > 14746) { |
| 740 | // slope > 1.8. |
| 741 | // Divide by 2, with proper rounding. |
| 742 | parameters.mute_slope = (temp_ratio + 1) / 2; |
| 743 | } else { |
| 744 | // Divide by 8, with proper rounding. |
| 745 | parameters.mute_slope = (temp_ratio + 4) / 8; |
| 746 | } |
| 747 | parameters.onset = true; |
| 748 | } else { |
| 749 | // Calculate (1 - slope) / distortion_lag. |
| 750 | // Shift |slope| by 7 to Q20 before the division. The result is in Q20. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 751 | parameters.mute_slope = WebRtcSpl_DivW32W16( |
henrik.lundin | 79dfdad | 2016-11-15 01:45:53 -0800 | [diff] [blame] | 752 | (8192 - slope) * 128, static_cast<int16_t>(distortion_lag)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 753 | if (parameters.voice_mix_factor <= 13107) { |
| 754 | // Make sure the mute factor decreases from 1.0 to 0.9 in no more than |
| 755 | // 6.25 ms. |
| 756 | // mute_slope >= 0.005 / fs_mult in Q20. |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 757 | parameters.mute_slope = std::max(5243 / fs_mult, parameters.mute_slope); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 758 | } else if (slope > 8028) { |
| 759 | parameters.mute_slope = 0; |
| 760 | } |
| 761 | parameters.onset = false; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 766 | Expand::ChannelParameters::ChannelParameters() |
| 767 | : mute_factor(16384), |
| 768 | ar_gain(0), |
| 769 | ar_gain_scale(0), |
| 770 | voice_mix_factor(0), |
| 771 | current_voice_mix_factor(0), |
| 772 | onset(false), |
| 773 | mute_slope(0) { |
| 774 | memset(ar_filter, 0, sizeof(ar_filter)); |
| 775 | memset(ar_filter_state, 0, sizeof(ar_filter_state)); |
| 776 | } |
| 777 | |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 778 | void Expand::Correlation(const int16_t* input, |
| 779 | size_t input_length, |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 780 | int16_t* output) const { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 781 | // Set parameters depending on sample rate. |
| 782 | const int16_t* filter_coefficients; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 783 | size_t num_coefficients; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 784 | int16_t downsampling_factor; |
| 785 | if (fs_hz_ == 8000) { |
| 786 | num_coefficients = 3; |
| 787 | downsampling_factor = 2; |
| 788 | filter_coefficients = DspHelper::kDownsample8kHzTbl; |
| 789 | } else if (fs_hz_ == 16000) { |
| 790 | num_coefficients = 5; |
| 791 | downsampling_factor = 4; |
| 792 | filter_coefficients = DspHelper::kDownsample16kHzTbl; |
| 793 | } else if (fs_hz_ == 32000) { |
| 794 | num_coefficients = 7; |
| 795 | downsampling_factor = 8; |
| 796 | filter_coefficients = DspHelper::kDownsample32kHzTbl; |
| 797 | } else { // fs_hz_ == 48000. |
| 798 | num_coefficients = 7; |
| 799 | downsampling_factor = 12; |
| 800 | filter_coefficients = DspHelper::kDownsample48kHzTbl; |
| 801 | } |
| 802 | |
| 803 | // Correlate from lag 10 to lag 60 in downsampled domain. |
| 804 | // (Corresponds to 20-120 for narrow-band, 40-240 for wide-band, and so on.) |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 805 | static const size_t kCorrelationStartLag = 10; |
| 806 | static const size_t kNumCorrelationLags = 54; |
| 807 | static const size_t kCorrelationLength = 60; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 808 | // Downsample to 4 kHz sample rate. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 809 | static const size_t kDownsampledLength = |
| 810 | kCorrelationStartLag + kNumCorrelationLags + kCorrelationLength; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 811 | int16_t downsampled_input[kDownsampledLength]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 812 | static const size_t kFilterDelay = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 813 | WebRtcSpl_DownsampleFast( |
| 814 | input + input_length - kDownsampledLength * downsampling_factor, |
| 815 | kDownsampledLength * downsampling_factor, downsampled_input, |
| 816 | kDownsampledLength, filter_coefficients, num_coefficients, |
| 817 | downsampling_factor, kFilterDelay); |
| 818 | |
| 819 | // Normalize |downsampled_input| to using all 16 bits. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 820 | int16_t max_value = |
| 821 | WebRtcSpl_MaxAbsValueW16(downsampled_input, kDownsampledLength); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 822 | int16_t norm_shift = 16 - WebRtcSpl_NormW32(max_value); |
| 823 | WebRtcSpl_VectorBitShiftW16(downsampled_input, kDownsampledLength, |
| 824 | downsampled_input, norm_shift); |
| 825 | |
| 826 | int32_t correlation[kNumCorrelationLags]; |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 827 | CrossCorrelationWithAutoShift( |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 828 | &downsampled_input[kDownsampledLength - kCorrelationLength], |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 829 | &downsampled_input[kDownsampledLength - kCorrelationLength - |
| 830 | kCorrelationStartLag], |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 831 | kCorrelationLength, kNumCorrelationLags, -1, correlation); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 832 | |
| 833 | // Normalize and move data from 32-bit to 16-bit vector. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 834 | int32_t max_correlation = |
| 835 | WebRtcSpl_MaxAbsValueW32(correlation, kNumCorrelationLags); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 836 | int16_t norm_shift2 = static_cast<int16_t>( |
| 837 | std::max(18 - WebRtcSpl_NormW32(max_correlation), 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 838 | WebRtcSpl_VectorBitShiftW32ToW16(output, kNumCorrelationLags, correlation, |
| 839 | norm_shift2); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void Expand::UpdateLagIndex() { |
| 843 | current_lag_index_ = current_lag_index_ + lag_index_direction_; |
| 844 | // Change direction if needed. |
| 845 | if (current_lag_index_ <= 0) { |
| 846 | lag_index_direction_ = 1; |
| 847 | } |
| 848 | if (current_lag_index_ >= kNumLags - 1) { |
| 849 | lag_index_direction_ = -1; |
| 850 | } |
| 851 | } |
| 852 | |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 853 | Expand* ExpandFactory::Create(BackgroundNoise* background_noise, |
| 854 | SyncBuffer* sync_buffer, |
| 855 | RandomVector* random_vector, |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 856 | StatisticsCalculator* statistics, |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 857 | int fs, |
| 858 | size_t num_channels) const { |
Henrik Lundin | bef77e2 | 2015-08-18 14:58:09 +0200 | [diff] [blame] | 859 | return new Expand(background_noise, sync_buffer, random_vector, statistics, |
| 860 | fs, num_channels); |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 861 | } |
| 862 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 863 | // TODO(turajs): This can be moved to BackgroundNoise class. |
| 864 | void Expand::GenerateBackgroundNoise(int16_t* random_vector, |
| 865 | size_t channel, |
Peter Kasting | 36b7cc3 | 2015-06-11 19:57:18 -0700 | [diff] [blame] | 866 | int mute_slope, |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 867 | bool too_many_expands, |
| 868 | size_t num_noise_samples, |
| 869 | int16_t* buffer) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 870 | static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 871 | int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 872 | assert(num_noise_samples <= (kMaxSampleRate / 8000 * 125)); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 873 | int16_t* noise_samples = &buffer[kNoiseLpcOrder]; |
| 874 | if (background_noise_->initialized()) { |
| 875 | // Use background noise parameters. |
| 876 | memcpy(noise_samples - kNoiseLpcOrder, |
| 877 | background_noise_->FilterState(channel), |
| 878 | sizeof(int16_t) * kNoiseLpcOrder); |
| 879 | |
| 880 | int dc_offset = 0; |
| 881 | if (background_noise_->ScaleShift(channel) > 1) { |
| 882 | dc_offset = 1 << (background_noise_->ScaleShift(channel) - 1); |
| 883 | } |
| 884 | |
| 885 | // Scale random vector to correct energy level. |
| 886 | WebRtcSpl_AffineTransformVector( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 887 | scaled_random_vector, random_vector, background_noise_->Scale(channel), |
| 888 | dc_offset, background_noise_->ScaleShift(channel), num_noise_samples); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 889 | |
| 890 | WebRtcSpl_FilterARFastQ12(scaled_random_vector, noise_samples, |
| 891 | background_noise_->Filter(channel), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 892 | kNoiseLpcOrder + 1, num_noise_samples); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 893 | |
| 894 | background_noise_->SetFilterState( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 895 | channel, &(noise_samples[num_noise_samples - kNoiseLpcOrder]), |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 896 | kNoiseLpcOrder); |
| 897 | |
| 898 | // Unmute the background noise. |
| 899 | int16_t bgn_mute_factor = background_noise_->MuteFactor(channel); |
Henrik Lundin | 6719017 | 2018-04-20 15:34:48 +0200 | [diff] [blame] | 900 | if (bgn_mute_factor < 16384) { |
| 901 | WebRtcSpl_AffineTransformVector(noise_samples, noise_samples, |
| 902 | bgn_mute_factor, 8192, 14, |
| 903 | num_noise_samples); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 904 | } |
| 905 | // Update mute_factor in BackgroundNoise class. |
| 906 | background_noise_->SetMuteFactor(channel, bgn_mute_factor); |
| 907 | } else { |
| 908 | // BGN parameters have not been initialized; use zero noise. |
| 909 | memset(noise_samples, 0, sizeof(int16_t) * num_noise_samples); |
| 910 | } |
| 911 | } |
| 912 | |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 913 | void Expand::GenerateRandomVector(int16_t seed_increment, |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 914 | size_t length, |
| 915 | int16_t* random_vector) { |
| 916 | // TODO(turajs): According to hlundin The loop should not be needed. Should be |
| 917 | // just as good to generate all of the vector in one call. |
| 918 | size_t samples_generated = 0; |
| 919 | const size_t kMaxRandSamples = RandomVector::kRandomTableSize; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 920 | while (samples_generated < length) { |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 921 | size_t rand_length = std::min(length - samples_generated, kMaxRandSamples); |
| 922 | random_vector_->IncreaseSeedIncrement(seed_increment); |
| 923 | random_vector_->Generate(rand_length, &random_vector[samples_generated]); |
| 924 | samples_generated += rand_length; |
| 925 | } |
| 926 | } |
henrik.lundin@webrtc.org | d9faa46 | 2014-01-14 10:18:45 +0000 | [diff] [blame] | 927 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 928 | } // namespace webrtc |