AudioBufferAmplifier.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 };//namespace CLAM
00138 
00139 #endif // AudioBufferAmplifier
00140 
Generated by  doxygen 1.6.3