Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: webrtc/modules/audio_processing/repetition_detector_unittest.cc

Issue 1287663002: Adding audio RepetitionDetector in AudioProcessingModule. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: restrict to float Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2015 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 <map>
12
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/scoped_ptr.h"
15 #include "webrtc/modules/audio_processing/repetition_detector.h"
16
17 namespace webrtc {
18
19 class RepetitionDetectorForTest : public RepetitionDetector {
20 public:
21 int GetCount(int id) {
22 auto it = counters_.find(id);
23 if (it == counters_.end()) {
24 return 0;
25 }
26 return counters_[id];
27 }
28
29 void ResetCounters() {
30 for (auto& item : counters_) {
31 item.second = 0;
32 }
33 }
34
35 void ResetRepetitionPattern(const RepetitionDetector::Pattern* patterns,
36 size_t num_patterns) {
37 states_.clear();
38 RegisterRepetitionPatterns(patterns, num_patterns);
39 }
40
41 protected:
42 void ReportRepetition(int id) override {
43 auto it = counters_.find(id);
44 if (it == counters_.end()) {
45 counters_[id] = 0;
46 }
47 counters_[id]++;
48 }
49
50 private:
51 std::map<int, size_t> counters_;
52 };
53
54 class RepetitionDetectorTest : public ::testing::Test {
55 protected:
56 struct ExpectedCount {
57 int id_;
58 int count_;
59 };
60
61 // Verify if the counts on the repetition patterns match expectation after
62 // injecting a signal. No reset on the counters
63 void Verify(const ExpectedCount* expected_counts, size_t num_patterns,
64 const float* tester, size_t num_frames,
65 int sample_rate_hz, size_t channels = 1) {
66 detector.Detect(tester, num_frames, channels, sample_rate_hz);
67 int id;
68 for (size_t idx = 0; idx < num_patterns; idx++) {
69 id = expected_counts[idx].id_;
70 EXPECT_EQ(expected_counts[idx].count_, detector.GetCount(id)) <<
71 "Repetition #" << id << " counted wrong.";
72 }
73 }
74
75 void VerifyStereo(const ExpectedCount* expected_counts, size_t num_patterns,
76 const float* tester, size_t num_frames,
77 int sample_rate_hz) {
78 const size_t kNumChannels = 2;
79
80 // Get memory to store interleaved stereo.
81 rtc::scoped_ptr<float[]> tester_stereo(
82 new float[num_frames * kNumChannels]);
83
84 for (size_t idx = 0; idx < num_frames; ++idx, ++tester) {
85 for (size_t channel = 0; channel < kNumChannels; ++channel) {
86 tester_stereo[idx * kNumChannels + channel] = *tester;
87 }
88 }
89
90 Verify(expected_counts, num_patterns, tester_stereo.get(),
91 num_frames, sample_rate_hz, kNumChannels);
92 }
93
94 void ResetRepetitionPattern(const RepetitionDetector::Pattern* patterns,
95 size_t num_patterns) {
96 detector.ResetRepetitionPattern(patterns, num_patterns);
97 }
98 void ResetCounters() {
99 detector.ResetCounters();
100 }
101
102 private:
103 RepetitionDetectorForTest detector;
104 };
105
106 TEST_F(RepetitionDetectorTest, Basic) {
107 // To make the test signal most obvious, we choose a special sample rate.
108 const int kSampleRateHz = 1000;
109
110 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
111 // id, look_back_ms, min_length_ms
112 {0, 3, 3}
113 };
114 const float kTestSignal[] = {1, 2, 3, 1, 2, 3};
115 const ExpectedCount kExpectedCounts_1[] = {
116 {0, 1}
117 };
118 const ExpectedCount kExpectedCounts_2[] = {
119 {0, 1}
120 };
121
122 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
123 sizeof(RepetitionDetector::Pattern));
124 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount),
125 kTestSignal, sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
126 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount),
127 kTestSignal, sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
128 ResetCounters();
129
130 VerifyStereo(kExpectedCounts_1,
131 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal,
132 sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
133 VerifyStereo(kExpectedCounts_2,
134 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal,
135 sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
136 }
137
138 TEST_F(RepetitionDetectorTest, StereoOutOfSync) {
139 // To make the test signal most obvious, we choose a special sample rate.
140 const int kSampleRateHz = 1000;
141
142 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
143 // id, look_back_ms, min_length_ms
144 {0, 3, 3}
145 };
146 const float kTestSignal[] = {
147 1, 1,
148 2, 2,
149 3, 3,
150 1, 1,
151 2, 2,
152 3, 1};
153 const ExpectedCount kExpectedCounts[] = {
154 {0, 0}
155 };
156
157 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
158 sizeof(RepetitionDetector::Pattern));
159 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount),
160 kTestSignal, sizeof(kTestSignal) / sizeof(float) / 2,
161 kSampleRateHz, 2);
162 }
163
164 TEST_F(RepetitionDetectorTest, IncompletePattern) {
165 // To make the test signal most obvious, we choose a special sample rate.
166 const int kSampleRateHz = 1000;
167
168 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
169 // id, look_back_ms, min_length_ms
170 {0, 3, 3},
171 };
172 const float kTestSignal[] = {1, 2, 1, 2, 3, 1, 2, 3};
173 const ExpectedCount kExpectedCounts[] = {
174 {0, 1},
175 };
176
177 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
178 sizeof(RepetitionDetector::Pattern));
179 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount),
180 kTestSignal, sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
181 ResetCounters();
182 VerifyStereo(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount),
183 kTestSignal, sizeof(kTestSignal) / sizeof(float),
184 kSampleRateHz);
185 }
186
187 TEST_F(RepetitionDetectorTest, PatternLongerThanFrame) {
188 // To make the test signal most obvious, we choose a special sample rate.
189 const int kSampleRateHz = 1000;
190
191 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
192 // id, look_back_ms, min_length_ms
193 {0, 6, 6},
194 };
195 const float kTestSignal_1[] = {1, 2, 3, 4, 5};
196 const float kTestSignal_2[] = {6, 1, 2, 3, 4, 5, 6};
197 const ExpectedCount kExpectedCounts_1[] = {
198 {0, 0},
199 };
200 const ExpectedCount kExpectedCounts_2[] = {
201 {0, 1},
202 };
203
204 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
205 sizeof(RepetitionDetector::Pattern));
206 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount),
207 kTestSignal_1, sizeof(kTestSignal_1) / sizeof(float),
208 kSampleRateHz);
209 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount),
210 kTestSignal_2, sizeof(kTestSignal_2) / sizeof(float),
211 kSampleRateHz);
212 ResetCounters();
213 VerifyStereo(kExpectedCounts_1,
214 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal_1,
215 sizeof(kTestSignal_1) / sizeof(float), kSampleRateHz);
216 VerifyStereo(kExpectedCounts_2,
217 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal_2,
218 sizeof(kTestSignal_2) / sizeof(float), kSampleRateHz);
219 }
220
221 TEST_F(RepetitionDetectorTest, TwoPatterns) {
222 // To make the test signal most obvious, we choose a special sample rate.
223 const int kSampleRateHz = 1000;
224
225 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
226 // id, look_back_ms, min_length_ms
227 {0, 3, 3},
228 {1, 4, 4},
229 };
230 const float kTestSignal[] = {1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4};
231 const ExpectedCount kExpectedCounts[] = {
232 // 1,2,3 belongs to both patterns.
233 {0, 1},
234 {1, 1}
235 };
236
237 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
238 sizeof(RepetitionDetector::Pattern));
239 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount),
240 kTestSignal, sizeof(kTestSignal) / sizeof(float),
241 kSampleRateHz);
242 ResetCounters();
243 VerifyStereo(kExpectedCounts,
244 sizeof(kExpectedCounts) / sizeof(ExpectedCount), kTestSignal,
245 sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
246 }
247
248 TEST_F(RepetitionDetectorTest, NestedPatterns) {
249 // To make the test signal most obvious, we choose a special sample rate.
250 const int kSampleRateHz = 1000;
251
252 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
253 // id, look_back_ms, min_length_ms
254 {0, 3, 3},
255 {1, 6, 6}, // When a triplet repeated 3 times, this is triggered.
256 };
257 const float kTestSignal[] = {1, 2, 3, 1, 2, 3};
258 const ExpectedCount kExpectedCounts_1[] = {
259 {0, 1},
260 {1, 0}
261 };
262 const ExpectedCount kExpectedCounts_2[] = {
263 {0, 1},
264 {1, 1}
265 };
266
267 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
268 sizeof(RepetitionDetector::Pattern));
269 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount),
270 kTestSignal, sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
271 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount),
272 kTestSignal, sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
273 ResetCounters();
274 VerifyStereo(kExpectedCounts_1,
275 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal,
276 sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
277 VerifyStereo(kExpectedCounts_2,
278 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal,
279 sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
280 }
281
282 TEST_F(RepetitionDetectorTest, NotFullLengthPattern) {
283 // To make the test signal most obvious, we choose a special sample rate.
284 const int kSampleRateHz = 1000;
285
286 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
287 // id, look_back_ms, min_length_ms
288 {0, 4, 3},
289 };
290 const float kTestSignal[] = {1, 2, 3, -1, 1, 2, 3, -2};
291 const ExpectedCount kExpectedCounts[] = {
292 {0, 1},
293 };
294
295 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
296 sizeof(RepetitionDetector::Pattern));
297 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount),
298 kTestSignal, sizeof(kTestSignal) / sizeof(float), kSampleRateHz);
299 ResetCounters();
300 VerifyStereo(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount),
301 kTestSignal, sizeof(kTestSignal) / sizeof(float),
302 kSampleRateHz);
303 }
304
305 TEST_F(RepetitionDetectorTest, ZerosCountOrNot) {
306 // To make the test signal most obvious, we choose a special sample rate.
307 const int kSampleRateHz = 1000;
308
309 const RepetitionDetector::Pattern kRepetitionPatterns[] = {
310 // id, look_back_ms, min_length_ms
311 {0, 3, 3},
312 };
313 const float kTestSignal_1[] = {0, 0, 0, 0, 0, 0};
314 const float kTestSignal_2[] = {0, 1, 2, 0, 1, 2};
315 const ExpectedCount kExpectedCounts_1[] = {
316 // Full zeros won't count.
317 {0, 0},
318 };
319 const ExpectedCount kExpectedCounts_2[] = {
320 // Partial zero will count.
321 {0, 1},
322 };
323
324 ResetRepetitionPattern(kRepetitionPatterns, sizeof(kRepetitionPatterns) /
325 sizeof(RepetitionDetector::Pattern));
326 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount),
327 kTestSignal_1, sizeof(kTestSignal_1) / sizeof(float),
328 kSampleRateHz);
329 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount),
330 kTestSignal_2, sizeof(kTestSignal_2) / sizeof(float),
331 kSampleRateHz);
332 ResetCounters();
333 VerifyStereo(kExpectedCounts_1,
334 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal_1,
335 sizeof(kTestSignal_1) / sizeof(float), kSampleRateHz);
336 VerifyStereo(kExpectedCounts_2,
337 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal_2,
338 sizeof(kTestSignal_2) / sizeof(float), kSampleRateHz);
339 }
340
341 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698