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