Spectral.h

Go to the documentation of this file.
00001 //
00002 // Spectral.h -- UnitGenerator for going to/from the spectral domain
00003 //  See the copyright notice and acknowledgment of authors in the file COPYRIGHT
00004 //
00005 
00006 // 
00007 // These classes require FFTW <http://www.fftw.org> version 3 to work.
00008 //
00009 // As currently stands, the IFFT code expects sample to be float. If sample
00010 // changes to double, this code will have to be revisited.
00011 //
00012 // FFTW Installation:
00013 //   You need to compile FFTW 3.x to handle single-precision floating point numbers.
00014 //   On Unix (incl. Mac OS X) here's what you need to do to compile FFTW in
00015 //   a form compatible with CSL 4:
00016 //     ./configure --enable-float
00017 //     make
00018 //     make install
00019 //
00020 // These all assume that the user is using a BlockResizer if he/she wants different IO block sizes and FFT sizes
00021 // The convolver can only handle monophonic IR files, and reads the whole file into memory for the analysis.
00022 
00023 #ifndef CSL_Spectral_H
00024 #define CSL_Spectral_H
00025 
00026 #include "CSL_Core.h"
00027 #include "BlockResizer.h"
00028 //#include "Window.h"
00029 #include "sndfile.h"        // Convolver reads a sound file
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);   // we override the general-case version because this needs a mono input
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 //  BlockResizer mBlockResizer;         ///< used to change the size of callbacks to equal the FFT size
00110     CSL_FFTW_sample *mWindowBuffer; 
00111 
00112     void initialize(FFTFlags flags);    
00113 };
00114 
00115 // Inverse FFT
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                             // set the values in the specified bin
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

Generated on Fri Apr 6 20:18:13 2007 for CSL by  doxygen 1.4.5-20051010