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"
00015 #include "Oscillator.h"
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, int start = -1, int stop = -1);
00062 Abst_SoundFile(string folder, string path, 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 bool isValid() { return mIsValid; }
00071 bool isActive();
00072 unsigned sampleSize() { return mBytesPerSample; };
00073
00074 virtual void setPath(string path);
00075 string path() { return mPath; }
00076 virtual void dump();
00077
00078 SoundFileMode mode() { return mMode; }
00079 int startFrame() { return mStart; }
00080 void setStart(int val);
00081 void setStartSec(float val);
00082 void setStartRatio(float val);
00083 int stopFrame() { return mStop; }
00084 void setStop(int val);
00085 void setStopSec(float val);
00086 void setStopRatio(float val);
00087
00088 double playbackRate() { return mRate; }
00089 void setRate(UnitGenerator & frequency);
00090 void setRate(float frequency);
00091
00092 bool isLooping() { return mIsLooping; }
00093 void setIsLooping(bool tLooping) { mIsLooping = tLooping; }
00094 virtual bool isCached();
00095
00096 virtual void openForRead() throw (CException) = 0;
00097
00098 virtual void openForWrite(SoundFileFormat format = kSoundFileFormatAIFF,
00099 unsigned channels = 1,
00100 unsigned rate = 44100,
00101 unsigned bitDepth = 16) throw (CException) = 0;
00103 virtual unsigned seekTo(int position, SeekPosition whence) throw(CException) = 0;
00104 unsigned seekTo(int position) throw(CException) { return seekTo(position, kPositionStart); };
00105 virtual void readBufferFromFile(unsigned numFr) = 0;
00106 void mergeToMono();
00107
00108 virtual void setToEnd();
00109 virtual void trigger();
00110 virtual void close() = 0;
00111 virtual void freeBuffer();
00112
00114 virtual void nextBuffer(Buffer &outB) throw (CException);
00115 virtual void writeBuffer(Buffer &inB) throw (CException) = 0;
00116
00117 protected:
00118 string mPath;
00119 SoundFileMode mMode;
00120 bool mIsValid;
00121 bool mIsLooping;
00122 int mStart, mStop;
00123 double mRate;
00124 unsigned mNumFrames;
00125 unsigned mBytesPerSample;
00126
00127 virtual void initFromSndfile() = 0;
00128 virtual void checkBuffer(unsigned numFrames);
00129 };
00130
00134
00135 class SoundCue : public UnitGenerator {
00136
00137 public:
00138 SoundCue();
00139 SoundCue(string name, Abst_SoundFile *file = 0, int start = 1, int stop = -1);
00140
00141 ~SoundCue();
00142
00143 string mName;
00144 Abst_SoundFile * mFile;
00145 int mStart, mStop, mCurrent;
00146 UnitGenerator *mReadRate;
00147
00148 void readFrom(FILE *input);
00149 void dump(void);
00150
00151 void nextBuffer(Buffer & outputBuffer) throw(CException);
00152
00153 bool isActive();
00154 unsigned channels() const { return (mFile->channels()); }
00155 void setToEnd(void);
00156 void trigger(void);
00157 float duration() const { return (mStop - mStart); }
00158
00159 protected:
00160 float mFloatCurrent;
00161 };
00162
00163 #ifdef UNDEFINED // this was never completed...
00164
00165 class SampleFile : public Abst_SoundFile {
00166 public:
00167 SampleFile();
00168 SampleFile(string name, Abst_SoundFile *file = 0, int start = 1, int stop = -1);
00169 ~SampleFile();
00171 unsigned mMIDIKey;
00172 double mFrequency;
00173 double mMinRatio;
00174 double mMaxRatio;
00175
00176 double ratioForKey(int desiredMIDI);
00177 double ratioForPitch(int desiredMIDI);
00178 };
00179
00180 #endif
00181
00182 }
00183
00184 #endif