summaryrefslogtreecommitdiff
path: root/klangfarb/main.gd
diff options
context:
space:
mode:
Diffstat (limited to 'klangfarb/main.gd')
-rw-r--r--klangfarb/main.gd24
1 files changed, 18 insertions, 6 deletions
diff --git a/klangfarb/main.gd b/klangfarb/main.gd
index 82f8ce9..fb29c8c 100644
--- a/klangfarb/main.gd
+++ b/klangfarb/main.gd
@@ -1,22 +1,34 @@
extends AudioStreamPlayer
-var playback: AudioStreamPlayback = null
+# controllable frequency interface
+export(float, 20, 8000, 10) var freq = 440.0
+
+# load the GDNative script connected to the rust lib
var SineWave = preload("res://SineWave.gdns")
+# make an instance of our one "class" in rust lib
var wave = SineWave.new()
-var freq = 440.0
+
+# initialize the Godot stream we fill up with samples
+var playback: AudioStreamPlayback = null
func _fill_buffer() -> void:
+ # get count of Frames (sample, sample) available in stream
var to_fill = playback.get_frames_available()
- print(to_fill)
if to_fill > 0:
+ # ask Rust to generate N frames at freq
+ # Array<Vector2> gets pushed to the
+ # playback stream buffer
playback.push_buffer(wave.frames(freq, to_fill))
func _process(_delta):
- _fill_buffer()
+ _fill_buffer()
func _ready() -> void:
- self.stream.mix_rate = freq * 256
- wave.set_sample_rate(freq * 256)
+ # ensure Godot/Sine have the same sample rate
+ wave.set_sample_rate(self.stream.mix_rate)
+ # get our AudioStreamPlayback object
playback = self.get_stream_playback()
+ # prefill the stream's sample buffer (which feeds DAC)
_fill_buffer()
+ # start the audio
self.play()