CLAM-Development  1.4.0
AudioBufferAmplifier.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-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 #ifndef _AudioBufferAmplifier_
23 #define _AudioBufferAmplifier_
24 
25 #include "Processing.hxx"
26 #include "ProcessingConfig.hxx"
27 #include "Audio.hxx"
28 #include "InPort.hxx"
29 #include "OutPort.hxx"
30 #include "InControl.hxx"
31 
32 namespace CLAM{
33 
37  class AudioAmplifierConfig : public ProcessingConfig
38  {
39  public:
40  DYNAMIC_TYPE_USING_INTERFACE( AudioAmplifierConfig, 3, ProcessingConfig );
41  DYN_ATTRIBUTE( 0, public, double, MaxGain );
42  DYN_ATTRIBUTE( 1, public, double, InitGain);
43  DYN_ATTRIBUTE( 2, public, int, PortsNumber);
44 
45  protected:
46  void DefaultInit();
47  };
48 
49 
56  {
60  const char *GetClassName() const { return "AudioBufferAmplifier"; }
61 
63  std::vector<InPort<Audio>*> _inputs;
64  std::vector<OutPort<Audio>*> _outputs;
65 
67  FloatInControl _gainControl;
68 
69  public:
71  : _gainControl("Gain", this)
72  {
73  Configure(config);
74  }
75 
77  {
78  ResizePorts(0);
79  }
80 
81  bool Do()
82  {
83  bool result=true;
84  TData gain = _gainControl.GetLastValue();
85  for (unsigned i=0; i<_inputs.size(); i++)
86  {
87  result &= Do( _inputs[i]->GetData(), _outputs[i]->GetData(), gain );
88  _inputs[i]->Consume();
89  _outputs[i]->Produce();
90  }
91  _previousGain = gain;
92  return result;
93  }
94 
95  bool Do(const Audio& in, Audio& out, float gain)
96  {
97  int size = in.GetSize();
98  out.SetSize(size);
99  const TData * inb = in.GetBuffer().GetPtr();
100  TData * outb = out.GetBuffer().GetPtr();
101  TData gainDelta=(gain-_previousGain)/size;
102  TData rampedGain = _previousGain;
103 
104  for (int i=0;i<size;i++)
105  {
106  outb[i] = inb[i]*rampedGain;
107  rampedGain+=gainDelta;
108  }
109  return true;
110  }
111 
113 
114  virtual bool SupportsVariableAudioSize() const {return true;}
115 
116  protected:
117  const ProcessingConfig& GetConfig() const { return _config; }
118  bool ConcreteConfigure(const ProcessingConfig& config) {
119 
120  CopyAsConcreteConfig( _config, config );
121  double max_gain = _config.GetMaxGain();
122  double init_gain = _config.HasInitGain() ? _config.GetInitGain() : 1. ;
123  _gainControl.SetBounds(0.,max_gain);
124  _gainControl.SetDefaultValue(init_gain);
125  _gainControl.DoControl(init_gain);
126  _previousGain = init_gain;
127  ResizePorts(_config.HasPortsNumber()?_config.GetPortsNumber():1);
128  return true;
129  }
130  private:
131  void ResizePorts(unsigned newSize);
132 
133  Config _config;
134  TData _previousGain;
135  };
136 
137 };//namespace CLAM
138 
139 #endif // AudioBufferAmplifier
140