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

Side by Side Diff: webrtc/modules/audio_processing/agc/circular_buffer_unittest.cc

Issue 1211563003: Pull the Voice Activity Detector out from the AGC (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 6 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) 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/modules/audio_processing/agc/circular_buffer.h"
12
13 #include <stdio.h>
14
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webrtc/base/scoped_ptr.h"
17
18 namespace webrtc {
19
20 static const int kWidthThreshold = 7;
21 static const double kValThreshold = 1.0;
22 static const int kLongBuffSize = 100;
23 static const int kShortBuffSize = 10;
24
25 static void InsertSequentially(int k, AgcCircularBuffer* circular_buffer) {
26 double mean_val;
27 for (int n = 1; n <= k; n++) {
28 EXPECT_TRUE(!circular_buffer->is_full());
29 circular_buffer->Insert(n);
30 mean_val = circular_buffer->Mean();
31 EXPECT_EQ((n + 1.0) / 2., mean_val);
32 }
33 }
34
35 static void Insert(double value, int num_insertion,
36 AgcCircularBuffer* circular_buffer) {
37 for (int n = 0; n < num_insertion; n++)
38 circular_buffer->Insert(value);
39 }
40
41 static void InsertZeros(int num_zeros, AgcCircularBuffer* circular_buffer) {
42 Insert(0.0, num_zeros, circular_buffer);
43 }
44
45 TEST(AgcCircularBufferTest, GeneralTest) {
46 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer(
47 AgcCircularBuffer::Create(kShortBuffSize));
48 double mean_val;
49
50 // Mean should return zero if nothing is inserted.
51 mean_val = circular_buffer->Mean();
52 EXPECT_DOUBLE_EQ(0.0, mean_val);
53 InsertSequentially(kShortBuffSize, circular_buffer.get());
54
55 // Should be full.
56 EXPECT_TRUE(circular_buffer->is_full());
57 // Correct update after being full.
58 for (int n = 1; n < kShortBuffSize; n++) {
59 circular_buffer->Insert(n);
60 mean_val = circular_buffer->Mean();
61 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val);
62 EXPECT_TRUE(circular_buffer->is_full());
63 }
64
65 // Check reset. This should be like starting fresh.
66 circular_buffer->Reset();
67 mean_val = circular_buffer->Mean();
68 EXPECT_DOUBLE_EQ(0, mean_val);
69 InsertSequentially(kShortBuffSize, circular_buffer.get());
70 EXPECT_TRUE(circular_buffer->is_full());
71 }
72
73 TEST(AgcCircularBufferTest, TransientsRemoval) {
74 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer(
75 AgcCircularBuffer::Create(kLongBuffSize));
76 // Let the first transient be in wrap-around.
77 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get());
78
79 double push_val = kValThreshold;
80 double mean_val;
81 for (int k = kWidthThreshold; k >= 1; k--) {
82 Insert(push_val, k, circular_buffer.get());
83 circular_buffer->Insert(0);
84 mean_val = circular_buffer->Mean();
85 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val);
86 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold);
87 mean_val = circular_buffer->Mean();
88 EXPECT_DOUBLE_EQ(0, mean_val);
89 }
90 }
91
92 TEST(AgcCircularBufferTest, TransientDetection) {
93 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer(
94 AgcCircularBuffer::Create(kLongBuffSize));
95 // Let the first transient be in wrap-around.
96 int num_insertion = kLongBuffSize - kWidthThreshold / 2;
97 InsertZeros(num_insertion, circular_buffer.get());
98
99 double push_val = 2;
100 // This is longer than a transient and shouldn't be removed.
101 int num_non_zero_elements = kWidthThreshold + 1;
102 Insert(push_val, num_non_zero_elements, circular_buffer.get());
103
104 double mean_val = circular_buffer->Mean();
105 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
106 circular_buffer->Insert(0);
107 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold,
108 kValThreshold));
109 mean_val = circular_buffer->Mean();
110 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
111
112 // A transient right after a non-transient, should be removed and mean is
113 // not changed.
114 num_insertion = 3;
115 Insert(push_val, num_insertion, circular_buffer.get());
116 circular_buffer->Insert(0);
117 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold,
118 kValThreshold));
119 mean_val = circular_buffer->Mean();
120 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
121
122 // Last input is larger than threshold, although the sequence is short but
123 // it shouldn't be considered transient.
124 Insert(push_val, num_insertion, circular_buffer.get());
125 num_non_zero_elements += num_insertion;
126 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold,
127 kValThreshold));
128 mean_val = circular_buffer->Mean();
129 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val);
130 }
131
132 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/agc/circular_buffer.cc ('k') | webrtc/modules/audio_processing/agc/common.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698