From bc6c9b85ffff2e0fa465ebf4969620c46d273ca5 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 1 Aug 2023 16:51:12 +0200 Subject: [PATCH] put parameters in separate struct --- synth/tx_envelope.h | 80 ++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/synth/tx_envelope.h b/synth/tx_envelope.h index 75d622b..71edd9c 100644 --- a/synth/tx_envelope.h +++ b/synth/tx_envelope.h @@ -15,9 +15,7 @@ enum env_state { release2 }; -class tx_envelope { -public: - env_state state; +struct env_params { float attack1_rate; float attack1_level; float attack2_rate; @@ -29,20 +27,14 @@ public: float release1_rate; float release1_level; float release2_rate; +}; + +class tx_envelope { +public: + env_state state; tx_envelope(bool _retrigger = false) : samplerate { 44100. } - , attack1_rate { 0 } - , attack1_level { 0 } - , attack2_rate { 0 } - , hold_rate { 0 } - , decay1_rate { 0 } - , decay1_level { 0 } - , decay2_rate { 0 } - , sustain_level { 0 } - , release1_rate { 0 } - , release1_level { 0 } - , release2_rate { 0 } , level { 0.f } , phase { 0 } , state { idle } @@ -54,15 +46,15 @@ public: { } - float process_sample(bool gate, bool trigger) { + float process_sample(bool gate, bool trigger, env_params& _params) { - int attack_mid_x1 = (int)ms_to_samples(attack1_rate); - int attack_mid_x2 = (int)ms_to_samples(attack2_rate); - int hold_samp = (int)ms_to_samples(hold_rate); - int decay_mid_x1 = (int)ms_to_samples(decay1_rate); - int decay_mid_x2 = (int)ms_to_samples(decay2_rate); - int release_mid_x1 = (int)ms_to_samples(release1_rate); - int release_mid_x2 = (int)ms_to_samples(release2_rate); + int attack_mid_x1 = (int)ms_to_samples(_params.attack1_rate); + int attack_mid_x2 = (int)ms_to_samples(_params.attack2_rate); + int hold_samp = (int)ms_to_samples(_params.hold_rate); + int decay_mid_x1 = (int)ms_to_samples(_params.decay1_rate); + int decay_mid_x2 = (int)ms_to_samples(_params.decay2_rate); + int release_mid_x1 = (int)ms_to_samples(_params.release1_rate); + int release_mid_x2 = (int)ms_to_samples(_params.release2_rate); // if note on is triggered, transition to attack phase if (trigger) { @@ -77,7 +69,7 @@ public: if (state == attack1) { // while in attack phase if (phase < attack_mid_x1) { - level = lerp(0, start_level, (float)attack_mid_x1, attack1_level, (float)phase); + level = lerp(0, start_level, (float)attack_mid_x1, _params.attack1_level, (float)phase); phase += 1; } // reset phase if parameter was changed @@ -94,7 +86,7 @@ public: if (state == attack2) { // while in attack phase if (phase < attack_mid_x2) { - level = lerp(0, attack1_level, (float)attack_mid_x2, 1, (float)phase); + level = lerp(0, _params.attack1_level, (float)attack_mid_x2, 1, (float)phase); phase += 1; } // reset phase if parameter was changed @@ -125,7 +117,7 @@ public: if (state == decay1) { // while in decay phase if (phase < decay_mid_x1) { - level = lerp(0, 1, (float)decay_mid_x1, decay1_level, (float)phase); + level = lerp(0, 1, (float)decay_mid_x1, _params.decay1_level, (float)phase); phase += 1; } // reset phase if parameter was changed @@ -142,7 +134,7 @@ public: if (state == decay2) { // while in decay phase if (phase < decay_mid_x2) { - level = lerp(0, decay1_level, (float)decay_mid_x2, sustain_level, (float)phase); + level = lerp(0, _params.decay1_level, (float)decay_mid_x2, _params.sustain_level, (float)phase); phase += 1; } // reset phase if parameter was changed @@ -153,19 +145,19 @@ public: if (phase == decay_mid_x2) { state = sustain; phase = 0; - level = sustain_level; + level = _params.sustain_level; } } // while sustain phase: if note off is triggered, transition to release phase if (state == sustain && !gate) { state = release1; - level = sustain_level; + level = _params.sustain_level; } // release 1st half if (state == release1) { // while in release phase if (phase < release_mid_x1) { - level = lerp(0, sustain_level, (float)release_mid_x1, release1_level, (float)phase); + level = lerp(0, _params.sustain_level, (float)release_mid_x1, _params.release1_level, (float)phase); phase += 1; } // reset phase if parameter was changed @@ -182,7 +174,7 @@ public: if (state == release2) { // while in release phase if (phase < release_mid_x2) { - level = lerp(0, release1_level, (float)release_mid_x2, 0, (float)phase); + level = lerp(0, _params.release1_level, (float)release_mid_x2, 0, (float)phase); phase += 1; } // reset phase if parameter was changed @@ -206,34 +198,34 @@ public: this->samplerate = sampleRate; } - // returns the x/y coordinates of the envelope points as a list for graphical representation. - std::array calc_coordinates() { + // converts the x/y coordinates of the envelope points as a list for graphical representation. + std::array calc_coordinates(env_params _params) { float a_x = 0; float a_y = 0; - float b_x = attack1_rate; - float b_y = attack1_level; + float b_x = _params.attack1_rate; + float b_y = _params.attack1_level; - float c_x = b_x + attack2_rate; + float c_x = b_x + _params.attack2_rate; float c_y = 1; - float d_x = c_x + hold_rate; + float d_x = c_x + _params.hold_rate; float d_y = 1; - float e_x = d_x + decay1_rate; - float e_y = decay1_level; + float e_x = d_x + _params.decay1_rate; + float e_y = _params.decay1_level; - float f_x = e_x + decay2_rate; - float f_y = sustain_level; + float f_x = e_x + _params.decay2_rate; + float f_y = _params.sustain_level; float g_x = f_x + 125; - float g_y = sustain_level; + float g_y = _params.sustain_level; - float h_x = g_x + release1_rate; - float h_y = release1_level; + float h_x = g_x + _params.release1_rate; + float h_y = _params.release1_level; - float i_x = h_x + release2_rate; + float i_x = h_x + _params.release2_rate; float i_y = 0; float total = i_x;