EnvelopeGenerator.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 _EnvelopeGenerator_
00023 #define _EnvelopeGenerator_
00024 
00025 #include "Processing.hxx"
00026 #include "ProcessingData.hxx"
00027 #include "ProcessingData.hxx"
00028 #include "Envelope.hxx"
00029 #include "AudioOutPort.hxx"
00030 #include "InPort.hxx"
00031 
00032 namespace CLAM
00033 {
00034 
00035 
00036 class EnvelopeGeneratorConfig: public ProcessingConfig
00037 {
00038 public:
00039         DYNAMIC_TYPE_USING_INTERFACE (EnvelopeGeneratorConfig, 4, ProcessingConfig);
00040         DYN_ATTRIBUTE (0, public, TData,       Duration);
00041         DYN_ATTRIBUTE (1, public, TData,       SampleRate);
00042         DYN_ATTRIBUTE (2, public, bool,        FrameEnvelopes);
00043         DYN_ATTRIBUTE (3, public, int,         FrameSize);
00044 
00045 protected:
00046         void DefaultInit(void)
00047         {
00048                 AddAll();
00049                 UpdateData();
00050 
00051                 SetDuration(1.0);
00052                 SetSampleRate(44100.0);
00053                 SetFrameEnvelopes(false);
00054                 SetFrameSize(512);
00055         }
00056 };
00057         
00058 class EnvelopeGenerator: public Processing
00059 {
00060 private:
00061         EnvelopeGeneratorConfig mConfig;
00062         TData mX;
00063         TData mDX;
00064         bool mXFrameReset;
00065 
00066         InControlTmpl<EnvelopeGenerator> mEnvelopePos;
00067 
00068         int UpdateEnvelopePosition(TControlData val)
00069         {
00070                 mX=val;
00071                 return 0;
00072         }
00073 
00074 public:
00075         EnvelopeGenerator(const EnvelopeGeneratorConfig& c = EnvelopeGeneratorConfig())
00076                 :
00077                 mEnvelopePos("EnvelopePosition",this, &EnvelopeGenerator::UpdateEnvelopePosition),
00078                 Input("Input",this),
00079                 Output("Output",this)
00080         {
00081                 Configure(c);
00082         }
00083 
00084         const char * GetClassName() const { return "EnvelopeGenerator";}
00085 
00086         InPort<Envelope> Input;
00087 
00088         AudioOutPort   Output;
00089 
00090         const ProcessingConfig &GetConfig() const { return mConfig;}
00091 
00092         bool ConcreteConfigure(const ProcessingConfig& c)
00093         {
00094                 CopyAsConcreteConfig(mConfig, c);
00095 
00096                 mXFrameReset = mConfig.GetFrameEnvelopes();
00097                 mX = 0;
00098                 mDX = TData(1000.)/(mConfig.GetSampleRate()*mConfig.GetDuration());
00099 
00100                 Output.SetParams(mConfig.GetFrameSize());
00101                                 
00102                 return true;
00103         }
00104 
00105         void Attach(Envelope& in,Audio& out)
00106         {
00107                 Input.Attach(in);
00108                 Output.Attach(out);
00109         }
00110 
00111         bool Do(void)
00112         {
00113                 bool res = Do(Input.GetData(),Output.GetData());
00114                 Input.Consume();
00115                 Output.Produce();
00116                 return res;
00117         }
00118 
00119         bool Do(Envelope& in,Audio& out)
00120         {
00121                 BPFTmpl<TTime,TData>   &envelope = in.GetAmplitudeBPF();
00122                 DataArray &audio = out.GetBuffer();
00123                 int size = audio.Size();
00124 
00125                 for (int i=0;i<size;i++)
00126                 {
00127                         audio[i] = envelope.GetValue(mX);
00128                         mX += mDX;
00129                         if (mX>1000.0) mX=1000.0;
00130                 }
00131                 if (mXFrameReset)
00132                         mX=0.0;
00133                 return true;
00134         }
00135 
00136 };
00137         
00138 }
00139 
00140 #endif
00141 
00142 
Generated by  doxygen 1.6.3