summaryrefslogtreecommitdiff
path: root/klangfarbrs/src/osc.rs
blob: c609286e8da9d93eef00a0d3f540f043a95bac4d (plain)
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
use std::f32::consts::TAU;
use rand::Rng;
use super::{Hz, Sample};
use super::phasor::Phasor;

/// The various waveforms the `MonoSynth` can generate.
pub enum Waveform {
    Sine,
    Square,
    Triangle,
    Sawtooth,
    WhiteNoise,
    BrownNoise,
}

pub struct Osc {
    pub phasor: Phasor,
    pub waveform: Waveform,
    pub last_value: Sample,
}

impl Osc {
    pub fn new(frequency: Hz, sample_rate: f32) -> Self {
        Self {
            phasor: Phasor::new(frequency, sample_rate),
            waveform: Waveform::Sine,
            last_value: 0.1,
        }
    }

    pub fn get_frequency(&self) -> Hz {
        self.phasor.frequency
    }

    pub fn set_frequency(&mut self, frequency: Hz) {
        self.phasor.frequency = frequency;
    }

    pub fn sample(&mut self) -> Sample {
        match self.next() {
            Some(s) => { s },
            None => 0.0
        }
    }
}

impl Iterator for Osc {
    type Item = Sample;

    fn next(&mut self) -> Option<Self::Item> {
        let phase = self.phasor.next().unwrap();
        let mut rng = rand::thread_rng();

        let sample = match self.waveform {
            Waveform::Sine => {
                (TAU * phase).sin()
            },

            Waveform::Square => {
                if phase < 0.5 {
                    -1.0
                } else {
                    1.0
                }
            },

            Waveform::Triangle => {
                if phase < 0.5 {
                    4.0 * phase - 1.0
                } else {
                    4.0 * (1.0 - phase) - 1.0
                }
            },

            Waveform::Sawtooth => {
                2.0 * phase - 1.0
            },

            Waveform::WhiteNoise => {
                (rng.gen::<f32>()).sin()
            },

            Waveform::BrownNoise => {
                (self.last_value + (rng.gen::<f32>()) * 0.2 - 0.1).clamp(-1.0, 1.0)
            },
        };

        Some(sample)
    }
}