blob: 7c9acc6d3afe10abc81ce5cdbd740bc3ed32b3ab [file] [log] [blame]
alessiob3ec96df2017-05-22 06:57:06 -07001/*
2 * Copyright (c) 2017 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#include <memory>
12#include <string>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/array_view.h"
15#include "modules/audio_processing/agc2/digital_gain_applier.h"
16#include "modules/audio_processing/agc2/gain_controller2.h"
17#include "modules/audio_processing/audio_buffer.h"
18#include "test/gtest.h"
alessiob3ec96df2017-05-22 06:57:06 -070019
20namespace webrtc {
21namespace test {
22
23namespace {
24
25constexpr size_t kNumFrames = 480u;
26constexpr size_t kStereo = 2u;
27
28void SetAudioBufferSamples(float value, AudioBuffer* ab) {
29 for (size_t k = 0; k < ab->num_channels(); ++k) {
30 auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames());
31 for (auto& sample : channel) { sample = value; }
32 }
33}
34
35template<typename Functor>
36bool CheckAudioBufferSamples(Functor validator, AudioBuffer* ab) {
37 for (size_t k = 0; k < ab->num_channels(); ++k) {
38 auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames());
39 for (auto& sample : channel) { if (!validator(sample)) { return false; } }
40 }
41 return true;
42}
43
44bool TestDigitalGainApplier(float sample_value, float gain, float expected) {
45 AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames);
46 SetAudioBufferSamples(sample_value, &ab);
47
48 DigitalGainApplier gain_applier;
49 for (size_t k = 0; k < ab.num_channels(); ++k) {
50 auto channel_view = rtc::ArrayView<float>(
51 ab.channels_f()[k], ab.num_frames());
52 gain_applier.Process(gain, channel_view);
53 }
54
55 auto check_expectation = [expected](float sample) {
56 return sample == expected; };
57 return CheckAudioBufferSamples(check_expectation, &ab);
58}
59
60} // namespace
61
62TEST(GainController2, Instance) {
63 std::unique_ptr<GainController2> gain_controller2;
64 gain_controller2.reset(new GainController2(
65 AudioProcessing::kSampleRate48kHz));
66}
67
68TEST(GainController2, ToString) {
69 AudioProcessing::Config config;
70
71 config.gain_controller2.enabled = false;
72 EXPECT_EQ("{enabled: false}",
73 GainController2::ToString(config.gain_controller2));
74
75 config.gain_controller2.enabled = true;
76 EXPECT_EQ("{enabled: true}",
77 GainController2::ToString(config.gain_controller2));
78}
79
80TEST(GainController2, DigitalGainApplierProcess) {
81 EXPECT_TRUE(TestDigitalGainApplier(1000.0f, 0.5, 500.0f));
82}
83
84TEST(GainController2, DigitalGainApplierCheckClipping) {
85 EXPECT_TRUE(TestDigitalGainApplier(30000.0f, 1.5, 32767.0f));
86 EXPECT_TRUE(TestDigitalGainApplier(-30000.0f, 1.5, -32767.0f));
87}
88
89TEST(GainController2, Usage) {
90 std::unique_ptr<GainController2> gain_controller2;
91 gain_controller2.reset(new GainController2(
92 AudioProcessing::kSampleRate48kHz));
93 AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames);
94 SetAudioBufferSamples(1000.0f, &ab);
95 gain_controller2->Process(&ab);
96}
97
98} // namespace test
99} // namespace webrtc