Nidnogg wrote:

- Code: Select all
`for (int *sample = 0*; sample < bufferToFill.numSamples; ++sample)`

{

float value = targetAmplitude * sin(2 * double_Pi * targetFrequency * time);

monoBuffer[sample] = value;

time += deltaTime;

}

Here's your problem: you are accumulating time, then multiplying it by frequency to get phase. When the frequency changes, the phase jumps around as the time gets scaled differently. When interpolating, this happens over some period of time, during which you just get garbage.

To fix the problem you need to accumulate phase instead, moving the frequency scaling into the running sum. This way the phase stays continuous without any jumps and the frequency then simply controls how fast the phase accumulates.

The result then looks something like this:

- Code: Select all
`for ( .... )`

{

phase += targetFrequency;

float value = targetAmplitude * sin(2 * double_Pi * phase);

monoBuffer[sample] = value;

}

It's also worth keeping in mind that if you let the phase accumulate without any wrap-around, you'll run into numeric precision issues eventually (and if phase is single precision float, that happens sooner rather than later).

So normally you'd want to keep it within [0,1] range or something similar. One way to do that looks like this:

- Code: Select all
`phase += targetFrequency;`

if(phase > 1) phase -= 1;