SoundFile.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #ifndef CSL_SoundFile_H
00012 #define CSL_SoundFile_H
00013 
00014 #include "CSL_Core.h"           // the core of CSL 5
00015 #include "Oscillator.h"         // the superclass of sound files
00016 
00017 #include <string.h>
00018 
00019 namespace csl {
00020 
00022 
00023 #ifdef CSL_ENUMS                // use enumerations or integers?
00024 
00025 typedef enum {                  
00026     kSoundFileRead,
00027     kSoundFileWrite,    
00028     kSoundFileReadWrite
00029 } SoundFileMode;
00030 
00031 typedef enum {                  
00032     kSoundFileFormatWAV,        
00033     kSoundFileFormatAIFF,       
00034     kSoundFileFormatSND,        
00035     kSoundFileFormatEBICSF,     
00036     kSoundFileFormatRaw,        
00037     kSoundFileFormatOther,      
00038 } SoundFileFormat;
00039 
00040 #else
00041     #define kSoundFileRead 0
00042     #define kSoundFileWrite 1
00043     #define kSoundFileReadWrite 2
00044     typedef int SoundFileMode;
00045     
00046     #define kSoundFileFormatWAV 0
00047     #define kSoundFileFormatAIFF 1
00048     #define kSoundFileFormatSND 2
00049     #define kSoundFileFormatEBICSF 3
00050     #define kSoundFileFormatRaw 4
00051     #define kSoundFileFormatOther 5
00052     typedef int SoundFileFormat;
00053 #endif
00054 
00058 
00059 class Abst_SoundFile : public WavetableOscillator, public Writeable, public Seekable {
00060 public:                                 
00061     Abst_SoundFile(string path, bool load = false, int start = -1, int stop = -1);
00062     Abst_SoundFile(string folder, string path, bool load = false, int start = -1, int stop = -1);
00063     Abst_SoundFile(Abst_SoundFile & otherSndFile);  
00064     ~Abst_SoundFile();
00066     unsigned channels() const;                      
00067     unsigned duration() const;                      
00068     float durationInSecs();                         
00069     virtual SoundFileFormat format() = 0;           
00070     unsigned sampleSize() { return mBytesPerSample; };  
00071     SoundFileMode mode() { return mMode; }          
00072     
00073     bool isValid() { return mIsValid; }             
00074     bool isActive();                                
00075     virtual bool isCached();                        
00076 
00077     virtual void setPath(string path);              
00078     string path() { return mPath; }                 
00079     virtual void dump();                            
00080     
00081     int startFrame() { return mStart; }             
00082     void setStart(int val);
00083     void setStartSec(float val);
00084     void setStartRatio(float val);
00085     int stopFrame() { return mStop; }               
00086     void setStop(int val);
00087     void setStopSec(float val);
00088     void setStopRatio(float val);
00089 
00090     double playbackRate() { return mRate; }         
00091     void setRate(UnitGenerator & frequency);        
00092     void setRate(float frequency);
00093 
00094     bool isLooping() { return mIsLooping; }         
00095     void setIsLooping(bool tLooping) { mIsLooping = tLooping; }
00096 
00097     virtual void openForRead() throw (CException) = 0;  
00098 
00099 
00100     virtual void openForWrite(SoundFileFormat format = kSoundFileFormatAIFF, 
00101                     unsigned channels = 1, 
00102                     unsigned rate = 44100,
00103                     unsigned bitDepth = 16) throw (CException) = 0;
00105     virtual unsigned seekTo(int position, SeekPosition whence) throw(CException) = 0;
00106     unsigned seekTo(int position) throw(CException) { return seekTo(position, kPositionStart); };
00107     
00109     virtual void readBufferFromFile(unsigned numFr) = 0;
00110 
00111     void mergeToMono();                             
00112     virtual void setToEnd();                        
00113     virtual void trigger();                         
00114     virtual void close() = 0;                       
00115     virtual void freeBuffer();                      
00116     
00119     virtual void nextBuffer(Buffer &outB) throw (CException);
00121     virtual void writeBuffer(Buffer &inB) throw (CException) = 0;
00122 
00123 protected:
00124     string mPath;                                   
00125     SoundFileMode mMode;                            
00126     bool mIsValid;                                  
00127     bool mIsLooping;                                
00128     int mStart, mStop;                              
00129     double mRate;                                   
00130     unsigned mNumFrames;                            
00131     unsigned mBytesPerSample;                       
00132 
00133     virtual void initFromSndfile() = 0;             
00134     virtual void checkBuffer(unsigned numFrames);   
00135 };
00136 
00140 
00141 class SoundCue : public UnitGenerator {
00142 
00143 public:
00144     SoundCue();                         
00145     SoundCue(string name, Abst_SoundFile *file = 0, int start = 1, int stop = -1);
00146 //  SoundCue(string name, int start = 1, int stop = -1);
00147     ~SoundCue();
00148 
00149     string mName;                       
00150     Abst_SoundFile * mFile;             
00151     int mStart, mStop, mCurrent;        
00152     UnitGenerator *mReadRate;           
00153 
00154     void readFrom(FILE *input);         
00155     void dump(void);                    
00156 
00157     void nextBuffer(Buffer & outputBuffer) throw(CException);   
00158 
00159     bool isActive();
00160     unsigned channels() const { return (mFile->channels()); }
00161     void setToEnd(void);
00162     void trigger(void);
00163     float duration() const { return (float) (mStop - mStart); } 
00164 
00165 protected:
00166     float mFloatCurrent;                
00167 };
00168 
00169 #ifdef UNDEFINED                        // this was never completed...
00170 
00171 class SampleFile : public Abst_SoundFile {
00172 public:
00173     SampleFile();                           
00174     SampleFile(string name, Abst_SoundFile *file = 0, int start = 1, int stop = -1);
00175     ~SampleFile();
00177     unsigned mMIDIKey;          // sample's MIDI key #
00178     double mFrequency;          // sample's actual frequency
00179     double mMinRatio;           // min transp.
00180     double mMaxRatio;           // max transp. (often 1.0)
00181     
00182     double ratioForKey(int desiredMIDI);
00183     double ratioForPitch(int desiredMIDI);
00184 };
00185 
00186 #endif
00187 
00188 }
00189 
00190 #endif

Generated on Sat Oct 17 14:12:32 2009 for CSL by  doxygen 1.4.5-20051010