CLAM-Development  1.4.0
AudioBufferMixer.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "AudioMixer.hxx"
23 #include "AudioBufferMixer.hxx"
24 #include "ProcessingFactory.hxx"
25 
26 
27 namespace CLAM
28 {
29 
30 namespace Hidden
31 {
32  static const char * metadata[] = {
33  "key", "AudioBufferMixer",
34  "category", "Arithmetic Operations",
35  "description", "AudioBufferMixer",
36  0
37  };
38  static FactoryRegistrator<ProcessingFactory, AudioBufferMixer> reg = metadata;
39 }
40 
42  : mOutputPort("Output Audio",this)
43 {
44  Configure( mConfig );
45 }
46 
47 void AudioBufferMixer::CreatePortsAndControls()
48 {
49  unsigned portSize = BackendBufferSize();
50 
51  for( int i=0; i<mConfig.GetNumberOfInPorts(); i++ )
52  {
53  std::stringstream number("");
54  number << i;
55  InPort<Audio>* inPort = new InPort<Audio>( "Input " + number.str(), this );
56  inPort->SetSize( 1 );
57  inPort->SetHop( 1 );
58  mInputPorts.push_back( inPort );
59 
60  mInputControls.push_back( new FloatInControl("Gain " + number.str(), this) );
61  }
62  unsigned int inPortsNumber=mConfig.GetNumberOfInPorts();
63  CLAM::Array<TControlData> gainsArray;
64  bool useConfigGains = mConfig.HasDefaultGains();
65  if (useConfigGains)
66  {
67  gainsArray=mConfig.GetDefaultGains();
68  unsigned numberofConfiguredGains=gainsArray.Size();
69  gainsArray.Resize(inPortsNumber);
70  gainsArray.SetSize(inPortsNumber);
71  for (unsigned i=numberofConfiguredGains;i<gainsArray.Size();i++)
72  {
73  gainsArray[i]=1;
74  }
75  mConfig.SetDefaultGains(gainsArray);
76  }
77  for( unsigned int i=0; i<inPortsNumber; i++ )
78  {
79  if (useConfigGains)
80  mInputControls[i]->DoControl(gainsArray[i]);
81  else
82  /* Set gain = 1 by default */
83  mInputControls[i]->DoControl(1.);
84  }
85 
86  mOutputPort.SetSize( 1 );
87  mOutputPort.SetHop( 1 );
88 }
89 
90 void AudioBufferMixer::RemovePortsAndControls()
91 {
92  std::vector< InPort<Audio>* >::iterator itInPort;
93  for(itInPort=mInputPorts.begin(); itInPort!=mInputPorts.end(); itInPort++)
94  delete *itInPort;
95  mInputPorts.clear();
96 
97  std::vector< FloatInControl* >::iterator itInControl;
98  for(itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
99  delete *itInControl;
100  mInputControls.clear();
101 
102  GetInPorts().Clear();
103  GetInControls().Clear();
104 }
105 
107 {
108  CopyAsConcreteConfig(mConfig, c);
109  RemovePortsAndControls();
110  CreatePortsAndControls();
111 
112  return true;
113 }
114 
116 {
117  unsigned int numInPorts = mConfig.GetNumberOfInPorts();
118  unsigned int frameSize = mInputPorts[0]->GetData().GetSize();
119 
120  //if(_network->IsPlaying())
121  std::cout << "AudioBufferMixer: "<< frameSize << std::endl;
122 
123  CLAM::Audio& so=mOutputPort.GetData();
124  so.SetSize(frameSize);
125 
126  TData normConstant = (TData)1.0 /TData(numInPorts);
127  TData * output = so.GetBuffer().GetPtr();
128  TData * inputs[numInPorts];
129  TControlData controls[numInPorts];
130  for (unsigned int i = 0; i<numInPorts; i++)
131  {
132  inputs[i]=mInputPorts[i]->GetData().GetBuffer().GetPtr();
133  controls[i]=mInputControls[i]->GetLastValue();
134  }
135 // std::cout << " - Valor buffer: ";
136  for (unsigned int sample=0; sample < frameSize; sample++)
137  {
138  TData sum=0.0;
139  for (unsigned int inPort=0; inPort< numInPorts; inPort++)
140  {
141  sum += inputs[inPort][sample] * controls[inPort];
142  }
143  output[sample] = sum * normConstant;
144 // std::cout << output[sample] << " ";
145  }
146 // std::cout << std::endl;
147 
148  // execute consume/produce methods
149  for (unsigned int inPort=0; inPort<numInPorts; inPort++)
150  mInputPorts[inPort]->Consume();
151  mOutputPort.Produce();
152  return true;
153 }
154 
155 } // namespace CLAM
156