put parameters in separate struct

This commit is contained in:
Chris
2023-08-01 16:51:12 +02:00
parent 252a741e32
commit bc6c9b85ff

View File

@@ -15,9 +15,7 @@ enum env_state {
release2 release2
}; };
class tx_envelope { struct env_params {
public:
env_state state;
float attack1_rate; float attack1_rate;
float attack1_level; float attack1_level;
float attack2_rate; float attack2_rate;
@@ -29,20 +27,14 @@ public:
float release1_rate; float release1_rate;
float release1_level; float release1_level;
float release2_rate; float release2_rate;
};
class tx_envelope {
public:
env_state state;
tx_envelope(bool _retrigger = false) tx_envelope(bool _retrigger = false)
: samplerate { 44100. } : 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 } , level { 0.f }
, phase { 0 } , phase { 0 }
, state { idle } , 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_x1 = (int)ms_to_samples(_params.attack1_rate);
int attack_mid_x2 = (int)ms_to_samples(attack2_rate); int attack_mid_x2 = (int)ms_to_samples(_params.attack2_rate);
int hold_samp = (int)ms_to_samples(hold_rate); int hold_samp = (int)ms_to_samples(_params.hold_rate);
int decay_mid_x1 = (int)ms_to_samples(decay1_rate); int decay_mid_x1 = (int)ms_to_samples(_params.decay1_rate);
int decay_mid_x2 = (int)ms_to_samples(decay2_rate); int decay_mid_x2 = (int)ms_to_samples(_params.decay2_rate);
int release_mid_x1 = (int)ms_to_samples(release1_rate); int release_mid_x1 = (int)ms_to_samples(_params.release1_rate);
int release_mid_x2 = (int)ms_to_samples(release2_rate); int release_mid_x2 = (int)ms_to_samples(_params.release2_rate);
// if note on is triggered, transition to attack phase // if note on is triggered, transition to attack phase
if (trigger) { if (trigger) {
@@ -77,7 +69,7 @@ public:
if (state == attack1) { if (state == attack1) {
// while in attack phase // while in attack phase
if (phase < attack_mid_x1) { 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; phase += 1;
} }
// reset phase if parameter was changed // reset phase if parameter was changed
@@ -94,7 +86,7 @@ public:
if (state == attack2) { if (state == attack2) {
// while in attack phase // while in attack phase
if (phase < attack_mid_x2) { 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; phase += 1;
} }
// reset phase if parameter was changed // reset phase if parameter was changed
@@ -125,7 +117,7 @@ public:
if (state == decay1) { if (state == decay1) {
// while in decay phase // while in decay phase
if (phase < decay_mid_x1) { 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; phase += 1;
} }
// reset phase if parameter was changed // reset phase if parameter was changed
@@ -142,7 +134,7 @@ public:
if (state == decay2) { if (state == decay2) {
// while in decay phase // while in decay phase
if (phase < decay_mid_x2) { 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; phase += 1;
} }
// reset phase if parameter was changed // reset phase if parameter was changed
@@ -153,19 +145,19 @@ public:
if (phase == decay_mid_x2) { if (phase == decay_mid_x2) {
state = sustain; state = sustain;
phase = 0; phase = 0;
level = sustain_level; level = _params.sustain_level;
} }
} }
// while sustain phase: if note off is triggered, transition to release phase // while sustain phase: if note off is triggered, transition to release phase
if (state == sustain && !gate) { if (state == sustain && !gate) {
state = release1; state = release1;
level = sustain_level; level = _params.sustain_level;
} }
// release 1st half // release 1st half
if (state == release1) { if (state == release1) {
// while in release phase // while in release phase
if (phase < release_mid_x1) { 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; phase += 1;
} }
// reset phase if parameter was changed // reset phase if parameter was changed
@@ -182,7 +174,7 @@ public:
if (state == release2) { if (state == release2) {
// while in release phase // while in release phase
if (phase < release_mid_x2) { 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; phase += 1;
} }
// reset phase if parameter was changed // reset phase if parameter was changed
@@ -206,34 +198,34 @@ public:
this->samplerate = sampleRate; this->samplerate = sampleRate;
} }
// returns the x/y coordinates of the envelope points as a list for graphical representation. // converts the x/y coordinates of the envelope points as a list for graphical representation.
std::array<float, 18> calc_coordinates() { std::array<float, 18> calc_coordinates(env_params _params) {
float a_x = 0; float a_x = 0;
float a_y = 0; float a_y = 0;
float b_x = attack1_rate; float b_x = _params.attack1_rate;
float b_y = attack1_level; 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 c_y = 1;
float d_x = c_x + hold_rate; float d_x = c_x + _params.hold_rate;
float d_y = 1; float d_y = 1;
float e_x = d_x + decay1_rate; float e_x = d_x + _params.decay1_rate;
float e_y = decay1_level; float e_y = _params.decay1_level;
float f_x = e_x + decay2_rate; float f_x = e_x + _params.decay2_rate;
float f_y = sustain_level; float f_y = _params.sustain_level;
float g_x = f_x + 125; 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_x = g_x + _params.release1_rate;
float h_y = release1_level; 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 i_y = 0;
float total = i_x; float total = i_x;