From bb833fe84ac4c3b89c8f8cc00f5fc4ac8c5b6d9f Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Fri, 3 Dec 2021 14:24:36 -0500 Subject: Additive synthesis works! Clean up needed --- klangfarb/.import/.gdignore | 1 + klangfarb/main.gd | 32 +++++++++--------- klangfarbrs/src/instrument.rs | 6 ++-- klangfarbrs/src/lib.rs | 76 +++++++++++++++++++++---------------------- 4 files changed, 58 insertions(+), 57 deletions(-) create mode 100644 klangfarb/.import/.gdignore diff --git a/klangfarb/.import/.gdignore b/klangfarb/.import/.gdignore new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/klangfarb/.import/.gdignore @@ -0,0 +1 @@ + diff --git a/klangfarb/main.gd b/klangfarb/main.gd index 86ebc87..590d45b 100644 --- a/klangfarb/main.gd +++ b/klangfarb/main.gd @@ -41,19 +41,19 @@ func _fill_buffer() -> void: # playback stream buffer playback.push_buffer(synth.frames(to_fill)) -func _check_waveform(): - if waveform == "square": - synth.square() - elif waveform == "sine": - synth.sine() - elif waveform == "triangle": - synth.triangle() - elif waveform == "sawtooth": - synth.sawtooth() - elif waveform == "white_noise": - synth.white_noise() - elif waveform == "brown_noise": - synth.brown_noise() +#func _check_waveform(): + #if waveform == "square": + #synth.square() + #elif waveform == "sine": + #synth.sine() + #elif waveform == "triangle": + #synth.triangle() + #elif waveform == "sawtooth": + #synth.sawtooth() + #elif waveform == "white_noise": + #synth.white_noise() + #elif waveform == "brown_noise": + #synth.brown_noise() func _process(_delta): if self.is_playing(): @@ -68,7 +68,7 @@ func _process(_delta): synth.set_decay(decay) synth.set_sustain(sustain) synth.set_release(release) - _check_waveform() + # _check_waveform() _fill_buffer() func _ready() -> void: @@ -79,7 +79,7 @@ func _ready() -> void: # get our AudioStreamPlayback object playback = self.get_stream_playback() # prefill the stream's sample buffer (which feeds DAC) - _check_waveform() + #_check_waveform() _fill_buffer() func _input(event): @@ -89,7 +89,7 @@ func _input(event): synth.trigger() elif event is InputEventMouseMotion: freq = event.position.x - synth.frequency(freq) + # synth.frequency(freq) # phasor_bend.x = event.position.x / 1024 # phasor_bend.y = event.position.y / 600 fm_multiplier = 600 / (event.position.y + 1) diff --git a/klangfarbrs/src/instrument.rs b/klangfarbrs/src/instrument.rs index 65da6b6..3a381dc 100644 --- a/klangfarbrs/src/instrument.rs +++ b/klangfarbrs/src/instrument.rs @@ -1,8 +1,8 @@ use super::{ Osc, Envelope, Sample }; -struct Instrument { - osc_bank: Vec, - envelope: Envelope, +pub struct Instrument { + pub osc_bank: Vec, + pub envelope: Envelope, } impl Instrument { diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index 753f211..7cdb214 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -37,7 +37,7 @@ type Millisecond = u32; #[derive(NativeClass)] #[inherit(Node)] pub struct MonoSynth { - pub osc: Instrument, + pub instrument: Instrument, pub sample_rate: SamplesPerSecond, pub frequency: Hz, pub apply_bend: bool, @@ -75,7 +75,7 @@ impl MonoSynth { let sprt = 48000.0; Self { - osc: Osc::new(freq, sprt), + instrument: Instrument{osc_bank: vec![Osc::new(freq, sprt), Osc::new(400.0, sprt)], envelope: Envelope::new(30, 500, 0.5, 1000, sprt)}, sample_rate: sprt, frequency: freq, apply_bend: false, @@ -100,41 +100,41 @@ impl MonoSynth { godot_print!("DAS IST KLANGFARBRS.") } - #[export] - fn sine(&mut self, _owner: &Node) { - self.osc.waveform = Waveform::Sine - } + // #[export] + // fn sine(&mut self, _owner: &Node) { + // self.osc.waveform = Waveform::Sine + // } - #[export] - fn square(&mut self, _owner: &Node) { - self.osc.waveform = Waveform::Square - } + // #[export] + // fn square(&mut self, _owner: &Node) { + // self.osc.waveform = Waveform::Square + // } - #[export] - fn triangle(&mut self, _owner: &Node) { - self.osc.waveform = Waveform::Triangle - } + // #[export] + // fn triangle(&mut self, _owner: &Node) { + // self.osc.waveform = Waveform::Triangle + // } - #[export] - fn sawtooth(&mut self, _owner: &Node) { - self.osc.waveform = Waveform::Sawtooth - } + // #[export] + // fn sawtooth(&mut self, _owner: &Node) { + // self.osc.waveform = Waveform::Sawtooth + // } - #[export] - fn white_noise(&mut self, _owner: &Node) { - self.osc.waveform = Waveform::WhiteNoise - } + // #[export] + // fn white_noise(&mut self, _owner: &Node) { + // self.osc.waveform = Waveform::WhiteNoise + // } - #[export] - fn brown_noise(&mut self, _owner: &Node) { - self.osc.waveform = Waveform::BrownNoise - } + // #[export] + // fn brown_noise(&mut self, _owner: &Node) { + // self.osc.waveform = Waveform::BrownNoise + // } - #[export] - fn frequency(&mut self, _owner: &Node, frequency: Hz) { - self.frequency = frequency; - self.osc.set_frequency(frequency) - } + // #[export] + // fn frequency(&mut self, _owner: &Node, frequency: Hz) { + // self.frequency = frequency; + // self.osc.set_frequency(frequency) + // } #[export] fn continuous(&mut self, _owner: &Node, state: bool) { @@ -202,7 +202,7 @@ impl MonoSynth { #[export] fn trigger(&mut self, _owner: &Node, ) { - self.envelope = Envelope::new(self.attack, self.decay, self.sustain, self.release, self.sample_rate); + self.instrument.envelope = Envelope::new(self.attack, self.decay, self.sustain, self.release, self.sample_rate); } #[export] @@ -212,13 +212,13 @@ impl MonoSynth { for _i in 0..samples { // let next_phase : f32; - if self.frequency_modulation { - let modulation_value = self.fm_osc.sample() * self.fm_depth; - self.osc.set_frequency(self.osc.get_frequency() + modulation_value); - } + // if self.frequency_modulation { + // let modulation_value = self.fm_osc.sample() * self.fm_depth; + // self.osc.set_frequency(self.osc.get_frequency() + modulation_value); + // } - let mut sample = self.osc.sample(); - self.osc.last_value = sample; + let mut sample = self.instrument.sample(); + // self.osc.last_value = sample; // TODO: // if self.apply_bend { -- cgit v1.2.3