I was having this issue a bit ago, so I followed @martinrobinson-2 advice in this thread and it seemed to fix the issue. Then, I was testing the same project today and noticed the very occasional click/pop, but not consistently at the beginning/end of midi notes like before. I changed the renderNextBlock()
function to keep track of when the output samples were making large jumps:
void FmVoice::renderNextBlock(juce::AudioBuffer<float> &outputBuffer, int startSample, int numSamples)
{
for(int i = 0; i < numSamples; ++i)
{
//handle FM routing
applyModulations();
//calculate LFO effects
for(int lfo = 0; lfo < 4; ++ lfo)
{
applyLfo(lfo);
}
auto sum = 0.0f;
//add up the samples from all the operators set to 'audible'
for(int o = 0; o < operatorCount; ++o)
{
auto newSample = operators[o]->sample(fundamental);
if(operators[o]->isAudible)
sum += newSample;
}
//add that sum to the output buffer
for(int channel = 0; channel < outputBuffer.getNumChannels(); ++channel)
{
outputBuffer.addSample(channel, i + startSample, sum);
}
auto difference = fabs(sum - lastSample);
if(difference > 0.2)
{
printf("Output jumped: %f at sample %d buffer %d\n", difference, i, numBuffers);
}
lastSample = sum;
}
printf("Buffer %d completed\n", numBuffers);
numBuffers++;
}
The results of those last two printf statements are strange, it seems like almost every buffer finishes without any big jump, but occasionally a jump will occur, always on sample 0. Those jumps then seem to come in groups, each one occurring maybe 3-4 buffers after the last. It looks like this:
buffer 9659 completed
buffer 9660 completed
Output jumped: 0.531951 at sample 0 buffer 9661
buffer 9661 completed
buffer 9662 completed
buffer 9663 completed
buffer 9664 completed
buffer 9665 completed
Output jumped: 0.555626 at sample 0 buffer 9666
buffer 9666 completed
Output jumped: 0.488747 at sample 0 buffer 9667
With 512 samples per buffer and a sample rate of 44.1kHz, it seems like those jumps are coming far too close together to be caused by a single midi on/off message, but I can’t think what might be causing the first sample of a buffer to change dramatically in this sort of pattern. Any ideas as to what might be causing this are greatly appreciated.