niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | a496b03 | 2012-03-20 12:53:06 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/vad/vad_sp.h" |
bjornv@webrtc.org | 2111d3b | 2011-10-14 12:58:34 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
| 14 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 15 | #include "common_audio/vad/vad_core.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 16 | #include "typedefs.h" // NOLINT(build/include) |
bjornv@webrtc.org | 2111d3b | 2011-10-14 12:58:34 +0000 | [diff] [blame] | 17 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 18 | // Allpass filter coefficients, upper and lower, in Q13. |
| 19 | // Upper: 0.64, Lower: 0.17. |
bjornv@webrtc.org | a496b03 | 2012-03-20 12:53:06 +0000 | [diff] [blame] | 20 | static const int16_t kAllPassCoefsQ13[2] = { 5243, 1392 }; // Q13. |
| 21 | static const int16_t kSmoothingDown = 6553; // 0.2 in Q15. |
| 22 | static const int16_t kSmoothingUp = 32439; // 0.99 in Q15. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 24 | // TODO(bjornv): Move this function to vad_filterbank.c. |
| 25 | // Downsampling filter based on splitting filter and allpass functions. |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 26 | void WebRtcVad_Downsampling(const int16_t* signal_in, |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 27 | int16_t* signal_out, |
| 28 | int32_t* filter_state, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 29 | size_t in_length) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 30 | int16_t tmp16_1 = 0, tmp16_2 = 0; |
| 31 | int32_t tmp32_1 = filter_state[0]; |
| 32 | int32_t tmp32_2 = filter_state[1]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 33 | size_t n = 0; |
| 34 | // Downsampling by 2 gives half length. |
| 35 | size_t half_length = (in_length >> 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 37 | // Filter coefficients in Q13, filter state in Q0. |
| 38 | for (n = 0; n < half_length; n++) { |
| 39 | // All-pass filtering upper branch. |
| 40 | tmp16_1 = (int16_t) ((tmp32_1 >> 1) + |
Bjorn Volcker | 3fbf99c | 2015-03-25 14:37:24 +0100 | [diff] [blame] | 41 | ((kAllPassCoefsQ13[0] * *signal_in) >> 14)); |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 42 | *signal_out = tmp16_1; |
Bjorn Volcker | 3fbf99c | 2015-03-25 14:37:24 +0100 | [diff] [blame] | 43 | tmp32_1 = (int32_t)(*signal_in++) - ((kAllPassCoefsQ13[0] * tmp16_1) >> 12); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 45 | // All-pass filtering lower branch. |
| 46 | tmp16_2 = (int16_t) ((tmp32_2 >> 1) + |
Bjorn Volcker | 3fbf99c | 2015-03-25 14:37:24 +0100 | [diff] [blame] | 47 | ((kAllPassCoefsQ13[1] * *signal_in) >> 14)); |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 48 | *signal_out++ += tmp16_2; |
Bjorn Volcker | 3fbf99c | 2015-03-25 14:37:24 +0100 | [diff] [blame] | 49 | tmp32_2 = (int32_t)(*signal_in++) - ((kAllPassCoefsQ13[1] * tmp16_2) >> 12); |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 50 | } |
| 51 | // Store the filter states. |
| 52 | filter_state[0] = tmp32_1; |
| 53 | filter_state[1] = tmp32_2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 56 | // Inserts |feature_value| into |low_value_vector|, if it is one of the 16 |
| 57 | // smallest values the last 100 frames. Then calculates and returns the median |
| 58 | // of the five smallest values. |
| 59 | int16_t WebRtcVad_FindMinimum(VadInstT* self, |
| 60 | int16_t feature_value, |
| 61 | int channel) { |
| 62 | int i = 0, j = 0; |
| 63 | int position = -1; |
| 64 | // Offset to beginning of the 16 minimum values in memory. |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 65 | const int offset = (channel << 4); |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 66 | int16_t current_median = 1600; |
| 67 | int16_t alpha = 0; |
| 68 | int32_t tmp32 = 0; |
| 69 | // Pointer to memory for the 16 minimum values and the age of each value of |
| 70 | // the |channel|. |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 71 | int16_t* age = &self->index_vector[offset]; |
| 72 | int16_t* smallest_values = &self->low_value_vector[offset]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | |
kwiberg | 1e8ed4a | 2016-08-26 04:33:34 -0700 | [diff] [blame] | 74 | RTC_DCHECK_LT(channel, kNumChannels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 76 | // Each value in |smallest_values| is getting 1 loop older. Update |age|, and |
| 77 | // remove old values. |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 78 | for (i = 0; i < 16; i++) { |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 79 | if (age[i] != 100) { |
| 80 | age[i]++; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 81 | } else { |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 82 | // Too old value. Remove from memory and shift larger values downwards. |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 83 | for (j = i; j < 16; j++) { |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 84 | smallest_values[j] = smallest_values[j + 1]; |
| 85 | age[j] = age[j + 1]; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 86 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 87 | age[15] = 101; |
| 88 | smallest_values[15] = 10000; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 92 | // Check if |feature_value| is smaller than any of the values in |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 93 | // |smallest_values|. If so, find the |position| where to insert the new value |
| 94 | // (|feature_value|). |
| 95 | if (feature_value < smallest_values[7]) { |
| 96 | if (feature_value < smallest_values[3]) { |
| 97 | if (feature_value < smallest_values[1]) { |
| 98 | if (feature_value < smallest_values[0]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 99 | position = 0; |
| 100 | } else { |
| 101 | position = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 103 | } else if (feature_value < smallest_values[2]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 104 | position = 2; |
| 105 | } else { |
| 106 | position = 3; |
| 107 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 108 | } else if (feature_value < smallest_values[5]) { |
| 109 | if (feature_value < smallest_values[4]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 110 | position = 4; |
| 111 | } else { |
| 112 | position = 5; |
| 113 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 114 | } else if (feature_value < smallest_values[6]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 115 | position = 6; |
| 116 | } else { |
| 117 | position = 7; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 119 | } else if (feature_value < smallest_values[15]) { |
| 120 | if (feature_value < smallest_values[11]) { |
| 121 | if (feature_value < smallest_values[9]) { |
| 122 | if (feature_value < smallest_values[8]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 123 | position = 8; |
| 124 | } else { |
| 125 | position = 9; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 127 | } else if (feature_value < smallest_values[10]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 128 | position = 10; |
| 129 | } else { |
| 130 | position = 11; |
| 131 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 132 | } else if (feature_value < smallest_values[13]) { |
| 133 | if (feature_value < smallest_values[12]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 134 | position = 12; |
| 135 | } else { |
| 136 | position = 13; |
| 137 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 138 | } else if (feature_value < smallest_values[14]) { |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 139 | position = 14; |
| 140 | } else { |
| 141 | position = 15; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | } |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 143 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 145 | // If we have detected a new small value, insert it at the correct position |
| 146 | // and shift larger values up. |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 147 | if (position > -1) { |
| 148 | for (i = 15; i > position; i--) { |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 149 | smallest_values[i] = smallest_values[i - 1]; |
| 150 | age[i] = age[i - 1]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | } |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 152 | smallest_values[position] = feature_value; |
| 153 | age[position] = 1; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 154 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 156 | // Get |current_median|. |
| 157 | if (self->frame_counter > 2) { |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 158 | current_median = smallest_values[2]; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 159 | } else if (self->frame_counter > 0) { |
bjornv@webrtc.org | eec739f | 2012-06-11 07:57:57 +0000 | [diff] [blame] | 160 | current_median = smallest_values[0]; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Smooth the median value. |
| 164 | if (self->frame_counter > 0) { |
| 165 | if (current_median < self->mean_value[channel]) { |
bjornv@webrtc.org | a496b03 | 2012-03-20 12:53:06 +0000 | [diff] [blame] | 166 | alpha = kSmoothingDown; // 0.2 in Q15. |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 167 | } else { |
bjornv@webrtc.org | a496b03 | 2012-03-20 12:53:06 +0000 | [diff] [blame] | 168 | alpha = kSmoothingUp; // 0.99 in Q15. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | } |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 170 | } |
bjornv@webrtc.org | d25c034 | 2015-01-26 15:32:47 +0000 | [diff] [blame] | 171 | tmp32 = (alpha + 1) * self->mean_value[channel]; |
| 172 | tmp32 += (WEBRTC_SPL_WORD16_MAX - alpha) * current_median; |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 173 | tmp32 += 16384; |
| 174 | self->mean_value[channel] = (int16_t) (tmp32 >> 15); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | |
bjornv@webrtc.org | 226c5a1 | 2012-01-04 09:15:12 +0000 | [diff] [blame] | 176 | return self->mean_value[channel]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 177 | } |