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