| 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/vad/vad_circular_buffer.h" |
| 12 | 12 |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 | 14 |
| 15 #include <memory> |
| 16 |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webrtc/base/scoped_ptr.h" | |
| 17 | 18 |
| 18 namespace webrtc { | 19 namespace webrtc { |
| 19 | 20 |
| 20 static const int kWidthThreshold = 7; | 21 static const int kWidthThreshold = 7; |
| 21 static const double kValThreshold = 1.0; | 22 static const double kValThreshold = 1.0; |
| 22 static const int kLongBuffSize = 100; | 23 static const int kLongBuffSize = 100; |
| 23 static const int kShortBuffSize = 10; | 24 static const int kShortBuffSize = 10; |
| 24 | 25 |
| 25 static void InsertSequentially(int k, VadCircularBuffer* circular_buffer) { | 26 static void InsertSequentially(int k, VadCircularBuffer* circular_buffer) { |
| 26 double mean_val; | 27 double mean_val; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 VadCircularBuffer* circular_buffer) { | 38 VadCircularBuffer* circular_buffer) { |
| 38 for (int n = 0; n < num_insertion; n++) | 39 for (int n = 0; n < num_insertion; n++) |
| 39 circular_buffer->Insert(value); | 40 circular_buffer->Insert(value); |
| 40 } | 41 } |
| 41 | 42 |
| 42 static void InsertZeros(int num_zeros, VadCircularBuffer* circular_buffer) { | 43 static void InsertZeros(int num_zeros, VadCircularBuffer* circular_buffer) { |
| 43 Insert(0.0, num_zeros, circular_buffer); | 44 Insert(0.0, num_zeros, circular_buffer); |
| 44 } | 45 } |
| 45 | 46 |
| 46 TEST(VadCircularBufferTest, GeneralTest) { | 47 TEST(VadCircularBufferTest, GeneralTest) { |
| 47 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( | 48 std::unique_ptr<VadCircularBuffer> circular_buffer( |
| 48 VadCircularBuffer::Create(kShortBuffSize)); | 49 VadCircularBuffer::Create(kShortBuffSize)); |
| 49 double mean_val; | 50 double mean_val; |
| 50 | 51 |
| 51 // Mean should return zero if nothing is inserted. | 52 // Mean should return zero if nothing is inserted. |
| 52 mean_val = circular_buffer->Mean(); | 53 mean_val = circular_buffer->Mean(); |
| 53 EXPECT_DOUBLE_EQ(0.0, mean_val); | 54 EXPECT_DOUBLE_EQ(0.0, mean_val); |
| 54 InsertSequentially(kShortBuffSize, circular_buffer.get()); | 55 InsertSequentially(kShortBuffSize, circular_buffer.get()); |
| 55 | 56 |
| 56 // Should be full. | 57 // Should be full. |
| 57 EXPECT_TRUE(circular_buffer->is_full()); | 58 EXPECT_TRUE(circular_buffer->is_full()); |
| 58 // Correct update after being full. | 59 // Correct update after being full. |
| 59 for (int n = 1; n < kShortBuffSize; n++) { | 60 for (int n = 1; n < kShortBuffSize; n++) { |
| 60 circular_buffer->Insert(n); | 61 circular_buffer->Insert(n); |
| 61 mean_val = circular_buffer->Mean(); | 62 mean_val = circular_buffer->Mean(); |
| 62 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val); | 63 EXPECT_DOUBLE_EQ((kShortBuffSize + 1.) / 2., mean_val); |
| 63 EXPECT_TRUE(circular_buffer->is_full()); | 64 EXPECT_TRUE(circular_buffer->is_full()); |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Check reset. This should be like starting fresh. | 67 // Check reset. This should be like starting fresh. |
| 67 circular_buffer->Reset(); | 68 circular_buffer->Reset(); |
| 68 mean_val = circular_buffer->Mean(); | 69 mean_val = circular_buffer->Mean(); |
| 69 EXPECT_DOUBLE_EQ(0, mean_val); | 70 EXPECT_DOUBLE_EQ(0, mean_val); |
| 70 InsertSequentially(kShortBuffSize, circular_buffer.get()); | 71 InsertSequentially(kShortBuffSize, circular_buffer.get()); |
| 71 EXPECT_TRUE(circular_buffer->is_full()); | 72 EXPECT_TRUE(circular_buffer->is_full()); |
| 72 } | 73 } |
| 73 | 74 |
| 74 TEST(VadCircularBufferTest, TransientsRemoval) { | 75 TEST(VadCircularBufferTest, TransientsRemoval) { |
| 75 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( | 76 std::unique_ptr<VadCircularBuffer> circular_buffer( |
| 76 VadCircularBuffer::Create(kLongBuffSize)); | 77 VadCircularBuffer::Create(kLongBuffSize)); |
| 77 // Let the first transient be in wrap-around. | 78 // Let the first transient be in wrap-around. |
| 78 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get()); | 79 InsertZeros(kLongBuffSize - kWidthThreshold / 2, circular_buffer.get()); |
| 79 | 80 |
| 80 double push_val = kValThreshold; | 81 double push_val = kValThreshold; |
| 81 double mean_val; | 82 double mean_val; |
| 82 for (int k = kWidthThreshold; k >= 1; k--) { | 83 for (int k = kWidthThreshold; k >= 1; k--) { |
| 83 Insert(push_val, k, circular_buffer.get()); | 84 Insert(push_val, k, circular_buffer.get()); |
| 84 circular_buffer->Insert(0); | 85 circular_buffer->Insert(0); |
| 85 mean_val = circular_buffer->Mean(); | 86 mean_val = circular_buffer->Mean(); |
| 86 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val); | 87 EXPECT_DOUBLE_EQ(k * push_val / kLongBuffSize, mean_val); |
| 87 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold); | 88 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold); |
| 88 mean_val = circular_buffer->Mean(); | 89 mean_val = circular_buffer->Mean(); |
| 89 EXPECT_DOUBLE_EQ(0, mean_val); | 90 EXPECT_DOUBLE_EQ(0, mean_val); |
| 90 } | 91 } |
| 91 } | 92 } |
| 92 | 93 |
| 93 TEST(VadCircularBufferTest, TransientDetection) { | 94 TEST(VadCircularBufferTest, TransientDetection) { |
| 94 rtc::scoped_ptr<VadCircularBuffer> circular_buffer( | 95 std::unique_ptr<VadCircularBuffer> circular_buffer( |
| 95 VadCircularBuffer::Create(kLongBuffSize)); | 96 VadCircularBuffer::Create(kLongBuffSize)); |
| 96 // Let the first transient be in wrap-around. | 97 // Let the first transient be in wrap-around. |
| 97 int num_insertion = kLongBuffSize - kWidthThreshold / 2; | 98 int num_insertion = kLongBuffSize - kWidthThreshold / 2; |
| 98 InsertZeros(num_insertion, circular_buffer.get()); | 99 InsertZeros(num_insertion, circular_buffer.get()); |
| 99 | 100 |
| 100 double push_val = 2; | 101 double push_val = 2; |
| 101 // This is longer than a transient and shouldn't be removed. | 102 // This is longer than a transient and shouldn't be removed. |
| 102 int num_non_zero_elements = kWidthThreshold + 1; | 103 int num_non_zero_elements = kWidthThreshold + 1; |
| 103 Insert(push_val, num_non_zero_elements, circular_buffer.get()); | 104 Insert(push_val, num_non_zero_elements, circular_buffer.get()); |
| 104 | 105 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 124 // it shouldn't be considered transient. | 125 // it shouldn't be considered transient. |
| 125 Insert(push_val, num_insertion, circular_buffer.get()); | 126 Insert(push_val, num_insertion, circular_buffer.get()); |
| 126 num_non_zero_elements += num_insertion; | 127 num_non_zero_elements += num_insertion; |
| 127 EXPECT_EQ(0, | 128 EXPECT_EQ(0, |
| 128 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); | 129 circular_buffer->RemoveTransient(kWidthThreshold, kValThreshold)); |
| 129 mean_val = circular_buffer->Mean(); | 130 mean_val = circular_buffer->Mean(); |
| 130 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); | 131 EXPECT_DOUBLE_EQ(num_non_zero_elements * push_val / kLongBuffSize, mean_val); |
| 131 } | 132 } |
| 132 | 133 |
| 133 } // namespace webrtc | 134 } // namespace webrtc |
| OLD | NEW |