summaryrefslogtreecommitdiff
path: root/src/synth/square.rs
diff options
context:
space:
mode:
authorGraham Northup <grissess@nexusg.org>2017-09-19 23:56:15 -0400
committerGraham Northup <grissess@nexusg.org>2017-09-19 23:56:15 -0400
commit9866c0f34c268a09ecaaa9a4361c1c267799358e (patch)
treed10bea3717c25105ced7457535412072d343686f /src/synth/square.rs
parent26e95364ad9073dd7cb571454cd52ae66f320a73 (diff)
Starting work on proto decode
Diffstat (limited to 'src/synth/square.rs')
-rw-r--r--src/synth/square.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/synth/square.rs b/src/synth/square.rs
new file mode 100644
index 0000000..8197110
--- /dev/null
+++ b/src/synth/square.rs
@@ -0,0 +1,29 @@
+use super::*;
+
+#[derive(Debug)]
+pub struct Square {
+ pub freq: GenBox,
+ pub phase: f32,
+ pub buf: SampleBuffer,
+}
+
+impl Generator for Square {
+ 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() {
+ self.buf[i] = if ((self.phase + pvel * (i as f32)) % 1.0) < 0.5 {
+ -1.0
+ } else {
+ 1.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)
+ }
+}