Given any audio shader I make based on a single sine wave like this one:
vec2 mainSound( int samp, float time )
{
// A 440 Hz wave
return vec2( sin(6.2831*440.0*time)*.2);
}
I start to hear weird harmonics over the main one over time. You can prove this by clicking the image button on the right bottom side of the text box. This downloads a WAV file. If you write the code above and open that WAV in Audacity, you’ll see this:
Audacity spectrogram
Why does this happen?
Is it because of floating point error due to increasing float numbers?
How can it be prevented or mitigated?
I have tried doing a mod of time with PI*4 before doing the sine calculation, but it does not solve the issue completely and can add slight discontinuities.
Spectrogram with mod
Pere Moyà is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.