From e2aec3161f791614fc19fc50a4dadf239d7422ad Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Tue, 9 Nov 2021 20:51:57 -0600 Subject: Add: stub of asdr module / Envelope struct --- klangfarbrs/src/adsr.rs | 32 ++++++++++++++++++++++++++++++++ klangfarbrs/src/lib.rs | 2 ++ 2 files changed, 34 insertions(+) create mode 100644 klangfarbrs/src/adsr.rs (limited to 'klangfarbrs') diff --git a/klangfarbrs/src/adsr.rs b/klangfarbrs/src/adsr.rs new file mode 100644 index 0000000..ca03735 --- /dev/null +++ b/klangfarbrs/src/adsr.rs @@ -0,0 +1,32 @@ +use crate::{Millisecond, Amplitude}; + +pub struct Envelope { + attack: Vec, + decay: Vec, + release: Vec, +} + +impl Envelope { + fn new(attack: Millisecond, decay: Millisecond, sustain: Amplitude, release: Millisecond) -> Self { + let attack = vec![sustain; attack as usize]; + let decay = vec![sustain; decay as usize]; + let release = vec![sustain; release as usize]; + + Self{attack, decay, release} + } + + pub fn len(&self) -> usize { + self.attack.len() + self.decay.len() + self.release.len() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_has_expected_total_length() { + let total = Envelope::new(10, 10, 1.0, 10).len(); + assert_eq! (30, total) + } +} diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index a96393f..087cbd7 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -15,6 +15,8 @@ use gdnative::core_types::TypedArray; mod osc; use osc::{Osc}; +mod adsr; + /// Aliasing some types to distinguish various audio properties. type Sample = f32; type SamplesPerSecond = f32; -- cgit v1.2.3