diff options
Diffstat (limited to 'klangfarbrs/src')
-rw-r--r-- | klangfarbrs/src/instrument.rs | 36 | ||||
-rw-r--r-- | klangfarbrs/src/lib.rs | 2 |
2 files changed, 38 insertions, 0 deletions
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<Osc>, + envelope: Envelope, +} + +impl Iterator for Instrument { + type Item = Sample; + + fn next(&mut self) -> Option<Self::Item> { + 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. |