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         unsigned portSize = BackendBufferSize();
00049         
00050         for( int i=0; i<mConfig.GetNumberOfInPorts(); i++ )
00051         {
00052                 std::stringstream number("");
00053                 number << i;
00054                 AudioInPort * inPort = new AudioInPort( "Input " + number.str(), this );
00055                 inPort->SetSize( portSize );
00056                 inPort->SetHop( portSize );
00057                 mInputPorts.push_back( inPort );
00058                 
00059                 mInputControls.push_back( new FloatInControl("Gain " + number.str(), this) );
00060         }
00061         unsigned int inPortsNumber=mConfig.GetNumberOfInPorts();
00062         CLAM::Array<TControlData> gainsArray;
00063         bool useConfigGains = mConfig.HasDefaultGains();
00064         if (useConfigGains)
00065         {
00066                 gainsArray=mConfig.GetDefaultGains();
00067                 unsigned numberofConfiguredGains=gainsArray.Size();
00068                 gainsArray.Resize(inPortsNumber);
00069                 gainsArray.SetSize(inPortsNumber);
00070                 for (unsigned i=numberofConfiguredGains;i<gainsArray.Size();i++)
00071                 {
00072                         gainsArray[i]=1;
00073                 }
00074                 mConfig.SetDefaultGains(gainsArray);
00075         }
00076         for( unsigned int i=0; i<inPortsNumber; i++ )
00077         {
00078                 if (useConfigGains) 
00079                         mInputControls[i]->DoControl(gainsArray[i]);
00080                 else
00081                         /* Set gain = 1 by default */
00082                         mInputControls[i]->DoControl(1.);
00083         }
00084         
00085         mOutputPort.SetSize( portSize );
00086         mOutputPort.SetHop( portSize );
00087 }
00088 
00089 void AudioMixer::RemovePortsAndControls()
00090 {
00091         std::vector< AudioInPort* >::iterator itInPort;
00092         for(itInPort=mInputPorts.begin(); itInPort!=mInputPorts.end(); itInPort++)
00093                 delete *itInPort;
00094         mInputPorts.clear();
00095 
00096         std::vector< FloatInControl* >::iterator itInControl;
00097         for(itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
00098                 delete *itInControl;
00099         mInputControls.clear();
00100                         
00101         GetInPorts().Clear();
00102         GetInControls().Clear();
00103 }
00104 
00105 bool AudioMixer::ConcreteConfigure(const ProcessingConfig& c)
00106 {
00107         CopyAsConcreteConfig(mConfig, c);
00108         RemovePortsAndControls();
00109         CreatePortsAndControls();
00110         return true;
00111 }
00112 
00113 bool AudioMixer::Do()
00114 {
00115 
00116         unsigned int frameSize = BackendBufferSize(); 
00117         unsigned int numInPorts = mConfig.GetNumberOfInPorts();
00118 
00119         TData normConstant = (TData)1.0 /TData(numInPorts);
00120         TData * output = mOutputPort.GetAudio().GetBuffer().GetPtr();
00121         TData * inputs[numInPorts];
00122         TControlData controls[numInPorts];
00123         for (unsigned int i = 0; i<numInPorts; i++)
00124         {
00125                 inputs[i]=mInputPorts[i]->GetAudio().GetBuffer().GetPtr();
00126                 controls[i]=mInputControls[i]->GetLastValue();
00127         }
00128 
00129         for (unsigned int sample=0; sample < frameSize; sample++) 
00130         {
00131                 TData sum=0.0;
00132                 for (unsigned int inPort=0; inPort< numInPorts; inPort++)
00133                 {
00134                         sum += inputs[inPort][sample] * controls[inPort];
00135                 }
00136                 output[sample] = sum * normConstant;
00137         }
00138 
00139         // execute consume/produce methods      
00140         for (unsigned int inPort=0; inPort<numInPorts; inPort++)
00141                 mInputPorts[inPort]->Consume();
00142         mOutputPort.Produce();
00143         
00144         return true;
00145 }
00146 
00147 } // namespace CLAM
00148 
Generated by  doxygen 1.6.3