blob: e662a7fc890458e38b58f7812f70299387fdae2d [file] [log] [blame]
Alex Loikoa05ee822018-02-20 15:58:36 +01001/*
2 * Copyright (c) 2018 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 Bazzica746d46b2018-10-30 10:48:38 +010011#include "modules/audio_processing/agc2/limiter.h"
Alex Loikoa05ee822018-02-20 15:58:36 +010012
13#include "common_audio/include/audio_util.h"
14#include "modules/audio_processing/agc2/agc2_common.h"
15#include "modules/audio_processing/agc2/agc2_testing_common.h"
16#include "modules/audio_processing/agc2/vector_float_frame.h"
Alex Loiko507e8d12018-02-27 13:51:47 +010017#include "modules/audio_processing/logging/apm_data_dumper.h"
Alex Loikoa05ee822018-02-20 15:58:36 +010018#include "rtc_base/gunit.h"
19
20namespace webrtc {
21
Alessio Bazzica746d46b2018-10-30 10:48:38 +010022TEST(Limiter, LimiterShouldConstructAndRun) {
Alex Loikoa05ee822018-02-20 15:58:36 +010023 const int sample_rate_hz = 48000;
24 ApmDataDumper apm_data_dumper(0);
25
Alessio Bazzica746d46b2018-10-30 10:48:38 +010026 Limiter limiter(sample_rate_hz, &apm_data_dumper, "");
Alex Loikoa05ee822018-02-20 15:58:36 +010027
28 VectorFloatFrame vectors_with_float_frame(1, sample_rate_hz / 100,
29 kMaxAbsFloatS16Value);
Alessio Bazzica746d46b2018-10-30 10:48:38 +010030 limiter.Process(vectors_with_float_frame.float_frame_view());
Alex Loikoa05ee822018-02-20 15:58:36 +010031}
32
Alessio Bazzica746d46b2018-10-30 10:48:38 +010033TEST(Limiter, OutputVolumeAboveThreshold) {
Alex Loikoa05ee822018-02-20 15:58:36 +010034 const int sample_rate_hz = 48000;
35 const float input_level =
36 (kMaxAbsFloatS16Value + DbfsToFloatS16(test::kLimiterMaxInputLevelDbFs)) /
37 2.f;
38 ApmDataDumper apm_data_dumper(0);
39
Alessio Bazzica746d46b2018-10-30 10:48:38 +010040 Limiter limiter(sample_rate_hz, &apm_data_dumper, "");
Alex Loikoa05ee822018-02-20 15:58:36 +010041
42 // Give the level estimator time to adapt.
43 for (int i = 0; i < 5; ++i) {
44 VectorFloatFrame vectors_with_float_frame(1, sample_rate_hz / 100,
45 input_level);
Alessio Bazzica746d46b2018-10-30 10:48:38 +010046 limiter.Process(vectors_with_float_frame.float_frame_view());
Alex Loikoa05ee822018-02-20 15:58:36 +010047 }
48
49 VectorFloatFrame vectors_with_float_frame(1, sample_rate_hz / 100,
50 input_level);
Alessio Bazzica746d46b2018-10-30 10:48:38 +010051 limiter.Process(vectors_with_float_frame.float_frame_view());
Alex Loikoa05ee822018-02-20 15:58:36 +010052 rtc::ArrayView<const float> channel =
53 vectors_with_float_frame.float_frame_view().channel(0);
54
55 for (const auto& sample : channel) {
56 EXPECT_LT(0.9f * kMaxAbsFloatS16Value, sample);
57 }
58}
59
60} // namespace webrtc