tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

agc2_testing_common.cc (3133B)


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