CLAM-Development  1.4.0
EnvelopeGenerator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _EnvelopeGenerator_
23 #define _EnvelopeGenerator_
24 
25 #include "Processing.hxx"
26 #include "ProcessingData.hxx"
27 #include "ProcessingData.hxx"
28 #include "Envelope.hxx"
29 #include "AudioOutPort.hxx"
30 #include "InPort.hxx"
31 
32 namespace CLAM
33 {
34 
35 
37 {
38 public:
40  DYN_ATTRIBUTE (0, public, TData, Duration);
41  DYN_ATTRIBUTE (1, public, TData, SampleRate);
42  DYN_ATTRIBUTE (2, public, bool, FrameEnvelopes);
43  DYN_ATTRIBUTE (3, public, int, FrameSize);
44 
45 protected:
46  void DefaultInit(void)
47  {
48  AddAll();
49  UpdateData();
50 
51  SetDuration(1.0);
52  SetSampleRate(44100.0);
53  SetFrameEnvelopes(false);
54  SetFrameSize(512);
55  }
56 };
57 
59 {
60 private:
62  TData mX;
63  TData mDX;
64  bool mXFrameReset;
65 
66  InControlTmpl<EnvelopeGenerator> mEnvelopePos;
67 
68  int UpdateEnvelopePosition(TControlData val)
69  {
70  mX=val;
71  return 0;
72  }
73 
74 public:
76  :
77  mEnvelopePos("EnvelopePosition",this, &EnvelopeGenerator::UpdateEnvelopePosition),
78  Input("Input",this),
79  Output("Output",this)
80  {
81  Configure(c);
82  }
83 
84  const char * GetClassName() const { return "EnvelopeGenerator";}
85 
87 
89 
90  const ProcessingConfig &GetConfig() const { return mConfig;}
91 
93  {
94  CopyAsConcreteConfig(mConfig, c);
95 
96  mXFrameReset = mConfig.GetFrameEnvelopes();
97  mX = 0;
98  mDX = TData(1000.)/(mConfig.GetSampleRate()*mConfig.GetDuration());
99 
100  Output.SetParams(mConfig.GetFrameSize());
101 
102  return true;
103  }
104 
105  void Attach(Envelope& in,Audio& out)
106  {
107  Input.Attach(in);
108  Output.Attach(out);
109  }
110 
111  bool Do(void)
112  {
113  bool res = Do(Input.GetData(),Output.GetData());
114  Input.Consume();
115  Output.Produce();
116  return res;
117  }
118 
119  bool Do(Envelope& in,Audio& out)
120  {
121  BPFTmpl<TTime,TData> &envelope = in.GetAmplitudeBPF();
122  DataArray &audio = out.GetBuffer();
123  int size = audio.Size();
124 
125  for (int i=0;i<size;i++)
126  {
127  audio[i] = envelope.GetValue(mX);
128  mX += mDX;
129  if (mX>1000.0) mX=1000.0;
130  }
131  if (mXFrameReset)
132  mX=0.0;
133  return true;
134  }
135 
136 };
137 
138 }
139 
140 #endif
141 
142