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/vad/vad_circular_buffer.h" | 11 #include "webrtc/modules/audio_processing/agc/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, VadCircularBuffer* circular_buffer) { | 25 static void InsertSequentially(int k, AgcCircularBuffer* 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, | 35 static void Insert(double value, int num_insertion, |
36 int num_insertion, | 36 AgcCircularBuffer* circular_buffer) { |
37 VadCircularBuffer* circular_buffer) { | |
38 for (int n = 0; n < num_insertion; n++) | 37 for (int n = 0; n < num_insertion; n++) |
39 circular_buffer->Insert(value); | 38 circular_buffer->Insert(value); |
40 } | 39 } |
41 | 40 |
42 static void InsertZeros(int num_zeros, VadCircularBuffer* circular_buffer) { | 41 static void InsertZeros(int num_zeros, AgcCircularBuffer* circular_buffer) { |
43 Insert(0.0, num_zeros, circular_buffer); | 42 Insert(0.0, num_zeros, circular_buffer); |
44 } | 43 } |
45 | 44 |
46 TEST(VadCircularBufferTest, GeneralTest) { | 45 TEST(AgcCircularBufferTest, GeneralTest) { |
47 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( | 46 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer( |
48 VadCircularBuffer::Create(kShortBuffSize)); | 47 AgcCircularBuffer::Create(kShortBuffSize)); |
49 double mean_val; | 48 double mean_val; |
50 | 49 |
51 // Mean should return zero if nothing is inserted. | 50 // Mean should return zero if nothing is inserted. |
52 mean_val = circular_buffer->Mean(); | 51 mean_val = circular_buffer->Mean(); |
53 EXPECT_DOUBLE_EQ(0.0, mean_val); | 52 EXPECT_DOUBLE_EQ(0.0, mean_val); |
54 InsertSequentially(kShortBuffSize, circular_buffer.get()); | 53 InsertSequentially(kShortBuffSize, circular_buffer.get()); |
55 | 54 |
56 // Should be full. | 55 // Should be full. |
57 EXPECT_TRUE(circular_buffer->is_full()); | 56 EXPECT_TRUE(circular_buffer->is_full()); |
58 // Correct update after being full. | 57 // Correct update after being full. |
59 for (int n = 1; n < kShortBuffSize; n++) { | 58 for (int n = 1; n < kShortBuffSize; n++) { |
60 circular_buffer->Insert(n); | 59 circular_buffer->Insert(n); |
61 mean_val = circular_buffer->Mean(); | 60 mean_val = circular_buffer->Mean(); |
62 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val); | 61 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val); |
63 EXPECT_TRUE(circular_buffer->is_full()); | 62 EXPECT_TRUE(circular_buffer->is_full()); |
64 } | 63 } |
65 | 64 |
66 // Check reset. This should be like starting fresh. | 65 // Check reset. This should be like starting fresh. |
67 circular_buffer->Reset(); | 66 circular_buffer->Reset(); |
68 mean_val = circular_buffer->Mean(); | 67 mean_val = circular_buffer->Mean(); |
69 EXPECT_DOUBLE_EQ(0, mean_val); | 68 EXPECT_DOUBLE_EQ(0, mean_val); |
70 InsertSequentially(kShortBuffSize, circular_buffer.get()); | 69 InsertSequentially(kShortBuffSize, circular_buffer.get()); |
71 EXPECT_TRUE(circular_buffer->is_full()); | 70 EXPECT_TRUE(circular_buffer->is_full()); |
72 } | 71 } |
73 | 72 |
74 TEST(VadCircularBufferTest, TransientsRemoval) { | 73 TEST(AgcCircularBufferTest, TransientsRemoval) { |
75 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( | 74 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer( |
76 VadCircularBuffer::Create(kLongBuffSize)); | 75 AgcCircularBuffer::Create(kLongBuffSize)); |
77 // Let the first transient be in wrap-around. | 76 // Let the first transient be in wrap-around. |
78 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get()); | 77 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get()); |
79 | 78 |
80 double push_val = kValThreshold; | 79 double push_val = kValThreshold; |
81 double mean_val; | 80 double mean_val; |
82 for (int k = kWidthThreshold; k >= 1; k--) { | 81 for (int k = kWidthThreshold; k >= 1; k--) { |
83 Insert(push_val, k, circular_buffer.get()); | 82 Insert(push_val, k, circular_buffer.get()); |
84 circular_buffer->Insert(0); | 83 circular_buffer->Insert(0); |
85 mean_val = circular_buffer->Mean(); | 84 mean_val = circular_buffer->Mean(); |
86 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val); | 85 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val); |
87 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold); | 86 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold); |
88 mean_val = circular_buffer->Mean(); | 87 mean_val = circular_buffer->Mean(); |
89 EXPECT_DOUBLE_EQ(0, mean_val); | 88 EXPECT_DOUBLE_EQ(0, mean_val); |
90 } | 89 } |
91 } | 90 } |
92 | 91 |
93 TEST(VadCircularBufferTest, TransientDetection) { | 92 TEST(AgcCircularBufferTest, TransientDetection) { |
94 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( | 93 rtc::scoped_ptr<AgcCircularBuffer> circular_buffer( |
95 VadCircularBuffer::Create(kLongBuffSize)); | 94 AgcCircularBuffer::Create(kLongBuffSize)); |
96 // Let the first transient be in wrap-around. | 95 // Let the first transient be in wrap-around. |
97 int num_insertion = kLongBuffSize - kWidthThreshold / 2; | 96 int num_insertion = kLongBuffSize - kWidthThreshold / 2; |
98 InsertZeros(num_insertion, circular_buffer.get()); | 97 InsertZeros(num_insertion, circular_buffer.get()); |
99 | 98 |
100 double push_val = 2; | 99 double push_val = 2; |
101 // This is longer than a transient and shouldn't be removed. | 100 // This is longer than a transient and shouldn't be removed. |
102 int num_non_zero_elements = kWidthThreshold + 1; | 101 int num_non_zero_elements = kWidthThreshold + 1; |
103 Insert(push_val, num_non_zero_elements, circular_buffer.get()); | 102 Insert(push_val, num_non_zero_elements, circular_buffer.get()); |
104 | 103 |
105 double mean_val = circular_buffer->Mean(); | 104 double mean_val = circular_buffer->Mean(); |
106 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 105 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
107 circular_buffer->Insert(0); | 106 circular_buffer->Insert(0); |
108 EXPECT_EQ(0, | 107 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold, |
109 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); | 108 kValThreshold)); |
110 mean_val = circular_buffer->Mean(); | 109 mean_val = circular_buffer->Mean(); |
111 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 110 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
112 | 111 |
113 // A transient right after a non-transient, should be removed and mean is | 112 // A transient right after a non-transient, should be removed and mean is |
114 // not changed. | 113 // not changed. |
115 num_insertion = 3; | 114 num_insertion = 3; |
116 Insert(push_val, num_insertion, circular_buffer.get()); | 115 Insert(push_val, num_insertion, circular_buffer.get()); |
117 circular_buffer->Insert(0); | 116 circular_buffer->Insert(0); |
118 EXPECT_EQ(0, | 117 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold, |
119 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); | 118 kValThreshold)); |
120 mean_val = circular_buffer->Mean(); | 119 mean_val = circular_buffer->Mean(); |
121 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 120 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
122 | 121 |
123 // Last input is larger than threshold, although the sequence is short but | 122 // Last input is larger than threshold, although the sequence is short but |
124 // it shouldn't be considered transient. | 123 // it shouldn't be considered transient. |
125 Insert(push_val, num_insertion, circular_buffer.get()); | 124 Insert(push_val, num_insertion, circular_buffer.get()); |
126 num_non_zero_elements += num_insertion; | 125 num_non_zero_elements += num_insertion; |
127 EXPECT_EQ(0, | 126 EXPECT_EQ(0, circular_buffer->RemoveTransient(kWidthThreshold, |
128 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); | 127 kValThreshold)); |
129 mean_val = circular_buffer->Mean(); | 128 mean_val = circular_buffer->Mean(); |
130 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 129 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
131 } | 130 } |
132 | 131 |
133 } // namespace webrtc | 132 } // namespace webrtc |
OLD | NEW |