alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 14 | #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" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace test { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | constexpr size_t kNumFrames = 480u; |
| 26 | constexpr size_t kStereo = 2u; |
| 27 | |
| 28 | void 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 | |
| 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 |
| 61 | |
| 62 | TEST(GainController2, Instance) { |
| 63 | std::unique_ptr<GainController2> gain_controller2; |
| 64 | gain_controller2.reset(new GainController2( |
| 65 | AudioProcessing::kSampleRate48kHz)); |
| 66 | } |
| 67 | |
| 68 | TEST(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 | |
| 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) { |
| 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 |