Sine wave problems

Hi everyone!

I feel rather stupid asking this, but:
I’m trying to implement a very simple sine oscillator (not caring about performance or aliasing yet), and I’m stumbling on a problem:

As you can see, it isn’t a continous sine wave.
My guess it has something to do with the buffer ‘resetting’, and my sine wave starting again from naught.

My code (in processBlock):

void GirafficSynthAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
const int totalNumInputChannels = getTotalNumInputChannels();
const int totalNumOutputChannels = getTotalNumOutputChannels();

for (int i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
    buffer.clear (i, 0, buffer.getNumSamples());
//float* samplesL = buffer.getWritePointer(0);
//float* samplesR = buffer.getWritePointer(1);
float samplerate = getSampleRate();
const int numSamples = buffer.getNumSamples();
for (int i = 0; i < numSamples; ++i) {
    buffer.setSample(0, i, sinf((2 * float_Pi / samplerate) * frequency * i));
    buffer.setSample(1, i, sinf((2 * float_Pi / samplerate) * frequency * i));


Does anyone have some input on this? I’ve googled a ton already without much succes.

Your loop iterating over samples is just dependent on i, so when the next buffer starts, your sine wave starts from zero as you realized.
You need a counter that survives over the boundaries of the processBlock call. Either you have a second counter as member variable or more performant, add numSamples at the end.
In the constructor you set offset = 0, in sinf you replace i by i+offset, and after the loop call offset += numSamples.
Good luck

Really helpful answer.

You may want to check out the JUCE tutorial on how to build a sine wave synth. It provides a minimal reference implementation of what you are doing: