1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
|