tor-browser

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

sinusoidal_linear_chirp_source.cc (2033B)


      1 /*
      2 *  Copyright (c) 2013 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 "common_audio/resampler/sinusoidal_linear_chirp_source.h"
     12 
     13 #include <cmath>
     14 #include <cstddef>
     15 #include <numbers>
     16 
     17 namespace webrtc {
     18 
     19 SinusoidalLinearChirpSource::SinusoidalLinearChirpSource(int sample_rate,
     20                                                         size_t samples,
     21                                                         double max_frequency,
     22                                                         double delay_samples)
     23    : sample_rate_(sample_rate),
     24      total_samples_(samples),
     25      max_frequency_(max_frequency),
     26      current_index_(0),
     27      delay_samples_(delay_samples) {
     28  // Chirp rate.
     29  double duration = static_cast<double>(total_samples_) / sample_rate_;
     30  k_ = (max_frequency_ - kMinFrequency) / duration;
     31 }
     32 
     33 void SinusoidalLinearChirpSource::Run(size_t frames, float* destination) {
     34  for (size_t i = 0; i < frames; ++i, ++current_index_) {
     35    // Filter out frequencies higher than Nyquist.
     36    if (Frequency(current_index_) > 0.5 * sample_rate_) {
     37      destination[i] = 0;
     38    } else {
     39      // Calculate time in seconds.
     40      if (current_index_ < delay_samples_) {
     41        destination[i] = 0;
     42      } else {
     43        // Sinusoidal linear chirp.
     44        double t = (current_index_ - delay_samples_) / sample_rate_;
     45        destination[i] =
     46            sin(2 * std::numbers::pi * (kMinFrequency * t + (k_ / 2) * t * t));
     47      }
     48    }
     49  }
     50 }
     51 
     52 double SinusoidalLinearChirpSource::Frequency(size_t position) {
     53  return kMinFrequency + (position - delay_samples_) *
     54                             (max_frequency_ - kMinFrequency) / total_samples_;
     55 }
     56 
     57 }  // namespace webrtc