From 1e64d804eda19f52b998f81a2d7fd1fc699f271d Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Fri, 3 Dec 2021 12:52:49 -0600 Subject: Add: sketch of Instrument implementation --- klangfarbrs/src/instrument.rs | 36 ++++++++++++++++++++++++++++++++++++ klangfarbrs/src/lib.rs | 2 ++ 2 files changed, 38 insertions(+) create mode 100644 klangfarbrs/src/instrument.rs (limited to 'klangfarbrs/src') diff --git a/klangfarbrs/src/instrument.rs b/klangfarbrs/src/instrument.rs new file mode 100644 index 0000000..3a929af --- /dev/null +++ b/klangfarbrs/src/instrument.rs @@ -0,0 +1,36 @@ +use super::{ Osc, Envelope, Sample }; + +struct Instrument { + osc_bank: Vec, + envelope: Envelope, +} + +impl Iterator for Instrument { + type Item = Sample; + + fn next(&mut self) -> Option { + let goo : f32 = self.osc_bank.iter_mut().map(|o| o.sample()).sum(); + let scaled = goo / self.osc_bank.len() as f32; + + match self.envelope.next() { + Some(a) => Some(scaled * a), + None => None + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_name() { + let sr = 44800.0; + let mut inst = Instrument { + osc_bank: vec![Osc::new(220.0, sr), Osc::new(440.0, sr), Osc::new(880.0, sr)], + envelope: Envelope::new(10, 200, 0.7, 1000, sr), + }; + + assert_eq!(inst.next(), Some(0.0)); + } +} diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 0d78e01..51f0c91 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -22,6 +22,8 @@ use osc::{Osc, Waveform}; pub mod envelope; use envelope::Envelope; +mod instrument; + mod utils; /// Aliasing some types to distinguish various audio properties. -- cgit v1.2.3