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
//
// apu/chnl/envelope.rs
//
// @author Natesh Narain <nnaraindev@gmail.com>
// @date May 17 2020
//
use super::Divider;
use crate::common::Clockable;

const DECAY_RELOAD: u8 = 15;

pub trait EnvelopeUnit {
    fn clock_envelope(&mut self);
    // fn envelope_start(&mut self);
}

#[macro_export]
macro_rules! impl_envelope {
    ($t:ident, $f:ident) => {
        impl EnvelopeUnit for $t {
            fn clock_envelope(&mut self) {
                self.$f.tick();
            }
        }
    };
}

#[derive(Default)]
pub struct Envelope {
    divider: Divider,
    decay: u8,

    volume: u8,
    constant: bool,
    loop_flag: bool,

    start_flag: bool,
}

impl Clockable for Envelope {
    fn tick(&mut self) {
        if !self.start_flag {
            if self.divider.tick() {

                if self.decay == 0 {
                    if self.loop_flag {
                        self.decay = DECAY_RELOAD;
                    }
                }
                else {
                    self.decay -= 1;
                }
            }
        }
        else {
            self.start_flag = false;
            self.divider.set_period(self.volume as u32);
            self.decay = DECAY_RELOAD;
        }
    }
}

impl Envelope {
    pub fn set_volume(&mut self, volume: u8) {
        self.volume = volume;
    }

    pub fn set_loop(&mut self, loop_flag: bool) {
        self.loop_flag = loop_flag;
    }

    pub fn set_constant(&mut self, constant: bool) {
        self.constant = constant;
    }

    pub fn start(&mut self) {
        self.start_flag = true;
    }

    pub fn output(&self) -> u8 {
        if self.constant {
            self.volume
        }
        else {
            self.decay
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /**
        Envelope Configuration
        ----------------------

        | Volume   | 15         |
        | Constant | true/false |
        | Loop     | true/false |
    */

    #[test]
    fn decay_reload() {
        let mut envelope = Envelope::default();
        envelope.set_constant(false);
        envelope.set_volume(10);
        envelope.set_loop(true);

        envelope.start();
        // First tick to initialize the envelope unit
        envelope.tick();

        assert_eq!(envelope.output(), 15);

        // Clock decay and check envelope output
        for output in (0..15).rev() {
            // Envelope period is volume + 1
            clock_period(&mut envelope, 10 + 1);
            assert_eq!(envelope.output(), output);
        }

        clock_period(&mut envelope, 10 + 1);

        assert_eq!(envelope.output(), 15);
    }

    #[test]
    fn decay_no_reload() {
        let mut envelope = Envelope::default();
        envelope.set_loop(false);
        envelope.set_volume(10);

        envelope.start();
        // First tick to initialize the envelope unit
        envelope.tick();

        assert_eq!(envelope.output(), 15);

        // Clock decay and check envelope output
        for output in (0..15).rev() {
            // Envelope period is volume + 1
            clock_period(&mut envelope, 10 + 1);
            assert_eq!(envelope.output(), output);
        }

        clock_period(&mut envelope, 10 + 1);

        assert_eq!(envelope.output(), 0);
    }

    #[test]
    fn constant() {
        let mut envelope = Envelope::default();
        envelope.set_volume(15);
        envelope.set_constant(true);

        envelope.start();

        for _ in 0..22 {
            envelope.tick();
        }

        assert_eq!(envelope.output(), 15);
    }

    fn clock_period(envelope: &mut Envelope, period: u32) {
        for _ in 0..period {
            envelope.tick();
        }
    }
}