Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 fs_hz_(fs), | 35 fs_hz_(fs), |
| 36 num_channels_(num_channels), | 36 num_channels_(num_channels), |
| 37 consecutive_expands_(0), | 37 consecutive_expands_(0), |
| 38 background_noise_(background_noise), | 38 background_noise_(background_noise), |
| 39 overlap_length_(5 * fs / 8000), | 39 overlap_length_(5 * fs / 8000), |
| 40 lag_index_direction_(0), | 40 lag_index_direction_(0), |
| 41 current_lag_index_(0), | 41 current_lag_index_(0), |
| 42 stop_muting_(false), | 42 stop_muting_(false), |
| 43 channel_parameters_(new ChannelParameters[num_channels_]) { | 43 channel_parameters_(new ChannelParameters[num_channels_]) { |
| 44 assert(fs == 8000 || fs == 16000 || fs == 32000 || fs == 48000); | 44 assert(fs == 8000 || fs == 16000 || fs == 32000 || fs == 48000); |
| 45 assert(fs <= kMaxSampleRate); // Should not be possible. | 45 assert(fs <= static_cast<int>(kMaxSampleRate)); // Should not be possible. |
| 46 assert(num_channels_ > 0); | 46 assert(num_channels_ > 0); |
| 47 memset(expand_lags_, 0, sizeof(expand_lags_)); | 47 memset(expand_lags_, 0, sizeof(expand_lags_)); |
| 48 Reset(); | 48 Reset(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 Expand::~Expand() = default; | 51 Expand::~Expand() = default; |
| 52 | 52 |
| 53 void Expand::Reset() { | 53 void Expand::Reset() { |
| 54 first_expand_ = true; | 54 first_expand_ = true; |
| 55 consecutive_expands_ = 0; | 55 consecutive_expands_ = 0; |
| 56 max_lag_ = 0; | 56 max_lag_ = 0; |
| 57 for (size_t ix = 0; ix < num_channels_; ++ix) { | 57 for (size_t ix = 0; ix < num_channels_; ++ix) { |
| 58 channel_parameters_[ix].expand_vector0.Clear(); | 58 channel_parameters_[ix].expand_vector0.Clear(); |
| 59 channel_parameters_[ix].expand_vector1.Clear(); | 59 channel_parameters_[ix].expand_vector1.Clear(); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 int Expand::Process(AudioMultiVector* output) { | 63 int Expand::Process(AudioMultiVector* output) { |
| 64 int16_t random_vector[kMaxSampleRate / 8000 * 120 + 30]; | 64 int16_t random_vector[kMaxSampleRate / 8000 * 120 + 30]; |
| 65 int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125]; | 65 int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125]; |
| 66 static const int kTempDataSize = 3600; | 66 static const int kTempDataSize = 3600; |
| 67 int16_t temp_data[kTempDataSize]; // TODO(hlundin) Remove this. | 67 int16_t temp_data[kTempDataSize]; // TODO(hlundin) Remove this. |
| 68 int16_t* voiced_vector_storage = temp_data; | 68 int16_t* voiced_vector_storage = temp_data; |
| 69 int16_t* voiced_vector = &voiced_vector_storage[overlap_length_]; | 69 int16_t* voiced_vector = &voiced_vector_storage[overlap_length_]; |
| 70 static const int kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; | 70 static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; |
| 71 int16_t unvoiced_array_memory[kNoiseLpcOrder + kMaxSampleRate / 8000 * 125]; | 71 int16_t unvoiced_array_memory[kNoiseLpcOrder + kMaxSampleRate / 8000 * 125]; |
| 72 int16_t* unvoiced_vector = unvoiced_array_memory + kUnvoicedLpcOrder; | 72 int16_t* unvoiced_vector = unvoiced_array_memory + kUnvoicedLpcOrder; |
| 73 int16_t* noise_vector = unvoiced_array_memory + kNoiseLpcOrder; | 73 int16_t* noise_vector = unvoiced_array_memory + kNoiseLpcOrder; |
| 74 | 74 |
| 75 int fs_mult = fs_hz_ / 8000; | 75 int fs_mult = fs_hz_ / 8000; |
| 76 | 76 |
| 77 if (first_expand_) { | 77 if (first_expand_) { |
| 78 // Perform initial setup if this is the first expansion since last reset. | 78 // Perform initial setup if this is the first expansion since last reset. |
| 79 AnalyzeSignal(random_vector); | 79 AnalyzeSignal(random_vector); |
| 80 first_expand_ = false; | 80 first_expand_ = false; |
| 81 } else { | 81 } else { |
| 82 // This is not the first expansion, parameters are already estimated. | 82 // This is not the first expansion, parameters are already estimated. |
| 83 // Extract a noise segment. | 83 // Extract a noise segment. |
| 84 int16_t rand_length = max_lag_; | 84 size_t rand_length = max_lag_; |
| 85 // This only applies to SWB where length could be larger than 256. | 85 // This only applies to SWB where length could be larger than 256. |
| 86 assert(rand_length <= kMaxSampleRate / 8000 * 120 + 30); | 86 assert(rand_length <= kMaxSampleRate / 8000 * 120 + 30); |
| 87 GenerateRandomVector(2, rand_length, random_vector); | 87 GenerateRandomVector(2, rand_length, random_vector); |
| 88 } | 88 } |
| 89 | 89 |
| 90 | 90 |
| 91 // Generate signal. | 91 // Generate signal. |
| 92 UpdateLagIndex(); | 92 UpdateLagIndex(); |
| 93 | 93 |
| 94 // Voiced part. | 94 // Voiced part. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 106 assert(expansion_vector_position + temp_length <= | 106 assert(expansion_vector_position + temp_length <= |
| 107 parameters.expand_vector0.Size()); | 107 parameters.expand_vector0.Size()); |
| 108 memcpy(voiced_vector_storage, | 108 memcpy(voiced_vector_storage, |
| 109 ¶meters.expand_vector0[expansion_vector_position], | 109 ¶meters.expand_vector0[expansion_vector_position], |
| 110 sizeof(int16_t) * temp_length); | 110 sizeof(int16_t) * temp_length); |
| 111 } else if (current_lag_index_ == 1) { | 111 } else if (current_lag_index_ == 1) { |
| 112 // Mix 3/4 of expand_vector0 with 1/4 of expand_vector1. | 112 // Mix 3/4 of expand_vector0 with 1/4 of expand_vector1. |
| 113 WebRtcSpl_ScaleAndAddVectorsWithRound( | 113 WebRtcSpl_ScaleAndAddVectorsWithRound( |
| 114 ¶meters.expand_vector0[expansion_vector_position], 3, | 114 ¶meters.expand_vector0[expansion_vector_position], 3, |
| 115 ¶meters.expand_vector1[expansion_vector_position], 1, 2, | 115 ¶meters.expand_vector1[expansion_vector_position], 1, 2, |
| 116 voiced_vector_storage, static_cast<int>(temp_length)); | 116 voiced_vector_storage, temp_length); |
| 117 } else if (current_lag_index_ == 2) { | 117 } else if (current_lag_index_ == 2) { |
| 118 // Mix 1/2 of expand_vector0 with 1/2 of expand_vector1. | 118 // Mix 1/2 of expand_vector0 with 1/2 of expand_vector1. |
| 119 assert(expansion_vector_position + temp_length <= | 119 assert(expansion_vector_position + temp_length <= |
| 120 parameters.expand_vector0.Size()); | 120 parameters.expand_vector0.Size()); |
| 121 assert(expansion_vector_position + temp_length <= | 121 assert(expansion_vector_position + temp_length <= |
| 122 parameters.expand_vector1.Size()); | 122 parameters.expand_vector1.Size()); |
| 123 WebRtcSpl_ScaleAndAddVectorsWithRound( | 123 WebRtcSpl_ScaleAndAddVectorsWithRound( |
| 124 ¶meters.expand_vector0[expansion_vector_position], 1, | 124 ¶meters.expand_vector0[expansion_vector_position], 1, |
| 125 ¶meters.expand_vector1[expansion_vector_position], 1, 1, | 125 ¶meters.expand_vector1[expansion_vector_position], 1, 1, |
| 126 voiced_vector_storage, static_cast<int>(temp_length)); | 126 voiced_vector_storage, temp_length); |
| 127 } | 127 } |
| 128 | 128 |
| 129 // Get tapering window parameters. Values are in Q15. | 129 // Get tapering window parameters. Values are in Q15. |
| 130 int16_t muting_window, muting_window_increment; | 130 int16_t muting_window, muting_window_increment; |
| 131 int16_t unmuting_window, unmuting_window_increment; | 131 int16_t unmuting_window, unmuting_window_increment; |
| 132 if (fs_hz_ == 8000) { | 132 if (fs_hz_ == 8000) { |
| 133 muting_window = DspHelper::kMuteFactorStart8kHz; | 133 muting_window = DspHelper::kMuteFactorStart8kHz; |
| 134 muting_window_increment = DspHelper::kMuteFactorIncrement8kHz; | 134 muting_window_increment = DspHelper::kMuteFactorIncrement8kHz; |
| 135 unmuting_window = DspHelper::kUnmuteFactorStart8kHz; | 135 unmuting_window = DspHelper::kUnmuteFactorStart8kHz; |
| 136 unmuting_window_increment = DspHelper::kUnmuteFactorIncrement8kHz; | 136 unmuting_window_increment = DspHelper::kUnmuteFactorIncrement8kHz; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 // Filter |scaled_random_vector| through |ar_filter_|. | 183 // Filter |scaled_random_vector| through |ar_filter_|. |
| 184 memcpy(unvoiced_vector - kUnvoicedLpcOrder, parameters.ar_filter_state, | 184 memcpy(unvoiced_vector - kUnvoicedLpcOrder, parameters.ar_filter_state, |
| 185 sizeof(int16_t) * kUnvoicedLpcOrder); | 185 sizeof(int16_t) * kUnvoicedLpcOrder); |
| 186 int32_t add_constant = 0; | 186 int32_t add_constant = 0; |
| 187 if (parameters.ar_gain_scale > 0) { | 187 if (parameters.ar_gain_scale > 0) { |
| 188 add_constant = 1 << (parameters.ar_gain_scale - 1); | 188 add_constant = 1 << (parameters.ar_gain_scale - 1); |
| 189 } | 189 } |
| 190 WebRtcSpl_AffineTransformVector(scaled_random_vector, random_vector, | 190 WebRtcSpl_AffineTransformVector(scaled_random_vector, random_vector, |
| 191 parameters.ar_gain, add_constant, | 191 parameters.ar_gain, add_constant, |
| 192 parameters.ar_gain_scale, | 192 parameters.ar_gain_scale, |
| 193 static_cast<int>(current_lag)); | 193 current_lag); |
| 194 WebRtcSpl_FilterARFastQ12(scaled_random_vector, unvoiced_vector, | 194 WebRtcSpl_FilterARFastQ12(scaled_random_vector, unvoiced_vector, |
| 195 parameters.ar_filter, kUnvoicedLpcOrder + 1, | 195 parameters.ar_filter, kUnvoicedLpcOrder + 1, |
| 196 static_cast<int>(current_lag)); | 196 current_lag); |
| 197 memcpy(parameters.ar_filter_state, | 197 memcpy(parameters.ar_filter_state, |
| 198 &(unvoiced_vector[current_lag - kUnvoicedLpcOrder]), | 198 &(unvoiced_vector[current_lag - kUnvoicedLpcOrder]), |
| 199 sizeof(int16_t) * kUnvoicedLpcOrder); | 199 sizeof(int16_t) * kUnvoicedLpcOrder); |
| 200 | 200 |
| 201 // Combine voiced and unvoiced contributions. | 201 // Combine voiced and unvoiced contributions. |
| 202 | 202 |
| 203 // Set a suitable cross-fading slope. | 203 // Set a suitable cross-fading slope. |
| 204 // For lag = | 204 // For lag = |
| 205 // <= 31 * fs_mult => go from 1 to 0 in about 8 ms; | 205 // <= 31 * fs_mult => go from 1 to 0 in about 8 ms; |
| 206 // (>= 31 .. <= 63) * fs_mult => go from 1 to 0 in about 16 ms; | 206 // (>= 31 .. <= 63) * fs_mult => go from 1 to 0 in about 16 ms; |
| 207 // >= 64 * fs_mult => go from 1 to 0 in about 32 ms. | 207 // >= 64 * fs_mult => go from 1 to 0 in about 32 ms. |
| 208 // temp_shift = getbits(max_lag_) - 5. | 208 // temp_shift = getbits(max_lag_) - 5. |
| 209 int temp_shift = (31 - WebRtcSpl_NormW32(max_lag_)) - 5; | 209 int temp_shift = |
| 210 (31 - WebRtcSpl_NormW32(static_cast<int32_t>(max_lag_))) - 5; | |
|
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
Done.
| |
| 210 int16_t mix_factor_increment = 256 >> temp_shift; | 211 int16_t mix_factor_increment = 256 >> temp_shift; |
| 211 if (stop_muting_) { | 212 if (stop_muting_) { |
| 212 mix_factor_increment = 0; | 213 mix_factor_increment = 0; |
| 213 } | 214 } |
| 214 | 215 |
| 215 // Create combined signal by shifting in more and more of unvoiced part. | 216 // Create combined signal by shifting in more and more of unvoiced part. |
| 216 temp_shift = 8 - temp_shift; // = getbits(mix_factor_increment). | 217 temp_shift = 8 - temp_shift; // = getbits(mix_factor_increment). |
| 217 size_t temp_length = (parameters.current_voice_mix_factor - | 218 size_t temp_length = (parameters.current_voice_mix_factor - |
| 218 parameters.voice_mix_factor) >> temp_shift; | 219 parameters.voice_mix_factor) >> temp_shift; |
| 219 temp_length = std::min(temp_length, current_lag); | 220 temp_length = std::min(temp_length, current_lag); |
| 220 DspHelper::CrossFade(voiced_vector, unvoiced_vector, temp_length, | 221 DspHelper::CrossFade(voiced_vector, unvoiced_vector, temp_length, |
| 221 ¶meters.current_voice_mix_factor, | 222 ¶meters.current_voice_mix_factor, |
| 222 mix_factor_increment, temp_data); | 223 mix_factor_increment, temp_data); |
| 223 | 224 |
| 224 // End of cross-fading period was reached before end of expanded signal | 225 // End of cross-fading period was reached before end of expanded signal |
| 225 // path. Mix the rest with a fixed mixing factor. | 226 // path. Mix the rest with a fixed mixing factor. |
| 226 if (temp_length < current_lag) { | 227 if (temp_length < current_lag) { |
| 227 if (mix_factor_increment != 0) { | 228 if (mix_factor_increment != 0) { |
| 228 parameters.current_voice_mix_factor = parameters.voice_mix_factor; | 229 parameters.current_voice_mix_factor = parameters.voice_mix_factor; |
| 229 } | 230 } |
| 230 int16_t temp_scale = 16384 - parameters.current_voice_mix_factor; | 231 int16_t temp_scale = 16384 - parameters.current_voice_mix_factor; |
| 231 WebRtcSpl_ScaleAndAddVectorsWithRound( | 232 WebRtcSpl_ScaleAndAddVectorsWithRound( |
| 232 voiced_vector + temp_length, parameters.current_voice_mix_factor, | 233 voiced_vector + temp_length, parameters.current_voice_mix_factor, |
| 233 unvoiced_vector + temp_length, temp_scale, 14, | 234 unvoiced_vector + temp_length, temp_scale, 14, |
| 234 temp_data + temp_length, static_cast<int>(current_lag - temp_length)); | 235 temp_data + temp_length, current_lag - temp_length); |
| 235 } | 236 } |
| 236 | 237 |
| 237 // Select muting slope depending on how many consecutive expands we have | 238 // Select muting slope depending on how many consecutive expands we have |
| 238 // done. | 239 // done. |
| 239 if (consecutive_expands_ == 3) { | 240 if (consecutive_expands_ == 3) { |
| 240 // Let the mute factor decrease from 1.0 to 0.95 in 6.25 ms. | 241 // Let the mute factor decrease from 1.0 to 0.95 in 6.25 ms. |
| 241 // mute_slope = 0.0010 / fs_mult in Q20. | 242 // mute_slope = 0.0010 / fs_mult in Q20. |
| 242 parameters.mute_slope = std::max(parameters.mute_slope, 1049 / fs_mult); | 243 parameters.mute_slope = std::max(parameters.mute_slope, 1049 / fs_mult); |
| 243 } | 244 } |
| 244 if (consecutive_expands_ == 7) { | 245 if (consecutive_expands_ == 7) { |
| 245 // Let the mute factor decrease from 1.0 to 0.90 in 6.25 ms. | 246 // Let the mute factor decrease from 1.0 to 0.90 in 6.25 ms. |
| 246 // mute_slope = 0.0020 / fs_mult in Q20. | 247 // mute_slope = 0.0020 / fs_mult in Q20. |
| 247 parameters.mute_slope = std::max(parameters.mute_slope, 2097 / fs_mult); | 248 parameters.mute_slope = std::max(parameters.mute_slope, 2097 / fs_mult); |
| 248 } | 249 } |
| 249 | 250 |
| 250 // Mute segment according to slope value. | 251 // Mute segment according to slope value. |
| 251 if ((consecutive_expands_ != 0) || !parameters.onset) { | 252 if ((consecutive_expands_ != 0) || !parameters.onset) { |
| 252 // Mute to the previous level, then continue with the muting. | 253 // Mute to the previous level, then continue with the muting. |
| 253 WebRtcSpl_AffineTransformVector(temp_data, temp_data, | 254 WebRtcSpl_AffineTransformVector(temp_data, temp_data, |
| 254 parameters.mute_factor, 8192, | 255 parameters.mute_factor, 8192, |
| 255 14, static_cast<int>(current_lag)); | 256 14, current_lag); |
| 256 | 257 |
| 257 if (!stop_muting_) { | 258 if (!stop_muting_) { |
| 258 DspHelper::MuteSignal(temp_data, parameters.mute_slope, current_lag); | 259 DspHelper::MuteSignal(temp_data, parameters.mute_slope, current_lag); |
| 259 | 260 |
| 260 // Shift by 6 to go from Q20 to Q14. | 261 // Shift by 6 to go from Q20 to Q14. |
| 261 // TODO(hlundin): Adding 8192 before shifting 6 steps seems wrong. | 262 // TODO(hlundin): Adding 8192 before shifting 6 steps seems wrong. |
| 262 // Legacy. | 263 // Legacy. |
| 263 int16_t gain = static_cast<int16_t>(16384 - | 264 int16_t gain = static_cast<int16_t>(16384 - |
| 264 (((current_lag * parameters.mute_slope) + 8192) >> 6)); | 265 (((current_lag * parameters.mute_slope) + 8192) >> 6)); |
| 265 gain = ((gain * parameters.mute_factor) + 8192) >> 14; | 266 gain = ((gain * parameters.mute_factor) + 8192) >> 14; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 } | 333 } |
| 333 | 334 |
| 334 bool Expand::TooManyExpands() { | 335 bool Expand::TooManyExpands() { |
| 335 return consecutive_expands_ >= kMaxConsecutiveExpands; | 336 return consecutive_expands_ >= kMaxConsecutiveExpands; |
| 336 } | 337 } |
| 337 | 338 |
| 338 void Expand::AnalyzeSignal(int16_t* random_vector) { | 339 void Expand::AnalyzeSignal(int16_t* random_vector) { |
| 339 int32_t auto_correlation[kUnvoicedLpcOrder + 1]; | 340 int32_t auto_correlation[kUnvoicedLpcOrder + 1]; |
| 340 int16_t reflection_coeff[kUnvoicedLpcOrder]; | 341 int16_t reflection_coeff[kUnvoicedLpcOrder]; |
| 341 int16_t correlation_vector[kMaxSampleRate / 8000 * 102]; | 342 int16_t correlation_vector[kMaxSampleRate / 8000 * 102]; |
| 342 int best_correlation_index[kNumCorrelationCandidates]; | 343 size_t best_correlation_index[kNumCorrelationCandidates]; |
| 343 int16_t best_correlation[kNumCorrelationCandidates]; | 344 int16_t best_correlation[kNumCorrelationCandidates]; |
| 344 int16_t best_distortion_index[kNumCorrelationCandidates]; | 345 size_t best_distortion_index[kNumCorrelationCandidates]; |
| 345 int16_t best_distortion[kNumCorrelationCandidates]; | 346 int16_t best_distortion[kNumCorrelationCandidates]; |
| 346 int32_t correlation_vector2[(99 * kMaxSampleRate / 8000) + 1]; | 347 int32_t correlation_vector2[(99 * kMaxSampleRate / 8000) + 1]; |
| 347 int32_t best_distortion_w32[kNumCorrelationCandidates]; | 348 int32_t best_distortion_w32[kNumCorrelationCandidates]; |
| 348 static const int kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; | 349 static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; |
| 349 int16_t unvoiced_array_memory[kNoiseLpcOrder + kMaxSampleRate / 8000 * 125]; | 350 int16_t unvoiced_array_memory[kNoiseLpcOrder + kMaxSampleRate / 8000 * 125]; |
| 350 int16_t* unvoiced_vector = unvoiced_array_memory + kUnvoicedLpcOrder; | 351 int16_t* unvoiced_vector = unvoiced_array_memory + kUnvoicedLpcOrder; |
| 351 | 352 |
| 352 int fs_mult = fs_hz_ / 8000; | 353 size_t fs_mult = static_cast<size_t>(fs_hz_ / 8000); |
| 353 | 354 |
| 354 // Pre-calculate common multiplications with fs_mult. | 355 // Pre-calculate common multiplications with fs_mult. |
| 355 int fs_mult_4 = fs_mult * 4; | 356 size_t fs_mult_4 = fs_mult * 4; |
| 356 int fs_mult_20 = fs_mult * 20; | 357 size_t fs_mult_20 = fs_mult * 20; |
| 357 int fs_mult_120 = fs_mult * 120; | 358 size_t fs_mult_120 = fs_mult * 120; |
| 358 int fs_mult_dist_len = fs_mult * kDistortionLength; | 359 size_t fs_mult_dist_len = fs_mult * kDistortionLength; |
| 359 int fs_mult_lpc_analysis_len = fs_mult * kLpcAnalysisLength; | 360 size_t fs_mult_lpc_analysis_len = fs_mult * kLpcAnalysisLength; |
| 360 | 361 |
| 361 const size_t signal_length = 256 * fs_mult; | 362 const size_t signal_length = 256 * fs_mult; |
| 362 const int16_t* audio_history = | 363 const int16_t* audio_history = |
| 363 &(*sync_buffer_)[0][sync_buffer_->Size() - signal_length]; | 364 &(*sync_buffer_)[0][sync_buffer_->Size() - signal_length]; |
| 364 | 365 |
| 365 // Initialize. | 366 // Initialize. |
| 366 InitializeForAnExpandPeriod(); | 367 InitializeForAnExpandPeriod(); |
| 367 | 368 |
| 368 // Calculate correlation in downsampled domain (4 kHz sample rate). | 369 // Calculate correlation in downsampled domain (4 kHz sample rate). |
| 369 int correlation_scale; | 370 int correlation_scale; |
| 370 int correlation_length = 51; // TODO(hlundin): Legacy bit-exactness. | 371 size_t correlation_length = 51; // TODO(hlundin): Legacy bit-exactness. |
| 371 // If it is decided to break bit-exactness |correlation_length| should be | 372 // If it is decided to break bit-exactness |correlation_length| should be |
| 372 // initialized to the return value of Correlation(). | 373 // initialized to the return value of Correlation(). |
| 373 Correlation(audio_history, signal_length, correlation_vector, | 374 Correlation(audio_history, signal_length, correlation_vector, |
| 374 &correlation_scale); | 375 &correlation_scale); |
| 375 | 376 |
| 376 // Find peaks in correlation vector. | 377 // Find peaks in correlation vector. |
| 377 DspHelper::PeakDetection(correlation_vector, correlation_length, | 378 DspHelper::PeakDetection(correlation_vector, correlation_length, |
| 378 kNumCorrelationCandidates, fs_mult, | 379 kNumCorrelationCandidates, fs_mult, |
| 379 best_correlation_index, best_correlation); | 380 best_correlation_index, best_correlation); |
| 380 | 381 |
| 381 // Adjust peak locations; cross-correlation lags start at 2.5 ms | 382 // Adjust peak locations; cross-correlation lags start at 2.5 ms |
| 382 // (20 * fs_mult samples). | 383 // (20 * fs_mult samples). |
| 383 best_correlation_index[0] += fs_mult_20; | 384 best_correlation_index[0] += fs_mult_20; |
| 384 best_correlation_index[1] += fs_mult_20; | 385 best_correlation_index[1] += fs_mult_20; |
| 385 best_correlation_index[2] += fs_mult_20; | 386 best_correlation_index[2] += fs_mult_20; |
| 386 | 387 |
| 387 // Calculate distortion around the |kNumCorrelationCandidates| best lags. | 388 // Calculate distortion around the |kNumCorrelationCandidates| best lags. |
| 388 int distortion_scale = 0; | 389 int distortion_scale = 0; |
| 389 for (int i = 0; i < kNumCorrelationCandidates; i++) { | 390 for (size_t i = 0; i < kNumCorrelationCandidates; i++) { |
| 390 int16_t min_index = std::max(fs_mult_20, | 391 size_t min_index = std::max(fs_mult_20, |
| 391 best_correlation_index[i] - fs_mult_4); | 392 best_correlation_index[i] - fs_mult_4); |
| 392 int16_t max_index = std::min(fs_mult_120 - 1, | 393 size_t max_index = std::min(fs_mult_120 - 1, |
| 393 best_correlation_index[i] + fs_mult_4); | 394 best_correlation_index[i] + fs_mult_4); |
| 394 best_distortion_index[i] = DspHelper::MinDistortion( | 395 best_distortion_index[i] = DspHelper::MinDistortion( |
| 395 &(audio_history[signal_length - fs_mult_dist_len]), min_index, | 396 &(audio_history[signal_length - fs_mult_dist_len]), min_index, |
| 396 max_index, fs_mult_dist_len, &best_distortion_w32[i]); | 397 max_index, fs_mult_dist_len, &best_distortion_w32[i]); |
| 397 distortion_scale = std::max(16 - WebRtcSpl_NormW32(best_distortion_w32[i]), | 398 distortion_scale = std::max(16 - WebRtcSpl_NormW32(best_distortion_w32[i]), |
| 398 distortion_scale); | 399 distortion_scale); |
| 399 } | 400 } |
| 400 // Shift the distortion values to fit in 16 bits. | 401 // Shift the distortion values to fit in 16 bits. |
| 401 WebRtcSpl_VectorBitShiftW32ToW16(best_distortion, kNumCorrelationCandidates, | 402 WebRtcSpl_VectorBitShiftW32ToW16(best_distortion, kNumCorrelationCandidates, |
| 402 best_distortion_w32, distortion_scale); | 403 best_distortion_w32, distortion_scale); |
| 403 | 404 |
| 404 // Find the maximizing index |i| of the cost function | 405 // Find the maximizing index |i| of the cost function |
| 405 // f[i] = best_correlation[i] / best_distortion[i]. | 406 // f[i] = best_correlation[i] / best_distortion[i]. |
| 406 int32_t best_ratio = std::numeric_limits<int32_t>::min(); | 407 int32_t best_ratio = std::numeric_limits<int32_t>::min(); |
| 407 int best_index = std::numeric_limits<int>::max(); | 408 size_t best_index = std::numeric_limits<size_t>::max(); |
| 408 for (int i = 0; i < kNumCorrelationCandidates; ++i) { | 409 for (size_t i = 0; i < kNumCorrelationCandidates; ++i) { |
| 409 int32_t ratio; | 410 int32_t ratio; |
| 410 if (best_distortion[i] > 0) { | 411 if (best_distortion[i] > 0) { |
| 411 ratio = (best_correlation[i] << 16) / best_distortion[i]; | 412 ratio = (best_correlation[i] << 16) / best_distortion[i]; |
| 412 } else if (best_correlation[i] == 0) { | 413 } else if (best_correlation[i] == 0) { |
| 413 ratio = 0; // No correlation set result to zero. | 414 ratio = 0; // No correlation set result to zero. |
| 414 } else { | 415 } else { |
| 415 ratio = std::numeric_limits<int32_t>::max(); // Denominator is zero. | 416 ratio = std::numeric_limits<int32_t>::max(); // Denominator is zero. |
| 416 } | 417 } |
| 417 if (ratio > best_ratio) { | 418 if (ratio > best_ratio) { |
| 418 best_index = i; | 419 best_index = i; |
| 419 best_ratio = ratio; | 420 best_ratio = ratio; |
| 420 } | 421 } |
| 421 } | 422 } |
| 422 | 423 |
| 423 int distortion_lag = best_distortion_index[best_index]; | 424 size_t distortion_lag = best_distortion_index[best_index]; |
| 424 int correlation_lag = best_correlation_index[best_index]; | 425 size_t correlation_lag = best_correlation_index[best_index]; |
| 425 max_lag_ = std::max(distortion_lag, correlation_lag); | 426 max_lag_ = std::max(distortion_lag, correlation_lag); |
| 426 | 427 |
| 427 // Calculate the exact best correlation in the range between | 428 // Calculate the exact best correlation in the range between |
| 428 // |correlation_lag| and |distortion_lag|. | 429 // |correlation_lag| and |distortion_lag|. |
| 429 correlation_length = | 430 correlation_length = |
| 430 std::max(std::min(distortion_lag + 10, fs_mult_120), 60 * fs_mult); | 431 std::max(std::min(distortion_lag + 10, fs_mult_120), 60 * fs_mult); |
| 431 | 432 |
| 432 int start_index = std::min(distortion_lag, correlation_lag); | 433 size_t start_index = std::min(distortion_lag, correlation_lag); |
| 433 int correlation_lags = | 434 size_t correlation_lags = static_cast<size_t>( |
| 434 WEBRTC_SPL_ABS_W16((distortion_lag-correlation_lag)) + 1; | 435 WEBRTC_SPL_ABS_W16((distortion_lag-correlation_lag)) + 1); |
| 435 assert(correlation_lags <= 99 * fs_mult + 1); // Cannot be larger. | 436 assert(correlation_lags <= 99 * fs_mult + 1); // Cannot be larger. |
| 436 | 437 |
| 437 for (size_t channel_ix = 0; channel_ix < num_channels_; ++channel_ix) { | 438 for (size_t channel_ix = 0; channel_ix < num_channels_; ++channel_ix) { |
| 438 ChannelParameters& parameters = channel_parameters_[channel_ix]; | 439 ChannelParameters& parameters = channel_parameters_[channel_ix]; |
| 439 // Calculate suitable scaling. | 440 // Calculate suitable scaling. |
| 440 int16_t signal_max = WebRtcSpl_MaxAbsValueW16( | 441 int16_t signal_max = WebRtcSpl_MaxAbsValueW16( |
| 441 &audio_history[signal_length - correlation_length - start_index | 442 &audio_history[signal_length - correlation_length - start_index |
| 442 - correlation_lags], | 443 - correlation_lags], |
| 443 correlation_length + start_index + correlation_lags - 1); | 444 correlation_length + start_index + correlation_lags - 1); |
| 444 correlation_scale = (31 - WebRtcSpl_NormW32(signal_max * signal_max)) + | 445 correlation_scale = (31 - WebRtcSpl_NormW32(signal_max * signal_max)) + |
| 445 (31 - WebRtcSpl_NormW32(correlation_length)) - 31; | 446 (31 - WebRtcSpl_NormW32(static_cast<int32_t>(correlation_length))) - 31; |
|
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
This shouldn't be necessary, as we're guaranteed t
hlundin-webrtc
2015/08/18 07:19:18
Acknowledged.
| |
| 446 correlation_scale = std::max(0, correlation_scale); | 447 correlation_scale = std::max(0, correlation_scale); |
| 447 | 448 |
| 448 // Calculate the correlation, store in |correlation_vector2|. | 449 // Calculate the correlation, store in |correlation_vector2|. |
| 449 WebRtcSpl_CrossCorrelation( | 450 WebRtcSpl_CrossCorrelation( |
| 450 correlation_vector2, | 451 correlation_vector2, |
| 451 &(audio_history[signal_length - correlation_length]), | 452 &(audio_history[signal_length - correlation_length]), |
| 452 &(audio_history[signal_length - correlation_length - start_index]), | 453 &(audio_history[signal_length - correlation_length - start_index]), |
| 453 correlation_length, correlation_lags, correlation_scale, -1); | 454 correlation_length, correlation_lags, correlation_scale, -1); |
| 454 | 455 |
| 455 // Find maximizing index. | 456 // Find maximizing index. |
| 456 best_index = WebRtcSpl_MaxIndexW32(correlation_vector2, correlation_lags); | 457 best_index = static_cast<size_t>( |
| 458 WebRtcSpl_MaxIndexW32(correlation_vector2, correlation_lags)); | |
| 457 int32_t max_correlation = correlation_vector2[best_index]; | 459 int32_t max_correlation = correlation_vector2[best_index]; |
| 458 // Compensate index with start offset. | 460 // Compensate index with start offset. |
| 459 best_index = best_index + start_index; | 461 best_index = best_index + start_index; |
| 460 | 462 |
| 461 // Calculate energies. | 463 // Calculate energies. |
| 462 int32_t energy1 = WebRtcSpl_DotProductWithScale( | 464 int32_t energy1 = WebRtcSpl_DotProductWithScale( |
| 463 &(audio_history[signal_length - correlation_length]), | 465 &(audio_history[signal_length - correlation_length]), |
| 464 &(audio_history[signal_length - correlation_length]), | 466 &(audio_history[signal_length - correlation_length]), |
| 465 correlation_length, correlation_scale); | 467 correlation_length, correlation_scale); |
| 466 int32_t energy2 = WebRtcSpl_DotProductWithScale( | 468 int32_t energy2 = WebRtcSpl_DotProductWithScale( |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 489 corr_coefficient = WebRtcSpl_DivW32W16(max_correlation, | 491 corr_coefficient = WebRtcSpl_DivW32W16(max_correlation, |
| 490 sqrt_energy_product); | 492 sqrt_energy_product); |
| 491 // Cap at 1.0 in Q14. | 493 // Cap at 1.0 in Q14. |
| 492 corr_coefficient = std::min(16384, corr_coefficient); | 494 corr_coefficient = std::min(16384, corr_coefficient); |
| 493 } else { | 495 } else { |
| 494 corr_coefficient = 0; | 496 corr_coefficient = 0; |
| 495 } | 497 } |
| 496 | 498 |
| 497 // Extract the two vectors expand_vector0 and expand_vector1 from | 499 // Extract the two vectors expand_vector0 and expand_vector1 from |
| 498 // |audio_history|. | 500 // |audio_history|. |
| 499 int16_t expansion_length = static_cast<int16_t>(max_lag_ + overlap_length_); | 501 size_t expansion_length = max_lag_ + overlap_length_; |
| 500 const int16_t* vector1 = &(audio_history[signal_length - expansion_length]); | 502 const int16_t* vector1 = &(audio_history[signal_length - expansion_length]); |
| 501 const int16_t* vector2 = vector1 - distortion_lag; | 503 const int16_t* vector2 = vector1 - distortion_lag; |
| 502 // Normalize the second vector to the same energy as the first. | 504 // Normalize the second vector to the same energy as the first. |
| 503 energy1 = WebRtcSpl_DotProductWithScale(vector1, vector1, expansion_length, | 505 energy1 = WebRtcSpl_DotProductWithScale(vector1, vector1, expansion_length, |
| 504 correlation_scale); | 506 correlation_scale); |
| 505 energy2 = WebRtcSpl_DotProductWithScale(vector2, vector2, expansion_length, | 507 energy2 = WebRtcSpl_DotProductWithScale(vector2, vector2, expansion_length, |
| 506 correlation_scale); | 508 correlation_scale); |
| 507 // Confirm that amplitude ratio sqrt(energy1 / energy2) is within 0.5 - 2.0, | 509 // Confirm that amplitude ratio sqrt(energy1 / energy2) is within 0.5 - 2.0, |
| 508 // i.e., energy1 / energy1 is within 0.25 - 4. | 510 // i.e., energy1 / energy1 is within 0.25 - 4. |
| 509 int16_t amplitude_ratio; | 511 int16_t amplitude_ratio; |
| 510 if ((energy1 / 4 < energy2) && (energy1 > energy2 / 4)) { | 512 if ((energy1 / 4 < energy2) && (energy1 > energy2 / 4)) { |
| 511 // Energy constraint fulfilled. Use both vectors and scale them | 513 // Energy constraint fulfilled. Use both vectors and scale them |
| 512 // accordingly. | 514 // accordingly. |
| 513 int32_t scaled_energy2 = std::max(16 - WebRtcSpl_NormW32(energy2), 0); | 515 int32_t scaled_energy2 = std::max(16 - WebRtcSpl_NormW32(energy2), 0); |
| 514 int32_t scaled_energy1 = scaled_energy2 - 13; | 516 int32_t scaled_energy1 = scaled_energy2 - 13; |
| 515 // Calculate scaled_energy1 / scaled_energy2 in Q13. | 517 // Calculate scaled_energy1 / scaled_energy2 in Q13. |
| 516 int32_t energy_ratio = WebRtcSpl_DivW32W16( | 518 int32_t energy_ratio = WebRtcSpl_DivW32W16( |
| 517 WEBRTC_SPL_SHIFT_W32(energy1, -scaled_energy1), | 519 WEBRTC_SPL_SHIFT_W32(energy1, -scaled_energy1), |
| 518 energy2 >> scaled_energy2); | 520 static_cast<int16_t>(energy2 >> scaled_energy2)); |
|
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
This shouldn't be necessary, as the shift result h
hlundin-webrtc
2015/08/18 07:19:18
Acknowledged.
| |
| 519 // Calculate sqrt ratio in Q13 (sqrt of en1/en2 in Q26). | 521 // Calculate sqrt ratio in Q13 (sqrt of en1/en2 in Q26). |
| 520 amplitude_ratio = WebRtcSpl_SqrtFloor(energy_ratio << 13); | 522 amplitude_ratio = |
| 523 static_cast<int16_t>(WebRtcSpl_SqrtFloor(energy_ratio << 13)); | |
|
hlundin-webrtc
2015/08/10 11:30:01
rtc::checked_cast
Peter Kasting
2015/08/17 22:49:46
See previous comment on WebRtcSpl_SqrtFloor().
hlundin-webrtc
2015/08/18 07:19:18
Acknowledged.
| |
| 521 // Copy the two vectors and give them the same energy. | 524 // Copy the two vectors and give them the same energy. |
| 522 parameters.expand_vector0.Clear(); | 525 parameters.expand_vector0.Clear(); |
| 523 parameters.expand_vector0.PushBack(vector1, expansion_length); | 526 parameters.expand_vector0.PushBack(vector1, expansion_length); |
| 524 parameters.expand_vector1.Clear(); | 527 parameters.expand_vector1.Clear(); |
| 525 if (parameters.expand_vector1.Size() < | 528 if (parameters.expand_vector1.Size() < expansion_length) { |
| 526 static_cast<size_t>(expansion_length)) { | |
| 527 parameters.expand_vector1.Extend( | 529 parameters.expand_vector1.Extend( |
| 528 expansion_length - parameters.expand_vector1.Size()); | 530 expansion_length - parameters.expand_vector1.Size()); |
| 529 } | 531 } |
| 530 WebRtcSpl_AffineTransformVector(¶meters.expand_vector1[0], | 532 WebRtcSpl_AffineTransformVector(¶meters.expand_vector1[0], |
| 531 const_cast<int16_t*>(vector2), | 533 const_cast<int16_t*>(vector2), |
| 532 amplitude_ratio, | 534 amplitude_ratio, |
| 533 4096, | 535 4096, |
| 534 13, | 536 13, |
| 535 expansion_length); | 537 expansion_length); |
| 536 } else { | 538 } else { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 607 if (stability != 1) { | 609 if (stability != 1) { |
| 608 // Set first coefficient to 4096 (1.0 in Q12). | 610 // Set first coefficient to 4096 (1.0 in Q12). |
| 609 parameters.ar_filter[0] = 4096; | 611 parameters.ar_filter[0] = 4096; |
| 610 // Set remaining |kUnvoicedLpcOrder| coefficients to zero. | 612 // Set remaining |kUnvoicedLpcOrder| coefficients to zero. |
| 611 WebRtcSpl_MemSetW16(parameters.ar_filter + 1, 0, kUnvoicedLpcOrder); | 613 WebRtcSpl_MemSetW16(parameters.ar_filter + 1, 0, kUnvoicedLpcOrder); |
| 612 } | 614 } |
| 613 } | 615 } |
| 614 | 616 |
| 615 if (channel_ix == 0) { | 617 if (channel_ix == 0) { |
| 616 // Extract a noise segment. | 618 // Extract a noise segment. |
| 617 int16_t noise_length; | 619 size_t noise_length; |
| 618 if (distortion_lag < 40) { | 620 if (distortion_lag < 40) { |
| 619 noise_length = 2 * distortion_lag + 30; | 621 noise_length = 2 * distortion_lag + 30; |
| 620 } else { | 622 } else { |
| 621 noise_length = distortion_lag + 30; | 623 noise_length = distortion_lag + 30; |
| 622 } | 624 } |
| 623 if (noise_length <= RandomVector::kRandomTableSize) { | 625 if (noise_length <= RandomVector::kRandomTableSize) { |
| 624 memcpy(random_vector, RandomVector::kRandomTable, | 626 memcpy(random_vector, RandomVector::kRandomTable, |
| 625 sizeof(int16_t) * noise_length); | 627 sizeof(int16_t) * noise_length); |
| 626 } else { | 628 } else { |
| 627 // Only applies to SWB where length could be larger than | 629 // Only applies to SWB where length could be larger than |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 722 parameters.onset = true; | 724 parameters.onset = true; |
| 723 } else { | 725 } else { |
| 724 // Calculate (1 - slope) / distortion_lag. | 726 // Calculate (1 - slope) / distortion_lag. |
| 725 // Shift |slope| by 7 to Q20 before the division. The result is in Q20. | 727 // Shift |slope| by 7 to Q20 before the division. The result is in Q20. |
| 726 parameters.mute_slope = WebRtcSpl_DivW32W16( | 728 parameters.mute_slope = WebRtcSpl_DivW32W16( |
| 727 (8192 - slope) << 7, static_cast<int16_t>(distortion_lag)); | 729 (8192 - slope) << 7, static_cast<int16_t>(distortion_lag)); |
| 728 if (parameters.voice_mix_factor <= 13107) { | 730 if (parameters.voice_mix_factor <= 13107) { |
| 729 // Make sure the mute factor decreases from 1.0 to 0.9 in no more than | 731 // Make sure the mute factor decreases from 1.0 to 0.9 in no more than |
| 730 // 6.25 ms. | 732 // 6.25 ms. |
| 731 // mute_slope >= 0.005 / fs_mult in Q20. | 733 // mute_slope >= 0.005 / fs_mult in Q20. |
| 732 parameters.mute_slope = std::max(5243 / fs_mult, parameters.mute_slope); | 734 parameters.mute_slope = std::max(static_cast<int>(5243 / fs_mult), |
| 735 parameters.mute_slope); | |
| 733 } else if (slope > 8028) { | 736 } else if (slope > 8028) { |
| 734 parameters.mute_slope = 0; | 737 parameters.mute_slope = 0; |
| 735 } | 738 } |
| 736 parameters.onset = false; | 739 parameters.onset = false; |
| 737 } | 740 } |
| 738 } | 741 } |
| 739 } | 742 } |
| 740 | 743 |
| 741 Expand::ChannelParameters::ChannelParameters() | 744 Expand::ChannelParameters::ChannelParameters() |
| 742 : mute_factor(16384), | 745 : mute_factor(16384), |
| 743 ar_gain(0), | 746 ar_gain(0), |
| 744 ar_gain_scale(0), | 747 ar_gain_scale(0), |
| 745 voice_mix_factor(0), | 748 voice_mix_factor(0), |
| 746 current_voice_mix_factor(0), | 749 current_voice_mix_factor(0), |
| 747 onset(false), | 750 onset(false), |
| 748 mute_slope(0) { | 751 mute_slope(0) { |
| 749 memset(ar_filter, 0, sizeof(ar_filter)); | 752 memset(ar_filter, 0, sizeof(ar_filter)); |
| 750 memset(ar_filter_state, 0, sizeof(ar_filter_state)); | 753 memset(ar_filter_state, 0, sizeof(ar_filter_state)); |
| 751 } | 754 } |
| 752 | 755 |
| 753 void Expand::Correlation(const int16_t* input, | 756 void Expand::Correlation(const int16_t* input, |
| 754 size_t input_length, | 757 size_t input_length, |
| 755 int16_t* output, | 758 int16_t* output, |
| 756 int* output_scale) const { | 759 int* output_scale) const { |
| 757 // Set parameters depending on sample rate. | 760 // Set parameters depending on sample rate. |
| 758 const int16_t* filter_coefficients; | 761 const int16_t* filter_coefficients; |
| 759 int16_t num_coefficients; | 762 size_t num_coefficients; |
| 760 int16_t downsampling_factor; | 763 int16_t downsampling_factor; |
| 761 if (fs_hz_ == 8000) { | 764 if (fs_hz_ == 8000) { |
| 762 num_coefficients = 3; | 765 num_coefficients = 3; |
| 763 downsampling_factor = 2; | 766 downsampling_factor = 2; |
| 764 filter_coefficients = DspHelper::kDownsample8kHzTbl; | 767 filter_coefficients = DspHelper::kDownsample8kHzTbl; |
| 765 } else if (fs_hz_ == 16000) { | 768 } else if (fs_hz_ == 16000) { |
| 766 num_coefficients = 5; | 769 num_coefficients = 5; |
| 767 downsampling_factor = 4; | 770 downsampling_factor = 4; |
| 768 filter_coefficients = DspHelper::kDownsample16kHzTbl; | 771 filter_coefficients = DspHelper::kDownsample16kHzTbl; |
| 769 } else if (fs_hz_ == 32000) { | 772 } else if (fs_hz_ == 32000) { |
| 770 num_coefficients = 7; | 773 num_coefficients = 7; |
| 771 downsampling_factor = 8; | 774 downsampling_factor = 8; |
| 772 filter_coefficients = DspHelper::kDownsample32kHzTbl; | 775 filter_coefficients = DspHelper::kDownsample32kHzTbl; |
| 773 } else { // fs_hz_ == 48000. | 776 } else { // fs_hz_ == 48000. |
| 774 num_coefficients = 7; | 777 num_coefficients = 7; |
| 775 downsampling_factor = 12; | 778 downsampling_factor = 12; |
| 776 filter_coefficients = DspHelper::kDownsample48kHzTbl; | 779 filter_coefficients = DspHelper::kDownsample48kHzTbl; |
| 777 } | 780 } |
| 778 | 781 |
| 779 // Correlate from lag 10 to lag 60 in downsampled domain. | 782 // Correlate from lag 10 to lag 60 in downsampled domain. |
| 780 // (Corresponds to 20-120 for narrow-band, 40-240 for wide-band, and so on.) | 783 // (Corresponds to 20-120 for narrow-band, 40-240 for wide-band, and so on.) |
| 781 static const int kCorrelationStartLag = 10; | 784 static const size_t kCorrelationStartLag = 10; |
| 782 static const int kNumCorrelationLags = 54; | 785 static const size_t kNumCorrelationLags = 54; |
| 783 static const int kCorrelationLength = 60; | 786 static const size_t kCorrelationLength = 60; |
| 784 // Downsample to 4 kHz sample rate. | 787 // Downsample to 4 kHz sample rate. |
| 785 static const int kDownsampledLength = kCorrelationStartLag | 788 static const size_t kDownsampledLength = kCorrelationStartLag |
| 786 + kNumCorrelationLags + kCorrelationLength; | 789 + kNumCorrelationLags + kCorrelationLength; |
| 787 int16_t downsampled_input[kDownsampledLength]; | 790 int16_t downsampled_input[kDownsampledLength]; |
| 788 static const int kFilterDelay = 0; | 791 static const size_t kFilterDelay = 0; |
| 789 WebRtcSpl_DownsampleFast( | 792 WebRtcSpl_DownsampleFast( |
| 790 input + input_length - kDownsampledLength * downsampling_factor, | 793 input + input_length - kDownsampledLength * downsampling_factor, |
| 791 kDownsampledLength * downsampling_factor, downsampled_input, | 794 kDownsampledLength * downsampling_factor, downsampled_input, |
| 792 kDownsampledLength, filter_coefficients, num_coefficients, | 795 kDownsampledLength, filter_coefficients, num_coefficients, |
| 793 downsampling_factor, kFilterDelay); | 796 downsampling_factor, kFilterDelay); |
| 794 | 797 |
| 795 // Normalize |downsampled_input| to using all 16 bits. | 798 // Normalize |downsampled_input| to using all 16 bits. |
| 796 int16_t max_value = WebRtcSpl_MaxAbsValueW16(downsampled_input, | 799 int16_t max_value = WebRtcSpl_MaxAbsValueW16(downsampled_input, |
| 797 kDownsampledLength); | 800 kDownsampledLength); |
| 798 int16_t norm_shift = 16 - WebRtcSpl_NormW32(max_value); | 801 int16_t norm_shift = 16 - WebRtcSpl_NormW32(max_value); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 839 num_channels); | 842 num_channels); |
| 840 } | 843 } |
| 841 | 844 |
| 842 // TODO(turajs): This can be moved to BackgroundNoise class. | 845 // TODO(turajs): This can be moved to BackgroundNoise class. |
| 843 void Expand::GenerateBackgroundNoise(int16_t* random_vector, | 846 void Expand::GenerateBackgroundNoise(int16_t* random_vector, |
| 844 size_t channel, | 847 size_t channel, |
| 845 int mute_slope, | 848 int mute_slope, |
| 846 bool too_many_expands, | 849 bool too_many_expands, |
| 847 size_t num_noise_samples, | 850 size_t num_noise_samples, |
| 848 int16_t* buffer) { | 851 int16_t* buffer) { |
| 849 static const int kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; | 852 static const size_t kNoiseLpcOrder = BackgroundNoise::kMaxLpcOrder; |
| 850 int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125]; | 853 int16_t scaled_random_vector[kMaxSampleRate / 8000 * 125]; |
| 851 assert(num_noise_samples <= static_cast<size_t>(kMaxSampleRate / 8000 * 125)); | 854 assert(num_noise_samples <= (kMaxSampleRate / 8000 * 125)); |
| 852 int16_t* noise_samples = &buffer[kNoiseLpcOrder]; | 855 int16_t* noise_samples = &buffer[kNoiseLpcOrder]; |
| 853 if (background_noise_->initialized()) { | 856 if (background_noise_->initialized()) { |
| 854 // Use background noise parameters. | 857 // Use background noise parameters. |
| 855 memcpy(noise_samples - kNoiseLpcOrder, | 858 memcpy(noise_samples - kNoiseLpcOrder, |
| 856 background_noise_->FilterState(channel), | 859 background_noise_->FilterState(channel), |
| 857 sizeof(int16_t) * kNoiseLpcOrder); | 860 sizeof(int16_t) * kNoiseLpcOrder); |
| 858 | 861 |
| 859 int dc_offset = 0; | 862 int dc_offset = 0; |
| 860 if (background_noise_->ScaleShift(channel) > 1) { | 863 if (background_noise_->ScaleShift(channel) > 1) { |
| 861 dc_offset = 1 << (background_noise_->ScaleShift(channel) - 1); | 864 dc_offset = 1 << (background_noise_->ScaleShift(channel) - 1); |
| 862 } | 865 } |
| 863 | 866 |
| 864 // Scale random vector to correct energy level. | 867 // Scale random vector to correct energy level. |
| 865 WebRtcSpl_AffineTransformVector( | 868 WebRtcSpl_AffineTransformVector( |
| 866 scaled_random_vector, random_vector, | 869 scaled_random_vector, random_vector, |
| 867 background_noise_->Scale(channel), dc_offset, | 870 background_noise_->Scale(channel), dc_offset, |
| 868 background_noise_->ScaleShift(channel), | 871 background_noise_->ScaleShift(channel), |
| 869 static_cast<int>(num_noise_samples)); | 872 num_noise_samples); |
| 870 | 873 |
| 871 WebRtcSpl_FilterARFastQ12(scaled_random_vector, noise_samples, | 874 WebRtcSpl_FilterARFastQ12(scaled_random_vector, noise_samples, |
| 872 background_noise_->Filter(channel), | 875 background_noise_->Filter(channel), |
| 873 kNoiseLpcOrder + 1, | 876 kNoiseLpcOrder + 1, |
| 874 static_cast<int>(num_noise_samples)); | 877 num_noise_samples); |
| 875 | 878 |
| 876 background_noise_->SetFilterState( | 879 background_noise_->SetFilterState( |
| 877 channel, | 880 channel, |
| 878 &(noise_samples[num_noise_samples - kNoiseLpcOrder]), | 881 &(noise_samples[num_noise_samples - kNoiseLpcOrder]), |
| 879 kNoiseLpcOrder); | 882 kNoiseLpcOrder); |
| 880 | 883 |
| 881 // Unmute the background noise. | 884 // Unmute the background noise. |
| 882 int16_t bgn_mute_factor = background_noise_->MuteFactor(channel); | 885 int16_t bgn_mute_factor = background_noise_->MuteFactor(channel); |
| 883 NetEq::BackgroundNoiseMode bgn_mode = background_noise_->mode(); | 886 NetEq::BackgroundNoiseMode bgn_mode = background_noise_->mode(); |
| 884 if (bgn_mode == NetEq::kBgnFade && too_many_expands && | 887 if (bgn_mode == NetEq::kBgnFade && too_many_expands && |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 911 static_cast<int>(num_noise_samples), | 914 static_cast<int>(num_noise_samples), |
| 912 &bgn_mute_factor, | 915 &bgn_mute_factor, |
| 913 mute_slope, | 916 mute_slope, |
| 914 noise_samples); | 917 noise_samples); |
| 915 } else { | 918 } else { |
| 916 // kBgnOn and stop muting, or | 919 // kBgnOn and stop muting, or |
| 917 // kBgnOff (mute factor is always 0), or | 920 // kBgnOff (mute factor is always 0), or |
| 918 // kBgnFade has reached 0. | 921 // kBgnFade has reached 0. |
| 919 WebRtcSpl_AffineTransformVector(noise_samples, noise_samples, | 922 WebRtcSpl_AffineTransformVector(noise_samples, noise_samples, |
| 920 bgn_mute_factor, 8192, 14, | 923 bgn_mute_factor, 8192, 14, |
| 921 static_cast<int>(num_noise_samples)); | 924 num_noise_samples); |
| 922 } | 925 } |
| 923 } | 926 } |
| 924 // Update mute_factor in BackgroundNoise class. | 927 // Update mute_factor in BackgroundNoise class. |
| 925 background_noise_->SetMuteFactor(channel, bgn_mute_factor); | 928 background_noise_->SetMuteFactor(channel, bgn_mute_factor); |
| 926 } else { | 929 } else { |
| 927 // BGN parameters have not been initialized; use zero noise. | 930 // BGN parameters have not been initialized; use zero noise. |
| 928 memset(noise_samples, 0, sizeof(int16_t) * num_noise_samples); | 931 memset(noise_samples, 0, sizeof(int16_t) * num_noise_samples); |
| 929 } | 932 } |
| 930 } | 933 } |
| 931 | 934 |
| 932 void Expand::GenerateRandomVector(int16_t seed_increment, | 935 void Expand::GenerateRandomVector(int16_t seed_increment, |
| 933 size_t length, | 936 size_t length, |
| 934 int16_t* random_vector) { | 937 int16_t* random_vector) { |
| 935 // TODO(turajs): According to hlundin The loop should not be needed. Should be | 938 // TODO(turajs): According to hlundin The loop should not be needed. Should be |
| 936 // just as good to generate all of the vector in one call. | 939 // just as good to generate all of the vector in one call. |
| 937 size_t samples_generated = 0; | 940 size_t samples_generated = 0; |
| 938 const size_t kMaxRandSamples = RandomVector::kRandomTableSize; | 941 const size_t kMaxRandSamples = RandomVector::kRandomTableSize; |
| 939 while (samples_generated < length) { | 942 while (samples_generated < length) { |
| 940 size_t rand_length = std::min(length - samples_generated, kMaxRandSamples); | 943 size_t rand_length = std::min(length - samples_generated, kMaxRandSamples); |
| 941 random_vector_->IncreaseSeedIncrement(seed_increment); | 944 random_vector_->IncreaseSeedIncrement(seed_increment); |
| 942 random_vector_->Generate(rand_length, &random_vector[samples_generated]); | 945 random_vector_->Generate(rand_length, &random_vector[samples_generated]); |
| 943 samples_generated += rand_length; | 946 samples_generated += rand_length; |
| 944 } | 947 } |
| 945 } | 948 } |
| 946 | 949 |
| 947 } // namespace webrtc | 950 } // namespace webrtc |
| OLD | NEW |