diff options
Diffstat (limited to 'src/synth/util.rs')
-rw-r--r-- | src/synth/util.rs | 60 |
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; |