From 4565611763cbb041384230580f9c7de4fea7da6f Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Sat, 30 Oct 2021 23:56:38 -0500 Subject: Add: square wave and waveform switch interface in godot --- klangfarbrs/src/lib.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'klangfarbrs') diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index a418995..7954eb2 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -23,9 +23,9 @@ pub struct Osc { } /// The various waveforms the `Osc` can generate. -enum Waveform { +pub enum Waveform { Sine, - // Square, + Square, // Triangle, // Saw, // Noise, @@ -36,6 +36,13 @@ fn generate_sample(osc: &Osc) -> f32 { match osc.waveform { Waveform::Sine => { (TAU * osc.phase).sin() + }, + Waveform::Square => { + if osc.phase < 0.5 { + -1.0 + } else { + 1.0 + } } } } @@ -78,6 +85,16 @@ impl Osc { godot_print!("DAS IST KLANGFARBRS.") } + #[export] + fn sine(&mut self, _owner: &Node) { + self.waveform = Waveform::Sine + } + + #[export] + fn square(&mut self, _owner: &Node) { + self.waveform = Waveform::Square + } + #[export] pub fn set_sample_rate(&mut self, _owner: &Node, sample_rate: f32) { self.sample_rate = sample_rate; -- cgit v1.2.3