0
$\begingroup$

I'm making a sort of Envelope Function Generator. At the moment, I'm calculating only the the "rise" stage, which goes from 0 to 10 V; than, brutally, I back it to the beginning.

Here's the code I have, which emulate 10 cycles with a change of the shape (from linear to exp) at the 4° cycle and 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;
    }
}

The problem is that changing the shape, change the "lenght" of the generated signal (i.e. change the rate; just looks at the number of samples used for each cycle).

How can I change it without change the rate? And keeping a "non steppy" signal (i.e. avoid brutal transient).

$\endgroup$
1
$\begingroup$

In music, we would call this "wavetable synthesis", but most electrical engineers would call this Direct Digital Synthesis (DDS) or a Numerically Controlled Oscillator (NCO).

You need a phase accumulator, a sufficient number of points in your wavetable and a means to interpolate between points (if there are many points in the wavetable, linear interpolation is good enough). And if you're changing the shape of the waveform, you need to be able to cross-fade from the old wavetable to the new.

$\endgroup$
  • $\begingroup$ I already did such an envelope. But the One I've posted? Which technique Is? The signal Is made Step by step $\endgroup$ – markzzz Mar 13 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.