Skip to content

Commit

Permalink
Cascade: smoothly morph between functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alefnull committed Sep 3, 2022
1 parent d8c2677 commit 156f23c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
52 changes: 42 additions & 10 deletions src/Envelope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Envelope {
};

Stage stage = IDLE;
Func func = EASE_OUT;
float func = 0.f;
float _env = MIN_VALUE;
float env = 0;
float rise_time = 0.01f;
Expand All @@ -40,7 +40,7 @@ struct Envelope {
void set_loop(bool loop) {
this->loop = loop;
}
void set_function(Func func){
void set_function(float func){
this->func = func;
}
void trigger() {
Expand Down Expand Up @@ -70,22 +70,54 @@ struct Envelope {
break;
case RISING:
eoc = false;
switch(func){
case EASE_OUT: _env += (1.f - _env) * 6.21461f * st / rise_time; break;
case LINEAR: _env += st / rise_time; break;
case EASE_IN: _env += (_env) * 6.21461f * st / rise_time; break;
if (func < 0.f) {
func = abs(func);
float func1 = (1.f - _env) * 6.21461f * st / rise_time;
float func2 = st / rise_time;
float out = func1 * func + func2 * (1.f - func);
_env += out;
}
else if (func > 0.f) {
float func1 = _env * 6.21461f * st / rise_time;
float func2 = st / rise_time;
float out = func1 * func + func2 * (1.f - func);
_env += out;
}
else {
_env += st / rise_time;
}
// switch(func){
// case EASE_OUT: _env += (1.f - _env) * 6.21461f * st / rise_time; break;
// case LINEAR: _env += st / rise_time; break;
// case EASE_IN: _env += (_env) * 6.21461f * st / rise_time; break;
// }
if (_env >= MAX_VALUE) {
_env = MAX_VALUE;
stage = FALLING;
}
break;
case FALLING:
switch(func){
case EASE_OUT: _env -= (_env) * 6.21461f * st / fall_time; break;
case LINEAR: _env -= st / fall_time; break;
case EASE_IN: _env -= (1.f- _env) * 6.21461f * st / fall_time; break;
if (func < 0.f) {
func = abs(func);
float func1 = _env * 6.21461f * st / fall_time;
float func2 = st / fall_time;
float out = func1 * func + func2 * (1.f - func);
_env -= out;
}
else if (func > 0.f) {
float func1 = (1.f - _env) * 6.21461f * st / fall_time;
float func2 = st / fall_time;
float out = func1 * func + func2 * (1.f - func);
_env -= out;
}
else {
_env -= st / fall_time;
}
// switch(func){
// case EASE_OUT: _env -= (_env) * 6.21461f * st / fall_time; break;
// case LINEAR: _env -= st / fall_time; break;
// case EASE_IN: _env -= (1.f- _env) * 6.21461f * st / fall_time; break;
// }
if (_env <= MIN_VALUE) {
_env = MIN_VALUE;
eoc = true;
Expand Down
7 changes: 4 additions & 3 deletions src/funcgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ struct Funcgen : Module {
configOutput(ABSDB_OUTPUT, "abs(B - D)");
configOutput(ABSDC_OUTPUT, "abs(C - D)");

configSwitch(ENV_FUNC_PARAM, 0, 2, 1, "Envelope Function", {"Ease Out","Linear","Ease In"});
configParam(ENV_FUNC_PARAM, -1.f, 1.f, 0.f, "Envelope Function");

if (mode == CASCADE) {
cm_envelope[0].retrigger();
Expand All @@ -175,7 +175,8 @@ struct Funcgen : Module {
mode = CHAOTIC_CASCADE;
}

Envelope::Func env_func = (Envelope::Func)params[ENV_FUNC_PARAM].getValue();
float env_func = params[ENV_FUNC_PARAM].getValue();

for (int i = 0; i < CHANNEL_COUNT; i++) {
float rise_time = params[RISE_PARAM + i].getValue();
float fall_time = params[FALL_PARAM + i].getValue();
Expand Down Expand Up @@ -438,7 +439,7 @@ struct FuncgenWidget : ModuleWidget {
}
x = x_start;
y = box.size.y - (RACK_GRID_WIDTH * 2);
addParam(createParamCentered<CKSSThree>(Vec(x, y), module, Funcgen::ENV_FUNC_PARAM));
addParam(createParamCentered<RoundSmallBlackKnob>(Vec(x, y), module, Funcgen::ENV_FUNC_PARAM));
x += dx;
addParam(createParamCentered<TL1105>(Vec(x, y), module, Funcgen::CASCADE_TRIGGER_PARAM));
x += dx;
Expand Down

0 comments on commit 156f23c

Please sign in to comment.