Pitch fluctuates unexpectedly in simple sine function I wrote a very basic C program to play a sine of a user-specified frequency. In the interest of portability, I have it spit values directly to stdout, so hopefully you can reproduce my problem on your own machine. This is the code I wrote:

#include &lt;math.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;

#define RATE 48000
#define TWOPI (3.14159 * 2.0)

int main(int argc, char** argv) {
        float frequency = atof(argv[1]);
        float phase = 0.0;
        float increment = TWOPI/RATE;
        int16_t sample;
        while (1) {
                sample = sin(phase) * 32767;
                putchar(sample &amp; 255);
                putchar(sample &gt;&gt; 8);
                phase += increment * frequency;
        }
        return 0;
}


It can be compiled with:

cc sine.c -lm


and played with

./a.out 440.0 | sox -r 48000 -c 1 -t s16 - -d


That is: 16 bit mono audio at a sampling rate of 48,000 Hz.

I've tested this on both OpenBSD and MacOS and have noticed the same behavior: at around 6 seconds, the pitch of the wave jumps slightly. This happens again around 12 seconds. Further changes will take place as the wave continues to play. I have also replaced the sin() while loop with a prebuilt wavetable of a sine accessed through linear interpolation, only to notice the same problems.

Any ideas what might be off here?
You are using 32-bit floats, and not resetting the phase by subtracting 2 pi.  That means the phase will eventually climb up to where the sin() function's phase unwrapping algorithm doesn't have enough bits of quantization, or valid mantissa, left over afterward unwrapping.  

Add this:

if (phase &gt; M_PI) { phase -= 2.0 * M_PI; }


inside your loop somewhere.