//----------------------------------------------------------
// author: "Grame"
// copyright: "(c)GRAME 2006"
// license: "BSD"
// name: "pitch-shifter"
// version: "1.0"
//
// Code generated with Faust 2.77.2 (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:
	float 	fVec0[65536]; // Ring Delay
	FAUSTFLOAT 	fslider0;
	FAUSTFLOAT 	fslider1;
	// Recursion delay fVeeec0 is of type kMonoDelay
	// While its definition is of type kZeroDelay
	float 	fVeeec0State; // Mono Delay
	FAUSTFLOAT 	fslider2;
	int 	IOTA;
	int fSampleRate;

  public:
	virtual void metadata(Meta* m) { 
		m->declare("author", "Grame");
		m->declare("copyright", "(c)GRAME 2006");
		m->declare("filename", "pitch_shifter.dsp");
		m->declare("license", "BSD");
		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", "pitch-shifter");
		m->declare("version", "1.0");
	}

	virtual int getNumInputs() { return 1; }
	virtual int getNumOutputs() { return 1; }
	static void classInit(int sample_rate) {
	}
	virtual void instanceConstants(int sample_rate) {
		fSampleRate = sample_rate;
	}
	virtual void instanceResetUserInterface() {
		fslider0 = 1e+03f;
		fslider1 = 0.0f;
		fslider2 = 1e+01f;
	}
	virtual void instanceClear() {
		for (int i = 0; i < 65536; i++) { fVec0[i] = 0; }
		fVeeec0State = 0;
		IOTA = 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("Pitch Shifter");
		ui_interface->addHorizontalSlider("shift (semitones)", &fslider1, 0.0f, -12.0f, 12.0f, 0.1f);
		ui_interface->addHorizontalSlider("window (samples)", &fslider0, 1e+03f, 5e+01f, 1e+04f, 1.0f);
		ui_interface->addHorizontalSlider("xfade (samples)", &fslider2, 1e+01f, 1.0f, 1e+04f, 1.0f);
		ui_interface->closeBox();
	}
	virtual void compute (int count, FAUSTFLOAT** input, FAUSTFLOAT** output) {
		float 	fSlow0 = float(fslider0); // step: 4
		float 	fSlow1 = powf(2.0f,(0.083333336f * float(fslider1))); // step: 9
		float 	fVeeec0;
		float 	fSlow2 = (1.0f / float(fslider2)); // step: 33
		int fullcount = count;
		for (int index = 0; index < fullcount; index += 32) {
			int count = min(32, fullcount-index);
			FAUSTFLOAT* input0 = &input[0][index]; // Zone 3
			FAUSTFLOAT* output0 = &output[0][index]; // Zone 3
			fVeeec0 = fVeeec0State;
			for (int i=0; i<count; i++) {
				float 	fTemp0 = (float)input0[i]; // step: 1
				int vIota0 = IOTA&65535;
				fVec0[vIota0] = fTemp0;
				fVeeec0 = fmodf((fSlow0 + ((fVeeec0 + 1.0f) - fSlow1)),fSlow0);
				float 	fTemp1 = fVeeec0; // step: 18
				int 	iTemp2 = int(fTemp1); // step: 19
				int vIota1 = (IOTA-(iTemp2 & 65535))&65535;
				float 	fTemp3 = float(iTemp2); // step: 22
				float 	fTemp4 = (1.0f - fTemp1); // step: 23
				int vIota2 = (IOTA-((iTemp2 + 1) & 65535))&65535;
				float 	fTemp5 = min((fSlow2 * fTemp1), 1.0f); // step: 35
				int 	iTemp6 = int((fSlow0 + fTemp1)); // step: 38
				int vIota3 = (IOTA-(iTemp6 & 65535))&65535;
				float 	fTemp7 = float(iTemp6); // step: 41
				int vIota4 = (IOTA-((iTemp6 + 1) & 65535))&65535;
				output0[i] = (FAUSTFLOAT)(((((fVec0[vIota1] * (fTemp3 + fTemp4)) + ((fTemp1 - fTemp3) * fVec0[vIota2])) * fTemp5) + (((fVec0[vIota3] * ((fTemp7 + fTemp4) - fSlow0)) + ((fSlow0 + (fTemp1 - fTemp7)) * fVec0[vIota4])) * (1.0f - fTemp5))));  // Zone Exec Code
				// post processing
				IOTA = IOTA+1;
			}
			fVeeec0State = fVeeec0;
		}
	}
};


