#pragma once

#include "ofMain.h"
//#include "fft.h"
#include "aubioAnalyzer.h"
#include "accFFT.h"

#define BUFFER_SIZE 256
#define NUM_WINDOWS 80


//Examples used for particles from "Mastering openFrameworks: Creative Coding Demystified"
//Particle class for the portal
class Particle {
    public:
        Particle();                //Class constructor
        void setup();              //Start particle
        void update( float dt );   //Recalculate physics
        void draw(float scaledVol);//Draw particle
        
        ofPoint pos;               //Position
        ofPoint vel;               //Velocity
        float time;                //Time of living
        float lifeTime;            //Allowed lifetime
        bool live;                 //Is particle live
};

//Control parameters class
class Params {
public:
    void setup();
    ofPoint eCenter;    //Emitter center
    float eRad;         //Emitter radius
    float velRad;       //Initial velocity limit
    float lifeTime;     //Lifetime in seconds
    
    float rotate;   //Direction rotation speed in angles per second
    
    float force;       //Attraction/repulsion force inside emitter
    float spinning;    //Spinning force inside emitter
    float friction;    //Friction, in the range [0, 1]
};


extern Params param; //Declaration of a global variable

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

        void keyPressed(int key);
        void keyReleased(int key);
        void mouseMoved(int x, int y );
        void mouseDragged(int x, int y, int button);
        void mousePressed(int x, int y, int button);
        void mouseReleased(int x, int y, int button);
        void windowResized(int w, int h);
        void dragEvent(ofDragInfo dragInfo);
        void gotMessage(ofMessage msg);
                    
        void audioIn(float * input, int bufferSize, int nChannels);
        
        float * left;
        float * right;
        vector <float> volHistory;
        
        int 	bufferCounter;
        int 	drawCounter;
    
        double multiplyer;
    
        float magnitude[BUFFER_SIZE];
        float phase[BUFFER_SIZE];
        float power[BUFFER_SIZE];
    
        float freq[NUM_WINDOWS][BUFFER_SIZE/2];
        float freq_phase[NUM_WINDOWS][BUFFER_SIZE/2];
    
        float smoothedVol;
        float scaledVol;
    
        float totalNoise;
        float decayDelay; //How long to wait before decaying the sound/closing portal
    
        //for particles
        vector<Particle> p;	  //Particles
        ofFbo fbo;            //Off-screen buffer for trails
        float history;        //Control parameter for trails
        float time0;          //Time value for computing dt
        float bornRate;       //Particles born rate per second
        float bornCount;      //Integrated number of particles to born
    
        int MAX_RAD;
        int MIN_RAD;
        int toContract;
        int lastNoiseFrame; //Keeps track of last time portal incremented
        
        aubioAnalyzer AA;
        float prevPitch;
        
        ofSoundStream soundStream;
    
    //spectrogram
    int fftSize, fftHopsize;
    fft_complex_double* fftOutput;
    accFFT* audiofft;//((int)256, (int)1);
    double* fftInput;
    double* fftWindow;
    
    typedef std::vector<double> DoubleVector;
    typedef std::vector<DoubleVector> DoubleMatrix;
    DoubleVector fftSampleBuffer;
    DoubleMatrix magnitudeSpectrum;
    void addToFFT(float sample);
    void setFFTinput();
    void set_window_hanning(double* windowBuffer, const int framesize);
    
    void drawSpectrogram();
    
    
		
};
