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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 // Shift 14 with proper rounding. | 123 // Shift 14 with proper rounding. |
124 (*output)[channel_ix][i] = | 124 (*output)[channel_ix][i] = |
125 static_cast<int16_t>((scaled_signal + 8192) >> 14); | 125 static_cast<int16_t>((scaled_signal + 8192) >> 14); |
126 // Increase mute_factor towards 16384. | 126 // Increase mute_factor towards 16384. |
127 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( | 127 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( |
128 external_mute_factor_array[channel_ix] + increment, 16384)); | 128 external_mute_factor_array[channel_ix] + increment, 16384)); |
129 } | 129 } |
130 | 130 |
131 // Interpolate the expanded data into the new vector. | 131 // Interpolate the expanded data into the new vector. |
132 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) | 132 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
133 RTC_DCHECK_LT(fs_shift, 3); // Will always be 0, 1, or, 2. | 133 size_t win_length = samples_per_ms_; |
134 increment = 4 >> fs_shift; | 134 int16_t win_slope_Q14 = default_win_slope_Q14_; |
135 int fraction = increment; | 135 RTC_DCHECK_LT(channel_ix, output->Channels()); |
136 // Don't interpolate over more samples than what is in output. When this | 136 if (win_length > output->Size()) { |
137 // cap strikes, the interpolation will likely sound worse, but this is an | 137 win_length = output->Size(); |
138 // emergency operation in response to unexpected input. | 138 win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length); |
139 const size_t interp_len_samples = | 139 } |
140 std::min(static_cast<size_t>(8 * fs_mult), output->Size()); | 140 int16_t win_up_Q14 = 0; |
141 for (size_t i = 0; i < interp_len_samples; ++i) { | 141 for (size_t i = 0; i < win_length; i++) { |
142 // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 | 142 win_up_Q14 += win_slope_Q14; |
143 // now for legacy bit-exactness. | |
144 RTC_DCHECK_LT(channel_ix, output->Channels()); | |
145 RTC_DCHECK_LT(i, output->Size()); | |
146 (*output)[channel_ix][i] = | 143 (*output)[channel_ix][i] = |
147 static_cast<int16_t>((fraction * (*output)[channel_ix][i] + | 144 (win_up_Q14 * (*output)[channel_ix][i] + |
148 (32 - fraction) * expanded[channel_ix][i] + 8) >> 5); | 145 ((1 << 14) - win_up_Q14) * expanded[channel_ix][i] + (1 << 13)) >> |
149 fraction += increment; | 146 14; |
| 147 } |
| 148 if (fs_hz_ == 48000) { |
| 149 RTC_DCHECK_EQ(win_up_Q14, (1 << 14) - 16); |
| 150 } else { |
| 151 RTC_DCHECK_EQ(win_up_Q14, 1 << 14); |
150 } | 152 } |
151 } | 153 } |
152 } else if (last_mode == kModeRfc3389Cng) { | 154 } else if (last_mode == kModeRfc3389Cng) { |
153 RTC_DCHECK_EQ(output->Channels(), 1); // Not adapted for multi-channel yet. | 155 RTC_DCHECK_EQ(output->Channels(), 1); // Not adapted for multi-channel yet. |
154 static const size_t kCngLength = 48; | 156 static const size_t kCngLength = 48; |
155 RTC_DCHECK_LE(8 * fs_mult, kCngLength); | 157 RTC_DCHECK_LE(8 * fs_mult, kCngLength); |
156 int16_t cng_output[kCngLength]; | 158 int16_t cng_output[kCngLength]; |
157 // Reset mute factor and start up fresh. | 159 // Reset mute factor and start up fresh. |
158 external_mute_factor_array[0] = 16384; | 160 external_mute_factor_array[0] = 16384; |
159 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); | 161 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
160 | 162 |
161 if (cng_decoder) { | 163 if (cng_decoder) { |
162 // Generate long enough for 48kHz. | 164 // Generate long enough for 48kHz. |
163 if (!cng_decoder->Generate(cng_output, 0)) { | 165 if (!cng_decoder->Generate(cng_output, 0)) { |
164 // Error returned; set return vector to all zeros. | 166 // Error returned; set return vector to all zeros. |
165 memset(cng_output, 0, sizeof(cng_output)); | 167 memset(cng_output, 0, sizeof(cng_output)); |
166 } | 168 } |
167 } else { | 169 } else { |
168 // If no CNG instance is defined, just copy from the decoded data. | 170 // If no CNG instance is defined, just copy from the decoded data. |
169 // (This will result in interpolating the decoded with itself.) | 171 // (This will result in interpolating the decoded with itself.) |
170 (*output)[0].CopyTo(fs_mult * 8, 0, cng_output); | 172 (*output)[0].CopyTo(fs_mult * 8, 0, cng_output); |
171 } | 173 } |
172 // Interpolate the CNG into the new vector. | 174 // Interpolate the CNG into the new vector. |
173 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) | 175 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
174 RTC_DCHECK_LT(fs_shift, 3); // Will always be 0, 1, or, 2. | 176 size_t win_length = samples_per_ms_; |
175 int16_t increment = 4 >> fs_shift; | 177 int16_t win_slope_Q14 = default_win_slope_Q14_; |
176 int16_t fraction = increment; | 178 if (win_length > kCngLength) { |
177 for (size_t i = 0; i < static_cast<size_t>(8 * fs_mult); i++) { | 179 win_length = kCngLength; |
178 // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 now | 180 win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length); |
179 // for legacy bit-exactness. | 181 } |
180 (*output)[0][i] = (fraction * (*output)[0][i] + | 182 int16_t win_up_Q14 = 0; |
181 (32 - fraction) * cng_output[i] + 8) >> 5; | 183 for (size_t i = 0; i < win_length; i++) { |
182 fraction += increment; | 184 win_up_Q14 += win_slope_Q14; |
| 185 (*output)[0][i] = |
| 186 (win_up_Q14 * (*output)[0][i] + |
| 187 ((1 << 14) - win_up_Q14) * cng_output[i] + (1 << 13)) >> |
| 188 14; |
| 189 } |
| 190 if (fs_hz_ == 48000) { |
| 191 RTC_DCHECK_EQ(win_up_Q14, (1 << 14) - 16); |
| 192 } else { |
| 193 RTC_DCHECK_EQ(win_up_Q14, 1 << 14); |
183 } | 194 } |
184 } else if (external_mute_factor_array[0] < 16384) { | 195 } else if (external_mute_factor_array[0] < 16384) { |
185 // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are | 196 // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are |
186 // still ramping up from previous muting. | 197 // still ramping up from previous muting. |
187 // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14). | 198 // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14). |
188 int increment = 64 / fs_mult; | 199 int increment = 64 / fs_mult; |
189 size_t length_per_channel = length / output->Channels(); | 200 size_t length_per_channel = length / output->Channels(); |
190 for (size_t i = 0; i < length_per_channel; i++) { | 201 for (size_t i = 0; i < length_per_channel; i++) { |
191 for (size_t channel_ix = 0; channel_ix < output->Channels(); | 202 for (size_t channel_ix = 0; channel_ix < output->Channels(); |
192 ++channel_ix) { | 203 ++channel_ix) { |
193 // Scale with mute factor. | 204 // Scale with mute factor. |
194 RTC_DCHECK_LT(channel_ix, output->Channels()); | 205 RTC_DCHECK_LT(channel_ix, output->Channels()); |
195 RTC_DCHECK_LT(i, output->Size()); | 206 RTC_DCHECK_LT(i, output->Size()); |
196 int32_t scaled_signal = (*output)[channel_ix][i] * | 207 int32_t scaled_signal = (*output)[channel_ix][i] * |
197 external_mute_factor_array[channel_ix]; | 208 external_mute_factor_array[channel_ix]; |
198 // Shift 14 with proper rounding. | 209 // Shift 14 with proper rounding. |
199 (*output)[channel_ix][i] = | 210 (*output)[channel_ix][i] = |
200 static_cast<int16_t>((scaled_signal + 8192) >> 14); | 211 static_cast<int16_t>((scaled_signal + 8192) >> 14); |
201 // Increase mute_factor towards 16384. | 212 // Increase mute_factor towards 16384. |
202 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( | 213 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( |
203 16384, external_mute_factor_array[channel_ix] + increment)); | 214 16384, external_mute_factor_array[channel_ix] + increment)); |
204 } | 215 } |
205 } | 216 } |
206 } | 217 } |
207 | 218 |
208 return static_cast<int>(length); | 219 return static_cast<int>(length); |
209 } | 220 } |
210 | 221 |
211 } // namespace webrtc | 222 } // namespace webrtc |
OLD | NEW |