andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 16 | #include <math.h> |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | SinusoidalLinearChirpSource::SinusoidalLinearChirpSource(int sample_rate, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 21 | size_t samples, |
| 22 | double max_frequency, |
| 23 | double delay_samples) |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 24 | : sample_rate_(sample_rate), |
| 25 | total_samples_(samples), |
| 26 | max_frequency_(max_frequency), |
| 27 | current_index_(0), |
| 28 | delay_samples_(delay_samples) { |
| 29 | // Chirp rate. |
| 30 | double duration = static_cast<double>(total_samples_) / sample_rate_; |
| 31 | k_ = (max_frequency_ - kMinFrequency) / duration; |
| 32 | } |
| 33 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 34 | void SinusoidalLinearChirpSource::Run(size_t frames, float* destination) { |
| 35 | for (size_t i = 0; i < frames; ++i, ++current_index_) { |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 36 | // Filter out frequencies higher than Nyquist. |
| 37 | if (Frequency(current_index_) > 0.5 * sample_rate_) { |
| 38 | destination[i] = 0; |
| 39 | } else { |
| 40 | // Calculate time in seconds. |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 41 | if (current_index_ < delay_samples_) { |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 42 | destination[i] = 0; |
| 43 | } else { |
| 44 | // Sinusoidal linear chirp. |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 45 | double t = (current_index_ - delay_samples_) / sample_rate_; |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 46 | destination[i] = |
| 47 | sin(2 * M_PI * (kMinFrequency * t + (k_ / 2) * t * t)); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 53 | double SinusoidalLinearChirpSource::Frequency(size_t position) { |
andrew@webrtc.org | 8fc05fe | 2013-04-26 14:56:51 +0000 | [diff] [blame] | 54 | return kMinFrequency + (position - delay_samples_) * |
| 55 | (max_frequency_ - kMinFrequency) / total_samples_; |
| 56 | } |
| 57 | |
| 58 | } // namespace webrtc |