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
168
169
170
171
172
173
174
175
176
extern crate pcm_flow;

use self::pcm_flow::processor::Processor;
use self::pcm_flow::graph::FrameSet;
use std::f32::consts::PI;

pub struct Sine {
    phase: f32,
    samplerate: usize,
    table: Vec<f32>
}

impl Processor<[f32; 1]> for Sine {

    fn frame_process(&mut self, inputs: &FrameSet<[f32; 1]>, outputs: &mut FrameSet<[f32; 1]>) {
        outputs[0][0] = self.table[(self.phase * self.table.len() as f32) as usize];
        self.phase = inputs[0][0] / self.samplerate as f32;
        if self.phase > 1.0 {
            self.phase -= 1.0;
        }

    }

    fn set_samplerate(&mut self, samplerate: usize) {
        self.samplerate = samplerate;
    }

    fn inputs_amt(&self) -> usize {
        1
    }

    fn outputs_amt(&self) -> usize {
        1
    }
}

impl Sine {
    pub fn new(tablesize: usize) -> Self {
        let mut table = Vec::new();
        for i in 0..tablesize {
            let val = f32::sin((i as f32/tablesize as f32) * PI * 2.0);
            table.push(val);
        }
        Sine {
            phase: 0.0,
            samplerate: 1,
            table: table
        }
    }
}

pub struct Saw {
    phase: f32,
    samplerate: usize,
}

impl Processor<[f32; 1]> for Saw {

    fn frame_process(&mut self, 
                     inputs: &FrameSet<[f32; 1]>,
                     outputs: &mut FrameSet<[f32; 1]>) {
        outputs[0][0] = self.phase * 2.0 - 1.0;
        self.phase = inputs[0][0] / self.samplerate as f32;
        if self.phase > 1.0 {
            self.phase -= 1.0;
        }

    }

    fn set_samplerate(&mut self, samplerate: usize) {
        self.samplerate = samplerate;
    }

    fn inputs_amt(&self) -> usize {
        1
    }

    fn outputs_amt(&self) -> usize {
        1
    }
}

impl Saw {
    pub fn new() -> Self {
        Saw {
            phase: 0.0,
            samplerate: 1
        }
    }
}

pub struct Tri {
    phase: f32,
    samplerate: usize,
}

impl Processor<[f32; 1]> for Tri {

    fn frame_process(&mut self, 
                     inputs: &FrameSet<[f32; 1]>,
                     outputs: &mut FrameSet<[f32; 1]>) {
        outputs[0][0] = if self.phase < 0.5 
                            { self.phase * 4.0 - 1.0 }
                            else
                            { self.phase * (-4.0) - 1.0 };
        self.phase = inputs[0][0] / self.samplerate as f32;
        if self.phase > 1.0 {
            self.phase -= 1.0;
        }

    }

    fn set_samplerate(&mut self, samplerate: usize) {
        self.samplerate = samplerate;
    }

    fn inputs_amt(&self) -> usize {
        1
    }

    fn outputs_amt(&self) -> usize {
        1
    }
}

impl Tri {
    pub fn new() -> Self {
        Tri {
            phase: 0.0,
            samplerate: 1
        }
    }
}

pub struct Square {
    phase: f32,
    samplerate: usize,
}

impl Processor<[f32; 1]> for Square {

    fn frame_process(&mut self, 
                     inputs: &FrameSet<[f32; 1]>,
                     outputs: &mut FrameSet<[f32; 1]>) {
        outputs[0][0] = if self.phase < 0.5 
                            { 1.0 }
                            else
                            { -1.0 };
        self.phase = inputs[0][0] / self.samplerate as f32;
        if self.phase > 1.0 {
            self.phase -= 1.0;
        }

    }

    fn set_samplerate(&mut self, samplerate: usize) {
        self.samplerate = samplerate;
    }

    fn inputs_amt(&self) -> usize {
        1
    }

    fn outputs_amt(&self) -> usize {
        1
    }
}

impl Square {
    pub fn new() -> Self {
        Square {
            phase: 0.0,
            samplerate: 1
        }
    }
}