Envelope implementation, help

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

nastyfingers wrote:
I tried storing a previous sample like with filters and mixing the 2 however this just causes signal overload.

Code: Select all

    currentSamp = prevSamp + (currentSamp * (1.0f / attack));
You're accumulating more than you should. Keep track of the current adsr level, separately:

Code: Select all

...
envLevel = envLevel + increment;
...
return envLevel;
Leave currentSamp out of the ADSR class and multiply it by the value returned by the ADSR::process method.

Post

Hi guys, thank for you input. I have been busy with other projects for a few days. Unfortunately this one is still bugging me. Have things moving through the envelope ( a bit buggily) as far as sustain. Release is proving tricky.

David, does making this change simply make the code easier to read? Not sure of the reasoning I ended up using while as oppose to if, and have never really seen the difference. Does it go deeper than the obvious?

bitwise, definitely no point in sending samples when the multiplier is all we need. I made changes made and have left another question.

How do I prevent the resulting coeff becoming to large before the relevant stage ends? I know there must be some sort of exponential maths to but just cannot figure it out. I suspect the answer is in on this http://www.kvraudio.com/forum/viewtopic ... highlight= thread but can't see how to translate it to my code.

my current code looks like this:

Code: Select all

#include <math.h>
#include "ADSR.h"


ADSR::ADSR()
{
	//stage = 1;
	//susTimer = 0;
	prevAttCoeff = 0.0f;
	currentAttCoeff = 0.0f;
	prevDecCoeff = 0.0f;
	currentDecCoeff = 0.0f;
	prevRelCoeff = 0.0f;
	currentRelCoeff = 0.0f;
}
ADSR::~ADSR()
{
}

double ADSR::process(float attack, float decay, float sustain, float release, int* pKeyState, long* pTime)
{
	//attack
	if(*pTime <= attack)
	{
		currentAttCoeff = (1.0f /attack) + prevAttCoeff;
		prevAttCoeff = currentAttCoeff;
		(*pTime)++;
		//stage = 1;
		return currentAttCoeff;
	}
	//decay	
	if(*pTime >= attack && *pTime < (attack + decay))
	{
		currentDecCoeff = ((1 - sustain) / decay) + prevDecCoeff;
		prevDecCoeff = currentDecCoeff;
		(*pTime)++;
		//stage = 2;
		return currentDecCoeff;
	}
	//sustain
	if(*pTime >= (attack + decay) && *pKeyState == 1)
	{
		//susTimer++;
		//stage = 3;
		return sustain;
	}
	//release
	if(*pKeyState == 2 && *pTime <= (attack + decay + release))
	{
		currentDecCoeff = sustain - (sustain / release) - prevRelCoeff;
		prevDecCoeff = currentDecCoeff;
		(*pTime)++;
		//stage = 4;
		return currentRelCoeff;
	}
	return -1.f;
}


Post

I replied ages ago guys but it just doesn't seem to want to appear on the thread. When I go to edit it all my text is there, just not on the thread. Can anyone see what I wrote?

Post Reply

Return to “DSP and Plugin Development”