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.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; }
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;
}
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;