AudioAmplifier.hxx
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 #ifndef _AudioAmplifier_
00023 #define _AudioAmplifier_
00024
00025 #include "Processing.hxx"
00026 #include "ProcessingConfig.hxx"
00027 #include "Audio.hxx"
00028 #include "AudioInPort.hxx"
00029 #include "AudioOutPort.hxx"
00030 #include "InControl.hxx"
00031
00032 namespace CLAM{
00033
00037 class AudioAmplifierConfig : public ProcessingConfig
00038 {
00039 public:
00040 DYNAMIC_TYPE_USING_INTERFACE( AudioAmplifierConfig, 3, ProcessingConfig );
00041 DYN_ATTRIBUTE( 0, public, double, MaxGain );
00042 DYN_ATTRIBUTE( 1, public, double, InitGain);
00043 DYN_ATTRIBUTE( 2, public, int, PortsNumber);
00044
00045 protected:
00046 void DefaultInit();
00047 };
00048
00049
00055 class AudioAmplifier: public Processing
00056 {
00060 const char *GetClassName() const { return "AudioAmplifier"; }
00061
00063 std::vector<AudioInPort*> _inputs;
00064 std::vector<AudioOutPort*> _outputs;
00065
00067 FloatInControl _gainControl;
00068
00069 public:
00070 AudioAmplifier(const ProcessingConfig & config=Config())
00071 : _gainControl("Gain", this)
00072 {
00073 Configure(config);
00074 }
00075
00076 ~AudioAmplifier()
00077 {
00078 ResizePorts(0);
00079 }
00080
00081 bool Do()
00082 {
00083 bool result=true;
00084 TData gain = _gainControl.GetLastValue();
00085 for (unsigned i=0; i<_inputs.size(); i++)
00086 {
00087 result &= Do( _inputs[i]->GetAudio(), _outputs[i]->GetAudio(), gain );
00088 _inputs[i]->Consume();
00089 _outputs[i]->Produce();
00090 }
00091 _previousGain = gain;
00092 return result;
00093 }
00094
00095 bool Do(const Audio& in, Audio& out, float gain)
00096 {
00097 int size = in.GetSize();
00098 const TData * inb = in.GetBuffer().GetPtr();
00099 TData * outb = out.GetBuffer().GetPtr();
00100 TData gainDelta=(gain-_previousGain)/size;
00101 TData rampedGain = _previousGain;
00102
00103 for (int i=0;i<size;i++)
00104 {
00105 outb[i] = inb[i]*rampedGain;
00106 rampedGain+=gainDelta;
00107 }
00108 return true;
00109 }
00110
00111 typedef AudioAmplifierConfig Config;
00112
00113
00114 protected:
00115 const ProcessingConfig& GetConfig() const { return _config; }
00116 bool ConcreteConfigure(const ProcessingConfig& config) {
00117
00118 CopyAsConcreteConfig( _config, config );
00119 double max_gain = _config.GetMaxGain();
00120 double init_gain = _config.HasInitGain() ? _config.GetInitGain() : 1. ;
00121 _gainControl.SetBounds(0.,max_gain);
00122 _gainControl.SetDefaultValue(init_gain);
00123 _gainControl.DoControl(init_gain);
00124 _previousGain = init_gain;
00125 ResizePorts(_config.HasPortsNumber()?_config.GetPortsNumber():1);
00126 return true;
00127 }
00128 private:
00129 void ResizePorts(unsigned newSize);
00130
00131 Config _config;
00132 TData _previousGain;
00133 };
00134
00135 };
00136
00137 #endif // _AudioAmplifier_
00138