/*===================================================

Written by Alan Wolfe 5/2012

http://demofox.org

===================================================*/

#include "utils.h"
#include "wavefile.h"
#include "oscillators.h"
#include "Song.h"

//this is for the bandlimited noise test
#include <stdlib.h>

//the entry point of our application
int main(int argc, char **argv)
{
	//our parameters that all the wave forms use
	int nSampleRate = 44100;
	int nNumSeconds = 4;
	int nNumChannels = 1;
	float fFrequency = CalcFrequency(3,3); // middle C

	//make our buffer to hold the samples
	int nNumSamples = nSampleRate * nNumChannels * nNumSeconds;
	float *pData = new float[nNumSamples];

	//the phase of our oscilator
	float fPhase = 0;

	//make a band limited saw wave
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Saw_BandLimited(fPhase,fFrequency,(float)nSampleRate);
	}

	WriteWaveFileInt16("SawBL.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make a spacey noise
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Saw_BandLimited(fPhase,fFrequency * ((float)nIndex / (float)nNumSamples),(float)nSampleRate);
	}

	WriteWaveFileInt16("Spacey.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make an aliasing saw wave
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Saw(fPhase,fFrequency,(float)nSampleRate);
	}

	WriteWaveFileInt16("Saw.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make a band limited square wave
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Square_BandLimited(fPhase,fFrequency,(float)nSampleRate);
	}
	WriteWaveFileInt16("SquareBL.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make an aliasing square wave
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Square(fPhase,fFrequency,(float)nSampleRate);
	}
	WriteWaveFileInt16("Square.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make a band limited triangle wave
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Triangle_BandLimited(fPhase,fFrequency,(float)nSampleRate,2);
	}
	WriteWaveFileInt16("TriangleBL.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make an aliasing triangle wave
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Triangle(fPhase,fFrequency,(float)nSampleRate);
	}
	WriteWaveFileInt16("Triangle.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make some band limited random beeps
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		if(nIndex % 4000 == 0)
		{
			fFrequency = CalcFrequency(0,(float)(rand()%115));
		}
		pData[nIndex] = AdvanceOscilator_Sine(fPhase,fFrequency,(float)nSampleRate);
	}
	WriteWaveFileInt16("RandomBeeps.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//try making some bandlimited noise by making the band limited beeps faster
	fPhase = 0;
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		if(nIndex % 40 == 0)
		{
			fFrequency = CalcFrequency(0,(float)(rand()%115));
		}
		pData[nIndex] = AdvanceOscilator_Sine(fPhase,fFrequency,(float)nSampleRate);
	}
	WriteWaveFileInt16("NoiseBL.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//make some aliasing noise
	fPhase = 0;
	fFrequency = CalcFrequency(3,3);
	for(int nIndex = 0; nIndex < nNumSamples; ++nIndex)
	{
		pData[nIndex] = AdvanceOscilator_Noise(fPhase,fFrequency,(float)nSampleRate,nIndex > 0 ? pData[nIndex-1] : 0.0f);
	}
	WriteWaveFileInt16("Noise.wav",pData,nNumSamples,nNumChannels,nSampleRate);

	//render an example song
	RenderSong();

	//free our data buffer
	delete[] pData;
}