Gamma  0.9.5
Generic Synthesis Library
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
/Users/ljp/code/gamma/trunk/Gamma/SoundFile.h
00001 #ifndef GAMMA_SOUNDFILE_H_INC
00002 #define GAMMA_SOUNDFILE_H_INC
00003 
00004 /*  Gamma - Generic processing library
00005     See COPYRIGHT file for authors and license information */
00006 
00007 #include <string>
00008 #include <stdio.h>
00009 #include "Gamma/mem.h"
00010 
00011 #define TEM template<class T>
00012 
00013 namespace gam{
00014     
00015     
00017 class SoundFile{
00018 public:
00019 
00021     enum Format{
00022         WAV = 1,    
00023         AIFF,       
00024         AU,         
00025         RAW,        
00026     };
00027 
00029     enum EncodingType{
00030         PCM_S8 = 1, 
00031         PCM_16,     
00032         PCM_24,     
00033         PCM_32,     
00034         PCM_U8,     
00036         FLOAT,      
00037         DOUBLE,     
00039         ULAW,       
00040         ALAW,       
00041     };
00042     
00043     static const char * toString(Format v);
00044     static const char * toString(EncodingType v);
00045 
00047     
00050     SoundFile(const std::string& path="");
00051     
00053     SoundFile(const std::string& path, const SoundFile& src);
00054     
00056     ~SoundFile();
00057     
00059     
00062     bool openRead();
00063     
00065     
00069     bool openWrite();
00070     
00071     bool close();       
00072     
00074     
00079     //ULONG read(float * dst, ULONG numFrames);
00080     TEM int read(T * dst, int numFrames);
00081     
00083     TEM int readAll(T * dst);
00084 
00086     
00089     TEM int readAllD(T * dst);
00090     
00092     
00097     TEM int write(const T * src, int numFrames);
00098 
00099     // Sound file properties
00100     EncodingType encoding() const;              
00101     Format format() const;                      
00102     double frameRate() const;                   
00103     int frames() const;                         
00104     int channels() const;                       
00105     int samples() const;                        
00106     const char * extension();                   
00107     const std::string& path() const;            
00108     
00109     SoundFile& encoding(EncodingType v);        
00110     SoundFile& format(Format v);                
00111     SoundFile& channels(int num);               
00112     SoundFile& frameRate(double hz);            
00113     SoundFile& info(const SoundFile& src);      
00114     SoundFile& path(const std::string& path);   
00115 
00116     void seek(int pos, int seekMode);
00117     
00118     void print();           
00119     
00120 private:
00121     class Impl; Impl * mImpl;
00122 
00123     std::string mPath;
00124 };
00125 
00126 
00127 
00128 
00129 // Implementation_______________________________________________________________
00130 
00131 inline SoundFile& SoundFile::path(const std::string& v){ mPath=v; return *this; }
00132 inline int SoundFile::samples() const { return frames() * channels(); }
00133 
00134 inline const std::string& SoundFile::path() const { return mPath; }
00135 
00136 TEM inline int SoundFile::readAll(T * dst){
00137     seek(0, SEEK_SET);
00138     return read(dst, frames());
00139 }
00140 
00141 TEM int SoundFile::readAllD(T * dst){
00142     int numChannels = channels();
00143 
00144     if(1 == numChannels) return readAll(dst);
00145     
00146     // Allocate memory for deinterleaving.  Don't know of any in-place methods.
00147     T * temp = new T[samples()];
00148     int framesRead = 0;
00149     
00150     if(temp){
00151         framesRead = readAll(temp);
00152         if(framesRead == frames()){
00153             if(2 == numChannels)    mem::deinterleave2(dst, temp, frames());
00154             else                    mem::deinterleave(dst, temp, frames(), numChannels);
00155         }
00156         delete[] temp;
00157     }
00158     return framesRead;
00159 }
00160 
00161 } // gam::
00162 
00163 #undef TEM
00164 #endif
00165