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/OscIStream.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: OscIStream.h,v $
00026  * Date modified: $Date: 2002/04/08 16:13:28 $
00027  * Version:       $Revision: 1.4 $
00028  * -----------------------------------------------------------------
00029  *
00030  ****************** <SYN heading END do not edit this line> ******************/
00031 #ifndef SUBSYNTH_FUNC_GEN_INPUT_STREAM
00032 #define SUBSYNTH_FUNC_GEN_INPUT_STREAM
00033 
00034 #include <vector>
00035 #include "syn/Util/AudioFormat.h"
00036 #include "syn/Util/Generator.h"
00037 #include "syn/Stream/AudioIStream.h"
00038 
00039 
00040 namespace syn
00041 {
00045    template <typename OSC_TYPE>
00046    class OscIStream : public AudioIStream
00047    {
00048    public:
00049       OscIStream() : mIsOpen( false ), mAmplitude( 1.0f ), mDcOffset( 0.0f ), mFreq( 80.0f )
00050       {
00051 
00052       }
00053       virtual ~OscIStream(){}
00054 
00059       virtual bool open( const syn::AudioFormat& fmt, Operation op = COPY )
00060       {
00061          mIsOpen = true;
00062          mSourceFormat = mDestFormat = fmt;
00063          mSourceFormat.channels = 1;
00064          mSourceFormat.datatype = AudioFormat::FLOAT32;
00065          mOsc.setSampRate( fmt.samplingRateHz );
00066          return mIsOpen == true;
00067       }
00068 
00073       virtual void read( void* data, unsigned int samples )
00074       {
00075          //std::cout<<" request: "<<samples<<std::flush;
00076 
00077          // handle the case where dest channels is different than src channels.
00078          // if we are converting to more channles, then we only need to read half as much
00079          // if we are converting to less channles, then we need to read twice as many samples...
00080          samples = (unsigned int)((float)samples * (float)mSourceFormat.channels / (float)mDestFormat.channels);
00081 
00082          if (samples > mBuffer.size())
00083             mBuffer.resize( samples );
00084 
00085          // dump the func generated samples to the buffer of FLOAT32s.
00086          for (unsigned int x = 0; x < samples; ++x)
00087          {
00088             mBuffer[x] = audio_clamp<float>( mOsc.generate() * mAmplitude + mDcOffset );
00089          }
00090 
00091          syn::audio_convert( mSourceFormat, mDestFormat,
00092                              (char*)&mBuffer[0], samples, data, mSampsRead );
00093          //std::cout<<" input:"<<samples<<" output:"<<mSampsRead<<"\n"<<std::flush;
00094       }
00095       
00096       int numsamps()
00097       {
00098          return mOsc.samplesPerOsc();
00099       }
00100 
00104       virtual bool close()
00105       {
00106          mBuffer.clear();
00107          mBuffer.resize( 0 );
00108          mIsOpen = false;
00109          return mIsOpen == false;
00110       }
00111 
00112       virtual bool isOpen() const { return mIsOpen; }
00113       virtual bool eof() const { return false; }
00114       virtual bool good() const { return mIsOpen; }
00115       virtual bool bad() const { return !mIsOpen; }
00116       
00120       virtual int gcount() const
00121       {
00122          return mSampsRead;
00123       }
00124 
00125       /* TODO: should these take a buffer of samples? */
00126       virtual void setFreq( float freq ) 
00127       { 
00128          mFreq = freq;
00129          mOsc.setFreq( mFreq );
00130       }
00131       virtual void setAmp( float amplitude ) { mAmplitude = amplitude; }
00132       virtual void setPhase( float phase ) { mPhase = phase; }
00133       virtual void setDcOffset( float off ) { mDcOffset = off; }
00134       virtual const AudioFormat& format() const { return mDestFormat; }
00135    protected:
00136       bool mIsOpen;
00137       int mSampsRead;
00138 
00142       AudioFormat mSourceFormat, mDestFormat;
00143       std::vector<float> mBuffer;
00144 
00145       /* func gen attributes. */
00146       float mAmplitude, mDcOffset;
00147       float mPhase;
00148       float mFreq;
00149       float p, dp;
00150       OSC_TYPE mOsc;
00151    };
00152 }
00153 
00154 #endif

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