summaryrefslogtreecommitdiff
path: root/klangfarbrs/src/envelope.rs
blob: ce3071d0b53c1fce5aa23418e48b4b6e516f7a1c (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
use super::{Millisecond, Amplitude, SamplesPerSecond, Line};

pub struct Envelope {
    pub attack: Line,
    pub decay: Line,
    pub release: Line,
}

impl Envelope {
    pub fn new(
        attack: Millisecond, decay: Millisecond, sustain: Amplitude, release: Millisecond, sample_rate: SamplesPerSecond
    ) -> Self {
        let attack = Line::new(0.0, 1.0, attack, sample_rate);
        let decay = Line::new(1.0, sustain, decay, sample_rate);
        let release = Line::new(sustain, 0.0, release, sample_rate);

        Self { attack, decay, release }
    }
}

impl Iterator for Envelope {
    type Item = Amplitude;

    fn next(&mut self) -> Option<Self::Item> {
        let mut val = self.attack.next();
        if val.is_none() {
            val = self.decay.next();
            if val.is_none() {
                self.release.next()
            } else {
                val
            }
        } else {
            val
        }
    }
}