From 31e34297ed073927a63d0d9fccafc868998b8f46 Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Sat, 13 Nov 2021 18:35:14 -0600 Subject: Clean: a bit of junk moved around, Add: Osc::sample method --- klangfarbrs/src/lib.rs | 29 ++++++----------------------- klangfarbrs/src/osc.rs | 9 ++++++++- klangfarbrs/src/phasor.rs | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 24 deletions(-) (limited to 'klangfarbrs/src') diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 6bc2e14..8bd1e50 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -13,7 +13,6 @@ use gdnative::prelude::*; use gdnative::core_types::TypedArray; mod phasor; -use phasor::{Phase}; mod osc; use osc::{Osc, Waveform}; @@ -51,24 +50,6 @@ pub struct MonoSynth { current_envelope_position: usize, } -pub struct Bender {} - -impl Bender { - // for (i = 0; i < nframes; ++i) { - // if (in[i] < x0) - // out[i] = (y0/x0)*in[i]; - // else - // out[i] = ((1-y0)/(1-x0)) * (in[i] - x0) + y0; - // } - fn bend(phase: Phase, phasor_bend: Vector2) -> f32 { - if phase < phasor_bend.x { - (phasor_bend.y / phasor_bend.x) * phase - } else { - ((1.0 - phasor_bend.y) / (1.0 - phasor_bend.x)) * (phase - phasor_bend.x) + phasor_bend.y - } - } -} - #[methods] impl MonoSynth { /// # Examples @@ -96,7 +77,7 @@ impl MonoSynth { frequency_modulation: false, fm_frequency: 10.0, fm_depth: 0.1, - fm_osc: Osc::new(10.0, sprt), + fm_osc: Osc::new(freq * 2.0, sprt), current_envelope_position: 0, } } @@ -138,6 +119,7 @@ impl MonoSynth { #[export] fn frequency(&mut self, _owner: &Node, frequency: Hz) { + self.frequency = frequency; self.osc.set_frequency(frequency) } @@ -167,7 +149,7 @@ impl MonoSynth { } #[export] - fn fm_frequency(&mut self, _owner: &Node, fm_frequency: f32) { + fn fm_frequency(&mut self, _owner: &Node, fm_frequency: Hz) { self.fm_osc.set_frequency(fm_frequency) } @@ -192,13 +174,14 @@ impl MonoSynth { // let next_phase : f32; if self.frequency_modulation { - let modulation_value = self.fm_osc.next().unwrap() * self.fm_depth; + let modulation_value = self.fm_osc.sample() * self.fm_depth; self.osc.set_frequency(self.osc.get_frequency() + modulation_value); } - let mut sample = self.osc.next().unwrap(); + let mut sample = self.osc.sample(); self.osc.last_value = sample; + // TODO: // if self.apply_bend { // self.phasor.phase = Bender::bend(next_phase, self.phasor_bend); // } else { diff --git a/klangfarbrs/src/osc.rs b/klangfarbrs/src/osc.rs index 8225eb1..c609286 100644 --- a/klangfarbrs/src/osc.rs +++ b/klangfarbrs/src/osc.rs @@ -1,7 +1,7 @@ use std::f32::consts::TAU; use rand::Rng; use super::{Hz, Sample}; -use super::phasor::{Phasor}; +use super::phasor::Phasor; /// The various waveforms the `MonoSynth` can generate. pub enum Waveform { @@ -35,6 +35,13 @@ impl Osc { pub fn set_frequency(&mut self, frequency: Hz) { self.phasor.frequency = frequency; } + + pub fn sample(&mut self) -> Sample { + match self.next() { + Some(s) => { s }, + None => 0.0 + } + } } impl Iterator for Osc { diff --git a/klangfarbrs/src/phasor.rs b/klangfarbrs/src/phasor.rs index ace374a..5bfc79e 100644 --- a/klangfarbrs/src/phasor.rs +++ b/klangfarbrs/src/phasor.rs @@ -46,3 +46,24 @@ mod tests { // 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) } } + + +// TODO: fixup bender/modulator to allow phase modulation again +// pub struct Bender {} + +// impl Bender { +// // for (i = 0; i < nframes; ++i) { +// // if (in[i] < x0) +// // out[i] = (y0/x0)*in[i]; +// // else +// // out[i] = ((1-y0)/(1-x0)) * (in[i] - x0) + y0; +// // } +// fn bend(phase: Phase, phasor_bend: Vector2) -> f32 { +// if phase < phasor_bend.x { +// (phasor_bend.y / phasor_bend.x) * phase +// } else { +// ((1.0 - phasor_bend.y) / (1.0 - phasor_bend.x)) * (phase - phasor_bend.x) + phasor_bend.y +// } +// } +// } + -- cgit v1.2.3