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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use super::signal::*;
use super::utils::ExpInterp;
use crate::{as_any_mut, gate, std_signal};
use std::{
    any::Any,
    ops::{Index, IndexMut},
};

#[derive(Copy, Clone)]
pub struct Adsr {
    tag: Tag,
    attack: In,
    decay: In,
    sustain: In,
    release: In,
    clock: Real,
    sustain_time: Real,
    triggered: bool,
    level: Real,
    a_param: Real,
    d_param: Real,
    r_param: Real,
    a_interp: ExpInterp,
    d_interp: ExpInterp,
    r_interp: ExpInterp,
}

impl Adsr {
    pub fn new(a_param: Real, d_param: Real, r_param: Real) -> Self {
        let a_interp = ExpInterp::new(0.0, 0.5, 1.0);
        let d_interp = ExpInterp::new(0.0, 0.5, 1.0);
        let r_interp = ExpInterp::new(0.0, 0.5, 1.0);
        Self {
            tag: mk_tag(),
            attack: (0.01).into(),
            decay: 0.into(),
            sustain: 1.into(),
            release: (0.1).into(),
            clock: 0.0,
            sustain_time: 0.0,
            triggered: false,
            level: 0.0,
            a_param,
            d_param,
            r_param,
            a_interp,
            d_interp,
            r_interp,
        }
    }

    pub fn linear() -> Self {
        Self::new(0.5, 0.5, 0.5)
    }

    pub fn exp_20() -> Self {
        Self::new(0.2, 0.2, 0.2)
    }

    pub fn attack<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.attack = arg.into();
        self
    }

    pub fn decay<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.decay = arg.into();
        self
    }

    pub fn sustain<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.sustain = arg.into();
        self
    }

    pub fn release<T: Into<In>>(&mut self, arg: T) -> &mut Self {
        self.release = arg.into();
        self
    }

    pub fn calc_level(&mut self, rack: &Rack) -> Real {
        fn max01(a: f64) -> f64 {
            if a > 0.01 {
                a
            } else {
                0.01
            }
        }

        let a = max01(In::val(rack, self.attack));
        let d = max01(In::val(rack, self.decay));
        let s = In::val(rack, self.sustain);
        let r = max01(In::val(rack, self.release));

        if self.triggered {
            match self.clock {
                // Attack
                t if t < a => self.a_interp.interp(t / a),
                // Decay
                t if t < a + d => self.d_interp.interp((t - a) / d),
                // Sustain
                t => {
                    self.sustain_time = t - a - d;
                    s
                }
            }
        } else {
            match self.clock {
                // Attack
                t if t < a => self.a_interp.interp(t / a),
                // Decay
                t if t < a + d => self.d_interp.interp((t - a) / d),
                // Release
                t if t < a + d + r + self.sustain_time => {
                    self.r_interp.interp((t - a - d - self.sustain_time) / r)
                }
                // Off
                _ => 0.,
            }
        }
    }

    pub fn on(&mut self) {
        self.triggered = true;
        self.sustain_time = 0.0;
        self.clock = self.a_interp.interp_inv(self.level);
    }

    pub fn off(&mut self) {
        self.triggered = false;
    }
}

impl Builder for Adsr {}

gate!(Adsr);

impl Signal for Adsr {
    std_signal!();
    fn signal(&mut self, rack: &Rack, sample_rate: Real) -> Real {
        self.a_interp.update(0.0, 1.0 - self.a_param, 1.0);
        let s = In::val(rack, self.sustain);
        self.d_interp.update(1.0, s + self.d_param * (1.0 - s), s);
        self.r_interp.update(s, self.r_param * s, 0.0);
        self.level = self.calc_level(rack);
        self.clock += 1. / sample_rate;
        self.level
    }
}

impl Index<&str> for Adsr {
    type Output = In;

    fn index(&self, index: &str) -> &Self::Output {
        match index {
            "attack" => &self.attack,
            "decay" => &self.decay,
            "sustain" => &self.sustain,
            "release" => &self.release,
            _ => panic!("Adsr does not have a field named: {}", index),
        }
    }
}

impl IndexMut<&str> for Adsr {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        match index {
            "attack" => &mut self.attack,
            "decay" => &mut self.decay,
            "sustain" => &mut self.sustain,
            "release" => &mut self.release,
            _ => panic!("Adsr does not have a field named: {}", index),
        }
    }
}