The code I posted is exactly the same as yours except that instead of loading a file I create the wavetable algorithmically. I don't see (or hear) any mistake in the linear interpolation code.
However, I now see that you are first incrementing the phase, then using it and only after using it you are wrapping it around, which means that you may be using an out-of-range value when you compute indexBelow
.
Try moving the while()
immediately after gPhase +=
.
If that is not enough to fix it, verify that there is nothing wrong with your wavetable (i.e.: the wav file). How did you create that file? Print out the values in setup after you assign gBuffer
and see if you can spot any weird value at the first or last element.
for(size_t n = 0; n < gBuffer.size(); ++n)
{
printf("%.3f ", gBuffer[n]);
if(0 == n % 16)
printf("\n");
}