summaryrefslogtreecommitdiff
path: root/src/synth/sine.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/synth/sine.rs')
-rw-r--r--src/synth/sine.rs24
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
+ }
+}