From 85925d69e08455bd91d32a27cd3690c9cb634a6b Mon Sep 17 00:00:00 2001 From: Graham Northup Date: Mon, 18 Sep 2017 23:12:14 -0400 Subject: Initial work on modular synthesis --- src/synth/sine.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/synth/sine.rs (limited to 'src/synth/sine.rs') diff --git a/src/synth/sine.rs b/src/synth/sine.rs new file mode 100644 index 0000000..3ee4857 --- /dev/null +++ b/src/synth/sine.rs @@ -0,0 +1,24 @@ +use std::f32::consts::PI; +use super::*; + +const TAU: f32 = 2f32 * PI; + +pub struct Sine { + freq: GenBox, + phase: f32, + buf: SampleBuffer, +} + +impl Generator for Sine { + fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer { + self.buf.rate = Rate::Control; + + let pvel = TAU * self.freq.eval(params).first() / params.env.sample_rate; + for i in 0..self.buf.len() { + self.buf[i] = (self.phase + pvel * (i as f32)).sin() + } + + self.phase = (self.phase + pvel * (self.buf.len() as f32)) % TAU; + &self.buf + } +} -- cgit v1.2.3-70-g09d2