I am experiencing precision loss when im using doubles and i cant seem to find where the precision is lost. I am writing a software synthesizer and for some reason the input frequency for an oscillator(sound-wave generator) gets heavily aliased somewhere along the way. The oscillator is supposed to generate a triangle waveform and the correct frequency is passed to the method but when the result is played in the speakers i can clearly hear that the sound snaps to different frequencies.
I am using NAudio (http://naudio.codeplex.com/) and this method is run once for every sample i want to generate.
Here is the code:
double counter = 0;
int samplecounter = 0;
public double GetNextTriangle(double frequency)
{
double samplesperwave = (double)Parent.WaveFormat.SampleRate / frequency;
double length = (double)samplecounter / samplesperwave;
if (length.CompareTo(0.25) == -1)
{
counter = length * 4.0;
}
else if (length.CompareTo(0.75) == -1)
{
counter = 1.0 - (length - 0.25) * 4.0;
}
else
{
counter = -1.0 + (length - 0.75) * 4.0;
}
samplecounter++;
samplecounter = samplecounter > samplesperwave ? 0 : samplecounter;
return counter;
}
Thanks in advance! //Mats