Fixing integer overflow in wave file
What you have there is &quot;wrap around&quot; and a bad case at that.
It's easy enough to fix though. Let's call your samples $x[n]$ and your max amplitude $x_{max}$, so your signal is currently bounded to $[-x_{max},+x_{max}]$
Go through the waveform sample by sample. Whenever you see a jump that's larger than your max amplitude, add or subtract twice the max amplitude to bring it back to where it was.
Something like
for (i = 1; i&lt;n; i++)
{
   sampleDiff = x[i]-x[i-1];
   if (sampleDiff &gt; xmax) { x[i] = x[i] - 2*xmax; }
   if (sampleDiff &lt; -xmax) { x[i] = x[i] + 2*xmax;}
}

Make sure you are using a data type that accommodate a data range of at least  $[-2x_{max},+2x_{max}]$, which is quite likely NOT the original data type. Also make sure you don't start in a &quot;wrapped&quot; region, i.e. x[0] needs to be good