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