AudioBufferMixer.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 CLAM::Audio& so=mOutputPort.GetData();
00121 so.SetSize(frameSize);
00122
00123 TData normConstant = (TData)1.0 /TData(numInPorts);
00124 TData * output = so.GetBuffer().GetPtr();
00125 TData * inputs[numInPorts];
00126 TControlData controls[numInPorts];
00127 for (unsigned int i = 0; i<numInPorts; i++)
00128 {
00129 inputs[i]=mInputPorts[i]->GetData().GetBuffer().GetPtr();
00130 controls[i]=mInputControls[i]->GetLastValue();
00131 }
00132
00133 for (unsigned int sample=0; sample < frameSize; sample++)
00134 {
00135 TData sum=0.0;
00136 for (unsigned int inPort=0; inPort< numInPorts; inPort++)
00137 {
00138 sum += inputs[inPort][sample] * controls[inPort];
00139 }
00140 output[sample] = sum * normConstant;
00141
00142 }
00143
00144
00145
00146 for (unsigned int inPort=0; inPort<numInPorts; inPort++)
00147 mInputPorts[inPort]->Consume();
00148 mOutputPort.Produce();
00149 return true;
00150 }
00151
00152 }
00153