audio_engine/
sine.rs

1use std::f64::consts::TAU;
2
3use crate::SoundSource;
4
5/// A SoundSource that generates a sine wave with a given frequency.
6pub struct SineWave {
7    // With a sample_rte of 96000 Hz, this u64 variable will wrap after 6 million years.
8    i: u64,
9    /// The sample_rate of this SoundSource.
10    pub sample_rate: u32,
11    /// The frequency of the sine wave, in Hertz
12    pub freq: f32,
13}
14impl SineWave {
15    /// Create a new SineWave SoundSource.
16    ///
17    /// Created one the given sample_rate and frequency, both in Hertz.
18    pub fn new(sample_rate: u32, freq: f32) -> Self {
19        Self {
20            i: 0,
21            sample_rate,
22            freq,
23        }
24    }
25}
26impl SoundSource for SineWave {
27    fn sample_rate(&self) -> u32 {
28        self.sample_rate
29    }
30    fn channels(&self) -> u16 {
31        1
32    }
33    fn reset(&mut self) {
34        self.i = 0
35    }
36    fn write_samples(&mut self, out: &mut [i16]) -> usize {
37        for o in out.iter_mut() {
38            // With a mantissa of 52 bits, at 96000 Hz, i as f64 will lose precision after 1486
39            // years.
40            let t = self.i as f64 / self.sample_rate() as f64;
41            let amplitude = (i16::max_value() / 4) as f64;
42            *o = ((self.freq as f64 * TAU * t).cos() * amplitude) as i16;
43            self.i += 1;
44        }
45        out.len()
46    }
47}