summaryrefslogtreecommitdiff
path: root/klangfarbrs
diff options
context:
space:
mode:
Diffstat (limited to 'klangfarbrs')
-rw-r--r--klangfarbrs/src/instrument.rs33
-rw-r--r--klangfarbrs/src/lib.rs8
2 files changed, 27 insertions, 14 deletions
diff --git a/klangfarbrs/src/instrument.rs b/klangfarbrs/src/instrument.rs
index 3a381dc..bf08fe4 100644
--- a/klangfarbrs/src/instrument.rs
+++ b/klangfarbrs/src/instrument.rs
@@ -1,11 +1,18 @@
-use super::{ Osc, Envelope, Sample };
+use super::{ Partial, Sample, Hz, SamplesPerSecond };
pub struct Instrument {
- pub osc_bank: Vec<Osc>,
- pub envelope: Envelope,
+ pub partials: Vec<Partial>,
}
impl Instrument {
+ pub fn new(base_freq: Hz, partial_multipliers: Vec<f32>, sample_rate: SamplesPerSecond) -> Self {
+ Self {
+ partials: partial_multipliers.iter()
+ .map(|&p| Partial::new(1.0, 1.0, p, 0.0, sample_rate, 2000, base_freq))
+ .collect()
+ }
+ }
+
pub fn sample(&mut self) -> Sample {
match self.next() {
Some(s) => { s },
@@ -18,12 +25,16 @@ 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;
+ let partial_samps : Vec<Option<Sample>> = self.partials.iter_mut()
+ .map(|o| o.next()).collect();
+
+ let filtered : Vec<Sample> = partial_samps.iter().filter(|opt| opt.is_some())
+ .map(|i| i.unwrap()).collect();
- match self.envelope.next() {
- Some(a) => Some(scaled * a),
- None => None
+ if filtered.is_empty() {
+ None
+ } else {
+ Some(filtered.iter().sum())
}
}
}
@@ -35,11 +46,9 @@ mod tests {
#[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),
- };
+ let mut inst = Instrument::new(220.0, vec![1.0, 2.0, 4.0], sr);
assert_eq!(inst.next(), Some(0.0));
+ assert_eq!(inst.last(), Some(0.0));
}
}
diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs
index 7cdb214..b0b9dc1 100644
--- a/klangfarbrs/src/lib.rs
+++ b/klangfarbrs/src/lib.rs
@@ -22,9 +22,13 @@ use osc::{Osc, Waveform};
pub mod envelope;
use envelope::Envelope;
+mod partial;
+use partial::Partial;
+
mod instrument;
use instrument::Instrument;
+
mod utils;
/// Aliasing some types to distinguish various audio properties.
@@ -75,7 +79,7 @@ impl MonoSynth {
let sprt = 48000.0;
Self {
- instrument: Instrument{osc_bank: vec![Osc::new(freq, sprt), Osc::new(400.0, sprt)], envelope: Envelope::new(30, 500, 0.5, 1000, sprt)},
+ instrument: Instrument::new(freq, vec![1.0, 0.909], sprt),
sample_rate: sprt,
frequency: freq,
apply_bend: false,
@@ -202,7 +206,7 @@ impl MonoSynth {
#[export]
fn trigger(&mut self, _owner: &Node,
) {
- self.instrument.envelope = Envelope::new(self.attack, self.decay, self.sustain, self.release, self.sample_rate);
+ ()
}
#[export]