BinaryAudioOp.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef _BINARY_AUDIO_OP_H_
00023 #define _BINARY_AUDIO_OP_H_
00024 
00025 #include "Processing.hxx"
00026 #include "DynamicType.hxx"
00027 #include "Audio.hxx"
00028 #include <typeinfo> // std::bad_cast
00029 #include "AudioInPort.hxx"
00030 #include "AudioOutPort.hxx"
00031 
00032 #include <iostream>
00033 
00034 namespace CLAM 
00035 {
00036         
00037         class BinaryAudioOpConfig: public ProcessingConfig
00038         {
00039         public:
00040                 DYNAMIC_TYPE_USING_INTERFACE (BinaryAudioOpConfig, 0, ProcessingConfig);
00041         };
00042         
00043         template < typename BinOp >
00044         class BinaryAudioOp
00045                 : public Processing 
00046         {
00047                 BinaryAudioOpConfig mConfig;
00048                 BinOp        mOperation;
00049                 
00053                 bool ConcreteConfigure(const ProcessingConfig& c)
00054                 {
00055                         try {
00056                                 mConfig = dynamic_cast<const BinaryAudioOpConfig&>(c);      
00057                         }
00058                         catch (std::bad_cast)
00059                         {
00060                                 CLAM_ASSERT(false,"Config should be a BynariaAudioOpConfig");
00061                         }
00062                         return true;
00063                         
00064                 }
00065                 
00066                 AudioInPort mFirstInput;
00067                 AudioInPort mSecondInput;
00068                 AudioOutPort mOutput;
00069                 
00070 
00071         public:
00072 
00073                 BinaryAudioOp()
00074                         :mFirstInput("First Audio Input",this),
00075                          mSecondInput("Second Audio Input",this),
00076                          mOutput("Audio Output",this)
00077                 {
00078                         Configure( BinaryAudioOpConfig() );
00079                 }
00080                 
00081                 BinaryAudioOp(const BinaryAudioOpConfig &c)
00082                         :mFirstInput("First Audio Input",this),
00083                          mSecondInput("Second Audio Input",this),
00084                          mOutput("Audio Output",this)
00085                                                                                          
00086                 {
00087                                 Configure( c );
00088                 }
00089 
00090                 ~BinaryAudioOp()
00091                 {
00092                 }
00093 
00094                 const ProcessingConfig &GetConfig() const { return mConfig;}
00095 
00096                 const char *GetClassName() const {return "BinaryAudioOperation";}
00097                 void Check(const Audio& in1, const Audio& in2, const Audio& out)
00098                 {
00099                         CLAM_ASSERT(in1.GetSize() <= in2.GetSize(),
00100                                 "BinaryAudioOperation::Do(): Incompatible Input Audio Data Sizes");
00101                         CLAM_ASSERT(in1.GetSize() <= out.GetSize(),
00102                                 "BinaryAudioOperation::Do(): Incompatible Output Audio Data Size");
00103                         
00104                 }
00105 
00106                 bool Do(void)
00107                 {
00108                         bool res = Do(mFirstInput.GetAudio(),
00109                                       mSecondInput.GetAudio(),
00110                                       mOutput.GetAudio());
00111                         mFirstInput.Consume();
00112                         mSecondInput.Consume();
00113                         mOutput.Produce();
00114                         return res;
00115                 }
00116 
00117                 bool Do(const Audio& in1, const Audio& in2, Audio& out)
00118                 {
00119 
00120 
00121                         int size = in1.GetSize();
00122                         int i;
00123 
00124                         Check(in1,in2,out);
00125 
00126                         TData* inb1 = in1.GetBuffer().GetPtr();
00127                         TData* inb2 = in2.GetBuffer().GetPtr();
00128                         TData* outb = out.GetBuffer().GetPtr();
00129 
00130                         for (i=0;i<size;i++) 
00131                         {
00132                                 *outb++ = mOperation( *inb1++ , *inb2++ );
00133                         }
00134 
00135                         return true;
00136                 }
00137 
00138                 // Port interfaces.
00139 
00140                 bool SetPrototypes(const Audio& in1, const Audio& in2, const Audio& out)
00141                 {
00142                         return false;
00143                 }
00144 
00145                 bool SetPrototypes()
00146                 {
00147                         return false;
00148                 }
00149 
00150                 bool UnsetPrototypes()
00151                 {
00152                         return true;
00153                 }
00154 
00155                 bool MayDisableExecution() const {return true;}
00156 
00157         private:
00158         };
00159         
00160 }
00161 
00162 #endif //       _BINARY_AUDIO_OP_H_
00163 
Generated by  doxygen 1.6.3