diff options
Diffstat (limited to 'klangfarbrs')
-rw-r--r-- | klangfarbrs/src/envelope.rs (renamed from klangfarbrs/src/adsr.rs) | 29 | ||||
-rw-r--r-- | klangfarbrs/src/lib.rs | 32 |
2 files changed, 39 insertions, 22 deletions
diff --git a/klangfarbrs/src/adsr.rs b/klangfarbrs/src/envelope.rs index 9d51ba3..3da7977 100644 --- a/klangfarbrs/src/adsr.rs +++ b/klangfarbrs/src/envelope.rs @@ -4,6 +4,7 @@ pub struct Envelope { pub attack: Vec<Amplitude>, pub decay: Vec<Amplitude>, pub release: Vec<Amplitude>, + pub index: usize, } impl Envelope { @@ -14,7 +15,7 @@ impl Envelope { let decay = interpolate(1.0, sustain, ms_to_samples(decay, sample_rate)); let release = interpolate(sustain, 0.0, ms_to_samples(release, sample_rate)); - Self{attack, decay, release} + Self { attack, decay, release, index: 0 } } pub fn len(&self) -> usize { @@ -22,6 +23,32 @@ impl Envelope { } } +impl Iterator for Envelope { + type Item = Amplitude; + + fn next(&mut self) -> Option<Self::Item> { + let idx = self.index; + let atk = self.attack.len(); + let atkdcy = atk + self.decay.len(); + + self.index += 1; + + if idx < self.len() { + let val = if idx < atk { + self.attack[idx] + } else if idx >= atk && idx < atkdcy { + self.decay[idx - atk] + } else { + self.release[idx - atkdcy] + }; + + Some(val) + } else { + None + } + } +} + fn interpolate(start: Amplitude, end: Amplitude, milliseconds: Millisecond) -> Vec<Amplitude> { let step_size = (end - start) / milliseconds as f32; let mut amps = vec!(); diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 8bd1e50..fc5e6f9 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -17,8 +17,8 @@ mod phasor; mod osc; use osc::{Osc, Waveform}; -pub mod adsr; -use adsr::Envelope; +pub mod envelope; +use envelope::Envelope; /// Aliasing some types to distinguish various audio properties. type Sample = f32; @@ -47,7 +47,6 @@ pub struct MonoSynth { pub fm_frequency: Hz, pub fm_depth: Amplitude, fm_osc: Osc, - current_envelope_position: usize, } #[methods] @@ -72,13 +71,12 @@ impl MonoSynth { phasor_bend: Vector2::new(0.0, 0.0), continuous: true, duration: 0, - envelope: Envelope::new(500, 1000, 0.5, 4000, sprt), + envelope: Envelope::new(30, 500, 0.5, 1000, sprt), cutoff: 0.0, frequency_modulation: false, fm_frequency: 10.0, fm_depth: 0.1, fm_osc: Osc::new(freq * 2.0, sprt), - current_envelope_position: 0, } } @@ -167,6 +165,11 @@ impl MonoSynth { } #[export] + fn trigger(&mut self, _owner: &Node) { + self.envelope.index = 0; + } + + #[export] pub fn frames(&mut self, _owner: &Node, samples: i32) -> TypedArray<Vector2> { let mut frames = TypedArray::new(); @@ -189,22 +192,9 @@ impl MonoSynth { // } if !self.continuous { - let pos = self.current_envelope_position; - let atk = self.envelope.attack.len(); - let atkdcy = atk + self.envelope.decay.len(); - - if pos < atk { - sample = sample * self.envelope.attack[pos] - } else if pos >= atk && pos < atkdcy { - sample = sample * self.envelope.decay[pos - atk] - } else if pos < self.envelope.len() { - sample = sample * self.envelope.release[pos - atkdcy] - } - - self.current_envelope_position += 1; - - if self.current_envelope_position >= self.envelope.len() { - self.current_envelope_position = 0; + sample *= match self.envelope.next() { + Some(a) => a, + None => 0.0, } } |