OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/agc/circular_buffer.h" | 11 #include "webrtc/modules/audio_processing/vad/vad_circular_buffer.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webrtc/base/scoped_ptr.h" | 16 #include "webrtc/base/scoped_ptr.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 static const int kWidthThreshold = 7; | 20 static const int kWidthThreshold = 7; |
21 static const double kValThreshold = 1.0; | 21 static const double kValThreshold = 1.0; |
22 static const int kLongBuffSize = 100; | 22 static const int kLongBuffSize = 100; |
23 static const int kShortBuffSize = 10; | 23 static const int kShortBuffSize = 10; |
24 | 24 |
25 static void InsertSequentially(int k, AgcCircularBuffer* circular_buffer) { | 25 static void InsertSequentially(int k, VadCircularBuffer* circular_buffer) { |
26 double mean_val; | 26 double mean_val; |
27 for (int n = 1; n <= k; n++) { | 27 for (int n = 1; n <= k; n++) { |
28 EXPECT_TRUE(!circular_buffer->is_full()); | 28 EXPECT_TRUE(!circular_buffer->is_full()); |
29 circular_buffer->Insert(n); | 29 circular_buffer->Insert(n); |
30 mean_val = circular_buffer->Mean(); | 30 mean_val = circular_buffer->Mean(); |
31 EXPECT_EQ((n + 1.0) / 2., mean_val); | 31 EXPECT_EQ((n + 1.0) / 2., mean_val); |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 static void Insert(double value, int num_insertion, | 35 static void Insert(double value, |
36 AgcCircularBuffer* circular_buffer) { | 36 int num_insertion, |
| 37 VadCircularBuffer* circular_buffer) { |
37 for (int n = 0; n < num_insertion; n++) | 38 for (int n = 0; n < num_insertion; n++) |
38 circular_buffer->Insert(value); | 39 circular_buffer->Insert(value); |
39 } | 40 } |
40 | 41 |
41 static void InsertZeros(int num_zeros, AgcCircularBuffer* circular_buffer) { | 42 static void InsertZeros(int num_zeros, VadCircularBuffer* circular_buffer) { |
42 Insert(0.0, num_zeros, circular_buffer); | 43 Insert(0.0, num_zeros, circular_buffer); |
43 } | 44 } |
44 | 45 |
45 TEST(AgcCircularBufferTest, GeneralTest) { | 46 TEST(VadCircularBufferTest, GeneralTest) { |
46 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer( | 47 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( |
47 AgcCircularBuffer::Create(kShortBuffSize)); | 48 VadCircularBuffer::Create(kShortBuffSize)); |
48 double mean_val; | 49 double mean_val; |
49 | 50 |
50 // Mean should return zero if nothing is inserted. | 51 // Mean should return zero if nothing is inserted. |
51 mean_val = circular_buffer->Mean(); | 52 mean_val = circular_buffer->Mean(); |
52 EXPECT_DOUBLE_EQ(0.0, mean_val); | 53 EXPECT_DOUBLE_EQ(0.0, mean_val); |
53 InsertSequentially(kShortBuffSize, circular_buffer.get()); | 54 InsertSequentially(kShortBuffSize, circular_buffer.get()); |
54 | 55 |
55 // Should be full. | 56 // Should be full. |
56 EXPECT_TRUE(circular_buffer->is_full()); | 57 EXPECT_TRUE(circular_buffer->is_full()); |
57 // Correct update after being full. | 58 // Correct update after being full. |
58 for (int n = 1; n < kShortBuffSize; n++) { | 59 for (int n = 1; n < kShortBuffSize; n++) { |
59 circular_buffer->Insert(n); | 60 circular_buffer->Insert(n); |
60 mean_val = circular_buffer->Mean(); | 61 mean_val = circular_buffer->Mean(); |
61 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val); | 62 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val); |
62 EXPECT_TRUE(circular_buffer->is_full()); | 63 EXPECT_TRUE(circular_buffer->is_full()); |
63 } | 64 } |
64 | 65 |
65 // Check reset. This should be like starting fresh. | 66 // Check reset. This should be like starting fresh. |
66 circular_buffer->Reset(); | 67 circular_buffer->Reset(); |
67 mean_val = circular_buffer->Mean(); | 68 mean_val = circular_buffer->Mean(); |
68 EXPECT_DOUBLE_EQ(0, mean_val); | 69 EXPECT_DOUBLE_EQ(0, mean_val); |
69 InsertSequentially(kShortBuffSize, circular_buffer.get()); | 70 InsertSequentially(kShortBuffSize, circular_buffer.get()); |
70 EXPECT_TRUE(circular_buffer->is_full()); | 71 EXPECT_TRUE(circular_buffer->is_full()); |
71 } | 72 } |
72 | 73 |
73 TEST(AgcCircularBufferTest, TransientsRemoval) { | 74 TEST(VadCircularBufferTest, TransientsRemoval) { |
74 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer( | 75 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( |
75 AgcCircularBuffer::Create(kLongBuffSize)); | 76 VadCircularBuffer::Create(kLongBuffSize)); |
76 // Let the first transient be in wrap-around. | 77 // Let the first transient be in wrap-around. |
77 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get()); | 78 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get()); |
78 | 79 |
79 double push_val = kValThreshold; | 80 double push_val = kValThreshold; |
80 double mean_val; | 81 double mean_val; |
81 for (int k = kWidthThreshold; k >= 1; k--) { | 82 for (int k = kWidthThreshold; k >= 1; k--) { |
82 Insert(push_val, k, circular_buffer.get()); | 83 Insert(push_val, k, circular_buffer.get()); |
83 circular_buffer->Insert(0); | 84 circular_buffer->Insert(0); |
84 mean_val = circular_buffer->Mean(); | 85 mean_val = circular_buffer->Mean(); |
85 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val); | 86 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val); |
86 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold); | 87 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold); |
87 mean_val = circular_buffer->Mean(); | 88 mean_val = circular_buffer->Mean(); |
88 EXPECT_DOUBLE_EQ(0, mean_val); | 89 EXPECT_DOUBLE_EQ(0, mean_val); |
89 } | 90 } |
90 } | 91 } |
91 | 92 |
92 TEST(AgcCircularBufferTest, TransientDetection) { | 93 TEST(VadCircularBufferTest, TransientDetection) { |
93 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer( | 94 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( |
94 AgcCircularBuffer::Create(kLongBuffSize)); | 95 VadCircularBuffer::Create(kLongBuffSize)); |
95 // Let the first transient be in wrap-around. | 96 // Let the first transient be in wrap-around. |
96 int num_insertion = kLongBuffSize - kWidthThreshold / 2; | 97 int num_insertion = kLongBuffSize - kWidthThreshold / 2; |
97 InsertZeros(num_insertion, circular_buffer.get()); | 98 InsertZeros(num_insertion, circular_buffer.get()); |
98 | 99 |
99 double push_val = 2; | 100 double push_val = 2; |
100 // This is longer than a transient and shouldn't be removed. | 101 // This is longer than a transient and shouldn't be removed. |
101 int num_non_zero_elements = kWidthThreshold + 1; | 102 int num_non_zero_elements = kWidthThreshold + 1; |
102 Insert(push_val, num_non_zero_elements, circular_buffer.get()); | 103 Insert(push_val, num_non_zero_elements, circular_buffer.get()); |
103 | 104 |
104 double mean_val = circular_buffer->Mean(); | 105 double mean_val = circular_buffer->Mean(); |
105 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 106 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
106 circular_buffer->Insert(0); | 107 circular_buffer->Insert(0); |
107 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold, | 108 EXPECT_EQ(0, |
108 kValThreshold)); | 109 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); |
109 mean_val = circular_buffer->Mean(); | 110 mean_val = circular_buffer->Mean(); |
110 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 111 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
111 | 112 |
112 // A transient right after a non-transient, should be removed and mean is | 113 // A transient right after a non-transient, should be removed and mean is |
113 // not changed. | 114 // not changed. |
114 num_insertion = 3; | 115 num_insertion = 3; |
115 Insert(push_val, num_insertion, circular_buffer.get()); | 116 Insert(push_val, num_insertion, circular_buffer.get()); |
116 circular_buffer->Insert(0); | 117 circular_buffer->Insert(0); |
117 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold, | 118 EXPECT_EQ(0, |
118 kValThreshold)); | 119 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); |
119 mean_val = circular_buffer->Mean(); | 120 mean_val = circular_buffer->Mean(); |
120 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 121 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
121 | 122 |
122 // Last input is larger than threshold, although the sequence is short but | 123 // Last input is larger than threshold, although the sequence is short but |
123 // it shouldn't be considered transient. | 124 // it shouldn't be considered transient. |
124 Insert(push_val, num_insertion, circular_buffer.get()); | 125 Insert(push_val, num_insertion, circular_buffer.get()); |
125 num_non_zero_elements += num_insertion; | 126 num_non_zero_elements += num_insertion; |
126 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold, | 127 EXPECT_EQ(0, |
127 kValThreshold)); | 128 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); |
128 mean_val = circular_buffer->Mean(); | 129 mean_val = circular_buffer->Mean(); |
129 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 130 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
130 } | 131 } |
131 | 132 |
132 } // namespace webrtc | 133 } // namespace webrtc |
OLD | NEW |