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 <memory> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "webrtc/modules/audio_processing/agc2/digital_gain_applier.h" | |
15 #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" | 14 #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" |
16 #include "webrtc/modules/audio_processing/audio_buffer.h" | 15 #include "webrtc/modules/audio_processing/audio_buffer.h" |
17 #include "webrtc/rtc_base/array_view.h" | 16 #include "webrtc/rtc_base/array_view.h" |
18 #include "webrtc/test/gtest.h" | 17 #include "webrtc/test/gtest.h" |
19 | 18 |
20 namespace webrtc { | 19 namespace webrtc { |
21 namespace test { | 20 namespace test { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 constexpr size_t kNumFrames = 480u; | 24 constexpr size_t kNumFrames = 480u; |
26 constexpr size_t kStereo = 2u; | 25 constexpr size_t kStereo = 2u; |
27 | 26 |
28 void SetAudioBufferSamples(float value, AudioBuffer* ab) { | 27 void SetAudioBufferSamples(float value, AudioBuffer* ab) { |
29 for (size_t k = 0; k < ab->num_channels(); ++k) { | 28 for (size_t k = 0; k < ab->num_channels(); ++k) { |
30 auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames()); | 29 auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames()); |
31 for (auto& sample : channel) { sample = value; } | 30 for (auto& sample : channel) { sample = value; } |
32 } | 31 } |
33 } | 32 } |
34 | 33 |
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 | 34 } // namespace |
61 | 35 |
62 TEST(GainController2, Instance) { | 36 TEST(GainController2, Instance) { |
63 std::unique_ptr<GainController2> gain_controller2; | 37 std::unique_ptr<GainController2> gain_controller2; |
64 gain_controller2.reset(new GainController2( | 38 gain_controller2.reset(new GainController2(5.f)); |
65 AudioProcessing::kSampleRate48kHz)); | |
66 } | 39 } |
67 | 40 |
68 TEST(GainController2, ToString) { | 41 TEST(GainController2, ToString) { |
69 AudioProcessing::Config config; | 42 AudioProcessing::Config config; |
| 43 config.gain_controller2.fixed_gain_db = 5.f; |
70 | 44 |
71 config.gain_controller2.enabled = false; | 45 config.gain_controller2.enabled = false; |
72 EXPECT_EQ("{enabled: false}", | 46 EXPECT_EQ("{enabled: false, fixed_gain_dB: 5.0}", |
73 GainController2::ToString(config.gain_controller2)); | 47 GainController2::ToString(config.gain_controller2)); |
74 | 48 |
75 config.gain_controller2.enabled = true; | 49 config.gain_controller2.enabled = true; |
76 EXPECT_EQ("{enabled: true}", | 50 EXPECT_EQ("{enabled: true, fixed_gain_dB: 5.0}", |
77 GainController2::ToString(config.gain_controller2)); | 51 GainController2::ToString(config.gain_controller2)); |
78 } | 52 } |
79 | 53 |
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 } | |
88 | |
89 TEST(GainController2, Usage) { | 54 TEST(GainController2, Usage) { |
90 std::unique_ptr<GainController2> gain_controller2; | 55 std::unique_ptr<GainController2> gain_controller2; |
91 gain_controller2.reset(new GainController2( | 56 gain_controller2.reset(new GainController2(5.f)); |
92 AudioProcessing::kSampleRate48kHz)); | |
93 AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames); | 57 AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames); |
94 SetAudioBufferSamples(1000.0f, &ab); | 58 SetAudioBufferSamples(1000.0f, &ab); |
95 gain_controller2->Process(&ab); | 59 gain_controller2->Process(&ab); |
96 } | 60 } |
97 | 61 |
98 } // namespace test | 62 } // namespace test |
99 } // namespace webrtc | 63 } // namespace webrtc |
OLD | NEW |