blob: 8c617107ffce57c1f0ba0c9248223692fa58c66f [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>
peahbbe42332016-06-08 06:42:02 -070016#include <algorithm>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017#include <cmath>
kwiberg88788ad2016-02-19 07:04:49 -080018#include <memory>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_processing/agc/utility.h"
21#include "test/gtest.h"
22#include "test/testsupport/fileutils.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023
24namespace webrtc {
25
26struct InputOutput {
27 double rms;
28 double activity_probability;
29 double audio_content;
30 double loudness;
31};
32
33const double kRelativeErrTol = 1e-10;
34
peahbbe42332016-06-08 06:42:02 -070035class LoudnessHistogramTest : public ::testing::Test {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000036 protected:
peahbbe42332016-06-08 06:42:02 -070037 void RunTest(bool enable_circular_buff, const char* filename);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000038
39 private:
40 void TestClean();
peahbbe42332016-06-08 06:42:02 -070041 std::unique_ptr<LoudnessHistogram> hist_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000042};
43
peahbbe42332016-06-08 06:42:02 -070044void LoudnessHistogramTest::TestClean() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000045 EXPECT_EQ(hist_->CurrentRms(), 7.59621091765857e-02);
46 EXPECT_EQ(hist_->AudioContent(), 0);
47 EXPECT_EQ(hist_->num_updates(), 0);
48}
49
peahbbe42332016-06-08 06:42:02 -070050void LoudnessHistogramTest::RunTest(bool enable_circular_buff,
51 const char* filename) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000052 FILE* in_file = fopen(filename, "rb");
53 ASSERT_TRUE(in_file != NULL);
54 if (enable_circular_buff) {
55 int buffer_size;
56 EXPECT_EQ(fread(&buffer_size, sizeof(buffer_size), 1, in_file), 1u);
peahbbe42332016-06-08 06:42:02 -070057 hist_.reset(LoudnessHistogram::Create(buffer_size));
pbos@webrtc.org788acd12014-12-15 09:41:24 +000058 } else {
peahbbe42332016-06-08 06:42:02 -070059 hist_.reset(LoudnessHistogram::Create());
pbos@webrtc.org788acd12014-12-15 09:41:24 +000060 }
61 TestClean();
62
63 InputOutput io;
64 int num_updates = 0;
65 int num_reset = 0;
66 while (fread(&io, sizeof(InputOutput), 1, in_file) == 1) {
67 if (io.rms < 0) {
68 // We have to reset.
69 hist_->Reset();
70 TestClean();
71 num_updates = 0;
72 num_reset++;
73 // Read the next chunk of input.
74 if (fread(&io, sizeof(InputOutput), 1, in_file) != 1)
75 break;
76 }
77 hist_->Update(io.rms, io.activity_probability);
78 num_updates++;
79 EXPECT_EQ(hist_->num_updates(), num_updates);
80 double audio_content = hist_->AudioContent();
81
peahbbe42332016-06-08 06:42:02 -070082 double abs_err =
83 std::min(audio_content, io.audio_content) * kRelativeErrTol;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000084
85 ASSERT_NEAR(audio_content, io.audio_content, abs_err);
86 double current_loudness = Linear2Loudness(hist_->CurrentRms());
peahbbe42332016-06-08 06:42:02 -070087 abs_err =
88 std::min(fabs(current_loudness), fabs(io.loudness)) * kRelativeErrTol;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000089 ASSERT_NEAR(current_loudness, io.loudness, abs_err);
90 }
91 fclose(in_file);
92}
93
peahbbe42332016-06-08 06:42:02 -070094TEST_F(LoudnessHistogramTest, ActiveCircularBuffer) {
95 RunTest(true, test::ResourcePath(
96 "audio_processing/agc/agc_with_circular_buffer", "dat")
97 .c_str());
pbos@webrtc.org788acd12014-12-15 09:41:24 +000098}
99
peahbbe42332016-06-08 06:42:02 -0700100TEST_F(LoudnessHistogramTest, InactiveCircularBuffer) {
101 RunTest(false, test::ResourcePath(
102 "audio_processing/agc/agc_no_circular_buffer", "dat")
103 .c_str());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000104}
105
106} // namespace webrtc