summaryrefslogtreecommitdiff
path: root/src/synth/util.rs
diff options
context:
space:
mode:
authorGraham Northup <grissess@nexusg.org>2017-10-13 12:54:04 -0400
committerGraham Northup <grissess@nexusg.org>2017-10-13 12:54:04 -0400
commit22cb7d7cbcacbfe3a7d3a2d454e6b8cf49ae989c (patch)
tree4d2d8cb2ce9c4441ce5cc525375665c44c31f9fb /src/synth/util.rs
parent255f0aa6de604e0bf9d37ba0ee5362ef3b9f235f (diff)
Added LUTs and ControlRate; improved parser
Diffstat (limited to 'src/synth/util.rs')
-rw-r--r--src/synth/util.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/synth/util.rs b/src/synth/util.rs
new file mode 100644
index 0000000..5bde721
--- /dev/null
+++ b/src/synth/util.rs
@@ -0,0 +1,60 @@
+use super::*;
+
+#[derive(Debug)]
+pub struct ControlRate {
+ pub value: GenBox,
+ pub buf: SampleBuffer,
+}
+
+impl ControlRate {
+ pub fn new(mut gen: GenBox) -> ControlRate {
+ gen.set_buffer(SampleBuffer::new(1));
+ ControlRate { value: gen, buf: SampleBuffer::new(1) }
+ }
+}
+
+impl Generator for ControlRate {
+ fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
+ self.buf.rate = Rate::Control;
+ self.buf.update_from(self.value.eval(params));
+ &self.buf
+ }
+ fn buffer(&self) -> &SampleBuffer { &self.buf }
+ fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer { mem::replace(&mut self.buf, buf) }
+}
+
+pub struct ControlRateFactory;
+
+impl GeneratorFactory for ControlRateFactory {
+ fn new(&self, params: &mut FactoryParameters) -> Result<GenBox, GenFactoryError> {
+ Ok(Box::new(ControlRate::new(params.remove_param("gen", 0)?.into_gen()?)))
+ }
+}
+
+pub static FactoryControlRate: ControlRateFactory = ControlRateFactory;
+
+#[derive(Debug)]
+pub struct SampleRate {
+ pub buf: SampleBuffer,
+}
+
+impl Generator for SampleRate {
+ fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
+ self.buf.set(params.env.sample_rate);
+ &self.buf
+ }
+ fn buffer(&self) -> &SampleBuffer { &self.buf }
+ fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer {
+ mem::replace(&mut self.buf, buf)
+ }
+}
+
+pub struct SampleRateFactory;
+
+impl GeneratorFactory for SampleRateFactory {
+ fn new(&self, params: &mut FactoryParameters) -> Result<GenBox, GenFactoryError> {
+ Ok(Box::new(SampleRate { buf: SampleBuffer::new(1) }))
+ }
+}
+
+pub static FactorySampleRate: SampleRateFactory = SampleRateFactory;