AudioBufferMixer.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 "AudioBufferMixer.hxx"
00024 #include "ProcessingFactory.hxx"
00025 
00026 
00027 namespace CLAM
00028 {
00029 
00030 namespace Hidden
00031 {
00032         static const char * metadata[] = {
00033                 "key", "AudioBufferMixer",
00034                 "category", "Arithmetic Operations",
00035                 "description", "AudioBufferMixer",
00036                 0
00037         };
00038         static FactoryRegistrator<ProcessingFactory, AudioBufferMixer> reg = metadata;
00039 }
00040 
00041 AudioBufferMixer::AudioBufferMixer()
00042         : mOutputPort("Output Audio",this)
00043 {
00044         Configure( mConfig );
00045 }
00046 
00047 void AudioBufferMixer::CreatePortsAndControls()
00048 {
00049         unsigned portSize = BackendBufferSize();
00050 
00051         for( int i=0; i<mConfig.GetNumberOfInPorts(); i++ )
00052         {
00053                 std::stringstream number("");
00054                 number << i;
00055                 InPort<Audio>* inPort = new InPort<Audio>( "Input " + number.str(), this );
00056                 inPort->SetSize( 1 );
00057                 inPort->SetHop( 1 );
00058                 mInputPorts.push_back( inPort );
00059 
00060                 mInputControls.push_back( new FloatInControl("Gain " + number.str(), this) );
00061         }
00062         unsigned int inPortsNumber=mConfig.GetNumberOfInPorts();
00063         CLAM::Array<TControlData> gainsArray;
00064         bool useConfigGains = mConfig.HasDefaultGains();
00065         if (useConfigGains)
00066         {
00067                 gainsArray=mConfig.GetDefaultGains();
00068                 unsigned numberofConfiguredGains=gainsArray.Size();
00069                 gainsArray.Resize(inPortsNumber);
00070                 gainsArray.SetSize(inPortsNumber);
00071                 for (unsigned i=numberofConfiguredGains;i<gainsArray.Size();i++)
00072                 {
00073                         gainsArray[i]=1;
00074                 }
00075                 mConfig.SetDefaultGains(gainsArray);
00076         }
00077         for( unsigned int i=0; i<inPortsNumber; i++ )
00078         {
00079                 if (useConfigGains)
00080                         mInputControls[i]->DoControl(gainsArray[i]);
00081                 else
00082                         /* Set gain = 1 by default */
00083                         mInputControls[i]->DoControl(1.);
00084         }
00085 
00086         mOutputPort.SetSize( 1 );
00087         mOutputPort.SetHop( 1 );
00088 }
00089 
00090 void AudioBufferMixer::RemovePortsAndControls()
00091 {
00092         std::vector< InPort<Audio>* >::iterator itInPort;
00093         for(itInPort=mInputPorts.begin(); itInPort!=mInputPorts.end(); itInPort++)
00094                 delete *itInPort;
00095         mInputPorts.clear();
00096 
00097         std::vector< FloatInControl* >::iterator itInControl;
00098         for(itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
00099                 delete *itInControl;
00100         mInputControls.clear();
00101 
00102         GetInPorts().Clear();
00103         GetInControls().Clear();
00104 }
00105 
00106 bool AudioBufferMixer::ConcreteConfigure(const ProcessingConfig& c)
00107 {
00108         CopyAsConcreteConfig(mConfig, c);
00109         RemovePortsAndControls();
00110         CreatePortsAndControls();
00111 
00112         return true;
00113 }
00114 
00115 bool AudioBufferMixer::Do()
00116 {
00117         unsigned int numInPorts = mConfig.GetNumberOfInPorts();
00118         unsigned int frameSize = mInputPorts[0]->GetData().GetSize();
00119 
00120         //if(_network->IsPlaying())
00121                 std::cout << "AudioBufferMixer: "<< frameSize << std::endl;
00122 
00123         CLAM::Audio& so=mOutputPort.GetData();
00124         so.SetSize(frameSize);
00125 
00126         TData normConstant = (TData)1.0 /TData(numInPorts);
00127         TData * output = so.GetBuffer().GetPtr();
00128         TData * inputs[numInPorts];
00129         TControlData controls[numInPorts];
00130         for (unsigned int i = 0; i<numInPorts; i++)
00131         {
00132                 inputs[i]=mInputPorts[i]->GetData().GetBuffer().GetPtr();
00133                 controls[i]=mInputControls[i]->GetLastValue();
00134         }
00135 //      std::cout << " - Valor buffer: ";
00136         for (unsigned int sample=0; sample < frameSize; sample++)
00137         {
00138                 TData sum=0.0;
00139                 for (unsigned int inPort=0; inPort< numInPorts; inPort++)
00140                 {
00141                         sum += inputs[inPort][sample] * controls[inPort];
00142                 }
00143                 output[sample] = sum * normConstant;
00144 //              std::cout << output[sample] << " ";
00145         }
00146 //      std::cout << std::endl;
00147 
00148         // execute consume/produce methods
00149         for (unsigned int inPort=0; inPort<numInPorts; inPort++)
00150                 mInputPorts[inPort]->Consume();
00151         mOutputPort.Produce();
00152         return true;
00153 }
00154 
00155 } // namespace CLAM
00156 
Generated by  doxygen 1.6.3