use std::time::Duration;
use crate::source::{Function, SignalGenerator};
use crate::Source;
use super::SeekError;
/// An infinite source that produces a sine.
///
/// Always has a rate of 48kHz and one channel.
#[derive(Clone, Debug)]
pub struct SineWave {
test_sine: SignalGenerator,
}
impl SineWave {
const SAMPLE_RATE: u32 = 48000;
/// The frequency of the sine.
#[inline]
pub fn new(freq: f32) -> SineWave {
let sr = cpal::SampleRate(Self::SAMPLE_RATE);
SineWave {
test_sine: SignalGenerator::new(sr, freq, Function::Sine),
}
}
}
impl Iterator for SineWave {
type Item = f32;
#[inline]
fn next(&mut self) -> Option<f32> {
self.test_sine.next()
}
}
impl Source for SineWave {
#[inline]
fn current_frame_len(&self) -> Option<usize> {
None
}
#[inline]
fn channels(&self) -> u16 {
1
}
#[inline]
fn sample_rate(&self) -> u32 {
Self::SAMPLE_RATE
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
None
}
/// `try_seek()` does nothing on the sine generator. If you need to
/// generate a sine tone with a precise phase or sample offset, consider
/// using `skip::skip_samples()`.
#[inline]
fn try_seek(&mut self, _: Duration) -> Result<(), SeekError> {
Ok(())
}
}