CLAM-Development  1.4.0
Oscillator.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 "Oscillator.hxx"
23 #include "ProcessingFactory.hxx"
24 
25 
26 namespace CLAM
27 {
28 
29 namespace Hidden
30 {
31  static const char * metadata[] = {
32  "key", "Oscillator",
33  "category", "Generators",
34  "description", "Oscillator",
35  0
36  };
37  static FactoryRegistrator<ProcessingFactory, Oscillator> reg = metadata;
38 }
39 
41 {
42  AddFrequency();
43  AddAmplitude();
44  AddModIndex();
45  AddPhase();
46  AddSamplingRate();
47 
48  UpdateData();
49 
50  SetFrequency(440.0);
51  SetAmplitude(1.0);
52  SetModIndex(1.0);
53  SetPhase(0.0);
54  SetSamplingRate( 44100 );
55 }
56 
57 
59  : mInputPhaseMod("Input Phase Modulation", this )
60  , mInputFreqMod("Input Frequency Modulation", this )
61  , mModIdxUpdated( false )
62  , mModIdxCtl("ModIndex", this, &Oscillator::UpdateModIdx )
63 {
64 
65  SimpleOscillatorConfig simpleCfg;
66  simpleCfg.SetFrequency( c.GetFrequency() );
67  simpleCfg.SetAmplitude( c.GetAmplitude() );
68  simpleCfg.SetSamplingRate( c.GetSamplingRate() );
69 
70  Configure( c );
71 }
72 
74 {
75 }
76 
78 {
79  CopyAsConcreteConfig(mConfig, c);
80 
81 
82  mAmp = mConfig.GetAmplitude();
83  mPhase = mConfig.GetPhase(); // TEMP HACK (See also constructor
84  mModIndex = mConfig.GetModIndex();
85  mSamplingRate = mConfig.GetSamplingRate();
86  mDeltaPhase = TData(2.*PI*mConfig.GetFrequency()/mSamplingRate);
87 
88  return true;
89 }
90 
92 {
93  bool res =Do(mInputFreqMod.GetAudio(),mInputPhaseMod.GetAudio(),mOutput.GetAudio());
94  mInputFreqMod.Consume();
95  mInputPhaseMod.Consume();
96  mOutput.Produce();
97  return res;
98 }
99 
100 bool Oscillator::Do( const Audio& pitchModIn, const Audio& phaseModIn, Audio& out )
101 {
102  if( !AbleToExecute() ) return true;
103 
104  ApplyControls();
105 
106  TData* ptr = out.GetBuffer().GetPtr();
107  TData* pitchModptr = pitchModIn.GetBuffer().GetPtr();
108  TData* phaseModptr = phaseModIn.GetBuffer().GetPtr();
109 
110  for (int i=0;i<out.GetSize();i++)
111  {
112  (*ptr++) = mAmp * TData(sin(mPhase + mModIndex*(*phaseModptr++)));
113  mPhase += mDeltaPhase*(*pitchModptr++);
114 
115  if (mPhase>2.*PI)
116  mPhase-=TData(2.*PI);
117 
118  if (mPhase<0)
119  mPhase+=TData(2.*PI);
120  }
121 
122  return true;
123 }
124 
125 bool Oscillator::Do( const Audio& pitchModIn, const int& dum, Audio& out )
126 {
127  if( !AbleToExecute() ) return true;
128 
129  ApplyControls();
130 
131  TData* ptr = out.GetBuffer().GetPtr();
132  TData* pitchModptr = pitchModIn.GetBuffer().GetPtr();
133 
134  for (int i=0;i<out.GetSize();i++)
135  {
136  (*ptr++) = mAmp * TData(sin(mPhase));
137  mPhase += mDeltaPhase*(*pitchModptr++);
138 
139  if (mPhase>TData(2.*PI) )
140  mPhase-=TData(2.*PI);
141 
142  if (mPhase<0)
143  mPhase+=TData(2.*PI);
144  }
145  return true;
146 }
147 
148 bool Oscillator::Do( const int& dum, const Audio& phaseModIn, Audio& out )
149 {
150  if( !AbleToExecute() ) return true;
151 
152  ApplyControls();
153 
154  TData* ptr = out.GetBuffer().GetPtr();
155  TData* phaseModptr = phaseModIn.GetBuffer().GetPtr();
156 
157  for (int i=0;i<out.GetSize();i++)
158  {
159  (*ptr++) = mAmp * TData(sin(mPhase + mModIndex*(*phaseModptr++)));
160  mPhase += mDeltaPhase;
161 
162  if (mPhase>TData(2.*PI) )
163  mPhase-=TData(2.*PI);
164 
165  if (mPhase<0)
166  mPhase+=TData(2.*PI);
167  }
168 
169  return true;
170 }
171 
172 void Oscillator::UpdateModIdx( TControlData value )
173 {
174  mModIdxUpdated = true;
175 }
176 
177 
178 } // namespace CLAM
179