00001 /* 00002 * Binaural.h 00003 * CSL 00004 * 00005 * Created by Jorge Castellanos on 7/19/06. 00006 * Copyright 2006 Jorge Castellanos. All rights reserved. 00007 * 00008 */ 00009 00012 00013 #ifndef CSL_BINAURAL_H 00014 #define CSL_BINAURAL_H 00015 00016 #include "CSL_Core.h" 00017 #include "Panner.h" 00018 #include <fftw3.h> 00019 #include <sndfile.h> // libsndfile header file 00020 00021 namespace csl { 00022 00023 class BinauralSourceCache; 00024 00029 class BinauralPanner : public Panner { 00030 friend class BinauralSourceCache; 00031 public: 00032 BinauralPanner (); 00033 ~BinauralPanner (); 00034 00035 virtual void nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw (CException); 00036 00037 protected: 00038 00039 virtual void *cache(); 00040 00041 unsigned numFFTBlocks; 00042 unsigned mFramesPerBlock; 00043 unsigned mCurrentBlockIdx; 00044 00045 float mInvFramesPerBlock; 00046 00047 sample *mTempInBuffer; 00048 sample *mInverseFFTOutL; 00049 sample *mInverseFFTOutR; 00050 00051 fftwf_complex *mHOutL, *mHOutR; 00052 fftwf_plan mInversePlanL, mInversePlanR; 00053 00054 }; 00055 00057 class BinauralSourceCache { 00058 public: 00059 BinauralSourceCache(BinauralPanner *parent); 00060 ~BinauralSourceCache(); 00061 00062 sample *mPrevOutL; 00063 sample *mPrevOutR; 00064 fftwf_complex **h_in; 00065 fftwf_plan *mFFTPlans; 00066 unsigned *mCurrentHRTF; // The current block of the "split" hrtf 00067 unsigned mNumBlocks; 00068 }; 00069 00070 00072 class HRTF { 00073 public: 00074 HRTF(unsigned hrtfLength, unsigned numBlocks, float theta = 0.f, float phi = 0.f); 00075 ~HRTF(); 00076 00077 void dump(); 00078 00079 CPoint mPosition; 00080 fftwf_complex **mHrtfL, **mHrtfR; 00081 00082 }; 00083 00084 00088 class HRTFDatabase { 00089 public: 00090 ~HRTFDatabase() {}; 00091 static HRTFDatabase* Database(); 00092 static void Destroy(); 00093 00094 unsigned numHRTFs() { return mHRTFVector.size(); }; 00095 unsigned windowSize() { return mWindowSize; }; 00096 unsigned hrtfLength() { return mHRTFLength; }; 00097 unsigned hrirLength() { return mHRIRLength; }; 00098 00099 HRTF *hrtfAtIndex(unsigned index) { return mHRTFVector[index]; }; 00100 00101 void dump(); 00102 00103 protected: 00104 HRTFDatabase(); 00105 00106 private: 00107 00108 vector<HRTF *> mHRTFVector; 00109 00110 static HRTFDatabase* mDatabase; 00111 00112 unsigned mWindowSize; 00113 unsigned mHRTFLength; 00114 unsigned mHRIRLength; 00115 00116 void loadFromFile(const char *folder) throw (CException); 00117 00118 //public: 00119 // 00120 // HRTF *operator [] (unsigned idx) const; 00121 00122 }; 00123 00124 00125 // THE PANNER BELOW HAS SOME CODE TO SPECIFY BLOCK SIZES INDEPENDENT FROM THE CALLBACK RATE. 00126 // NOT FULLY IMPLEMENTED, AND CHOSE THE METHOD ABOVE AS THIS NEEDS LOTS OF BUFFERING (AND MEMORY USAGE) 00127 // 00128 //class BinauralPanner : public Panner { 00129 //public: 00130 // BinauralPanner (); 00131 // ~BinauralPanner (); 00132 // 00133 // virtual void nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw (CException); 00134 // 00135 //protected: 00136 // void selectHRTF(CPoint *position); 00137 // virtual void *cache(); ///< Returns an instance of it's cache data per sound source. 00138 // 00139 // sample *mTempInBuffer; 00140 // sample *mInputRingBuffer; 00141 // sample *mOutputRingBufferL; ///< Holds the output samples, used in case callbacks differ, so it can buffer data. 00142 // sample *mOutputRingBufferR; ///< Holds the output samples, used in case callbacks differ, so it can buffer data. 00143 // sample *mInverseFFTOutL; ///< The Output of the Inverse FFT Plan, after input data has been multiplied by the HRTF 00144 // sample *mInverseFFTOutR; ///< The Output of the Inverse FFT Plan, after input data has been multiplied by the HRTF 00145 // sample *mPrevOutL; ///< The Cached mInverseFFTOutL. It holds the prevous out from the Inverse FFT Plan 00146 // sample *mPrevOutR; ///< The Cached mInverseFFTOutR. It holds the prevous out from the Inverse FFT Plan 00147 // 00148 // float mInvFramesPerBlock; ///< The inverse of the number of frames per block, used for normalization 00149 // 00150 // unsigned numFFTBlocks; ///< 00151 // unsigned mFramesPerBlock; ///< 00152 // unsigned mCurrentBlockIdx; 00153 // 00154 // unsigned currOutSampleRead; 00155 // unsigned currOutSampleWrite; 00156 // unsigned currInSampleRead; 00157 // unsigned currInSampleWrite; 00158 // unsigned mBigBufferSize; 00159 // 00160 // unsigned *mCurrentHRTF; // The current block of the "split" hrtf 00161 // int samplesDone; 00162 // 00163 // fftwf_complex **h_in, *mHOutL, *mHOutR; 00164 // fftwf_plan *mFFTPlans, mInversePlanL, mInversePlanR; 00165 // 00166 // int preBuffer; 00167 // 00168 //}; 00169 00170 00171 00172 } // end namespace 00173 00174 #endif
1.4.5-20051010