00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef SUBSYNTH_SAMPLEBUF_INPUT_STREAM
00032 #define SUBSYNTH_SAMPLEBUF_INPUT_STREAM
00033
00034 #include <vector>
00035 #include "syn/Util/AudioFormat.h"
00036 #include "syn/Stream/AudioIStream.h"
00037
00038 namespace syn
00039 {
00040 class SampleBufferIStream : public AudioIStream
00041 {
00042 public:
00043 SampleBufferIStream() : mIsOpen( false ), mSourceData( NULL ), mSourceSize( 0 ), mDestSize( 0 ), mSeekPosition( 0 )
00044 {
00045
00046 }
00047 virtual ~SampleBufferIStream(){}
00048
00053 void setSampleBuf( const void* buf, unsigned int size, const syn::AudioFormat& fmt )
00054 {
00055 mSourceData = (float*)buf;
00056 mSourceFormat = fmt;
00057 mSourceSize = size;
00058 }
00059
00060
00065 virtual bool open( const syn::AudioFormat& fmt, Operation op = COPY )
00066 {
00067 mState = op;
00068 mIsOpen = true;
00069 mDestFormat = fmt;
00070 return mIsOpen == true;
00071 }
00072
00077 virtual void read( void* data, unsigned int samples )
00078 {
00079
00080
00081
00082
00083
00084 samples = (unsigned int)((float)samples * (float)mSourceFormat.channels / (float)mDestFormat.channels);
00085 samples = (samples > (mDestSize - mSeekPosition)) ? (mDestSize - mSeekPosition) : samples;
00086
00087
00088 assert( mDestFormat == mSourceFormat );
00089 assert( mDestFormat.datatype == AudioFormat::FLOAT32 );
00090
00091 float* float_data = (float*)data;
00092 if (mState == ADD)
00093 for (unsigned int x = 0; x < samples; ++x)
00094 float_data[x] += mSourceData[x];
00095
00096 else if (mState == SUB)
00097 for (unsigned int x = 0; x < samples; ++x)
00098 float_data[x] -= mSourceData[x];
00099
00100
00101 else if (mState == COPY)
00102 for (unsigned int x = 0; x < samples; ++x)
00103 float_data[x] = mSourceData[x];
00104
00105 else if (mState == MUL)
00106 for (unsigned int x = 0; x < samples; ++x)
00107 float_data[x] *= mSourceData[x];
00108
00109 mSampsRead = samples;
00110 mSeekPosition += mSampsRead;
00111 }
00112
00116 virtual bool close()
00117 {
00118 mIsOpen = false;
00119 return mIsOpen == false;
00120 }
00121
00122 virtual bool isOpen() const { return mIsOpen; }
00123 virtual bool eof() const { return mSeekPosition >= mDestSize; }
00124 virtual bool good() const { return mIsOpen; }
00125 virtual bool bad() const { return !mIsOpen; }
00126
00130 virtual int gcount() const
00131 {
00132 return mSampsRead;
00133 }
00134
00135 virtual const AudioFormat& format() const { return mDestFormat; }
00136 private:
00137 bool mIsOpen;
00138 int mSampsRead;
00139 AudioFormat mSourceFormat, mDestFormat;
00140 float* mSourceData;
00141 unsigned int mSourceSize, mDestSize;
00142 unsigned int mSeekPosition;
00143 };
00144 }
00145
00146 #endif