summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--klangfarb/main.gd17
-rw-r--r--klangfarbrs/src/lib.rs21
2 files changed, 33 insertions, 5 deletions
diff --git a/klangfarb/main.gd b/klangfarb/main.gd
index f91dbb7..8abf461 100644
--- a/klangfarb/main.gd
+++ b/klangfarb/main.gd
@@ -1,10 +1,13 @@
extends AudioStreamPlayer
# controllable frequency interface
-export(float, 20, 8000, 10) var freq = 440.0
+export(float, 20, 8000, 5) var freq = 440.0
+# control wave form
+export(String, "sine", "square") var waveform = "sine"
# load the GDNative script connected to the rust lib
var Osc = preload("res://Osc.gdns")
+
# make an instance of our one "class" in rust lib
var wave = Osc.new()
@@ -20,10 +23,18 @@ func _fill_buffer() -> void:
# playback stream buffer
playback.push_buffer(wave.frames(freq, to_fill))
-func _process(_delta):
- _fill_buffer()
+func _process(delta):
+ if waveform == "square":
+ wave.square()
+ playback.clear_buffer()
+ else:
+ wave.sine()
+ playback.clear_buffer()
+ _fill_buffer()
func _ready() -> void:
+ # buffer length of 100ms gives us ~realtime response to input changes
+ self.stream.buffer_length = 0.1
# ensure Godot/Sine have the same sample rate
wave.set_sample_rate(self.stream.mix_rate)
# get our AudioStreamPlayback object
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;
}