From 7bd9dd418ac6215094feb87087fcb59499c3aa69 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Wed, 27 Oct 2021 22:24:23 -0400 Subject: Fix pitch in rust code and limit to 3 second duration --- klangfarb/Main.tscn | 1 - klangfarb/libklangfarbrs.so | Bin 20371272 -> 20371304 bytes klangfarb/main.gd | 28 ++++++++++++++++------------ klangfarbrs/src/lib.rs | 8 ++++---- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/klangfarb/Main.tscn b/klangfarb/Main.tscn index 0a0ad3b..1a32577 100644 --- a/klangfarb/Main.tscn +++ b/klangfarb/Main.tscn @@ -3,7 +3,6 @@ [ext_resource path="res://main.gd" type="Script" id=2] [sub_resource type="AudioStreamGenerator" id=1] -mix_rate = 8000.0 [node name="Audio" type="AudioStreamPlayer"] stream = SubResource( 1 ) diff --git a/klangfarb/libklangfarbrs.so b/klangfarb/libklangfarbrs.so index 75b22dc..c7b448a 100755 Binary files a/klangfarb/libklangfarbrs.so and b/klangfarb/libklangfarbrs.so differ diff --git a/klangfarb/main.gd b/klangfarb/main.gd index 62137bc..d88d431 100644 --- a/klangfarb/main.gd +++ b/klangfarb/main.gd @@ -1,4 +1,3 @@ -tool extends AudioStreamPlayer var playback: AudioStreamPlayback = null @@ -6,28 +5,33 @@ var MonoBuffer = preload("res://MonoBuffer.gdns") var buffer = MonoBuffer.new() var samples = buffer.frames var sample_count = samples.size() +var current_count = 0 - -func _create_generator() -> void: - stream = AudioStreamGenerator.new() - stream.mix_rate = 8000.0 # Setting mix rate is only possible before play(). - playback = get_stream_playback() +#func _create_generator() -> void: +# stream = AudioStreamGenerator.new() +# stream.mix_rate = 44100.0 # Setting mix rate is only possible before play(). +# playback = get_stream_playback() func _fill_buffer() -> void: var to_fill = playback.get_frames_available() - while to_fill > 0: - var sample_index = to_fill % 512 + + 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. # _phase = fmod(_phase + frequency() / GDawConfig.sample_rate, 1.0) # _update_state() to_fill -= 1 + current_count += 1 -func _process(delta): - _fill_buffer() +func _process(_delta): + if current_count < sample_count: + _fill_buffer() + else: + self.stop() func _ready() -> void: - _create_generator() - print(sample_count) + self.stream.mix_rate = 44100.0 + playback = self.get_stream_playback() _fill_buffer() self.play() # _update_envelope() diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 9bf1ded..9638690 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -10,13 +10,13 @@ pub struct MonoBuffer { } pub fn fill_frames() -> TypedArray { - let tau = std::f32::consts::FRAC_PI_2; let frequency = 440.0; - let sample_rate = 8000.0; + let sample_rate = 44100.0; + let duration = sample_rate * 3.0; let mut frames = TypedArray::new(); - for i in 0..512 { - frames.push(f32::sin(tau * frequency * i as f32/sample_rate)); + for i in 0..duration as i32 { + frames.push(f32::sin(std::f32::consts::TAU * frequency * i as f32/sample_rate)); } return frames -- cgit v1.2.3