CLAM-Development  1.4.0
EnvelopeModulator.cxx
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 #include "EnvelopeModulator.hxx"
23 #include "CLAM_Math.hxx"
24 
25 
26 namespace CLAM {
27 
29  {
30  AddAll();
31  UpdateData();
32  SetSampleRate(44100);
33  SetFrameSize(512);
34  SetEnvelopeCompression(true);
35  }
36 
37 
39  : InputEnvelope("Input Envelope",this),
40  InputAudio("Input Audio",this),
41  Output("Output Audio",this)
42  {
43  Configure(c);
44  }
45 
46  bool EnvelopeModulator::ConcreteConfigure(const ProcessingConfig& c)
47  {
48  CopyAsConcreteConfig(mConfig, c);
49 
50  mDeltaX = 1000.0/((TTime)mConfig.GetSampleRate());
51 
52  mCompress = mConfig.GetEnvelopeCompression();
53 
54  InputAudio.SetSize(mConfig.GetFrameSize());
55  Output.SetSize(mConfig.GetFrameSize());
56  return true;
57  }
58 
59  TData EnvelopeModulator::Compress(TData x)
60  {
61  if (x>=0)
62  return 1.0 - exp(-x);
63  else
64  return - (1.0 - exp(x));
65  }
66 
67 
69  {
70  bool res = Do(InputEnvelope.GetData(),
72  Output.GetData());
75  Output.Produce();
76  return res;
77  }
78 
79  bool EnvelopeModulator::Do(const Envelope& env, const Audio& inp, Audio& out)
80  {
81  Array<TData> &inputArray = inp.GetBuffer();
82  Array<TData> &outputArray = out.GetBuffer();
83  BPFTmpl<TTime,TData> &amplitudeBpf = env.GetAmplitudeBPF();
84  TTime pos = 0.0;
85  const int size = std::min(inp.GetSize(), out.GetSize());
86  if (mCompress)
87  for (int i=0;i<size;i++) {
88  outputArray[i]=inputArray[i]*amplitudeBpf.GetValue(pos);
89  pos += mDeltaX;
90  }
91  else
92  for (int i=0;i<size;i++) {
93  outputArray[i]=inputArray[i]*Compress(amplitudeBpf.GetValue(pos));
94  pos += mDeltaX;
95  }
96  return true;
97  }
98 
99 
100 }
101