SoundFileL.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 #ifndef CSL_SoundFileL_H
00011 #define CSL_SoundFileL_H
00012 
00013 #include "SoundFile.h"          // abstract class header
00014 #include <sndfile.h>            // libsndfile header file
00015 
00016                                 // NB: set these as compile-time flags now
00017 //#define USE_MP3               // Support MP3 file reading using libMAD
00018                                 // otherwise assume AIFF or WAV file
00019 //#define CSL_USE_SRConv        // Support sample rate conversion                               
00020                                 
00021 #include "CSL_Includes.h"
00022 #include <sndfile.h>            // libsndfile header file
00023 #include <string.h>
00024 
00025 #ifdef CSL_USE_SRConv
00026     #include <samplerate.h>     // libsamplerate header file
00027 
00028                                 // Sample Rate Convertor interpolation modes -- slow to fast
00029     #define CSL_SRC_MODE SRC_SINC_BEST_QUALITY
00030     // #define CSL_SRC_MODE SRC_SINC_MEDIUM_QUALITY
00031     // #define CSL_SRC_MODE SRC_SINC_FASTEST
00032     // #define CSL_SRC_MODE SRC_ZERO_ORDER_HOLD
00033     // #define CSL_SRC_MODE SRC_LINEAR
00034 #endif
00035 
00036 namespace csl {
00037 
00041 
00042 class LSoundFile : public Abst_SoundFile {
00043 public:                         
00044     LSoundFile(string path, int start = -1, int stop = -1);
00045     LSoundFile(string folder, string path, int start = -1, int stop = -1);
00046     LSoundFile(LSoundFile & otherSndFile);          
00047     ~LSoundFile();
00048 
00049     SoundFileFormat format();                       
00050     
00051     void openForRead() throw (CException);  
00052 
00053 
00054     void openForWrite(SoundFileFormat format = kSoundFileFormatAIFF, 
00055                     unsigned channels = 1, 
00056                     unsigned rate = 44100,
00057                     unsigned bitDepth = 16) throw (CException);
00058     void openForReadWrite() throw (CException);     
00059     void close();                                   
00060 
00061     unsigned seekTo(int position, SeekPosition whence = kPositionStart) throw(CException);
00063     void readBufferFromFile(unsigned numFrames);
00064 
00066     void nextBuffer(Buffer &outB) throw (CException);               
00067     void writeBuffer(Buffer &inputBuffer) throw (CException);       
00068 
00069 #ifdef CSL_USE_SRConv                               
00070     void convertRate(char * mTempPath, int fromRate, int toRate);
00071 #endif
00072 
00073     SF_INFO * sfInfo() { return mSFInfo; }          
00074     SNDFILE * sndFile() { return mSndfile; }        
00075 
00076 protected:
00077     SF_INFO * mSFInfo;                              
00078     SNDFILE * mSndfile;                             
00079     Interleaver mInterleaver;                       
00080 #ifdef CSL_USE_SRConv
00081     Buffer mSRConvBuffer;                           
00082     SRC_STATE * mSRateConv;                         
00083     SRC_DATA mSRateData;                            
00084     int mSRCReturn;                                 
00085 #endif
00086     void initFromSndfile();                         
00087     void checkBuffer(unsigned numFrames);           
00088 };
00089 
00093 
00094 #ifdef USE_MP3
00095 
00096 #define MP3_TEMP_EXT ".aiff"    // what format to save temp decoded MP3 files
00097 
00098 #ifdef CSL_MACOSX               // where to save temp decoded MP3 files - platform-dependent
00099 #define MP3_TEMP_DIR "/tmp/"
00100 #else
00101 #define MP3_TEMP_DIR "/tmp/"        
00102 // #define MP3_TEMP_DIR "."     
00103 #endif
00104 
00105 #define DEFAULT_MP3_RATE 44100          // default sample rate for MP3 files
00106 
00107 class MP3File : public LSoundFile {
00108 public:                         
00109     MP3File(string path = "", int start = -1, int stop = -1);
00110     ~MP3File();
00111 
00112     char * tempPath() { return mTempPath; }         
00113     virtual void openForRead() throw (CException);  
00114 
00115     virtual void openForWrite(SoundFileFormat format = kSoundFileFormatAIFF, 
00116                     unsigned channels = 1, 
00117                     unsigned rate = 44100,
00118                     unsigned bitDepth = 16) throw (CException);
00119     virtual void openForReadWrite() throw (CException);     
00120     
00121     char * mTempDir;                                
00122     unsigned mMP3Rate;                              
00123 
00124 protected:
00125     int decodeMP3() throw (CException);             
00126 //  int closef(int which);
00127     char * mTempPath;                               
00128     LSoundFile * mTempFile;                         
00129 };
00130 
00131 // Temp name macro -- also used by other services
00132 // NB: This is UNIX-specific - ToDo: use run-time file sep
00133 
00134 #define MP3_TEMP_NAME(in_path, out_path, temp_dir) {                \
00135     sprintf(out_path, "%s%s", temp_dir, in_path);                   \
00136     char * lastDot = strrchr(out_path, (int) '.');                  \
00137     if (lastDot) sprintf(lastDot, MP3_TEMP_EXT);                    \
00138     char * pos = out_path;                                          \
00139     pos += strlen(temp_dir);                                        \
00140     pos = strchr(pos, (int) '/');                                   \
00141     while (pos) {                                                   \
00142         *pos = '_';                                                 \
00143         pos = strchr(pos, (int) '/');                               \
00144     }}
00145 
00146 // AIFF name macro -- also used by other services
00147 // This assumes the in_path ends with ".mp3"
00148 
00149 #define AIFF_TEMP_NAME(in_path, out_path) {                         \
00150         strcpy(out_path, in_path);                                  \
00151         strcpy((out_path + strlen(out_path) - 4), ".aiff"); }
00152 
00153 #endif // USE_MP3
00154 
00155 } // end of namespace
00156 
00157 #endif

Generated on Thu Sep 17 23:14:15 2009 for CSL by  doxygen 1.5.8