summaryrefslogtreecommitdiff
path: root/src/synth/math.rs
blob: 8adad4b2914bf13ec94fd546d39f0fd336e7ca45 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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)
    }
}

#[derive(Debug)]
pub struct Negate {
    pub value: GenBox,
    pub buf: SampleBuffer,
}

impl Generator for Negate {
    fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
        self.buf.update_from(self.value.eval(params));
        match self.buf.rate {
            Rate::Sample => {
                for v in self.buf.iter_mut() {
                    *v *= -1.0;
                }
            },
            Rate::Control => {
                self.buf[0] *= -1.0;
            },
        }
        &self.buf
    }
    fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer {
        mem::replace(&mut self.buf, buf)
    }
}

#[derive(Debug)]
pub struct Reciprocate {
    pub value: GenBox,
    pub buf: SampleBuffer,
}

impl Generator for Reciprocate {
    fn eval<'a>(&'a mut self, params: &Parameters) -> &'a SampleBuffer {
        self.buf.update_from(self.value.eval(params));
        match self.buf.rate {
            Rate::Sample => {
                for v in self.buf.iter_mut() {
                    *v = v.powf(-1.0);
                }
            },
            Rate::Control => {
                self.buf[0] = self.buf[0].powf(-1.0);
            },
        }
        &self.buf
    }
    fn set_buffer(&mut self, buf: SampleBuffer) -> SampleBuffer {
        mem::replace(&mut self.buf, buf)
    }
}