summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/design.org11
-rw-r--r--klangfarbrs/src/instrument.rs36
-rw-r--r--klangfarbrs/src/lib.rs2
3 files changed, 49 insertions, 0 deletions
diff --git a/docs/design.org b/docs/design.org
index 4f5e701..d52ce05 100644
--- a/docs/design.org
+++ b/docs/design.org
@@ -79,3 +79,14 @@ This is how its working now. It is possible this will only get us so far, but
I think we can roll with it. The currently implemented [[../klangfarbrs/src/envelope.rs][Envelope]] module is actually
wrapping three of the =line~= type objects. ~attack~ ~decay~ and ~release~ could
all be described by a ~Line~ struct that has a target amplitude and a duration.
+
+* Instrument (basic)
+
+An Instrument is N Sine(?) waves with an Envelope applied to it.
+
+(OscBank, Envelope)
+
+it implements Iterator so that ~next()~ sums the oscillators,
+scales them down by 1/N, and multiplies by the Envelope value.
+
+
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.