CLAM-Development  1.4.0
SinusoidalSynthesis.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 "SinusoidalSynthesis.hxx"
23 
24 #include "SpectrumConfig.hxx"
25 #include "ProcessingFactory.hxx"
26 
27 
28 
29 namespace CLAM
30 {
31 
32 namespace Hidden
33 {
34  static FactoryRegistrator<ProcessingFactory, SinusoidalSynthesis> regSinusoidalSynthesis("SinusoidalSynthesis");
35 }
36 
37 
38 void SinusoidalSynthesis::AttachChildren()
39 {
40  mSinSpectralSynthesis.SetParent(this);
41  mSynthSineSpectrum.SetParent(this);
42  mPhaseMan.SetParent(this);
43  mSpectrumAdder.SetParent(this);
44  mOverlapAddSin.SetParent(this);
45 }
46 
48  : mInputSinSpectralPeaks("InputSinPeaks",this)
49  , mOutputSpectrum("OutputSpectrum",this)
50  , mOutputAudio("OutputAudio",this)
51  , mCurrentTimeControl("CurrentTime",this)
52  , mCurrentPitch("CurrentPitch",this)
53 {
54  mCurrentFrame = 0;
55  Configure(cfg);
56  AttachChildren();
57 }
58 
60 {
61 
62 }
63 
64 bool SinusoidalSynthesis::ConfigureChildren()
65 {
66 // mConfig.SetSpectrumSize( mAudioFrame.GetSize()/2+1 );
67 
68  //configure sinusoidal spectral synthesis
69  mConfig.GetSpectralSynth().SetResidual(false);
70  if(!mSinSpectralSynthesis.Configure(mConfig.GetSpectralSynth()))
71  return false;
72 
73  //SynthSineSpectrum
74  if(!mSynthSineSpectrum.Configure(mConfig.GetSynthSineSpectrum()))
75  return false;
76 
77  //Phase Management
78  if(!mPhaseMan.Configure(mConfig.GetPhaseMan()))
79  return false;
80 
81  mPhaseMan.Init();
82 
83  //Spectrum Adder
84  if(!mSpectrumAdder.Configure(SpecAdder2Config()))
85  return false;
86 
87  //Overlap and add PO
88  if(!mOverlapAddSin.Configure(mConfig.GetOverlapAddSin()))
89  return false;
90 
91  return true;
92 }
93 
94 void SinusoidalSynthesis::ConfigureData()
95 {
96  mAudioFrame.SetSize(mConfig.GetHopSize()*2);//audio used as input of the overlap and add
97  mOutputAudio.SetSize( mAudioFrame.GetSize()/2 );
98 
99  mOutputAudio.SetHop( mConfig.GetHopSize() );
100 
101  mOutputSpectrum.GetData().SetSize( mAudioFrame.GetSize()/2+1);
102  mOutputSinSpectrum.GetData().SetSize( mAudioFrame.GetSize()/2+1);
103 }
104 
105 
106 bool SinusoidalSynthesis::ConcreteConfigure(const ProcessingConfig& c)
107 {
108  CopyAsConcreteConfig(mConfig, c);
109  ConfigureChildren();
110  ConfigureData();
112  return true;
113 }
114 
116 {
117  bool result = Do(mInputSinSpectralPeaks.GetData(),
120 
122 
126 
127  return result;
128 }
129 
130 
132  SpectralPeakArray& inputSinusoidalPeaks,
133  Audio& outputAudio)
134 {
135  Spectrum tmpOutputSpec;
136  // init its size since we'll operate with these spectrums
137 // tmpOutputSinSpec.SetSize( inputResidualSpectrum.GetSize() );
138 
139  return Do(inputSinusoidalPeaks,tmpOutputSpec,outputAudio);
140 
141 }
142 
144  SpectralPeakArray& inputSinusoidalPeaks,
145  Spectrum& outputSpectrum, //
146  Audio& outputAudio)
147 {
148  //First we do the phase managing. Note that if the Do(frame) overload is not used,
149  //the time and pitch controls in this processing should be set by hand before this
150  //method is used
151 
152 
153 // mPhaseMan.mCurrentTime.DoControl(mCurrentTimeControl.GetLastValue()); //TODO used in SMSBase (Synth from segment)
154  TData currentTime = 0;
155  if (mCurrentTimeControl.GetLastValue() < -0.9)
156  {
157  int framesize = outputAudio.GetSize();
158  //TData samplerate = inputResidualSpectrum.GetSpectralRange()*2;
159  TData samplerate = mConfig.GetSynthSineSpectrum().GetSamplingRate();
160  currentTime = TData( mCurrentFrame*framesize ) / samplerate;
161  }
162  else
163  {
164  currentTime = mCurrentTimeControl.GetLastValue();
165  }
166  mPhaseMan.mCurrentTime.DoControl( currentTime );
167  mCurrentFrame ++;
168 
170  mPhaseMan.Do(inputSinusoidalPeaks);
171 
172  outputSpectrum.SetSize(mConfig.GetSpectrumSize());
173  mSynthSineSpectrum.Do(inputSinusoidalPeaks,outputSpectrum);
174 
175  mSinSpectralSynthesis.Do(outputSpectrum, mAudioFrame);
176  //Finally the overlap and add is accomplished
177 
178  mOverlapAddSin.Do(mAudioFrame, outputAudio);
179 
180  return true;
181 }
182 
184 {
185  if(in.GetCenterTime()<0) return false;//such frames should not be synthesized
186 
187 //We initialize input frame, adding necessary attributes
188  InitFrame(in);
189 //First we set the controls sa mCurrentTimeControl.DoControl(in.GetCenterTime());
190  mCurrentPitch.DoControl(in.GetFundamental().GetFreq(0));
191 
192 
193 //We make sure that spectrums in input frame has the appropiate size and spectral range.
194 //Note that the only spectrum we can be sure has the correct spectral range is the residual
195 //because it its the only one that has been stored in the analysis process.
196  in.GetOutSpec().SetSize(mConfig.GetSpectrumSize());
197  in.GetOutSpec().SetSpectralRange(in.GetResidualSpec().GetSpectralRange());
198  in.GetSinusoidalSpec().SetSpectralRange(in.GetResidualSpec().GetSpectralRange());
199 
200  return Do(in.GetSpectralPeakArray(),in.GetOutSpec(),in.GetSynthAudioFrame());
201 
202 }
203 
205 {
206  return Do(in.GetFrame(in.mCurrentFrameIndex++));
207 }
208 
209 
211 {
212  in.AddOutSpec();
213  in.AddSinusoidalSpec();
214  in.AddSinusoidalAudioFrame();
215  in.AddResidualAudioFrame();
216  in.AddSynthAudioFrame();
217  in.UpdateData();
218 
219  in.GetSinusoidalAudioFrame().SetSize(mConfig.GetFrameSize());
220  in.GetResidualAudioFrame().SetSize(mConfig.GetFrameSize());
221  in.GetSynthAudioFrame().SetSize(mConfig.GetFrameSize());
222 
223 }
224 
225 } // namespace CLAM
226