summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Shangreaux <grant@unabridgedsoftware.com>2021-12-03 22:28:20 -0600
committerGrant Shangreaux <grant@unabridgedsoftware.com>2021-12-03 22:28:20 -0600
commitb73dde8aa4a5701eb05a5232d907f008dc8f6b00 (patch)
tree8d39d83e55598b3805eec79ccb6df7a011139c4a
parentaf6f09e21e46fa6fd3990862a69efb52813c378a (diff)
Fix: missing Partial file
-rw-r--r--klangfarbrs/src/partial.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/klangfarbrs/src/partial.rs b/klangfarbrs/src/partial.rs
new file mode 100644
index 0000000..336f005
--- /dev/null
+++ b/klangfarbrs/src/partial.rs
@@ -0,0 +1,49 @@
+use super::{ Amplitude, Hz, Line, Millisecond, Osc, Sample, SamplesPerSecond };
+
+pub struct Partial {
+ amplitude: Amplitude,
+ relative_duration: f32,
+ relative_frequency: f32,
+ detune: Hz,
+ osc: Osc,
+ attack: Line,
+ decay: Line,
+}
+
+impl Partial {
+ pub fn new(
+ amplitude: Amplitude,
+ relative_duration: f32,
+ relative_frequency: f32,
+ detune: Hz,
+ sample_rate: SamplesPerSecond,
+ base_duration: Millisecond,
+ base_frequency: Hz,
+ ) -> Self {
+ let freq = base_frequency * relative_frequency + detune;
+ let decay_duration = base_duration as f32 * relative_duration;
+
+ Self {
+ amplitude, relative_duration, relative_frequency, detune,
+ osc: Osc::new(freq, sample_rate),
+ attack: Line::new(0.0, amplitude, 5, sample_rate),
+ decay: Line::new(amplitude, 0.0, decay_duration as u32, sample_rate),
+ }
+ }
+}
+
+impl Iterator for Partial {
+ type Item = Sample;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let amplitude = match self.attack.next() {
+ Some(amp) => Some(amp),
+ None => self.decay.next()
+ };
+
+ match amplitude {
+ Some(amp) => Some(self.osc.next().unwrap() * amp),
+ None => None
+ }
+ }
+}