OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" |
12 #include <string> | |
13 | |
14 #include "webrtc/api/array_view.h" | 12 #include "webrtc/api/array_view.h" |
15 #include "webrtc/modules/audio_processing/agc2/digital_gain_applier.h" | |
16 #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" | |
17 #include "webrtc/modules/audio_processing/audio_buffer.h" | 13 #include "webrtc/modules/audio_processing/audio_buffer.h" |
| 14 #include "webrtc/rtc_base/checks.h" |
18 #include "webrtc/test/gtest.h" | 15 #include "webrtc/test/gtest.h" |
19 | 16 |
20 namespace webrtc { | 17 namespace webrtc { |
21 namespace test { | 18 namespace test { |
22 | 19 |
23 namespace { | 20 namespace { |
24 | 21 |
25 constexpr size_t kNumFrames = 480u; | 22 constexpr size_t kFrameSizeMs = 10u; |
26 constexpr size_t kStereo = 2u; | 23 constexpr size_t kStereo = 2u; |
27 | 24 |
28 void SetAudioBufferSamples(float value, AudioBuffer* ab) { | 25 void SetAudioBufferSamples(float value, AudioBuffer* ab) { |
29 for (size_t k = 0; k < ab->num_channels(); ++k) { | 26 for (size_t k = 0; k < ab->num_channels(); ++k) { |
30 auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames()); | 27 auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames()); |
31 for (auto& sample : channel) { sample = value; } | 28 for (auto& sample : channel) { sample = value; } |
32 } | 29 } |
33 } | 30 } |
34 | 31 |
35 template<typename Functor> | |
36 bool 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 | |
44 bool 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 | 32 } // namespace |
61 | 33 |
62 TEST(GainController2, Instance) { | 34 TEST(GainController2, CreateApplyConfig) { |
63 std::unique_ptr<GainController2> gain_controller2; | 35 std::unique_ptr<GainController2> gain_controller2; |
64 gain_controller2.reset(new GainController2( | 36 gain_controller2.reset(new GainController2()); |
65 AudioProcessing::kSampleRate48kHz)); | 37 |
| 38 // Check that the default config is valid. |
| 39 AudioProcessing::Config::GainController2 config; |
| 40 EXPECT_TRUE(GainController2::Validate(config)); |
| 41 gain_controller2->ApplyConfig(config); |
| 42 |
| 43 // Check that attenuation is not allowed. |
| 44 config.fixed_gain_db = -5.f; |
| 45 EXPECT_FALSE(GainController2::Validate(config)); |
| 46 |
| 47 // Check that the configuration is applied. |
| 48 float prev_fixed_gain = 0.f; |
| 49 for (const float& fixed_gain_db : {0.f, 5.f, 10.f, 50.f}) { |
| 50 config.fixed_gain_db = fixed_gain_db; |
| 51 EXPECT_TRUE(GainController2::Validate(config)); |
| 52 gain_controller2->ApplyConfig(config); |
| 53 EXPECT_LT(prev_fixed_gain, gain_controller2->fixed_gain()); |
| 54 prev_fixed_gain = gain_controller2->fixed_gain(); |
| 55 } |
66 } | 56 } |
67 | 57 |
68 TEST(GainController2, ToString) { | 58 TEST(GainController2, ToString) { |
69 AudioProcessing::Config config; | 59 AudioProcessing::Config::GainController2 config; |
| 60 config.fixed_gain_db = 5.f; |
70 | 61 |
71 config.gain_controller2.enabled = false; | 62 config.enabled = false; |
72 EXPECT_EQ("{enabled: false}", | 63 EXPECT_EQ("{enabled: false, fixed_gain_dB: 5}", |
73 GainController2::ToString(config.gain_controller2)); | 64 GainController2::ToString(config)); |
74 | 65 |
75 config.gain_controller2.enabled = true; | 66 config.enabled = true; |
76 EXPECT_EQ("{enabled: true}", | 67 EXPECT_EQ("{enabled: true, fixed_gain_dB: 5}", |
77 GainController2::ToString(config.gain_controller2)); | 68 GainController2::ToString(config)); |
78 } | |
79 | |
80 TEST(GainController2, DigitalGainApplierProcess) { | |
81 EXPECT_TRUE(TestDigitalGainApplier(1000.0f, 0.5, 500.0f)); | |
82 } | |
83 | |
84 TEST(GainController2, DigitalGainApplierCheckClipping) { | |
85 EXPECT_TRUE(TestDigitalGainApplier(30000.0f, 1.5, 32767.0f)); | |
86 EXPECT_TRUE(TestDigitalGainApplier(-30000.0f, 1.5, -32767.0f)); | |
87 } | 69 } |
88 | 70 |
89 TEST(GainController2, Usage) { | 71 TEST(GainController2, Usage) { |
90 std::unique_ptr<GainController2> gain_controller2; | 72 std::unique_ptr<GainController2> gain_controller2; |
91 gain_controller2.reset(new GainController2( | 73 gain_controller2->Initialize(AudioProcessing::kSampleRate48kHz); |
92 AudioProcessing::kSampleRate48kHz)); | 74 const size_t num_frames = rtc::CheckedDivExact( |
93 AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames); | 75 kFrameSizeMs * gain_controller2->sample_rate_hz(), 1000ul); |
94 SetAudioBufferSamples(1000.0f, &ab); | 76 AudioBuffer ab(num_frames, kStereo, num_frames, kStereo, num_frames); |
| 77 constexpr float sample_value = 1000.f; |
| 78 SetAudioBufferSamples(sample_value, &ab); |
| 79 AudioProcessing::Config::GainController2 config; |
| 80 |
| 81 // Check that samples are not modified when the fixed gain is 0 dB. |
| 82 ASSERT_EQ(config.fixed_gain_db, 0.f); |
| 83 gain_controller2->ApplyConfig(config); |
95 gain_controller2->Process(&ab); | 84 gain_controller2->Process(&ab); |
| 85 EXPECT_EQ(ab.channels_f()[0][0], sample_value); |
| 86 |
| 87 // Check that samples are amplified when the fixed gain is greater than 0 dB. |
| 88 config.fixed_gain_db = 5.f; |
| 89 gain_controller2->ApplyConfig(config); |
| 90 gain_controller2->Process(&ab); |
| 91 EXPECT_LT(ab.channels_f()[0][0], sample_value); |
96 } | 92 } |
97 | 93 |
98 } // namespace test | 94 } // namespace test |
99 } // namespace webrtc | 95 } // namespace webrtc |
OLD | NEW |