CLAM-Development  1.4.0
AudioAmplifier.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 _AudioAmplifier_
23 #define _AudioAmplifier_
24 
25 #include "Processing.hxx"
26 #include "ProcessingConfig.hxx"
27 #include "Audio.hxx"
28 #include "AudioInPort.hxx"
29 #include "AudioOutPort.hxx"
30 #include "InControl.hxx"
31 
32 namespace CLAM{
33 
38  {
39  public:
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 
55  class AudioAmplifier: public Processing
56  {
60  const char *GetClassName() const { return "AudioAmplifier"; }
61 
63  std::vector<AudioInPort*> _inputs;
64  std::vector<AudioOutPort*> _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]->GetAudio(), _outputs[i]->GetAudio(), 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  const TData * inb = in.GetBuffer().GetPtr();
99  TData * outb = out.GetBuffer().GetPtr();
100  TData gainDelta=(gain-_previousGain)/size;
101  TData rampedGain = _previousGain;
102 
103  for (int i=0;i<size;i++)
104  {
105  outb[i] = inb[i]*rampedGain;
106  rampedGain+=gainDelta;
107  }
108  return true;
109  }
110 
112 
113 
114  protected:
115  const ProcessingConfig& GetConfig() const { return _config; }
116  bool ConcreteConfigure(const ProcessingConfig& config) {
117 
118  CopyAsConcreteConfig( _config, config );
119  double max_gain = _config.GetMaxGain();
120  double init_gain = _config.HasInitGain() ? _config.GetInitGain() : 1. ;
121  _gainControl.SetBounds(0.,max_gain);
122  _gainControl.SetDefaultValue(init_gain);
123  _gainControl.DoControl(init_gain);
124  _previousGain = init_gain;
125  ResizePorts(_config.HasPortsNumber()?_config.GetPortsNumber():1);
126  return true;
127  }
128  private:
129  void ResizePorts(unsigned newSize);
130 
131  Config _config;
132  TData _previousGain;
133  };
134 
135 };//namespace CLAM
136 
137 #endif // _AudioAmplifier_
138