CLAM-Development  1.4.0
AudioMixer.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 "ProcessingFactory.hxx"
24 
25 
26 namespace CLAM
27 {
28 
29 namespace Hidden
30 {
31  static const char * metadata[] = {
32  "key", "AudioMixer",
33  "category", "Arithmetic Operations",
34  "description", "AudioMixer",
35  0
36  };
37  static FactoryRegistrator<ProcessingFactory, AudioMixer> reg = metadata;
38 }
39 
41  : mOutputPort("Output Audio",this)
42 {
43  Configure( mConfig );
44 }
45 
46 void AudioMixer::CreatePortsAndControls()
47 {
48  unsigned portSize = BackendBufferSize();
49 
50  for( int i=0; i<mConfig.GetNumberOfInPorts(); i++ )
51  {
52  std::stringstream number("");
53  number << i;
54  AudioInPort * inPort = new AudioInPort( "Input " + number.str(), this );
55  inPort->SetSize( portSize );
56  inPort->SetHop( portSize );
57  mInputPorts.push_back( inPort );
58 
59  mInputControls.push_back( new FloatInControl("Gain " + number.str(), this) );
60  }
61  unsigned int inPortsNumber=mConfig.GetNumberOfInPorts();
62  CLAM::Array<TControlData> gainsArray;
63  bool useConfigGains = mConfig.HasDefaultGains();
64  if (useConfigGains)
65  {
66  gainsArray=mConfig.GetDefaultGains();
67  unsigned numberofConfiguredGains=gainsArray.Size();
68  gainsArray.Resize(inPortsNumber);
69  gainsArray.SetSize(inPortsNumber);
70  for (unsigned i=numberofConfiguredGains;i<gainsArray.Size();i++)
71  {
72  gainsArray[i]=1;
73  }
74  mConfig.SetDefaultGains(gainsArray);
75  }
76  for( unsigned int i=0; i<inPortsNumber; i++ )
77  {
78  if (useConfigGains)
79  mInputControls[i]->DoControl(gainsArray[i]);
80  else
81  /* Set gain = 1 by default */
82  mInputControls[i]->DoControl(1.);
83  }
84 
85  mOutputPort.SetSize( portSize );
86  mOutputPort.SetHop( portSize );
87 }
88 
89 void AudioMixer::RemovePortsAndControls()
90 {
91  std::vector< AudioInPort* >::iterator itInPort;
92  for(itInPort=mInputPorts.begin(); itInPort!=mInputPorts.end(); itInPort++)
93  delete *itInPort;
94  mInputPorts.clear();
95 
96  std::vector< FloatInControl* >::iterator itInControl;
97  for(itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
98  delete *itInControl;
99  mInputControls.clear();
100 
101  GetInPorts().Clear();
102  GetInControls().Clear();
103 }
104 
106 {
107  CopyAsConcreteConfig(mConfig, c);
108  RemovePortsAndControls();
109  CreatePortsAndControls();
110  return true;
111 }
112 
114 {
115 
116  unsigned int frameSize = BackendBufferSize();
117  unsigned int numInPorts = mConfig.GetNumberOfInPorts();
118 
119  TData normConstant = (TData)1.0 /TData(numInPorts);
120  TData * output = mOutputPort.GetAudio().GetBuffer().GetPtr();
121  TData * inputs[numInPorts];
122  TControlData controls[numInPorts];
123  for (unsigned int i = 0; i<numInPorts; i++)
124  {
125  inputs[i]=mInputPorts[i]->GetAudio().GetBuffer().GetPtr();
126  controls[i]=mInputControls[i]->GetLastValue();
127  }
128 
129  for (unsigned int sample=0; sample < frameSize; sample++)
130  {
131  TData sum=0.0;
132  for (unsigned int inPort=0; inPort< numInPorts; inPort++)
133  {
134  sum += inputs[inPort][sample] * controls[inPort];
135  }
136  output[sample] = sum * normConstant;
137  }
138 
139  // execute consume/produce methods
140  for (unsigned int inPort=0; inPort<numInPorts; inPort++)
141  mInputPorts[inPort]->Consume();
142  mOutputPort.Produce();
143 
144  return true;
145 }
146 
147 } // namespace CLAM
148