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
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
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
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
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
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
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
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
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 }
00238
00239 #endif //OPERATORMODULE_H