| 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/common_audio/vad/vad_core.h" | 11 #include "webrtc/common_audio/vad/vad_core.h" |
| 12 | 12 |
| 13 #include "webrtc/base/sanitizer.h" |
| 13 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" | 14 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar
y.h" |
| 14 #include "webrtc/common_audio/vad/vad_filterbank.h" | 15 #include "webrtc/common_audio/vad/vad_filterbank.h" |
| 15 #include "webrtc/common_audio/vad/vad_gmm.h" | 16 #include "webrtc/common_audio/vad/vad_gmm.h" |
| 16 #include "webrtc/common_audio/vad/vad_sp.h" | 17 #include "webrtc/common_audio/vad/vad_sp.h" |
| 17 #include "webrtc/typedefs.h" | 18 #include "webrtc/typedefs.h" |
| 18 | 19 |
| 19 // Spectrum Weighting | 20 // Spectrum Weighting |
| 20 static const int16_t kSpectrumWeight[kNumChannels] = { 6, 8, 10, 12, 14, 16 }; | 21 static const int16_t kSpectrumWeight[kNumChannels] = { 6, 8, 10, 12, 14, 16 }; |
| 21 static const int16_t kNoiseUpdateConst = 655; // Q15 | 22 static const int16_t kNoiseUpdateConst = 655; // Q15 |
| 22 static const int16_t kSpeechUpdateConst = 6554; // Q15 | 23 static const int16_t kSpeechUpdateConst = 6554; // Q15 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 int k; | 104 int k; |
| 104 int32_t weighted_average = 0; | 105 int32_t weighted_average = 0; |
| 105 | 106 |
| 106 for (k = 0; k < kNumGaussians; k++) { | 107 for (k = 0; k < kNumGaussians; k++) { |
| 107 data[k * kNumChannels] += offset; | 108 data[k * kNumChannels] += offset; |
| 108 weighted_average += data[k * kNumChannels] * weights[k * kNumChannels]; | 109 weighted_average += data[k * kNumChannels] * weights[k * kNumChannels]; |
| 109 } | 110 } |
| 110 return weighted_average; | 111 return weighted_average; |
| 111 } | 112 } |
| 112 | 113 |
| 114 // An s16 x s32 -> s32 multiplication that's allowed to overflow. (It's still |
| 115 // undefined behavior, so not a good idea; this just makes UBSan ignore the |
| 116 // violation, so that our old code can continue to do what it's always been |
| 117 // doing.) |
| 118 static inline int32_t OverflowingMulS16ByS32ToS32(int16_t a, int32_t b) |
| 119 RTC_NO_SANITIZE("signed-integer-overflow") { |
| 120 return a * b; |
| 121 } |
| 122 |
| 113 // Calculates the probabilities for both speech and background noise using | 123 // Calculates the probabilities for both speech and background noise using |
| 114 // Gaussian Mixture Models (GMM). A hypothesis-test is performed to decide which | 124 // Gaussian Mixture Models (GMM). A hypothesis-test is performed to decide which |
| 115 // type of signal is most probable. | 125 // type of signal is most probable. |
| 116 // | 126 // |
| 117 // - self [i/o] : Pointer to VAD instance | 127 // - self [i/o] : Pointer to VAD instance |
| 118 // - features [i] : Feature vector of length |kNumChannels| | 128 // - features [i] : Feature vector of length |kNumChannels| |
| 119 // = log10(energy in frequency band) | 129 // = log10(energy in frequency band) |
| 120 // - total_power [i] : Total power in audio frame. | 130 // - total_power [i] : Total power in audio frame. |
| 121 // - frame_length [i] : Number of input samples | 131 // - frame_length [i] : Number of input samples |
| 122 // | 132 // |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 // Update GMM variance vectors. | 381 // Update GMM variance vectors. |
| 372 // deltaN * (features[channel] - nmk) - 1 | 382 // deltaN * (features[channel] - nmk) - 1 |
| 373 // Q4 - (Q7 >> 3) = Q4. | 383 // Q4 - (Q7 >> 3) = Q4. |
| 374 tmp_s16 = features[channel] - (nmk >> 3); | 384 tmp_s16 = features[channel] - (nmk >> 3); |
| 375 // (Q11 * Q4 >> 3) = Q12. | 385 // (Q11 * Q4 >> 3) = Q12. |
| 376 tmp1_s32 = (deltaN[gaussian] * tmp_s16) >> 3; | 386 tmp1_s32 = (deltaN[gaussian] * tmp_s16) >> 3; |
| 377 tmp1_s32 -= 4096; | 387 tmp1_s32 -= 4096; |
| 378 | 388 |
| 379 // (Q14 >> 2) * Q12 = Q24. | 389 // (Q14 >> 2) * Q12 = Q24. |
| 380 tmp_s16 = (ngprvec[gaussian] + 2) >> 2; | 390 tmp_s16 = (ngprvec[gaussian] + 2) >> 2; |
| 381 tmp2_s32 = tmp_s16 * tmp1_s32; | 391 tmp2_s32 = OverflowingMulS16ByS32ToS32(tmp_s16, tmp1_s32); |
| 382 // Q20 * approx 0.001 (2^-10=0.0009766), hence, | 392 // Q20 * approx 0.001 (2^-10=0.0009766), hence, |
| 383 // (Q24 >> 14) = (Q24 >> 4) / 2^10 = Q20. | 393 // (Q24 >> 14) = (Q24 >> 4) / 2^10 = Q20. |
| 384 tmp1_s32 = tmp2_s32 >> 14; | 394 tmp1_s32 = tmp2_s32 >> 14; |
| 385 | 395 |
| 386 // Q20 / Q7 = Q13. | 396 // Q20 / Q7 = Q13. |
| 387 if (tmp1_s32 > 0) { | 397 if (tmp1_s32 > 0) { |
| 388 tmp_s16 = (int16_t) WebRtcSpl_DivW32W16(tmp1_s32, nsk); | 398 tmp_s16 = (int16_t) WebRtcSpl_DivW32W16(tmp1_s32, nsk); |
| 389 } else { | 399 } else { |
| 390 tmp_s16 = (int16_t) WebRtcSpl_DivW32W16(-tmp1_s32, nsk); | 400 tmp_s16 = (int16_t) WebRtcSpl_DivW32W16(-tmp1_s32, nsk); |
| 391 tmp_s16 = -tmp_s16; | 401 tmp_s16 = -tmp_s16; |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 | 677 |
| 668 // Get power in the bands | 678 // Get power in the bands |
| 669 total_power = WebRtcVad_CalculateFeatures(inst, speech_frame, frame_length, | 679 total_power = WebRtcVad_CalculateFeatures(inst, speech_frame, frame_length, |
| 670 feature_vector); | 680 feature_vector); |
| 671 | 681 |
| 672 // Make a VAD | 682 // Make a VAD |
| 673 inst->vad = GmmProbability(inst, feature_vector, total_power, frame_length); | 683 inst->vad = GmmProbability(inst, feature_vector, total_power, frame_length); |
| 674 | 684 |
| 675 return inst->vad; | 685 return inst->vad; |
| 676 } | 686 } |
| OLD | NEW |