diff options
-rw-r--r-- | klangfarb/Main.tscn | 1 | ||||
-rw-r--r-- | klangfarb/MonoSynth.gdns (renamed from klangfarb/Osc.gdns) | 4 | ||||
-rw-r--r-- | klangfarb/main.gd | 32 | ||||
-rw-r--r-- | klangfarbrs/src/lib.rs | 19 |
4 files changed, 43 insertions, 13 deletions
diff --git a/klangfarb/Main.tscn b/klangfarb/Main.tscn index 896aa8c..e7a1b90 100644 --- a/klangfarb/Main.tscn +++ b/klangfarb/Main.tscn @@ -8,4 +8,3 @@ stream = SubResource( 1 ) volume_db = -13.216 script = ExtResource( 2 ) -freq = 130.0 diff --git a/klangfarb/Osc.gdns b/klangfarb/MonoSynth.gdns index 65d44ef..d795047 100644 --- a/klangfarb/Osc.gdns +++ b/klangfarb/MonoSynth.gdns @@ -3,6 +3,6 @@ [ext_resource path="res://klangfarbrs.gdnlib" type="GDNativeLibrary" id=1] [resource] -resource_name = "Osc" -class_name = "Osc" +resource_name = "MonoSynth" +class_name = "MonoSynth" library = ExtResource( 1 ) diff --git a/klangfarb/main.gd b/klangfarb/main.gd index 439bb50..95c0f8f 100644 --- a/klangfarb/main.gd +++ b/klangfarb/main.gd @@ -1,16 +1,28 @@ extends AudioStreamPlayer -# controllable frequency interface -export(float, 20, 8000, 5) var freq = 440.0 -export(float, 0, 1, 0.1) var bend = 0.5 # control wave form export(String, "sine", "square", "triangle", "sawtooth") var waveform = "sine" +# controllable frequency interface +export(float, 20, 8000, 5) var freq = 440.0 +# bending the waveform +export(bool) var apply_bend = false +export(Vector2) var phasor_bend = Vector2(0.5, 0.5) +# duration related +export(bool) var continuous = true +export(int, 0, 5000, 100) var duration = 3000 +#Attack/Decay/Release/Sustain +export(int, 0, 5000, 100) var attack = 100 +export(int, 0, 5000, 100) var decay = 100 +export(int, 0, 5000, 100) var release = 100 +export(float, 0.0, 1.0, 0.1) var sustain = 0.5 +#Cutoff +export(float, 20, 8000, 5) var cutoff = 6000 # load the GDNative script connected to the rust lib -var Osc = preload("res://Osc.gdns") +var MonoSynth = preload("res://MonoSynth.gdns") # make an instance of our one "class" in rust lib -var wave = Osc.new() +var wave = MonoSynth.new() # initialize the Godot stream we fill up with samples var playback: AudioStreamPlayback = null @@ -22,7 +34,7 @@ func _fill_buffer() -> void: # 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, bend)) + playback.push_buffer(wave.frames(to_fill)) func _check_waveform(): if waveform == "square": @@ -36,6 +48,9 @@ func _check_waveform(): func _process(_delta): if self.is_playing(): + wave.apply_bend(apply_bend) + wave.frequency(freq) + wave.phasor_bend(phasor_bend) _check_waveform() _fill_buffer() @@ -55,5 +70,6 @@ func _input(event): if event is InputEventMouseButton: print("Mouse Click/Unclick at: ", event.position) elif event is InputEventMouseMotion: - freq = event.position.x - print("Mouse Motion at: ", event.position) +# freq = event.position.x + phasor_bend.x = event.position.x / 1024 + phasor_bend.y = event.position.y / 600 diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 715d781..479885c 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -189,19 +189,34 @@ impl MonoSynth { } #[export] + fn frequency(&mut self, _owner: &Node, frequency: Hz) { + self.frequency = frequency + } + + #[export] + fn phasor_bend(&mut self, _owner: &Node, phasor_bend: Vector2) { + self.phasor_bend = phasor_bend + } + + #[export] + fn apply_bend(&mut self, _owner: &Node, apply_bend: bool) { + self.apply_bend = apply_bend + } + + #[export] pub fn set_sample_rate(&mut self, _owner: &Node, sample_rate: f32) { self.sample_rate = sample_rate; } #[export] - pub fn frames(&mut self, _owner: &Node, frequency: f32, samples: i32) -> TypedArray<Vector2> { + pub fn frames(&mut self, _owner: &Node, samples: i32) -> TypedArray<Vector2> { let mut frames = TypedArray::new(); for _i in 0..samples { let sample = Osc::generate_sample(&self.waveform, self.phasor.phase); frames.push(Vector2::new(sample, sample)); - let next_phase = self.phasor.next_phase(frequency, self.sample_rate); + let next_phase = self.phasor.next_phase(self.frequency, self.sample_rate); if self.apply_bend { self.phasor.phase = Bender::bend(next_phase, self.phasor_bend); } else { |