From 21e720bb89e494cc89e99169e13fff9bf0dbdc6f Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Fri, 29 Oct 2021 19:44:06 -0500 Subject: Add: pack an Array> of samples and push_buffer - add a method to set the sample rate of the sine wave --- klangfarb/Main.tscn | 2 +- klangfarb/main.gd | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 17 deletions(-) (limited to 'klangfarb') diff --git a/klangfarb/Main.tscn b/klangfarb/Main.tscn index da55d83..95d51f7 100644 --- a/klangfarb/Main.tscn +++ b/klangfarb/Main.tscn @@ -6,5 +6,5 @@ [node name="Audio" type="AudioStreamPlayer"] stream = SubResource( 1 ) -volume_db = -27.262 +volume_db = -7.38 script = ExtResource( 2 ) diff --git a/klangfarb/main.gd b/klangfarb/main.gd index 568af36..82f8ce9 100644 --- a/klangfarb/main.gd +++ b/klangfarb/main.gd @@ -2,29 +2,21 @@ extends AudioStreamPlayer var playback: AudioStreamPlayback = null var SineWave = preload("res://SineWave.gdns") -var buffer = SineWave.new() -var duration = 3 -var samples = buffer.frames(440.0, 44100.0, duration) -var sample_count = samples.size() -var current_count = 0 +var wave = SineWave.new() +var freq = 440.0 func _fill_buffer() -> void: var to_fill = playback.get_frames_available() - - while to_fill > 0 && current_count != sample_count: - var sample_index = current_count - playback.push_frame(Vector2.ONE * samples[sample_index]) # Audio frames are stereo. - to_fill -= 1 - current_count += 1 + print(to_fill) + if to_fill > 0: + playback.push_buffer(wave.frames(freq, to_fill)) func _process(_delta): - if current_count < sample_count: - _fill_buffer() - else: - self.stop() + _fill_buffer() func _ready() -> void: - self.stream.mix_rate = 44100.0 + self.stream.mix_rate = freq * 256 + wave.set_sample_rate(freq * 256) playback = self.get_stream_playback() _fill_buffer() self.play() -- cgit v1.2.3