diff options
author | Jacob Lee <jacob@unabridgedsoftware.com> | 2021-11-24 23:32:31 -0500 |
---|---|---|
committer | Jacob Lee <jacob@unabridgedsoftware.com> | 2021-11-24 23:32:31 -0500 |
commit | af484658565920005a502553422530493a5d91de (patch) | |
tree | 15852bf7ee4b261e39f10a9a590cacfbb2762d15 /klangfarbrs/src/line.rs | |
parent | 1a44b91562d41343da19e7521ab48b144881e2d0 (diff) |
Refactor envelope to use line iterator
Diffstat (limited to 'klangfarbrs/src/line.rs')
-rw-r--r-- | klangfarbrs/src/line.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/klangfarbrs/src/line.rs b/klangfarbrs/src/line.rs new file mode 100644 index 0000000..f57b103 --- /dev/null +++ b/klangfarbrs/src/line.rs @@ -0,0 +1,68 @@ +use super::{Millisecond, Amplitude, SamplesPerSecond}; + +pub struct Line { + pub start: Amplitude, + pub end: Amplitude, + pub duration: Millisecond, + pub index: u32, + slope: f32, + samples: u32 +} + +impl Line { + pub fn new( + start: Amplitude, end: Amplitude, duration: Millisecond, sample_rate: SamplesPerSecond + ) -> Self { + Self { start, end, duration, index: 0, slope: slope(start, end, ms_to_samples(duration, sample_rate)), samples: ms_to_samples(duration, sample_rate) } + } +} + +impl Iterator for Line { + type Item = Amplitude; + + fn next(&mut self) -> Option<Self::Item> { + let idx = self.index; + let val = self.slope * idx as f32 + self.start; + self.index += 1; + + if idx <= self.samples { + Some(val) + } else { + None + } + + } +} + +fn slope(start: Amplitude, end: Amplitude, duration: Millisecond) -> f32 { + return (end - start) / duration as f32 ; +} + +fn ms_to_samples(ms: Millisecond, sample_rate: SamplesPerSecond) -> u32 { + let multiplier = sample_rate as u32 / 1000; + multiplier * ms +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_calculates_a_slope() { + let expected = 0.5; + let slope = slope(0.0, 0.5, 1); + assert_eq! (expected, slope) + } + + #[test] + fn it_calculates_the_next_values() { + let mut line = Line::new(0.0, 0.5, 1, 5000.0); + assert_eq!(0.0, line.next().unwrap()); + assert_eq!(0.1, line.next().unwrap()); + assert_eq!(0.2, line.next().unwrap()); + assert_eq!(0.3, line.next().unwrap()); + assert_eq!(0.4, line.next().unwrap()); + assert_eq!(0.5, line.next().unwrap()); + assert_eq!(None, line.next()); + } +} |