Envelope help

#1

So I had implemented a sinewave oscillator with a delay buffer and it worked with sound coming through and delaying, however, I wanted to take it a step further by adding a envelope for the wave and when I implemented the envelope no sound was coming through at all. Below I list the classes that the methods that are working with the envelope.

    void getEnvelopeParams(float* attack, float* decay, float* sustain, float* release)
    {
        env1.setAttack(*attack);
        env1.setDecay(*decay);
        env1.setSustain(*sustain);
        env1.setRelease(*release);
    }
    
    double setEnvelope()
    {
        return env1.adsr(setOscType(), env1.trigger);
    }
    double setEnvelopeWithTailOff()
    {
        return env1.adsr(setOscType(), env1.trigger);
    }
 void renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples) override
    {
        if (time != 0.0)
        {
            if (tailOff > 0)
            {
                while (--numSamples >= 0)
                {
                    const float currentSample = setEnvelopeWithTailOff();
                    
                    for (int i = outputBuffer.getNumChannels(); --i >= 0;)
                        outputBuffer.addSample (i, startSample, currentSample);
                    
                    phaseAngle += time;
                    ++startSample;
                    
                    tailOff *= 0.99;
                    
                    if (tailOff <= 0.005)
                    {
                        clearCurrentNote();
                        
                        time = 0.0;
                    }
                }
            }
            else
            {
                while (--numSamples >= 0)
                {
                    const float currentSample = setEnvelope();
                    
                    for (int i = outputBuffer.getNumChannels(); --i >= 0;)
                        outputBuffer.addSample (i, startSample, currentSample);
                    
                    phaseAngle += time;
                    ++startSample;
                }
            }
        }
    }
double oscEnv::adsr(double input, int trigger) {
    
    if (trigger==1 && attackphase!=1 && holdphase!=1 && decayphase!=1){
        holdcount=0;
        decayphase=0;
        sustainphase=0;
        releasephase=0;
        attackphase=1;
    }
    
    if (attackphase==1) {
        releasephase=0;
        amplitude+=(1*attack);
        output=input*amplitude;
        
        if (amplitude>=1) {
            amplitude=1;
            attackphase=0;
            decayphase=1;
        }
    }
    
    
    if (decayphase==1) {
        output=input*(amplitude*=decay);
        if (amplitude<=sustain) {
            decayphase=0;
            holdphase=1;
        }
    }
    
    if (holdcount<holdtime && holdphase==1) {
        output=input*amplitude;
        holdcount++;
    }
    
    if (holdcount>=holdtime && trigger==1) {
        output=input*amplitude;
    }
    
    if (holdcount>=holdtime && trigger!=1) {
        holdphase=0;
        releasephase=1;
    }
    
    if (releasephase==1 && amplitude>0.) {
        output=input*(amplitude*=release);
        
    }
    
    return output;
}
void oscEnv::setAttack(double attackMS) {
    attack = ( attackMS * sampleRate * 0.001 );
}

void oscEnv::setRelease(double releaseMS) {
    release = ( releaseMS * sampleRate * 0.001 );
}

void oscEnv::setSustain(double sustainL) {
    sustain = sustainL;
}

void oscEnv::setDecay(double decayMS) {
    decay = ( decayMS * sampleRate * 0.001 );
}

Thanks!

#2

This sort of thing is easy to figure out with a debugger. Put a breakpoint in each of the if statemetns in your adsr function and see which, if any of them, are being hit?

#3

Does currentSample actually contain audio? I guess not. Your function names make this very hard to follow though. ‘setEnvelopeWithTailOff’ actually returns audio? What does “setOscType()” do?

#4

I actually was able to solve my problem it had to do with my actual wave generator, sorry for not editing.