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/Module/OperatorModule.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: OperatorModule.h,v $
00026  * Date modified: $Date: 2002/04/15 07:28:46 $
00027  * Version:       $Revision: 1.7 $
00028  * -----------------------------------------------------------------
00029  *
00030  ****************** <SYN heading END do not edit this line> ******************/
00031 /* Generated by Together */
00032 
00033 #ifndef OPERATORMODULE_H
00034 #define OPERATORMODULE_H
00035 
00036 #include "syn/Core/SampleBufferQueue.h"
00037 #include "syn/Core/SampleBufferQueuePtr.h"
00038 #include "syn/Core/Module.h"
00039 
00040 namespace syn
00041 {
00060    template <typename OPERATOR>
00061    class OperatorModule : public Module
00062    {
00063    public:
00064       OperatorModule() : Module(), mConstant( 0 )
00065       {
00066          mReadBuf[0] = NULL;
00067          mReadBuf[1] = NULL;
00068       
00069          // terminal setup
00070          mMonoAudioInput[0] = this->createInput( "mono audio0" );
00071          mMonoAudioInput[1] = this->createInput( "mono audio1" );
00072          mMonoAudioOutput = this->createOutput( "mono audio" );
00073       }
00074 
00075       virtual ~OperatorModule() {};
00076       virtual void update();
00077       virtual bool open() { return true; }
00078       virtual bool isOpen() { return true; }
00079       virtual void close() {}
00080       
00081    public:
00082 
00084       virtual Module* clone() const
00085       {
00086          OperatorModule* mod = new OperatorModule;
00087          mod->setConstant( this->constant() );
00088          return mod;
00089       }
00090 
00094       void setConstant( float constant )
00095       {
00096          mConstant = constant;
00097       }
00098 
00100       float constant() const { return mConstant; }
00101 
00105       virtual void getParam( const std::string& key, MultivariateType& value )
00106       {
00107          if (key == "constant")
00108             value.setValue( this->constant() );
00109       }
00110 
00112       virtual void setParam( const std::string& key, const MultivariateType& value )
00113       {
00114          if (key == "constant")
00115             this->setConstant( value.getValue( float() ) );
00116       }
00118       
00119    private:
00120 
00121       TerminalPtr mMonoAudioOutput;
00122       TerminalPtr mMonoAudioInput[2];
00123       SampleBuffer1f* mReadBuf[2];
00124       unsigned int mReadBufIt[2];
00125       float mConstant;
00126    };
00127    
00131    typedef OperatorModule<Math::ADDEQUAL> AddModule;
00132 
00134    typedef OperatorModule<Math::MULTEQUAL> MultModule;
00135    
00137    typedef OperatorModule<Math::EQUAL> CopyModule;
00139 
00140    template <typename OPERATOR>
00141    inline void OperatorModule<OPERATOR>::update()
00142    {
00143       this->setPutCount( 0 );
00144       
00145       // both connected case...
00146       if (!mMonoAudioInput[0]->empty() && !mMonoAudioInput[1]->empty())
00147       {
00148          if (mReadBuf[0] == NULL)
00149          {
00150             mReadBuf[0] = mMonoAudioInput[0]->front();
00151             mMonoAudioInput[0]->pop();
00152             mReadBufIt[0] = 0;
00153          }
00154 
00155          if (mReadBuf[1] == NULL)
00156          {
00157             mReadBuf[1] = mMonoAudioInput[1]->front();
00158             mMonoAudioInput[1]->pop();
00159             mReadBufIt[1] = 0;
00160          }
00161       
00162          // combine...
00163          while (mReadBufIt[0] < mReadBuf[0]->size() && mReadBufIt[1] < mReadBuf[1]->size())
00164          {
00165             OPERATOR::oper( (*(mReadBuf[0]))[mReadBufIt[0]], (*(mReadBuf[1]))[mReadBufIt[1]] ); 
00166             ++mReadBufIt[0];
00167             ++mReadBufIt[1];
00168          }
00169       
00170          // if buffer has been filled, then send it
00171          if (mReadBufIt[0] >= mReadBuf[0]->size())
00172          {
00173             mMonoAudioOutput->push( mReadBuf[0] );
00174             mMonoAudioOutput->setDone( false );
00175             this->setPutCount( mReadBuf[0]->size() );
00176             mReadBuf[0] = NULL;
00177             mReadBufIt[0] = 0;
00178          }
00179       
00180          // if buffer has been emptied, then put it back so we can get a new one next.
00181          if (mReadBufIt[1] >= mReadBuf[1]->size())
00182          {
00183             mReadBufIt[1] = 0;
00184             SampleBufferRepos::instance()->putback( mReadBuf[1] );
00185             mReadBuf[1] = NULL;
00186          }
00187          
00188       }
00189 
00190       // only one connected
00191       else if ( (!mMonoAudioInput[0]->empty() && mMonoAudioInput[1]->empty() && mMonoAudioInput[1]->done()) ||
00192                 (!mMonoAudioInput[1]->empty() && mMonoAudioInput[0]->empty() && mMonoAudioInput[0]->done()) )
00193       {
00194          int use;
00195          if (mMonoAudioInput[0]->empty())
00196          {
00197             use = 1;
00198          }
00199          else
00200          {
00201             use = 0;
00202          }
00203 
00204          if (mReadBuf[use] == NULL)
00205          {
00206             assert( mMonoAudioInput[use]->empty() == false );
00207             mReadBuf[use] = mMonoAudioInput[use]->front();
00208             mMonoAudioInput[use]->pop();
00209             mReadBufIt[use] = 0;
00210          }
00211       
00212          while (mReadBufIt[use] < mReadBuf[use]->size())
00213          {
00214             OPERATOR::oper( (*(mReadBuf[use]))[mReadBufIt[use]], mConstant ); 
00215             ++mReadBufIt[use];
00216          }
00217 
00218          // if buffer has been filled, then send it
00219          if (mReadBufIt[use] >= mReadBuf[use]->size())
00220          {
00221             mMonoAudioOutput->push( mReadBuf[use] );
00222             mMonoAudioOutput->setDone( false );
00223             this->setPutCount( mReadBuf[use]->size() );
00224             mReadBuf[use] = NULL;
00225             mReadBufIt[use] = 0;
00226          }
00227          mMonoAudioOutput->setDone( false );
00228       }
00229 
00230       else if ( mMonoAudioInput[0]->empty() && mMonoAudioInput[0]->done() && 
00231                 mMonoAudioInput[1]->empty() && mMonoAudioInput[1]->done() )
00232       {
00233          mMonoAudioOutput->setDone( true );
00234       }
00235    }
00236 
00237 } // end namespace syn
00238 
00239 #endif //OPERATORMODULE_H

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