summaryrefslogtreecommitdiff
path: root/klangfarbrs/src/osc.rs
diff options
context:
space:
mode:
Diffstat (limited to 'klangfarbrs/src/osc.rs')
-rw-r--r--klangfarbrs/src/osc.rs37
1 files changed, 37 insertions, 0 deletions
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
+ }
+ }
+ }
+}
+