summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--klangfarb/Main.tscn2
-rw-r--r--klangfarb/main.gd24
-rw-r--r--klangfarbrs/src/lib.rs19
4 files changed, 25 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index 320eeb1..60df760 100644
--- a/Makefile
+++ b/Makefile
@@ -7,3 +7,6 @@ build:
refresh_lib: build
cp $(LIB_PATH) klangfarb
+
+release:
+ cd klangfarbrs && cargo build --release
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()
diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs
index 1489667..0628e34 100644
--- a/klangfarbrs/src/lib.rs
+++ b/klangfarbrs/src/lib.rs
@@ -5,12 +5,14 @@ use std::f32::consts::TAU;
#[derive(NativeClass)]
#[inherit(Node)]
-pub struct SineWave {}
+pub struct SineWave {
+ sample_rate: f32
+}
#[methods]
impl SineWave {
fn new(_owner: &Node) -> Self {
- SineWave {}
+ SineWave { sample_rate: 44100.0 }
}
#[export]
@@ -19,12 +21,17 @@ impl SineWave {
}
#[export]
- pub fn frames(&self, _owner: &Node, frequency: f32, sample_rate: f32, duration: i32) -> TypedArray<f32> {
+ fn set_sample_rate(&mut self, _owner: &Node, sample_rate: f32) {
+ self.sample_rate = sample_rate;
+ }
+
+ #[export]
+ pub fn frames(&self, _owner: &Node, frequency: f32, duration: i32) -> TypedArray<Vector2> {
let mut frames = TypedArray::new();
- let calculated_duration = sample_rate * duration as f32;
- for i in 0..calculated_duration as i32 {
- frames.push((TAU * frequency * i as f32/sample_rate).sin());
+ for i in 0..duration as i32 {
+ let sample = (TAU * frequency * i as f32/self.sample_rate).sin();
+ frames.push(Vector2::new(sample, sample));
}
return frames