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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::f32::consts::PI;


#[derive(Clone)]
pub struct SineWave {
    pub frequency: f32,
    pub sample_rate: usize,
    pub position: usize,
}

impl SineWave {
    pub fn new(frequency: f32, sample_rate: usize) -> Self {
        Self {
            frequency,
            sample_rate,
            position: 0,
        }
    }

    pub fn set_frequency(&mut self, frequency: f32) {
        let ratio = self.frequency / frequency;
        self.frequency = frequency;
        self.position = (self.position as f32 * ratio) as usize;
    }

    pub fn reset(&mut self) {
        self.position = 0;
    }
}

impl Iterator for SineWave {
    type Item = f32;

    fn next(&mut self) -> Option<f32> {
        self.position += 1;
        let result = (2.0 * PI * self.frequency * self.position as f32 / (self.sample_rate as f32)).sin();
        Some(result)
    }
}

#[derive(Clone)]
pub struct SquareWave {
    pub frequency: f32,
    pub sample_rate: usize,
    pub position: usize,
}

impl SquareWave {
    pub fn new(frequency: f32, sample_rate: usize) -> Self {
        Self {
            frequency,
            sample_rate,
            position: 0,
        }
    }

    pub fn set_frequency(&mut self, frequency: f32) {
        let ratio = self.frequency / frequency;
        self.frequency = frequency;
        self.position = (self.position as f32 * ratio) as usize;
    }

    pub fn reset(&mut self) {
        self.position = 0;
    }
}

impl Iterator for SquareWave {
    type Item = f32;

    fn next(&mut self) -> Option<f32> {
        self.position += 1;
        let samples_per_hz = self.sample_rate as f32 / self.frequency;
        let result = if (self.position as f32 % samples_per_hz) < (samples_per_hz / 2.0) {
            1.0
        } else {
            -1.0
        };
        Some(result)
    }
}



#[derive(Copy, Clone)]
pub enum SkewedSquareWaveStage {
    Rising,
    Positive,
    Falling,
    Negative,
}

#[derive(Clone)]
pub struct SkewedSquareWave {
    pub stage: SkewedSquareWaveStage,
    pub frequency: f32,
    pub skew: f32,
    pub sample_rate: usize,
    pub position: usize,
    pub sample: f32,
}

impl SkewedSquareWave {
    pub fn new(frequency: f32, sample_rate: usize) -> Self {
        Self {
            stage: SkewedSquareWaveStage::Rising,
            frequency,
            skew: 0.1,
            sample_rate,
            position: 0,
            sample: 0.0,
        }
    }

    pub fn set_frequency(&mut self, frequency: f32) {
        let ratio = self.frequency / frequency;
        self.frequency = frequency;
        self.position = (self.position as f32 * ratio) as usize;
    }

    pub fn reset(&mut self) {
        self.stage = SkewedSquareWaveStage::Rising;
        self.position = 0;
    }
}

impl Iterator for SkewedSquareWave {
    type Item = f32;

    fn next(&mut self) -> Option<f32> {
        let samples_per_hz = self.sample_rate as f32 / self.frequency;
        self.position += 1;

        match self.stage {
            SkewedSquareWaveStage::Rising => {
                self.sample += self.skew;
                if self.sample >= 1.0 {
                    self.sample = 1.0;
                    self.stage = SkewedSquareWaveStage::Positive;
                }
            },
            SkewedSquareWaveStage::Positive => {
                if (self.position as f32 % samples_per_hz) >= (samples_per_hz / 2.0) {
                    self.stage = SkewedSquareWaveStage::Falling;
                }
            },
            SkewedSquareWaveStage::Falling => {
                self.sample -= self.skew;
                if self.sample <= -1.0 {
                    self.sample = -1.0;
                    self.stage = SkewedSquareWaveStage::Negative;
                }
            },
            SkewedSquareWaveStage::Negative => {
                if (self.position as f32 % samples_per_hz) < (samples_per_hz / 2.0) {
                    self.stage = SkewedSquareWaveStage::Rising;
                }
            },
        }
        Some(self.sample)
    }
}

#[inline]
pub fn db_to_gain(db: f32) -> f32 {
    (10.0_f32).powf(db / 20.0)
}