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 // Use CreateHistUnittestFile.m to generate the input file. | 11 // Use CreateHistUnittestFile.m to generate the input file. |
12 | 12 |
13 #include "webrtc/modules/audio_processing/agc/histogram.h" | 13 #include "webrtc/modules/audio_processing/agc/loudness_histogram.h" |
14 | 14 |
15 #include <stdio.h> | 15 #include <stdio.h> |
| 16 #include <algorithm> |
16 #include <cmath> | 17 #include <cmath> |
17 #include <memory> | 18 #include <memory> |
18 | 19 |
19 #include "gtest/gtest.h" | 20 #include "gtest/gtest.h" |
20 #include "webrtc/test/testsupport/fileutils.h" | 21 #include "webrtc/test/testsupport/fileutils.h" |
21 #include "webrtc/modules/audio_processing/agc/utility.h" | 22 #include "webrtc/modules/audio_processing/agc/utility.h" |
22 | 23 |
23 namespace webrtc { | 24 namespace webrtc { |
24 | 25 |
25 struct InputOutput { | 26 struct InputOutput { |
26 double rms; | 27 double rms; |
27 double activity_probability; | 28 double activity_probability; |
28 double audio_content; | 29 double audio_content; |
29 double loudness; | 30 double loudness; |
30 }; | 31 }; |
31 | 32 |
32 const double kRelativeErrTol = 1e-10; | 33 const double kRelativeErrTol = 1e-10; |
33 | 34 |
34 class HistogramTest : public ::testing::Test { | 35 class LoudnessHistogramTest : public ::testing::Test { |
35 protected: | 36 protected: |
36 void RunTest(bool enable_circular_buff, | 37 void RunTest(bool enable_circular_buff, const char* filename); |
37 const char* filename); | |
38 | 38 |
39 private: | 39 private: |
40 void TestClean(); | 40 void TestClean(); |
41 std::unique_ptr<Histogram> hist_; | 41 std::unique_ptr<LoudnessHistogram> hist_; |
42 }; | 42 }; |
43 | 43 |
44 void HistogramTest::TestClean() { | 44 void LoudnessHistogramTest::TestClean() { |
45 EXPECT_EQ(hist_->CurrentRms(), 7.59621091765857e-02); | 45 EXPECT_EQ(hist_->CurrentRms(), 7.59621091765857e-02); |
46 EXPECT_EQ(hist_->AudioContent(), 0); | 46 EXPECT_EQ(hist_->AudioContent(), 0); |
47 EXPECT_EQ(hist_->num_updates(), 0); | 47 EXPECT_EQ(hist_->num_updates(), 0); |
48 } | 48 } |
49 | 49 |
50 void HistogramTest::RunTest(bool enable_circular_buff, const char* filename) { | 50 void LoudnessHistogramTest::RunTest(bool enable_circular_buff, |
| 51 const char* filename) { |
51 FILE* in_file = fopen(filename, "rb"); | 52 FILE* in_file = fopen(filename, "rb"); |
52 ASSERT_TRUE(in_file != NULL); | 53 ASSERT_TRUE(in_file != NULL); |
53 if (enable_circular_buff) { | 54 if (enable_circular_buff) { |
54 int buffer_size; | 55 int buffer_size; |
55 EXPECT_EQ(fread(&buffer_size, sizeof(buffer_size), 1, in_file), 1u); | 56 EXPECT_EQ(fread(&buffer_size, sizeof(buffer_size), 1, in_file), 1u); |
56 hist_.reset(Histogram::Create(buffer_size)); | 57 hist_.reset(LoudnessHistogram::Create(buffer_size)); |
57 } else { | 58 } else { |
58 hist_.reset(Histogram::Create()); | 59 hist_.reset(LoudnessHistogram::Create()); |
59 } | 60 } |
60 TestClean(); | 61 TestClean(); |
61 | 62 |
62 InputOutput io; | 63 InputOutput io; |
63 int num_updates = 0; | 64 int num_updates = 0; |
64 int num_reset = 0; | 65 int num_reset = 0; |
65 while (fread(&io, sizeof(InputOutput), 1, in_file) == 1) { | 66 while (fread(&io, sizeof(InputOutput), 1, in_file) == 1) { |
66 if (io.rms < 0) { | 67 if (io.rms < 0) { |
67 // We have to reset. | 68 // We have to reset. |
68 hist_->Reset(); | 69 hist_->Reset(); |
69 TestClean(); | 70 TestClean(); |
70 num_updates = 0; | 71 num_updates = 0; |
71 num_reset++; | 72 num_reset++; |
72 // Read the next chunk of input. | 73 // Read the next chunk of input. |
73 if (fread(&io, sizeof(InputOutput), 1, in_file) != 1) | 74 if (fread(&io, sizeof(InputOutput), 1, in_file) != 1) |
74 break; | 75 break; |
75 } | 76 } |
76 hist_->Update(io.rms, io.activity_probability); | 77 hist_->Update(io.rms, io.activity_probability); |
77 num_updates++; | 78 num_updates++; |
78 EXPECT_EQ(hist_->num_updates(), num_updates); | 79 EXPECT_EQ(hist_->num_updates(), num_updates); |
79 double audio_content = hist_->AudioContent(); | 80 double audio_content = hist_->AudioContent(); |
80 | 81 |
81 double abs_err = std::min(audio_content, io.audio_content) * | 82 double abs_err = |
82 kRelativeErrTol; | 83 std::min(audio_content, io.audio_content) * kRelativeErrTol; |
83 | 84 |
84 ASSERT_NEAR(audio_content, io.audio_content, abs_err); | 85 ASSERT_NEAR(audio_content, io.audio_content, abs_err); |
85 double current_loudness = Linear2Loudness(hist_->CurrentRms()); | 86 double current_loudness = Linear2Loudness(hist_->CurrentRms()); |
86 abs_err = std::min(fabs(current_loudness), fabs(io.loudness)) * | 87 abs_err = |
87 kRelativeErrTol; | 88 std::min(fabs(current_loudness), fabs(io.loudness)) * kRelativeErrTol; |
88 ASSERT_NEAR(current_loudness, io.loudness, abs_err); | 89 ASSERT_NEAR(current_loudness, io.loudness, abs_err); |
89 } | 90 } |
90 fclose(in_file); | 91 fclose(in_file); |
91 } | 92 } |
92 | 93 |
93 TEST_F(HistogramTest, ActiveCircularBuffer) { | 94 TEST_F(LoudnessHistogramTest, ActiveCircularBuffer) { |
94 RunTest(true, | 95 RunTest(true, test::ResourcePath( |
95 test::ResourcePath("audio_processing/agc/agc_with_circular_buffer", | 96 "audio_processing/agc/agc_with_circular_buffer", "dat") |
96 "dat").c_str()); | 97 .c_str()); |
97 } | 98 } |
98 | 99 |
99 TEST_F(HistogramTest, InactiveCircularBuffer) { | 100 TEST_F(LoudnessHistogramTest, InactiveCircularBuffer) { |
100 RunTest(false, | 101 RunTest(false, test::ResourcePath( |
101 test::ResourcePath("audio_processing/agc/agc_no_circular_buffer", | 102 "audio_processing/agc/agc_no_circular_buffer", "dat") |
102 "dat").c_str()); | 103 .c_str()); |
103 } | 104 } |
104 | 105 |
105 } // namespace webrtc | 106 } // namespace webrtc |
OLD | NEW |