Index: webrtc/modules/audio_processing/agc2/gain_controller2_unittest.cc |
diff --git a/webrtc/modules/audio_processing/agc2/gain_controller2_unittest.cc b/webrtc/modules/audio_processing/agc2/gain_controller2_unittest.cc |
index 7668d232985902d1140b2261e5a5fdb72c2fea2b..282041e12d8649a7128498f49ee1e174fec23e0c 100644 |
--- a/webrtc/modules/audio_processing/agc2/gain_controller2_unittest.cc |
+++ b/webrtc/modules/audio_processing/agc2/gain_controller2_unittest.cc |
@@ -8,13 +8,10 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
-#include <memory> |
-#include <string> |
- |
-#include "webrtc/api/array_view.h" |
-#include "webrtc/modules/audio_processing/agc2/digital_gain_applier.h" |
#include "webrtc/modules/audio_processing/agc2/gain_controller2.h" |
+#include "webrtc/api/array_view.h" |
#include "webrtc/modules/audio_processing/audio_buffer.h" |
+#include "webrtc/rtc_base/checks.h" |
#include "webrtc/test/gtest.h" |
namespace webrtc { |
@@ -22,7 +19,7 @@ namespace test { |
namespace { |
-constexpr size_t kNumFrames = 480u; |
+constexpr size_t kFrameSizeMs = 10u; |
constexpr size_t kStereo = 2u; |
void SetAudioBufferSamples(float value, AudioBuffer* ab) { |
@@ -32,67 +29,66 @@ void SetAudioBufferSamples(float value, AudioBuffer* ab) { |
} |
} |
-template<typename Functor> |
-bool CheckAudioBufferSamples(Functor validator, AudioBuffer* ab) { |
- for (size_t k = 0; k < ab->num_channels(); ++k) { |
- auto channel = rtc::ArrayView<float>(ab->channels_f()[k], ab->num_frames()); |
- for (auto& sample : channel) { if (!validator(sample)) { return false; } } |
- } |
- return true; |
-} |
- |
-bool TestDigitalGainApplier(float sample_value, float gain, float expected) { |
- AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames); |
- SetAudioBufferSamples(sample_value, &ab); |
- |
- DigitalGainApplier gain_applier; |
- for (size_t k = 0; k < ab.num_channels(); ++k) { |
- auto channel_view = rtc::ArrayView<float>( |
- ab.channels_f()[k], ab.num_frames()); |
- gain_applier.Process(gain, channel_view); |
- } |
- |
- auto check_expectation = [expected](float sample) { |
- return sample == expected; }; |
- return CheckAudioBufferSamples(check_expectation, &ab); |
-} |
- |
} // namespace |
-TEST(GainController2, Instance) { |
+TEST(GainController2, CreateApplyConfig) { |
std::unique_ptr<GainController2> gain_controller2; |
- gain_controller2.reset(new GainController2( |
- AudioProcessing::kSampleRate48kHz)); |
+ gain_controller2.reset(new GainController2()); |
+ |
+ // Check that the default config is valid. |
+ AudioProcessing::Config::GainController2 config; |
+ EXPECT_TRUE(GainController2::Validate(config)); |
+ gain_controller2->ApplyConfig(config); |
+ |
+ // Check that attenuation is not allowed. |
+ config.fixed_gain_db = -5.f; |
+ EXPECT_FALSE(GainController2::Validate(config)); |
+ |
+ // Check that the configuration is applied. |
+ float prev_fixed_gain = 0.f; |
+ for (const float& fixed_gain_db : {0.f, 5.f, 10.f, 50.f}) { |
+ config.fixed_gain_db = fixed_gain_db; |
+ EXPECT_TRUE(GainController2::Validate(config)); |
+ gain_controller2->ApplyConfig(config); |
+ EXPECT_LT(prev_fixed_gain, gain_controller2->fixed_gain()); |
+ prev_fixed_gain = gain_controller2->fixed_gain(); |
+ } |
} |
TEST(GainController2, ToString) { |
- AudioProcessing::Config config; |
+ AudioProcessing::Config::GainController2 config; |
+ config.fixed_gain_db = 5.f; |
- config.gain_controller2.enabled = false; |
- EXPECT_EQ("{enabled: false}", |
- GainController2::ToString(config.gain_controller2)); |
+ config.enabled = false; |
+ EXPECT_EQ("{enabled: false, fixed_gain_dB: 5}", |
+ GainController2::ToString(config)); |
- config.gain_controller2.enabled = true; |
- EXPECT_EQ("{enabled: true}", |
- GainController2::ToString(config.gain_controller2)); |
-} |
- |
-TEST(GainController2, DigitalGainApplierProcess) { |
- EXPECT_TRUE(TestDigitalGainApplier(1000.0f, 0.5, 500.0f)); |
-} |
- |
-TEST(GainController2, DigitalGainApplierCheckClipping) { |
- EXPECT_TRUE(TestDigitalGainApplier(30000.0f, 1.5, 32767.0f)); |
- EXPECT_TRUE(TestDigitalGainApplier(-30000.0f, 1.5, -32767.0f)); |
+ config.enabled = true; |
+ EXPECT_EQ("{enabled: true, fixed_gain_dB: 5}", |
+ GainController2::ToString(config)); |
} |
TEST(GainController2, Usage) { |
std::unique_ptr<GainController2> gain_controller2; |
- gain_controller2.reset(new GainController2( |
- AudioProcessing::kSampleRate48kHz)); |
- AudioBuffer ab(kNumFrames, kStereo, kNumFrames, kStereo, kNumFrames); |
- SetAudioBufferSamples(1000.0f, &ab); |
+ gain_controller2->Initialize(AudioProcessing::kSampleRate48kHz); |
+ const size_t num_frames = rtc::CheckedDivExact( |
+ kFrameSizeMs * gain_controller2->sample_rate_hz(), 1000ul); |
+ AudioBuffer ab(num_frames, kStereo, num_frames, kStereo, num_frames); |
+ constexpr float sample_value = 1000.f; |
+ SetAudioBufferSamples(sample_value, &ab); |
+ AudioProcessing::Config::GainController2 config; |
+ |
+ // Check that samples are not modified when the fixed gain is 0 dB. |
+ ASSERT_EQ(config.fixed_gain_db, 0.f); |
+ gain_controller2->ApplyConfig(config); |
+ gain_controller2->Process(&ab); |
+ EXPECT_EQ(ab.channels_f()[0][0], sample_value); |
+ |
+ // Check that samples are amplified when the fixed gain is greater than 0 dB. |
+ config.fixed_gain_db = 5.f; |
+ gain_controller2->ApplyConfig(config); |
gain_controller2->Process(&ab); |
+ EXPECT_LT(ab.channels_f()[0][0], sample_value); |
} |
} // namespace test |