Line data Source code
1 : /*
2 : * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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 :
11 : #include "webrtc/common_audio/vad/vad_sp.h"
12 :
13 : #include "webrtc/base/checks.h"
14 : #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
15 : #include "webrtc/common_audio/vad/vad_core.h"
16 : #include "webrtc/typedefs.h"
17 :
18 : // Allpass filter coefficients, upper and lower, in Q13.
19 : // Upper: 0.64, Lower: 0.17.
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.
23 :
24 : // TODO(bjornv): Move this function to vad_filterbank.c.
25 : // Downsampling filter based on splitting filter and allpass functions.
26 0 : void WebRtcVad_Downsampling(const int16_t* signal_in,
27 : int16_t* signal_out,
28 : int32_t* filter_state,
29 : size_t in_length) {
30 0 : int16_t tmp16_1 = 0, tmp16_2 = 0;
31 0 : int32_t tmp32_1 = filter_state[0];
32 0 : int32_t tmp32_2 = filter_state[1];
33 0 : size_t n = 0;
34 : // Downsampling by 2 gives half length.
35 0 : size_t half_length = (in_length >> 1);
36 :
37 : // Filter coefficients in Q13, filter state in Q0.
38 0 : for (n = 0; n < half_length; n++) {
39 : // All-pass filtering upper branch.
40 0 : tmp16_1 = (int16_t) ((tmp32_1 >> 1) +
41 0 : ((kAllPassCoefsQ13[0] * *signal_in) >> 14));
42 0 : *signal_out = tmp16_1;
43 0 : tmp32_1 = (int32_t)(*signal_in++) - ((kAllPassCoefsQ13[0] * tmp16_1) >> 12);
44 :
45 : // All-pass filtering lower branch.
46 0 : tmp16_2 = (int16_t) ((tmp32_2 >> 1) +
47 0 : ((kAllPassCoefsQ13[1] * *signal_in) >> 14));
48 0 : *signal_out++ += tmp16_2;
49 0 : tmp32_2 = (int32_t)(*signal_in++) - ((kAllPassCoefsQ13[1] * tmp16_2) >> 12);
50 : }
51 : // Store the filter states.
52 0 : filter_state[0] = tmp32_1;
53 0 : filter_state[1] = tmp32_2;
54 0 : }
55 :
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 0 : int16_t WebRtcVad_FindMinimum(VadInstT* self,
60 : int16_t feature_value,
61 : int channel) {
62 0 : int i = 0, j = 0;
63 0 : int position = -1;
64 : // Offset to beginning of the 16 minimum values in memory.
65 0 : const int offset = (channel << 4);
66 0 : int16_t current_median = 1600;
67 0 : int16_t alpha = 0;
68 0 : int32_t tmp32 = 0;
69 : // Pointer to memory for the 16 minimum values and the age of each value of
70 : // the |channel|.
71 0 : int16_t* age = &self->index_vector[offset];
72 0 : int16_t* smallest_values = &self->low_value_vector[offset];
73 :
74 0 : RTC_DCHECK_LT(channel, kNumChannels);
75 :
76 : // Each value in |smallest_values| is getting 1 loop older. Update |age|, and
77 : // remove old values.
78 0 : for (i = 0; i < 16; i++) {
79 0 : if (age[i] != 100) {
80 0 : age[i]++;
81 : } else {
82 : // Too old value. Remove from memory and shift larger values downwards.
83 0 : for (j = i; j < 16; j++) {
84 0 : smallest_values[j] = smallest_values[j + 1];
85 0 : age[j] = age[j + 1];
86 : }
87 0 : age[15] = 101;
88 0 : smallest_values[15] = 10000;
89 : }
90 : }
91 :
92 : // Check if |feature_value| is smaller than any of the values in
93 : // |smallest_values|. If so, find the |position| where to insert the new value
94 : // (|feature_value|).
95 0 : if (feature_value < smallest_values[7]) {
96 0 : if (feature_value < smallest_values[3]) {
97 0 : if (feature_value < smallest_values[1]) {
98 0 : if (feature_value < smallest_values[0]) {
99 0 : position = 0;
100 : } else {
101 0 : position = 1;
102 : }
103 0 : } else if (feature_value < smallest_values[2]) {
104 0 : position = 2;
105 : } else {
106 0 : position = 3;
107 : }
108 0 : } else if (feature_value < smallest_values[5]) {
109 0 : if (feature_value < smallest_values[4]) {
110 0 : position = 4;
111 : } else {
112 0 : position = 5;
113 : }
114 0 : } else if (feature_value < smallest_values[6]) {
115 0 : position = 6;
116 : } else {
117 0 : position = 7;
118 : }
119 0 : } else if (feature_value < smallest_values[15]) {
120 0 : if (feature_value < smallest_values[11]) {
121 0 : if (feature_value < smallest_values[9]) {
122 0 : if (feature_value < smallest_values[8]) {
123 0 : position = 8;
124 : } else {
125 0 : position = 9;
126 : }
127 0 : } else if (feature_value < smallest_values[10]) {
128 0 : position = 10;
129 : } else {
130 0 : position = 11;
131 : }
132 0 : } else if (feature_value < smallest_values[13]) {
133 0 : if (feature_value < smallest_values[12]) {
134 0 : position = 12;
135 : } else {
136 0 : position = 13;
137 : }
138 0 : } else if (feature_value < smallest_values[14]) {
139 0 : position = 14;
140 : } else {
141 0 : position = 15;
142 : }
143 : }
144 :
145 : // If we have detected a new small value, insert it at the correct position
146 : // and shift larger values up.
147 0 : if (position > -1) {
148 0 : for (i = 15; i > position; i--) {
149 0 : smallest_values[i] = smallest_values[i - 1];
150 0 : age[i] = age[i - 1];
151 : }
152 0 : smallest_values[position] = feature_value;
153 0 : age[position] = 1;
154 : }
155 :
156 : // Get |current_median|.
157 0 : if (self->frame_counter > 2) {
158 0 : current_median = smallest_values[2];
159 0 : } else if (self->frame_counter > 0) {
160 0 : current_median = smallest_values[0];
161 : }
162 :
163 : // Smooth the median value.
164 0 : if (self->frame_counter > 0) {
165 0 : if (current_median < self->mean_value[channel]) {
166 0 : alpha = kSmoothingDown; // 0.2 in Q15.
167 : } else {
168 0 : alpha = kSmoothingUp; // 0.99 in Q15.
169 : }
170 : }
171 0 : tmp32 = (alpha + 1) * self->mean_value[channel];
172 0 : tmp32 += (WEBRTC_SPL_WORD16_MAX - alpha) * current_median;
173 0 : tmp32 += 16384;
174 0 : self->mean_value[channel] = (int16_t) (tmp32 >> 15);
175 :
176 0 : return self->mean_value[channel];
177 : }
|