1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! # Sawtooth
//! 
//! A sawtooth sample generator.

use super::*;
use super::FreqMod;
use super::Generator;

/// Struct for generating sawtooth samples.
pub struct Sawtooth {
    irate: MathT,
    inc: SampleT,
}

impl FreqMod for Sawtooth {
    fn new(f: MathT) -> Self {
        Sawtooth {
            irate: 2.0*f*INV_SAMPLE_RATE,
            inc: 0.0,
        }
    }

    fn set_frequency(&mut self, f: MathT) {
        self.irate = 2.0*f*INV_SAMPLE_RATE;
    }

    fn get_frequency(&self) -> MathT {
        self.irate / (2.0 * INV_SAMPLE_RATE)
    }
}

impl Generator for Sawtooth {
    fn process(&mut self) -> SampleT {
        let y = self.inc;

        self.inc += self.irate as SampleT;

        if self.inc >= 1.0 {
            self.inc -= 2.0;
        }

        y
    }
}

impl Clone for Sawtooth {
    fn clone(&self) -> Self {
        Sawtooth {
            irate: self.irate,
            inc: 0.0
        }
    }
}