AudioAmplifier.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 _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 };//namespace CLAM
00136 
00137 #endif // _AudioAmplifier_
00138 
Generated by  doxygen 1.6.3