About AdEnv and AudioCallback()

Hi everyone,

I realize that this is probably a very noobish question because I feel that I’m missing sth. very fundamental about how DSP programming works in general. I hope this is the correct place to post this so bear with me, thank you.

I’ve been playing with a simple OSC with an AdEnv triggered repeatedly by a Metro based on the examples.

I have experience in other programming languages but am fairly new to C++ so I was surprised that the following stopped working as expected when I moved some bits of code out of the for loop in the AudioCallback because I assumed that shouldn’t break things.

static void AudioCallback(float *in, float *out, size_t size)
{
    float osc_out, env_out;

    // this works when inside the for loop but doesn't like this
    if(tick.Process())
    {
        env.Trigger();
    }
    env_out = env.Process();

    osc.SetAmp(env_out);
    osc_out = osc.Process();
    // until here

    for(size_t i = 0; i < size; i += 2)
    {
        out[LEFT]  = osc_out;
        out[RIGHT] = osc_out;
    }
}

In this case the envelop stops working correctly. It still triggers but really slow, the oscillator continues to generate sound just fine.

I assumed the env.Process() just returns a float to scale the amplitude of OSC. But why is it not enough to call env.Process() once per Samplerate Callback but has to be called in the for loop or is this maybe related to the Metro?

I hope nobody takes offense by the basic nature of my question. If anyone can point me to some resources to read up on I’d be very thankful!

Thanks in advance and have a great weekend

Metronome runs at audio rate - that means that tick.process() must be called once per sample, which is what happens when it’s inside for loop. When you move it outside the for loop, it’s called once per audio block. This means that your metronome runs 48 times slower (by default - unless you’ve also changed audio block size). So tick.process() must be called per sample whether you like it or not (referencing the comment in your code).

You could theoretically set metronome at x48 speed from what you want to have and run that outside the loop. Then you’ll get desired metronome speed. But also you would create unnecessary jitter, because you would be triggering metronome once per buffer.

2 Likes

Aha! Thank you so much for the clarification!

I completely neglected that AudioCallback works on a block of audio. That explains everything (I was afraid it was sth. obvious I was missing).

Thanks again for taking the time to help me out. Hope you have a great day :slight_smile:

1 Like