AudioBufferAmplifier.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 _AudioBufferAmplifier_
00023 #define _AudioBufferAmplifier_
00024
00025 #include "Processing.hxx"
00026 #include "ProcessingConfig.hxx"
00027 #include "Audio.hxx"
00028 #include "InPort.hxx"
00029 #include "OutPort.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 AudioBufferAmplifier: public Processing
00056 {
00060 const char *GetClassName() const { return "AudioBufferAmplifier"; }
00061
00063 std::vector<InPort<Audio>*> _inputs;
00064 std::vector<OutPort<Audio>*> _outputs;
00065
00067 FloatInControl _gainControl;
00068
00069 public:
00070 AudioBufferAmplifier(const ProcessingConfig & config=Config())
00071 : _gainControl("Gain", this)
00072 {
00073 Configure(config);
00074 }
00075
00076 ~AudioBufferAmplifier()
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]->GetData(), _outputs[i]->GetData(), 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 out.SetSize(size);
00099 const TData * inb = in.GetBuffer().GetPtr();
00100 TData * outb = out.GetBuffer().GetPtr();
00101 TData gainDelta=(gain-_previousGain)/size;
00102 TData rampedGain = _previousGain;
00103
00104 for (int i=0;i<size;i++)
00105 {
00106 outb[i] = inb[i]*rampedGain;
00107 rampedGain+=gainDelta;
00108 }
00109 return true;
00110 }
00111
00112 typedef AudioAmplifierConfig Config;
00113
00114 virtual bool SupportsVariableAudioSize() const {return true;}
00115
00116 protected:
00117 const ProcessingConfig& GetConfig() const { return _config; }
00118 bool ConcreteConfigure(const ProcessingConfig& config) {
00119
00120 CopyAsConcreteConfig( _config, config );
00121 double max_gain = _config.GetMaxGain();
00122 double init_gain = _config.HasInitGain() ? _config.GetInitGain() : 1. ;
00123 _gainControl.SetBounds(0.,max_gain);
00124 _gainControl.SetDefaultValue(init_gain);
00125 _gainControl.DoControl(init_gain);
00126 _previousGain = init_gain;
00127 ResizePorts(_config.HasPortsNumber()?_config.GetPortsNumber():1);
00128 return true;
00129 }
00130 private:
00131 void ResizePorts(unsigned newSize);
00132
00133 Config _config;
00134 TData _previousGain;
00135 };
00136
00137 };
00138
00139 #endif // AudioBufferAmplifier
00140