summaryrefslogtreecommitdiff
path: root/klangfarbrs
diff options
context:
space:
mode:
authorGrant Shangreaux <grant@unabridgedsoftware.com>2021-12-03 12:52:49 -0600
committerGrant Shangreaux <grant@unabridgedsoftware.com>2021-12-03 12:52:49 -0600
commit1e64d804eda19f52b998f81a2d7fd1fc699f271d (patch)
tree6152faffa62c67364646329be1a183eb64ae774d /klangfarbrs
parent7574c5c7acaebe120de3efbaba56b8e9f474d330 (diff)
Add: sketch of Instrument implementation
Diffstat (limited to 'klangfarbrs')
-rw-r--r--klangfarbrs/src/instrument.rs36
-rw-r--r--klangfarbrs/src/lib.rs2
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.