diff options
author | Graham Northup <grissess@nexusg.org> | 2017-09-18 23:12:14 -0400 |
---|---|---|
committer | Graham Northup <grissess@nexusg.org> | 2017-09-18 23:12:14 -0400 |
commit | 85925d69e08455bd91d32a27cd3690c9cb634a6b (patch) | |
tree | 1023c044a3350aadb4eb06bf73e209f81f21f8ae /src/synth/sine.rs |
Initial work on modular synthesis
Diffstat (limited to 'src/synth/sine.rs')
-rw-r--r-- | src/synth/sine.rs | 24 |
1 files changed, 24 insertions, 0 deletions
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 + } +} |