summaryrefslogtreecommitdiff
path: root/klangfarbrs/src
diff options
context:
space:
mode:
Diffstat (limited to 'klangfarbrs/src')
-rw-r--r--klangfarbrs/src/lib.rs2
-rw-r--r--klangfarbrs/src/phasor.rs46
2 files changed, 48 insertions, 0 deletions
diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs
index 1fdc0fb..a118d38 100644
--- a/klangfarbrs/src/lib.rs
+++ b/klangfarbrs/src/lib.rs
@@ -14,6 +14,8 @@ use gdnative::core_types::TypedArray;
use rand::Rng;
use std::f32::consts::TAU;
+mod phasor;
+
mod osc;
use osc::Osc;
diff --git a/klangfarbrs/src/phasor.rs b/klangfarbrs/src/phasor.rs
new file mode 100644
index 0000000..00372c0
--- /dev/null
+++ b/klangfarbrs/src/phasor.rs
@@ -0,0 +1,46 @@
+use super::{SamplesPerSecond, Hz};
+
+type Phase = f32; // Phase will always be between 0.0 and 1.0.
+
+pub struct PhasorIter {
+ pub phase: Phase,
+ pub frequency: Hz,
+ sample_rate: SamplesPerSecond,
+}
+
+impl PhasorIter {
+ fn new(frequency: Hz, sample_rate: SamplesPerSecond) -> Self {
+ Self { phase: 0.0, frequency, sample_rate }
+ }
+}
+
+impl Iterator for PhasorIter {
+ type Item = Phase;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.phase += (self.frequency / self.sample_rate);
+
+ if self.phase > 1.0 { self.phase -= 1.0; }
+
+ Some(self.phase)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn it_produces_expected_next_value() {
+ let mut phasor = PhasorIter::new(10.0, 100.0);
+ assert_eq!(phasor.next(), Some(0.1))
+ }
+
+ #[test]
+ fn it_produces_expected_next_values() {
+ let phasor = PhasorIter::new(10.0, 100.0);
+ let next = phasor.take(11);
+ assert_eq!(0.1, next.last().unwrap())
+ // 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)
+ }
+}