summaryrefslogtreecommitdiff
path: root/klangfarbrs
diff options
context:
space:
mode:
Diffstat (limited to 'klangfarbrs')
-rw-r--r--klangfarbrs/src/lib.rs21
1 files changed, 19 insertions, 2 deletions
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
+ }
}
}
}
@@ -79,6 +86,16 @@ impl Osc {
}
#[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;
}