diff options
author | Grant Shangreaux <grant@unabridgedsoftware.com> | 2021-12-02 21:45:12 -0600 |
---|---|---|
committer | Grant Shangreaux <grant@unabridgedsoftware.com> | 2021-12-02 21:45:12 -0600 |
commit | 7574c5c7acaebe120de3efbaba56b8e9f474d330 (patch) | |
tree | c22046e612414fbd9d0f3ec55a789984215e74e6 | |
parent | af484658565920005a502553422530493a5d91de (diff) |
Clean: factor out a utils module and fix some tests
-rw-r--r-- | klangfarbrs/src/lib.rs | 2 | ||||
-rw-r--r-- | klangfarbrs/src/line.rs | 49 | ||||
-rw-r--r-- | klangfarbrs/src/phasor.rs | 7 | ||||
-rw-r--r-- | klangfarbrs/src/utils.rs | 10 |
4 files changed, 38 insertions, 30 deletions
diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index e64246c..0d78e01 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 utils; + /// Aliasing some types to distinguish various audio properties. type Sample = f32; type SamplesPerSecond = f32; diff --git a/klangfarbrs/src/line.rs b/klangfarbrs/src/line.rs index f57b103..5f862a8 100644 --- a/klangfarbrs/src/line.rs +++ b/klangfarbrs/src/line.rs @@ -1,19 +1,25 @@ use super::{Millisecond, Amplitude, SamplesPerSecond}; +use super::utils::*; pub struct Line { - pub start: Amplitude, - pub end: Amplitude, - pub duration: Millisecond, - pub index: u32, + index: u32, + samples: u32, slope: f32, - samples: u32 + y_intercept: f32, } 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) } + let number_of_samples = ms_to_samples(duration, sample_rate); + + Self { + index: 0, + slope: slope(start, end, number_of_samples), + samples: number_of_samples, + y_intercept: start, + } } } @@ -22,25 +28,16 @@ impl Iterator for Line { fn next(&mut self) -> Option<Self::Item> { let idx = self.index; - let val = self.slope * idx as f32 + self.start; + let val = self.slope * idx as f32 + self.y_intercept; 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)] @@ -51,18 +48,18 @@ mod tests { fn it_calculates_a_slope() { let expected = 0.5; let slope = slope(0.0, 0.5, 1); - assert_eq! (expected, slope) + assert_eq! (slope, expected) } #[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()); + assert_eq!(line.next(), Some(0.0)); + assert_eq!(line.next(), Some(0.1)); + assert_eq!(line.next(), Some(0.2)); + assert_eq!(line.next(), Some(0.3)); + assert_eq!(line.next(), Some(0.4)); + assert_eq!(line.next(), Some(0.5)); + assert_eq!(line.next(), None); } } diff --git a/klangfarbrs/src/phasor.rs b/klangfarbrs/src/phasor.rs index 5bfc79e..4d6a714 100644 --- a/klangfarbrs/src/phasor.rs +++ b/klangfarbrs/src/phasor.rs @@ -39,11 +39,10 @@ mod tests { } #[test] - fn it_produces_expected_next_values() { + fn it_wraps_around_as_expected() { let phasor = Phasor::new(10.0, 100.0); - let taken_iterator = phasor.take(11); - assert_eq!(taken_iterator.last(), Some(0.1)) - // assert_eq!([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.1], next) + let last_val = phasor.take(11).last().unwrap(); + assert_eq!(last_val.floor(), 0.0) } } diff --git a/klangfarbrs/src/utils.rs b/klangfarbrs/src/utils.rs new file mode 100644 index 0000000..9f99e23 --- /dev/null +++ b/klangfarbrs/src/utils.rs @@ -0,0 +1,10 @@ +use super::{ Amplitude, Millisecond, SamplesPerSecond }; + +pub fn slope(start: Amplitude, end: Amplitude, duration: Millisecond) -> f32 { + return (end - start) / duration as f32 ; +} + +pub fn ms_to_samples(ms: Millisecond, sample_rate: SamplesPerSecond) -> u32 { + let multiplier = sample_rate as u32 / 1000; + multiplier * ms +} |