#ifndef FMODAUDIOPROCESSOR_H_
#define FMODAUDIOPROCESSOR_H_

#include <vector>
#include <map>
#include <fmod.h>
#include <fmod_errors.h>
#include "Graphics/Graphics.h"
#include "DFunc/debug.h"
#include "Components/Component.h"
#include "Libraries/fftw3.h"

//propagates the fft spectrum data in the fmod sound system as well as the 
//statistical variance spectrum data.  it also sends out a message on a beat
//change the threshold values at low/mid/high to change the sensitivity of the
//algorithm

class FModAudioProcessor : public Component
{
	public:
		FModAudioProcessor();
		virtual void processEvent(Event *event);
		virtual void update();
		virtual void process();
		virtual ~FModAudioProcessor();
	private:
		FMOD_SYSTEM 							*system;
		FMOD_RESULT 							result;							//for error handling
//stuff to compute the spectrum analysis.  spectrum data stays in the
//FModManager
		float									bass_beat_energy,				//when a beat's detected, these are to be set to the 
												mid_beat_energy,				//"power" of the beat.
												high_beat_energy;
												
		float 									decibel_cutoff;					//cutoff value used for converting spectrum into decibels
		
		unsigned short							bb_up_counter, 					//used to count the processes between
												bb_mid_up_counter, 				//successive beats, so very tight beats
												bb_high_up_counter;				//get filtered and considered 1 beat.

		unsigned short							bass_cutoff, 					//band count (which can be directly converted to freq. :44100/2/1024
												mid_cutoff, 					//for crossovers.  bass_cutoff of 24 for instance will cross over at
												high_cutoff;					//44100/2/1024 * 24 = 515.625hz [a good number to use]

		unsigned short							history_size;					//how far to compute history. anything more than 1 second is bad

		float 									bass_threshold, 				//constants to by mulitplied by the variance of the mean energies
												mid_threshold, 					//in each of the subbands to determine if a beat is strong enough
												high_threshold;					//to count
												
		std::vector<std::vector<float> >		e_history;						//history of energy over a period of time for each subband
		
//stuff needed for fftw (fast fourier transformations)
		std::vector<float>						e_fft_buffer;					//stores the fourier transformed data for each subband
		fftw_complex 							*fftw_in, *fftw_out;			//buffers to hold the fftw data
		fftw_plan 								fftw_p;							//fftw execution plan
};

#endif
