From 3eb0824c6477bc7720b3ed0ac49f58a34cfa0546 Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Fri, 17 Dec 2021 20:41:38 -0600 Subject: Add: more detailed Risset's Bell implementation also hooks duration up to the main synth to control the length of the Instrument notes that get played, since they aren't subject to the ADSR envelope --- klangfarb/main.gd | 4 +++- klangfarbrs/src/instrument.rs | 26 +++++++++++++++++++++++++- klangfarbrs/src/lib.rs | 13 +++++++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/klangfarb/main.gd b/klangfarb/main.gd index 46c8c0b..d36a49e 100644 --- a/klangfarb/main.gd +++ b/klangfarb/main.gd @@ -11,7 +11,8 @@ 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 +# Used for the total duration of an Instrument note +export(int, 0, 30000, 100) var duration = 3000 #Attack/Decay/Release/Sustain export(int, 0, 5000, 100) var attack = 100 export(int, 0, 5000, 100) var decay = 100 @@ -71,6 +72,7 @@ func _process(_delta): synth.set_sustain(sustain) synth.set_release(release) synth.play_instrument(play_instrument) + synth.duration(duration) _check_waveform() _fill_buffer() diff --git a/klangfarbrs/src/instrument.rs b/klangfarbrs/src/instrument.rs index bf08fe4..3170464 100644 --- a/klangfarbrs/src/instrument.rs +++ b/klangfarbrs/src/instrument.rs @@ -1,4 +1,4 @@ -use super::{ Partial, Sample, Hz, SamplesPerSecond }; +use super::{ Partial, Millisecond, Sample, Hz, SamplesPerSecond }; pub struct Instrument { pub partials: Vec, @@ -13,6 +13,28 @@ impl Instrument { } } + pub fn bell(base_freq: Hz, duration: Millisecond, sample_rate: SamplesPerSecond) -> Self { + let bell_partials = vec![ + (1.0, 1.0, 0.56, 0.0), + (0.67, 0.9, 0.56, 1.0), + (1.0, 0.65, 0.82, 0.0), + (1.8, 0.55, 0.92, 1.7), + (2.67, 0.325, 1.19, 0.0), + (1.67, 0.35, 1.7, 0.0), + (1.46, 0.25, 2.0, 0.0), + (1.33, 0.2, 2.74, 0.0), + (1.33, 0.15, 3.0, 0.0), + (1.0, 0.1, 3.76, 0.0), + (1.33, 0.075, 4.07, 0.0), + ]; + + Self { + partials: bell_partials.iter() + .map(|&p| Partial::new(p.0, p.1, p.2, p.3, sample_rate, duration, base_freq)) + .collect() + } + } + pub fn sample(&mut self) -> Sample { match self.next() { Some(s) => { s }, @@ -52,3 +74,5 @@ mod tests { assert_eq!(inst.last(), Some(0.0)); } } + +// Partial::new(1.0, 1.0, p, 0.0, sample_rate, 2000, base_freq) diff --git a/klangfarbrs/src/lib.rs b/klangfarbrs/src/lib.rs index de3322a..aa133e7 100644 --- a/klangfarbrs/src/lib.rs +++ b/klangfarbrs/src/lib.rs @@ -82,14 +82,14 @@ impl MonoSynth { Self { osc: Osc::new(freq, sprt), - instrument: Instrument::new(freq, vec![0.56, 0.92, 1.19, 1.7, 2.0, 2.74, 3.0, 3.76, 4.07], sprt), + instrument: Instrument::bell(freq, 10000, sprt), sample_rate: sprt, frequency: freq, apply_bend: false, phasor_bend: Vector2::new(0.0, 0.0), play_instrument: false, continuous: true, - duration: 0, + duration: 5000, envelope: Envelope::new(30, 500, 0.5, 1000, sprt), cutoff: 0.0, frequency_modulation: false, @@ -184,6 +184,11 @@ impl MonoSynth { self.fm_depth = fm_depth } + #[export] + fn duration(&mut self, _owner: &Node, duration: Millisecond) { + self.duration = duration + } + #[export] fn set_attack(&mut self, _owner: &Node, attack: Millisecond) { self.attack = attack @@ -215,7 +220,7 @@ impl MonoSynth { #[export] fn trigger(&mut self, _owner: &Node, ) { - self.instrument = Instrument::new(self.frequency, vec![0.56, 0.92, 1.19, 1.7, 2.0, 2.74, 3.0, 3.76, 4.07], self.sample_rate); + self.instrument = Instrument::bell(self.frequency, self.duration, self.sample_rate); self.envelope = Envelope::new(self.attack, self.decay, self.sustain, self.release, self.sample_rate); } @@ -244,7 +249,7 @@ impl MonoSynth { // self.phasor.phase = next_phase; // } - if !self.continuous { + if !self.continuous && !self.play_instrument { sample *= match self.envelope.next() { Some(a) => a, None => 0.0, -- cgit v1.2.3