00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef CSL_Spectral_H
00024 #define CSL_Spectral_H
00025
00026 #include "CSL_Core.h"
00027 #include "BlockResizer.h"
00028
00029 #include "sndfile.h"
00030
00031 #include <fftw3.h>
00032
00033 #define FFTW_ENABLE_FLOAT
00034
00035 #ifdef FFTW_ENABLE_FLOAT
00036
00037 #define CSL_FFTW_sample sample
00038 #define CSL_FFTW_cmplx fftwf_complex
00039 #define CSL_FFTW_plan fftwf_plan
00040 #define CSL_FFTW_c2r_plan fftwf_plan_dft_c2r_1d
00041 #define CSL_FFTW_r2c_plan fftwf_plan_dft_r2c_1d
00042 #define CSL_FFTW_deplan fftwf_destroy_plan
00043 #define CSL_FFTW_free fftwf_free
00044 #define CSL_FFTW_exec fftwf_execute
00045 #define CSL_FFTW_alloc fftwf_malloc
00046
00047 #else
00048
00049 #define CSL_FFTW_sample double
00050 #define CSL_FFTW_plan fftw_plan
00051 #define CSL_FFTW_cmplx fftw_complex
00052 #define CSL_FFTW_c2r_plan fftw_plan_dft_c2r_1d
00053 #define CSL_FFTW_r2c_plan fftw_plan_dft_r2c_1d
00054 #define CSL_FFTW_deplan fftw_destroy_plan
00055 #define CSL_FFTW_free fftw_free
00056 #define CSL_FFTW_exec fftw_execute
00057 #define CSL_FFTW_alloc fftw_malloc
00058
00059 #endif
00060
00061 #define CSL_FFTW_MEASURE FFTW_MEASURE
00062 #define CSL_FFTW_ESTIMATE FFTW_ESTIMATE
00063
00064 namespace csl {
00065
00066 #ifdef CSL_ENUMS
00067 typedef enum {
00068 CSL_FFT_REAL = 0,
00069 CSL_FFT_COMPLEX
00070 } CSLFFTType;
00071
00072 typedef enum {
00073 kFFTMeasure,
00074 kFFTEstimate
00075 } FFTFlags;
00076
00077 #else
00078 #define CSL_FFT_REAL 0
00079 #define CSL_FFT_COMPLEX 1
00080 typedef int CSLFFTType;
00081 #define kFFTMeasure 0
00082 #define kFFTEstimate 1
00083 typedef int FFTFlags;
00084 #endif
00085
00088
00089 class FFT : public Effect {
00090
00091 public:
00092 FFT(UnitGenerator & in, CSLFFTType type = CSL_FFT_REAL, FFTFlags flags = kFFTMeasure, int size = CGestalt::blockSize());
00093 ~FFT();
00095 int fftSize() { return mFFTSize; }
00096
00097 void nextBuffer(Buffer& outputBuffer) throw (CException);
00098
00099 CSL_FFTW_cmplx *mCmplxSpectrum;
00100 CSL_FFTW_sample *mRealSpectrum;
00101 bool mOverwriteOutput;
00102
00103 protected:
00104 int mFFTSize;
00105 CSLFFTType mType;
00106 CSL_FFTW_plan mPlan;
00107 CSL_FFTW_sample *mSampleBuffer;
00108 CSL_FFTW_sample *mInputBuffer;
00109
00110 CSL_FFTW_sample *mWindowBuffer;
00111
00112 void initialize(FFTFlags flags);
00113 };
00114
00115
00116
00117 class IFFT : public UnitGenerator {
00118
00119 public:
00120 IFFT(FFTFlags flags = kFFTMeasure, int size = CGestalt::blockSize());
00121 ~IFFT();
00123 int fftSize() { return mFFTSize; }
00124
00125 void binValue(int binNumber, float* outRealPart, float* outComplexPart);
00126 void binValueMagPhase(int binNumber, float* outMag, float* outPhase);
00127
00128 void setBin(int binNumber, float realPart, float complexPart);
00129 void setBins(float* real, float* complex);
00130 void setBins(int lower, int upper, float* real, float* complex);
00131 void setBinMagPhase(int binNumber, float mag, float phase);
00132 void setBinsMagPhase(float* mags, float* phases);
00133
00134 protected:
00135 int mFFTSize;
00136 CSL_FFTW_plan mPlan;
00137 CSL_FFTW_cmplx *mSpectrum;
00138 CSL_FFTW_cmplx *mTempSpectrum;
00139 CSL_FFTW_sample *mSampleBuffer;
00140 BlockResizer mBlockResizer;
00141
00142 void initialize(FFTFlags flags);
00143 void nextBuffer(Buffer& outputBuffer, unsigned outBufNum) throw (CException);
00144
00145 };
00146
00147 }
00148
00149 #endif