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

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

Powered by Google App Engine
This is Rietveld 408576698