OLD | NEW |
(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, |
| 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, int num_patterns) { |
| 28 ClearRepititionPatterns(); |
| 29 RegisterRepititionPatterns(patterns, num_patterns); |
| 30 } |
| 31 |
| 32 // Verify if the counts on the repetition patterns match expectation after |
| 33 // injecting a signal. No reset on the counters |
| 34 void Verify(const ExpectedCount* expected_counts, int num_patterns, |
| 35 const int16_t* tester, int samples_per_channel, |
| 36 int channels = 1) { |
| 37 Detect(tester, samples_per_channel, channels); |
| 38 int id; |
| 39 for (int idx = 0; idx < num_patterns; idx++) { |
| 40 id = expected_counts[idx].id_; |
| 41 EXPECT_EQ(expected_counts[idx].count_, GetCount(id)) << |
| 42 "Repetition #" << id << " counted wrong."; |
| 43 } |
| 44 } |
| 45 |
| 46 void VerifyStereo(const ExpectedCount* expected_counts, int num_patterns, |
| 47 const int16_t* tester, int samples_per_channel) { |
| 48 int16_t* tester_stereo = new int16_t[samples_per_channel * 2]; |
| 49 for (int idx = 0; idx < samples_per_channel; idx++) { |
| 50 tester_stereo[idx * 2] = tester_stereo[idx * 2 + 1] = tester[idx]; |
| 51 } |
| 52 Verify(expected_counts, num_patterns, tester_stereo, |
| 53 samples_per_channel, 2); |
| 54 delete[] tester_stereo; |
| 55 } |
| 56 |
| 57 void ReportRepetition(int id) override { |
| 58 auto it = counters_.find(id); |
| 59 if (it == counters_.end()) { |
| 60 counters_[id] = 0; |
| 61 } |
| 62 counters_[id]++; |
| 63 } |
| 64 |
| 65 int GetCount(int id) { |
| 66 auto it = counters_.find(id); |
| 67 if (it == counters_.end()) { |
| 68 return 0; |
| 69 } |
| 70 return counters_[id]; |
| 71 } |
| 72 |
| 73 void ResetCounters() { |
| 74 for (auto& item : counters_) { |
| 75 item.second = 0; |
| 76 } |
| 77 } |
| 78 |
| 79 private: |
| 80 std::map<int, int> counters_; |
| 81 }; |
| 82 |
| 83 TEST_F(RepetitionDetectorTest, Basic) { |
| 84 // 123456123456 look_back=6, loop_back_range=0, length=6, length_range=0 |
| 85 const Pattern kRepetitionPatterns[] = { |
| 86 // id, look_back, loop_back_range, length, length_range |
| 87 {0, 3, 0, 3, 0} |
| 88 }; |
| 89 const int16_t kTestSignal[] = {1, 2, 3, 1, 2, 3}; |
| 90 const ExpectedCount kExpectedCounts_1[] = { |
| 91 {0, 1} |
| 92 }; |
| 93 const ExpectedCount kExpectedCounts_2[] = { |
| 94 {0, 3} |
| 95 }; |
| 96 |
| 97 ResetRepetitionPattern(kRepetitionPatterns, |
| 98 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 99 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), |
| 100 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 101 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), |
| 102 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 103 ResetCounters(); |
| 104 VerifyStereo(kExpectedCounts_1, |
| 105 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal, |
| 106 sizeof(kTestSignal) / sizeof(int16_t)); |
| 107 VerifyStereo(kExpectedCounts_2, |
| 108 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal, |
| 109 sizeof(kTestSignal) / sizeof(int16_t)); |
| 110 } |
| 111 |
| 112 TEST_F(RepetitionDetectorTest, StereoOutOfSync) { |
| 113 // 123456123456 look_back=6, loop_back_range=0, length=6, length_range=0 |
| 114 const Pattern kRepetitionPatterns[] = { |
| 115 // id, look_back, loop_back_range, length, length_range |
| 116 {0, 3, 0, 3, 0} |
| 117 }; |
| 118 const int16_t kTestSignal[] = { |
| 119 1, 1, |
| 120 2, 2, |
| 121 3, 3, |
| 122 1, 1, |
| 123 2, 2, |
| 124 3, 1}; |
| 125 const ExpectedCount kExpectedCounts[] = { |
| 126 {0, 0} |
| 127 }; |
| 128 |
| 129 ResetRepetitionPattern(kRepetitionPatterns, |
| 130 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 131 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount), |
| 132 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t) / 2, 2); |
| 133 } |
| 134 |
| 135 TEST_F(RepetitionDetectorTest, IncompletePattern) { |
| 136 const Pattern kRepetitionPatterns[] = { |
| 137 // id, look_back, loop_back_range, length, length_range |
| 138 {0, 3, 0, 3, 0}, |
| 139 }; |
| 140 const int16_t kTestSignal[] = {1, 2, 1, 2, 3, 1, 2, 3}; |
| 141 const ExpectedCount kExpectedCounts[] = { |
| 142 {0, 1}, |
| 143 }; |
| 144 |
| 145 ResetRepetitionPattern(kRepetitionPatterns, |
| 146 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 147 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount), |
| 148 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 149 ResetCounters(); |
| 150 VerifyStereo(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount), |
| 151 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 152 } |
| 153 |
| 154 TEST_F(RepetitionDetectorTest, PatternLongerThanFrame) { |
| 155 const Pattern kRepetitionPatterns[] = { |
| 156 // id, look_back, loop_back_range, length, length_range |
| 157 {0, 6, 0, 6, 0}, |
| 158 }; |
| 159 const int16_t kTestSignal_1[] = {1, 2, 3, 4, 5}; |
| 160 const int16_t kTestSignal_2[] = {6, 1, 2, 3, 4, 5, 6}; |
| 161 const ExpectedCount kExpectedCounts_1[] = { |
| 162 {0, 0}, |
| 163 }; |
| 164 const ExpectedCount kExpectedCounts_2[] = { |
| 165 {0, 1}, |
| 166 }; |
| 167 |
| 168 ResetRepetitionPattern(kRepetitionPatterns, |
| 169 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 170 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), |
| 171 kTestSignal_1, sizeof(kTestSignal_1) / sizeof(int16_t)); |
| 172 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), |
| 173 kTestSignal_2, sizeof(kTestSignal_2) / sizeof(int16_t)); |
| 174 ResetCounters(); |
| 175 VerifyStereo(kExpectedCounts_1, |
| 176 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal_1, |
| 177 sizeof(kTestSignal_1) / sizeof(int16_t)); |
| 178 VerifyStereo(kExpectedCounts_2, |
| 179 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal_2, |
| 180 sizeof(kTestSignal_2) / sizeof(int16_t)); |
| 181 } |
| 182 |
| 183 TEST_F(RepetitionDetectorTest, TwoPatterns) { |
| 184 const Pattern kRepetitionPatterns[] = { |
| 185 // id, look_back, loop_back_range, length, length_range |
| 186 {0, 3, 0, 3, 0}, |
| 187 {1, 4, 0, 4, 0}, |
| 188 }; |
| 189 const int16_t kTestSignal_1[] = {1, 2, 3, 1, 2, 3}; |
| 190 const int16_t kTestSignal_2[] = {1, 2, 3, 4, 1, 2, 3, 4}; |
| 191 const ExpectedCount kExpectedCounts_1[] = { |
| 192 {0, 1}, |
| 193 {1, 0} |
| 194 }; |
| 195 const ExpectedCount kExpectedCounts_2[] = { |
| 196 {0, 2}, // Add 1 because the third 1,2,3 belongs to both patterns. |
| 197 {1, 1} |
| 198 }; |
| 199 |
| 200 ResetRepetitionPattern(kRepetitionPatterns, |
| 201 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 202 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), |
| 203 kTestSignal_1, sizeof(kTestSignal_1) / sizeof(int16_t)); |
| 204 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), |
| 205 kTestSignal_2, sizeof(kTestSignal_2) / sizeof(int16_t)); |
| 206 ResetCounters(); |
| 207 VerifyStereo(kExpectedCounts_1, |
| 208 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal_1, |
| 209 sizeof(kTestSignal_1) / sizeof(int16_t)); |
| 210 VerifyStereo(kExpectedCounts_2, |
| 211 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal_2, |
| 212 sizeof(kTestSignal_2) / sizeof(int16_t)); |
| 213 } |
| 214 |
| 215 TEST_F(RepetitionDetectorTest, NestedPatterns) { |
| 216 const Pattern kRepetitionPatterns[] = { |
| 217 // id, look_back, loop_back_range, length, length_range |
| 218 {0, 3, 0, 3, 0}, |
| 219 {1, 6, 0, 6, 0}, // When a triplet repeated 3 times, this is triggered. |
| 220 }; |
| 221 const int16_t kTestSignal[] = {1, 2, 3, 1, 2, 3}; |
| 222 const ExpectedCount kExpectedCounts_1[] = { |
| 223 {0, 1}, |
| 224 {1, 0} |
| 225 }; |
| 226 const ExpectedCount kExpectedCounts_2[] = { |
| 227 {0, 3}, // Add 1 because the third 1,2,3 belongs to both patterns. |
| 228 {1, 1} |
| 229 }; |
| 230 |
| 231 ResetRepetitionPattern(kRepetitionPatterns, |
| 232 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 233 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), |
| 234 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 235 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), |
| 236 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 237 ResetCounters(); |
| 238 VerifyStereo(kExpectedCounts_1, |
| 239 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal, |
| 240 sizeof(kTestSignal) / sizeof(int16_t)); |
| 241 VerifyStereo(kExpectedCounts_2, |
| 242 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal, |
| 243 sizeof(kTestSignal) / sizeof(int16_t)); |
| 244 } |
| 245 |
| 246 TEST_F(RepetitionDetectorTest, RangedLookBack) { |
| 247 const Pattern kRepetitionPatterns[] = { |
| 248 // id, look_back, loop_back_range, length, length_range |
| 249 {0, 3, 1, 3, 1}, |
| 250 }; |
| 251 const int16_t kTestSignal_1[] = {1, 2, 3, 1, 2, 3}; |
| 252 const int16_t kTestSignal_2[] = {1, 2, 3, 4, 1, 2, 3, 4}; |
| 253 const ExpectedCount kExpectedCounts_1[] = { |
| 254 {0, 1}, |
| 255 }; |
| 256 const ExpectedCount kExpectedCounts_2[] = { |
| 257 {0, 3}, |
| 258 }; |
| 259 |
| 260 ResetRepetitionPattern(kRepetitionPatterns, |
| 261 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 262 Verify(kExpectedCounts_1, sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), |
| 263 kTestSignal_1, sizeof(kTestSignal_1) / sizeof(int16_t)); |
| 264 Verify(kExpectedCounts_2, sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), |
| 265 kTestSignal_2, sizeof(kTestSignal_2) / sizeof(int16_t)); |
| 266 ResetCounters(); |
| 267 VerifyStereo(kExpectedCounts_1, |
| 268 sizeof(kExpectedCounts_1) / sizeof(ExpectedCount), kTestSignal_1, |
| 269 sizeof(kTestSignal_1) / sizeof(int16_t)); |
| 270 VerifyStereo(kExpectedCounts_2, |
| 271 sizeof(kExpectedCounts_2) / sizeof(ExpectedCount), kTestSignal_2, |
| 272 sizeof(kTestSignal_2) / sizeof(int16_t)); |
| 273 } |
| 274 |
| 275 TEST_F(RepetitionDetectorTest, NotFullLengthPattern) { |
| 276 const Pattern kRepetitionPatterns[] = { |
| 277 // id, look_back, loop_back_range, length, length_range |
| 278 {0, 4, 0, 3, 0}, |
| 279 }; |
| 280 const int16_t kTestSignal[] = {1, 2, 3, -1, 1, 2, 3, -2}; |
| 281 const ExpectedCount kExpectedCounts[] = { |
| 282 {0, 1}, |
| 283 }; |
| 284 |
| 285 ResetRepetitionPattern(kRepetitionPatterns, |
| 286 sizeof(kRepetitionPatterns) / sizeof(Pattern)); |
| 287 Verify(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount), |
| 288 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 289 ResetCounters(); |
| 290 VerifyStereo(kExpectedCounts, sizeof(kExpectedCounts) / sizeof(ExpectedCount), |
| 291 kTestSignal, sizeof(kTestSignal) / sizeof(int16_t)); |
| 292 } |
| 293 |
| 294 } // namespace webrtc |
OLD | NEW |