Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

/root/src/subsynth/syn/Stream/SampleBufferIStream.h

Go to the documentation of this file.
00001 
00002 /****************** <SYN heading BEGIN do not edit this line> *****************
00003  *
00004  * subsynth - modular audio synthesizer
00005  * subsynth is (C) Copyright 2001-2002 by Kevin Meinert
00006  *
00007  * Original Author: Kevin Meinert
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Library General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Library General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Library General Public
00020  * License along with this library; if not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00022  * Boston, MA 02111-1307, USA.
00023  *
00024  * -----------------------------------------------------------------
00025  * File:          $RCSfile: SampleBufferIStream.h,v $
00026  * Date modified: $Date: 2002/02/16 19:24:16 $
00027  * Version:       $Revision: 1.2 $
00028  * -----------------------------------------------------------------
00029  *
00030  ****************** <SYN heading END do not edit this line> ******************/
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          //std::cout<<" request: "<<samples<<std::flush;
00080 
00081          // handle the case where dest channels is different than src channels.
00082          // if we are converting to more channles, then we only need to read half as much
00083          // if we are converting to less channles, then we need to read twice as many samples...
00084          samples = (unsigned int)((float)samples * (float)mSourceFormat.channels / (float)mDestFormat.channels);
00085          samples = (samples > (mDestSize - mSeekPosition)) ? (mDestSize - mSeekPosition) : samples;
00086          
00087          // this isn't very flexible.
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          // TODO: do a memcpy if mSourceFormat == mDestFormat and state == COPY
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

Generated at Mon Apr 15 09:26:08 2002 for subsynth by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001