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