alessiob | 3ec96df | 2017-05-22 13:57:06 | [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 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 11 | #include <algorithm> |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "api/array_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 14 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | e36e8bb | 2018-02-16 10:54:07 | [diff] [blame] | 15 | #include "modules/audio_processing/gain_controller2.h" |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "test/gtest.h" |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | namespace { |
| 23 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 24 | constexpr size_t kFrameSizeMs = 10u; |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 25 | constexpr size_t kStereo = 2u; |
| 26 | |
| 27 | void SetAudioBufferSamples(float value, AudioBuffer* ab) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 28 | // Sets all the samples in |ab| to |value|. |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 29 | for (size_t k = 0; k < ab->num_channels(); ++k) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 30 | std::fill(ab->channels_f()[k], ab->channels_f()[k] + ab->num_frames(), |
| 31 | value); |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 35 | } // namespace |
| 36 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 37 | TEST(GainController2, CreateApplyConfig) { |
| 38 | // Instances GainController2 and applies different configurations. |
| 39 | std::unique_ptr<GainController2> gain_controller2(new GainController2()); |
| 40 | |
| 41 | // Check that the default config is valid. |
| 42 | AudioProcessing::Config::GainController2 config; |
| 43 | EXPECT_TRUE(GainController2::Validate(config)); |
| 44 | gain_controller2->ApplyConfig(config); |
| 45 | |
| 46 | // Check that attenuation is not allowed. |
| 47 | config.fixed_gain_db = -5.f; |
| 48 | EXPECT_FALSE(GainController2::Validate(config)); |
| 49 | |
| 50 | // Check that valid configurations are applied. |
| 51 | for (const float& fixed_gain_db : {0.f, 5.f, 10.f, 50.f}) { |
| 52 | config.fixed_gain_db = fixed_gain_db; |
| 53 | EXPECT_TRUE(GainController2::Validate(config)); |
| 54 | gain_controller2->ApplyConfig(config); |
| 55 | } |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST(GainController2, ToString) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 59 | // Tests GainController2::ToString(). |
| 60 | AudioProcessing::Config::GainController2 config; |
| 61 | config.fixed_gain_db = 5.f; |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 62 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 63 | config.enabled = false; |
Alex Loiko | 9d2788f | 2018-03-29 09:02:43 | [diff] [blame] | 64 | EXPECT_EQ("{enabled: false, fixed_gain_dB: 5}", |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 65 | GainController2::ToString(config)); |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 66 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 67 | config.enabled = true; |
Alex Loiko | 9d2788f | 2018-03-29 09:02:43 | [diff] [blame] | 68 | EXPECT_EQ("{enabled: true, fixed_gain_dB: 5}", |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 69 | GainController2::ToString(config)); |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | TEST(GainController2, Usage) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 73 | // Tests GainController2::Process() on an AudioBuffer instance. |
| 74 | std::unique_ptr<GainController2> gain_controller2(new GainController2()); |
| 75 | gain_controller2->Initialize(AudioProcessing::kSampleRate48kHz); |
| 76 | const size_t num_frames = rtc::CheckedDivExact<size_t>( |
| 77 | kFrameSizeMs * AudioProcessing::kSampleRate48kHz, 1000); |
| 78 | AudioBuffer ab(num_frames, kStereo, num_frames, kStereo, num_frames); |
| 79 | constexpr float sample_value = 1000.f; |
| 80 | SetAudioBufferSamples(sample_value, &ab); |
| 81 | AudioProcessing::Config::GainController2 config; |
| 82 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 09:05:17 | [diff] [blame] | 83 | // Check that samples are amplified when the fixed gain is greater than 0 dB. |
| 84 | config.fixed_gain_db = 5.f; |
| 85 | gain_controller2->ApplyConfig(config); |
| 86 | gain_controller2->Process(&ab); |
| 87 | EXPECT_LT(sample_value, ab.channels_f()[0][0]); |
alessiob | 3ec96df | 2017-05-22 13:57:06 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | } // namespace test |
| 91 | } // namespace webrtc |