diff options
Diffstat (limited to 'src/synth/triangle.rs')
-rw-r--r-- | src/synth/triangle.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/synth/triangle.rs b/src/synth/triangle.rs new file mode 100644 index 0000000..c8380a1 --- /dev/null +++ b/src/synth/triangle.rs @@ -0,0 +1,32 @@ +use super::*; + +#[derive(Debug)] +pub struct Triangle { + pub freq: GenBox, + pub phase: f32, + pub buf: SampleBuffer, +} + +impl Generator for Triangle { + fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer { + self.buf.rate = Rate::Sample; + + let pvel = self.freq.eval(params).first() / params.env.sample_rate; + for i in 0..self.buf.len() { + let ph = (self.phase + pvel * (i as f32)) % 1.0; + self.buf[i] = if ph < 0.25 { + 4.0 * ph + } else if ph > 0.75 { + 4.0 * ph - 4.0 + } else { + -4.0 * ph + 2.0 + }; + } + + self.phase = (self.phase + pvel * (self.buf.len() as f32)) % 1.0; + &self.buf + } + fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { + mem::replace(&mut self.buf, buf) + } +} |