AudioMixer.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 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 #include "AudioMixer.hxx"
00023 #include "ProcessingFactory.hxx"
00024 
00025 
00026 namespace CLAM
00027 {
00028 
00029 namespace Hidden
00030 {
00031         static const char * metadata[] = {
00032                 "key", "AudioMixer",
00033                 "category", "Arithmetic Operations",
00034                 "description", "AudioMixer",
00035                 0
00036         };
00037         static FactoryRegistrator<ProcessingFactory, AudioMixer> reg = metadata;
00038 }
00039 
00040 AudioMixer::AudioMixer()
00041         : mOutputPort("Output Audio",this)
00042 {
00043         Configure( mConfig );
00044 }
00045 
00046 void AudioMixer::CreatePortsAndControls()
00047 {
00048         for( int i=0; i<mConfig.GetNumberOfInPorts(); i++ )
00049         {
00050                 std::stringstream number("");
00051                 number << i;
00052                 AudioInPort * inPort = new AudioInPort( "Input " + number.str(), this );
00053                 inPort->SetSize( mConfig.GetFrameSize() );
00054                 inPort->SetHop( mConfig.GetFrameSize() );
00055                 mInputPorts.push_back( inPort );
00056                 
00057                 mInputControls.push_back( new InControl("Gain " + number.str(), this) );
00058         }
00059         for( int i=0; i<mConfig.GetNumberOfInPorts(); i++ )
00060         {
00061                 /* Set gain = 1 by default */
00062                 mInputControls[i]->DoControl(1.);
00063         }
00064         
00065         mOutputPort.SetSize( mConfig.GetFrameSize());
00066         mOutputPort.SetHop( mConfig.GetFrameSize());
00067 }
00068 
00069 void AudioMixer::RemovePortsAndControls()
00070 {
00071         std::vector< AudioInPort* >::iterator itInPort;
00072         for(itInPort=mInputPorts.begin(); itInPort!=mInputPorts.end(); itInPort++)
00073                 delete *itInPort;
00074         mInputPorts.clear();
00075 
00076         std::vector< InControl* >::iterator itInControl;
00077         for(itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
00078                 delete *itInControl;
00079         mInputControls.clear();
00080                         
00081         GetInPorts().Clear();
00082         GetInControls().Clear();
00083 }
00084 
00085 bool AudioMixer::ConcreteConfigure(const ProcessingConfig& c)
00086 {
00087         CopyAsConcreteConfig(mConfig, c);
00088         RemovePortsAndControls();
00089         CreatePortsAndControls();
00090         return true;
00091 }
00092 
00093 bool AudioMixer::Do()
00094 {
00095         unsigned int frameSize = mConfig.GetFrameSize();
00096         unsigned int numInPorts = mConfig.GetNumberOfInPorts();
00097 
00098         TData normConstant = (TData)1.0 /TData(numInPorts);
00099         TData * output = mOutputPort.GetAudio().GetBuffer().GetPtr();
00100         TData * inputs[numInPorts];
00101         TControlData controls[numInPorts];
00102         for (unsigned int i = 0; i<numInPorts; i++)
00103         {
00104                 inputs[i]=mInputPorts[i]->GetAudio().GetBuffer().GetPtr();
00105                 controls[i]=mInputControls[i]->GetLastValue();
00106         }
00107 
00108         for (unsigned int sample=0; sample < frameSize; sample++) 
00109         {
00110                 TData sum=0.0;
00111                 for (unsigned int inPort=0; inPort< numInPorts; inPort++)
00112                 {
00113                         sum += inputs[inPort][sample] * controls[inPort];
00114                 }
00115                 output[sample] = sum * normConstant;
00116         }
00117 
00118         // execute consume/produce methods      
00119         for (unsigned int inPort=0; inPort<numInPorts; inPort++)
00120                 mInputPorts[inPort]->Consume();
00121         mOutputPort.Produce();
00122         
00123         return true;
00124 }
00125 
00126 } // namespace CLAM
00127 

Generated on Tue Aug 12 22:33:42 2008 for CLAM by  doxygen 1.5.5