Env function generator - Change shape keeping rate

I’m trying to make my first module keeping inspiration from Befaco Rampage.

I see that if I change the shape of the rise (or fall) stage, it doens’t change only the shape, but also the “rate” (i.e. the time it would take) to finish the stage.

How would you keep the rate?

Here’s the quick code that shown the number of steps that change, switching shape at some cycle/sample (at the 4° cycle, 120° sample):

#include <iostream>
#include <math.h>

const double sampleRate = 44100.0;

inline float interpolate(float a, float b, float frac) { return a + frac * (b - a); }

inline float shapeDelta(float delta, float length, float shape) {
	float lin = (copysignf(1.0f, delta) * 10.0) / length;

	float exp = (M_E * delta) / length;
	return interpolate(lin, exp, shape * 0.90f);
}

int main() {
    float in = 10.0;
    float out = 0.0;    
    
    float shape = 0.0;
    float lengthInSeconds = 1e-2; // * powf(2.0, 1.0 * 10.0);

    int numCycles = 10;
    for(int cycle = 0; cycle < numCycles; cycle++) {
        int numSteps = 0;
        while (in - out > 1e-3) {    
            // change shapes
            if(cycle == 4 && numSteps == 120) {
                shape = 1.0;
            }

            float delta = in - out;
            out += shapeDelta(delta, lengthInSeconds, shape) * (1.0 / sampleRate);
            
            numSteps++;
        }
        
        std::cout << "cycle: " << cycle << " | num steps: " << numSteps << " | value: " << out << std::endl;
        out -= 10.0;
    }
}

It should always takes 441 sample to complete the cycle :slight_smile:

Can you help me? Some tips?

In the for loop numSteps can only reach 10 before breaking out of the loop so it will never go into the if (cycle&&numSteps), shape will always be 0.0 as far I can tell. numSteps would need an initial assignment of 12 to increment to 120, in order to enter the if statement.

true && true == true
true && false == false
true || false == true

Also where does cout get printed to. Never thought of using it myself.

?!? Not It loops correctly. Check the code and the outputs. There is a while into for, be aware of that.

Cout is just for test in a dev env, not used in Rack or course.

I know what cout does where does it output?

In the for loop numSteps++; which will eventually be numSteps = 10; It will only loop 10 times because numCycles = 10; when cycle gets to 10 it breaks from the loop. numSteps can’t reach 120 it can only reach 10. The if in the while will never execute because numSteps is not equal to 120 it’s equal to 10.

Did you open the link mate? http://coliru.stacked-crooked.com/a/1c496c30759a597f

Its all there. Check the output in the bottom.
numSteps is ++ within the “while” (>1e-3).

Then reset on each for loop, as out, and so on :stuck_out_tongue:

I see its in the while now my mistake. Not sure how it executes the if though because numSteps is still not equal to 120. :face_with_raised_eyebrow: 441 != 120 numSteps == 120 is false

It goes to your terminal, if you start Rack from a terminal with the -d switch.

2 Likes

cout and printf always print to stdout, but Rack’s DEBUG, INFO, and ERROR functions write to stdout or log.txt depending on the runtime mode of Rack.

1 Like

The console, if you are running a Linux build. :wink:

Ok, now that we have clear where cout print… (:stuck_out_tongue:)

Whats about my question? Is a phase acc needed? Or I can do the same with some tricks? (Such as rescale “rise” due to shape).

Thanks