summaryrefslogtreecommitdiff
path: root/src/synth/math.rs
blob: 91e58e3b53bc6e8863cb2d1a5b5ba682afa43c8b (plain)
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
use super::*;
use std::mem;

#[derive(Debug)]
pub struct Add {
    pub terms: Vec<GenBox>,
    pub buf: SampleBuffer,
}

impl Generator for Add {
    fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
        if self.terms.is_empty() {
            self.buf.zero();
        } else {
            let (first, next) = self.terms.split_at_mut(1);
            self.buf.update_from(first[0].eval(params));
            for term in next {
                self.buf.sum_into(term.eval(params));
            }
        }
        &self.buf
    }
    fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer {
        mem::replace(&mut self.buf, buf)
    }
}

#[derive(Debug)]
pub struct Mul {
    pub factors: Vec<GenBox>,
    pub buf: SampleBuffer,
}

impl Generator for Mul {
    fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
        if self.factors.is_empty() {
            self.buf.zero();
        } else {
            let (first, next) = self.factors.split_at_mut(1);
            self.buf.update_from(first[0].eval(params));
            for factor in next {
                self.buf.mul_into(factor.eval(params));
            }
        }
        &self.buf
    }
    fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer {
        mem::replace(&mut self.buf, buf)
    }
}