#include "Osc.h"
#include <stdio.h>

Osc :: Osc(double aF, double aA, double aPhase)
{
  f = aF;
  A = aA;
  phase = aPhase;

  rVal = cos(phase);
  iVal = sin(phase);
  rInc = cos(f * TWO_PI_OVER_SRATE);
  iInc = sin(f * TWO_PI_OVER_SRATE);
}

Osc :: ~Osc()
{
}

void Osc :: setFreq(double aF)
{
  f = aF;
  rInc = cos(f * TWO_PI_OVER_SRATE);
  iInc = sin(f * TWO_PI_OVER_SRATE);
}

void Osc :: setPhase(double aPhase)
{
  phase = aPhase;
}

void Osc :: setAmp(double aA)
{
  A = aA;
}


double Osc :: tick()
{
  double temp;

		
  temp = rVal*rInc - iVal*iInc;
  iVal = rVal*iInc + iVal*rInc;
  rVal = temp;             //rVal + iVal*i = (a+b*i)*(c+d*i)
  count++;

  if (count>=CorrectionThreshold)
  {
    temp = rVal*rVal + iVal*iVal;
    temp = (3-temp)*.5;  //an approximation for 1/sqrt(temp)
    rVal *= temp;
    iVal *= temp;
    count=0;
  }
  
  return A*iVal;
}
