diff options
Diffstat (limited to 'klangfarbrs/src')
-rw-r--r-- | klangfarbrs/src/lib.rs | 38 | ||||
-rw-r--r-- | klangfarbrs/src/osc.rs | 37 |
2 files changed, 40 insertions, 35 deletions
diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 2721e2f..a96393f 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -11,7 +11,9 @@ use gdnative::prelude::*; use gdnative::core_types::TypedArray; -use std::f32::consts::TAU; + +mod osc; +use osc::{Osc}; /// Aliasing some types to distinguish various audio properties. type Sample = f32; @@ -56,40 +58,6 @@ pub struct MonoSynth { fm_phasor: Phasor, } -pub struct Osc {} - -impl Osc { - pub fn generate_sample(waveform: &Waveform, phase: Phase) -> Sample { - let phase = phase; - - match waveform { - Waveform::Sine => { - (TAU * phase).sin() - }, - - Waveform::Square => { - if phase < 0.5 { - -1.0 - } else { - 1.0 - } - }, - - Waveform::Triangle => { - if phase < 0.5 { - 4.0 * phase - 1.0 - } else { - 4.0 * (1.0 - phase) - 1.0 - } - }, - - Waveform::Sawtooth => { - 2.0 * phase - 1.0 - } - } - } -} - pub struct Phasor { pub phase: Phase, } diff --git a/klangfarbrs/src/osc.rs b/klangfarbrs/src/osc.rs new file mode 100644 index 0000000..9e3b147 --- /dev/null +++ b/klangfarbrs/src/osc.rs @@ -0,0 +1,37 @@ +use std::f32::consts::TAU; +use crate::{Waveform, Phase, Sample}; + +pub struct Osc {} + +impl Osc { + pub fn generate_sample(waveform: &Waveform, phase: Phase) -> Sample { + let phase = phase; + + match waveform { + Waveform::Sine => { + (TAU * phase).sin() + }, + + Waveform::Square => { + if phase < 0.5 { + -1.0 + } else { + 1.0 + } + }, + + Waveform::Triangle => { + if phase < 0.5 { + 4.0 * phase - 1.0 + } else { + 4.0 * (1.0 - phase) - 1.0 + } + }, + + Waveform::Sawtooth => { + 2.0 * phase - 1.0 + } + } + } +} + |