//----------------------------------------------------------
// name: "phasor"
//
// Code generated with Faust 2.81.0 (https://faust.grame.fr)
//----------------------------------------------------------

/* link with  */
#include <math.h>
#ifndef FAUSTFLOAT
#define FAUSTFLOAT float
#endif 


#ifndef FAUSTCLASS 
#define FAUSTCLASS mydsp
#endif

class mydsp : public dsp {
  private:
	static float 	fWave0[4];
	int 	idxfWave0;
	static float 	fWave1[11];
	int 	idxfWave1;
	int 	iVec0State; // Single Delay
	int 	pfPerm0;
	float 	fConst0; // step: 19
	// Recursion delay fVeeec0 is of type kMonoDelay
	// While its definition is of type kZeroDelay
	float 	fVeeec0State; // Mono Delay
	int fSampleRate;

  public:
	virtual void metadata(Meta* m) { 
		m->declare("filename", "phasor.dsp");
		m->declare("math.lib/author", "GRAME");
		m->declare("math.lib/copyright", "GRAME");
		m->declare("math.lib/deprecated", "This library is deprecated and is not maintained anymore. It will be removed in August 2017.");
		m->declare("math.lib/license", "LGPL with exception");
		m->declare("math.lib/name", "Math Library");
		m->declare("math.lib/version", "1.0");
		m->declare("music.lib/author", "GRAME");
		m->declare("music.lib/copyright", "GRAME");
		m->declare("music.lib/license", "LGPL with exception");
		m->declare("music.lib/name", "Music Library");
		m->declare("music.lib/version", "1.0");
		m->declare("name", "phasor");
	}

	virtual int getNumInputs() { return 0; }
	virtual int getNumOutputs() { return 3; }
	static void classInit(int sample_rate) {
	}
	virtual void instanceConstants(int sample_rate) {
		fSampleRate = sample_rate;
		idxfWave0 = 0;
		idxfWave1 = 0;
		pfPerm0 = 1;
		fConst0 = (7e+02f / min(1.92e+05f, max(1.0f, float(fSampleRate)))); // step: 19
	}
	virtual void instanceResetUserInterface() {
	}
	virtual void instanceClear() {
		iVec0State = 0;
		fVeeec0State = 0;
	}
	virtual void init(int sample_rate) {
		classInit(sample_rate);
		instanceInit(sample_rate);
	}
	virtual void instanceInit(int sample_rate) {
		instanceConstants(sample_rate);
		instanceResetUserInterface();
		instanceClear();
	}
	virtual mydsp* clone() {
		return new mydsp();
	}
	virtual int getSampleRate() {
		return fSampleRate;
	}
	virtual void buildUserInterface(UI* ui_interface) {
		ui_interface->openVerticalBox("phasor");
		ui_interface->closeBox();
	}
	virtual void compute (int count, FAUSTFLOAT** input, FAUSTFLOAT** output) {
		int 	iVec0[2];
		float 	fVeeec0;
		int fullcount = count;
		for (int index = 0; index < fullcount; index += 32) {
			int count = min(32, fullcount-index);
			FAUSTFLOAT* output0 = &output[0][index]; // Zone 3
			FAUSTFLOAT* output1 = &output[1][index]; // Zone 3
			FAUSTFLOAT* output2 = &output[2][index]; // Zone 3
			iVec0[1] = iVec0State;
			fVeeec0 = fVeeec0State;
			for (int i=0; i<count; i++) {
				float 	fTemp0 = fWave0[idxfWave0]; // step: 6
				float 	fTemp1 = fWave1[idxfWave1]; // step: 10
				iVec0[0] = (fTemp1 > 0.0f);
				int 	pfTemp0 = pfPerm0;
				pfPerm0 = iVec0[0];
				float 	fTemp2 = ((pfTemp0) ? fTemp0 : (fConst0 + fVeeec0)); // step: 22
				fVeeec0 = (fTemp2 - floorf(fTemp2));
				output0[i] = (FAUSTFLOAT)(fVeeec0);  // Zone Exec Code
				output1[i] = (FAUSTFLOAT)(fTemp1);  // Zone Exec Code
				output2[i] = (FAUSTFLOAT)(fTemp0);  // Zone Exec Code
				// post processing
				iVec0[1] = iVec0[0];
				idxfWave1 = (idxfWave1 + 1) % 11;
				idxfWave0 = (idxfWave0 + 1) % 4;
			}
			iVec0State = iVec0[1];
			fVeeec0State = fVeeec0;
		}
	}
};


float 	mydsp::fWave0[4] = {0.1f,0.8f,0.9f,0.2f};
float 	mydsp::fWave1[11] = {0.0f,0.0f,0.0f,0.0f,1.0f,1.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
