Question

Problem

I'm trying to figure this out for a while to no avail - how to continuously change the pitch of a note between two frequencies.

I'm generating the audio signal with a function similar to this:

double SineGenerator(double time, double frequency)
{
    return Math.Sin(frequency * time * 2 * Math.PI);
}

While I manage time manually outside of this function, like so:

time += 1.0 / sampleRate;
double sample = SineGenerator(time, frequency);

And the obvious solution of linearly interpolating the frequency parameter over the desired amount of samples does not work as expected. The sound wave gets filled with pops and clicks when doing this.

How must I implement this instead?

Solution (Edit)

Thanks to geofftnzless, this was surprisingly easy to solve. I have switched to using this generator now:

double SineGenerator(ref double time, double frequency, int sampleRate)
{
    return Math.Sin(time += (frequency * 2 * Math.PI) / sampleRate);
}

Which takes care of incrementing time internally.

Using this generator interpolating frequency works smoothly and with not audible problems. Thanks!

Was it helpful?

Solution

Instead of multiplying frequency by time, use frequency to increment time by the appropriate amount. That way time is continuous and you wont get jumps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top