xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/agc2_testing_common.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/audio_processing/agc2/agc2_testing_common.h"
12 
13 #include <cmath>
14 
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 namespace test {
19 
LinSpace(double l,double r,int num_points)20 std::vector<double> LinSpace(double l, double r, int num_points) {
21   RTC_CHECK_GE(num_points, 2);
22   std::vector<double> points(num_points);
23   const double step = (r - l) / (num_points - 1.0);
24   points[0] = l;
25   for (int i = 1; i < num_points - 1; i++) {
26     points[i] = static_cast<double>(l) + i * step;
27   }
28   points[num_points - 1] = r;
29   return points;
30 }
31 
WhiteNoiseGenerator(int min_amplitude,int max_amplitude)32 WhiteNoiseGenerator::WhiteNoiseGenerator(int min_amplitude, int max_amplitude)
33     : rand_gen_(42),
34       min_amplitude_(min_amplitude),
35       max_amplitude_(max_amplitude) {
36   RTC_DCHECK_LT(min_amplitude_, max_amplitude_);
37   RTC_DCHECK_LE(kMinS16, min_amplitude_);
38   RTC_DCHECK_LE(min_amplitude_, kMaxS16);
39   RTC_DCHECK_LE(kMinS16, max_amplitude_);
40   RTC_DCHECK_LE(max_amplitude_, kMaxS16);
41 }
42 
operator ()()43 float WhiteNoiseGenerator::operator()() {
44   return static_cast<float>(rand_gen_.Rand(min_amplitude_, max_amplitude_));
45 }
46 
SineGenerator(float amplitude,float frequency_hz,int sample_rate_hz)47 SineGenerator::SineGenerator(float amplitude,
48                              float frequency_hz,
49                              int sample_rate_hz)
50     : amplitude_(amplitude),
51       frequency_hz_(frequency_hz),
52       sample_rate_hz_(sample_rate_hz),
53       x_radians_(0.0f) {
54   RTC_DCHECK_GT(amplitude_, 0);
55   RTC_DCHECK_LE(amplitude_, kMaxS16);
56 }
57 
operator ()()58 float SineGenerator::operator()() {
59   constexpr float kPi = 3.1415926536f;
60   x_radians_ += frequency_hz_ / sample_rate_hz_ * 2 * kPi;
61   if (x_radians_ >= 2 * kPi) {
62     x_radians_ -= 2 * kPi;
63   }
64   return amplitude_ * std::sinf(x_radians_);
65 }
66 
PulseGenerator(float pulse_amplitude,float no_pulse_amplitude,float frequency_hz,int sample_rate_hz)67 PulseGenerator::PulseGenerator(float pulse_amplitude,
68                                float no_pulse_amplitude,
69                                float frequency_hz,
70                                int sample_rate_hz)
71     : pulse_amplitude_(pulse_amplitude),
72       no_pulse_amplitude_(no_pulse_amplitude),
73       samples_period_(
74           static_cast<int>(static_cast<float>(sample_rate_hz) / frequency_hz)),
75       sample_counter_(0) {
76   RTC_DCHECK_GE(pulse_amplitude_, kMinS16);
77   RTC_DCHECK_LE(pulse_amplitude_, kMaxS16);
78   RTC_DCHECK_GT(no_pulse_amplitude_, kMinS16);
79   RTC_DCHECK_LE(no_pulse_amplitude_, kMaxS16);
80   RTC_DCHECK_GT(sample_rate_hz, frequency_hz);
81 }
82 
operator ()()83 float PulseGenerator::operator()() {
84   sample_counter_++;
85   if (sample_counter_ >= samples_period_) {
86     sample_counter_ -= samples_period_;
87   }
88   return static_cast<float>(sample_counter_ == 0 ? pulse_amplitude_
89                                                  : no_pulse_amplitude_);
90 }
91 
92 }  // namespace test
93 }  // namespace webrtc
94