rodio/source/
sawtooth.rs

1use crate::common::{ChannelCount, SampleRate};
2use crate::source::{Function, SignalGenerator};
3use crate::Source;
4use std::time::Duration;
5
6use super::SeekError;
7
8/// An infinite source that produces a sawtooth wave.
9///
10/// Always has a sample rate of 48kHz and one channel.
11///
12/// This source is a thin interface on top of `SignalGenerator` provided for
13/// your convenience.
14#[derive(Clone, Debug)]
15pub struct SawtoothWave {
16    test_saw: SignalGenerator,
17}
18
19impl SawtoothWave {
20    const SAMPLE_RATE: SampleRate = 48000;
21
22    /// The frequency of the sine.
23    #[inline]
24    pub fn new(freq: f32) -> SawtoothWave {
25        SawtoothWave {
26            test_saw: SignalGenerator::new(Self::SAMPLE_RATE, freq, Function::Sawtooth),
27        }
28    }
29}
30
31impl Iterator for SawtoothWave {
32    type Item = f32;
33
34    #[inline]
35    fn next(&mut self) -> Option<f32> {
36        self.test_saw.next()
37    }
38}
39
40impl Source for SawtoothWave {
41    #[inline]
42    fn current_span_len(&self) -> Option<usize> {
43        None
44    }
45
46    #[inline]
47    fn channels(&self) -> ChannelCount {
48        1
49    }
50
51    #[inline]
52    fn sample_rate(&self) -> SampleRate {
53        Self::SAMPLE_RATE
54    }
55
56    #[inline]
57    fn total_duration(&self) -> Option<Duration> {
58        None
59    }
60
61    #[inline]
62    fn try_seek(&mut self, duration: Duration) -> Result<(), SeekError> {
63        self.test_saw.try_seek(duration)
64    }
65}