blob: 30ea5d326c84aa9ca99d52a579abda5424fa1151 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_processing/agc/loudness_histogram.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000014
15#include <stdio.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
peahbbe42332016-06-08 06:42:02 -070017#include <algorithm>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018#include <cmath>
kwiberg88788ad2016-02-19 07:04:49 -080019#include <memory>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/agc/utility.h"
22#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "test/testsupport/file_utils.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024
25namespace webrtc {
26
27struct InputOutput {
28 double rms;
29 double activity_probability;
30 double audio_content;
31 double loudness;
32};
33
34const double kRelativeErrTol = 1e-10;
35
peahbbe42332016-06-08 06:42:02 -070036class LoudnessHistogramTest : public ::testing::Test {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037 protected:
peahbbe42332016-06-08 06:42:02 -070038 void RunTest(bool enable_circular_buff, const char* filename);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000039
40 private:
41 void TestClean();
peahbbe42332016-06-08 06:42:02 -070042 std::unique_ptr<LoudnessHistogram> hist_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000043};
44
peahbbe42332016-06-08 06:42:02 -070045void LoudnessHistogramTest::TestClean() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000046 EXPECT_EQ(hist_->CurrentRms(), 7.59621091765857e-02);
47 EXPECT_EQ(hist_->AudioContent(), 0);
48 EXPECT_EQ(hist_->num_updates(), 0);
49}
50
peahbbe42332016-06-08 06:42:02 -070051void LoudnessHistogramTest::RunTest(bool enable_circular_buff,
52 const char* filename) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000053 FILE* in_file = fopen(filename, "rb");
54 ASSERT_TRUE(in_file != NULL);
55 if (enable_circular_buff) {
56 int buffer_size;
57 EXPECT_EQ(fread(&buffer_size, sizeof(buffer_size), 1, in_file), 1u);
peahbbe42332016-06-08 06:42:02 -070058 hist_.reset(LoudnessHistogram::Create(buffer_size));
pbos@webrtc.org788acd12014-12-15 09:41:24 +000059 } else {
peahbbe42332016-06-08 06:42:02 -070060 hist_.reset(LoudnessHistogram::Create());
pbos@webrtc.org788acd12014-12-15 09:41:24 +000061 }
62 TestClean();
63
64 InputOutput io;
65 int num_updates = 0;
66 int num_reset = 0;
67 while (fread(&io, sizeof(InputOutput), 1, in_file) == 1) {
68 if (io.rms < 0) {
69 // We have to reset.
70 hist_->Reset();
71 TestClean();
72 num_updates = 0;
73 num_reset++;
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
peahbbe42332016-06-08 06:42:02 -070083 double abs_err =
84 std::min(audio_content, io.audio_content) * kRelativeErrTol;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000085
86 ASSERT_NEAR(audio_content, io.audio_content, abs_err);
87 double current_loudness = Linear2Loudness(hist_->CurrentRms());
peahbbe42332016-06-08 06:42:02 -070088 abs_err =
89 std::min(fabs(current_loudness), fabs(io.loudness)) * kRelativeErrTol;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000090 ASSERT_NEAR(current_loudness, io.loudness, abs_err);
91 }
92 fclose(in_file);
93}
94
peahbbe42332016-06-08 06:42:02 -070095TEST_F(LoudnessHistogramTest, ActiveCircularBuffer) {
96 RunTest(true, test::ResourcePath(
97 "audio_processing/agc/agc_with_circular_buffer", "dat")
98 .c_str());
pbos@webrtc.org788acd12014-12-15 09:41:24 +000099}
100
peahbbe42332016-06-08 06:42:02 -0700101TEST_F(LoudnessHistogramTest, InactiveCircularBuffer) {
102 RunTest(false, test::ResourcePath(
103 "audio_processing/agc/agc_no_circular_buffer", "dat")
104 .c_str());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000105}
106
107} // namespace webrtc