Sine wavetable

Hey all,

I’m new to Juce and C++. I’m having some difficulty with this code.
I don’t understand why this sounds extremely noisy. any help would be greatly appreciated.

void SinWavetableAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{

frequency = 440;
phase = 0;
wtSize = 1024;
increment = frequency * wtSize / sampleRate; 
amplitude = 0.25;

// One cycle of a sine wave
for (int i = 0; i < wtSize; i++) {
    wavetable.insert(i, sin(2.0 * double_Pi * i / wtSize));
}

}

void SinWavetableAudioProcessor::processBlock (AudioBuffer& buffer, MidiBuffer& midiMessages)
{

ScopedNoDenormals noDenormals;
auto totalNumInputChannels  = getTotalNumInputChannels();
auto totalNumOutputChannels = getTotalNumOutputChannels();

channels.
for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
buffer.clear (i, 0, buffer.getNumSamples());

for (int channel = 0; channel < totalNumInputChannels; ++channel)
{

    auto* samples = buffer.getWritePointer (channel);
    
    for (auto i = 0; i < buffer.getNumSamples(); i++) {
        samples[i] = wavetable[int(phase)] * amplitude;
        phase = fmod(phase + increment,wtSize);

    }
}

}

All your channels use the same phase variable, so the actual phase information for the continuity of each channel is lost.
A solution is a local phase variable for each channel, initialised with the phase member, and advancing the phase member for a whole block only once.

ahh I see. Thank you so much Daniel

1 Like

thanks. This helped a lot