CLAM-Development  1.4.0
SegmentSMSMorph.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 #include "SegmentSMSMorph.hxx"
22 #include "ProcessingFactory.hxx"
23 #include "SpectrumConfig.hxx"
24 
25 namespace CLAM
26 {
27 
28 namespace Hidden
29 {
30  static const char * metadata[] = {
31  "key", "SegmentSMSMorph",
32 // "category", "SMS Transformations",
33  "description", "SegmentSMSMorph",
34  0
35  };
36  static FactoryRegistrator<ProcessingFactory, SegmentSMSMorph> reg = metadata;
37 }
38 
40  mHybBPF("MorphFactor",this),
41  mSynchronizeTime("Time", this),
42  mHybSinAmp("SinAmp", this),
43  mHybSinSpectralShape("SinShape", this),
44  mHybSinShapeW1("SinShapeW1", this),
45  mHybSinShapeW2("SinShapeW2", this),
46  mHybPitch("Pitch", this),
47  mHybSinFreq("SinFreq", this),
48  mHybResAmp("ResAmp", this),
49  mHybResSpectralShape("ResShape", this),
50  mHybResShapeW1("ResShapeW1", this),
51  mHybResShapeW2("ResShapeW2", this),
52  mpInput2(0)
53 
54 {
56  mUseTemporalBPF = true;
58  mUseGlobalFactor=false;
59  mUseSynchronizeTime=false;
60  mUseSinAmp=false;
61  mUsePitch=false;
62  mUseSinFreq=false;
63  mUseResAmp=false;
66 }
67 
69  mHybBPF("MorphFactor",this),
70  mSynchronizeTime("Time", this),
71  mHybSinAmp("SinAmp", this),
72  mHybSinSpectralShape("SinShape", this),
73  mHybSinShapeW1("SinShapeW1", this),
74  mHybSinShapeW2("SinShapeW2", this),
75  mHybPitch("Pitch", this),
76  mHybSinFreq("SinFreq", this),
77  mHybResAmp("ResAmp", this),
78  mHybResSpectralShape("ResShape", this),
79  mHybResShapeW1("ResShapeW1", this),
80  mHybResShapeW2("ResShapeW2", this),
81  mpInput2(0)
82 
83 {
85  mUseTemporalBPF = true;
86 
88  mUseGlobalFactor=false;
89  mUseSynchronizeTime=false;
90  mUseSinAmp=false;
91  mUsePitch=false;
92  mUseSinFreq=false;
93  mUseResAmp=false;
96  Configure(c);
97 }
98 
100 {
102  mHaveInternalSegment=false;
103 
104  if(mConfig.HasFileName())
105  {
106  if(LoadSDIF(mConfig.GetFileName(),mSegment))
107  {
110  }
111  }
112 
113  SpecTypeFlags type;
114  SpectrumConfig cfg;
115  type.bMagPhase=false;
116  type.bMagPhaseBPF=true;
117  cfg.SetType(type);
118  cfg.SetSpectralRange(mConfig.GetSamplingRate()/2);
119 
120  mUseSinSpectralShape=mConfig.GetUseSpectralShapes();
121  mUseResSpectralShape=mConfig.GetUseSpectralShapes();
122 
123 
125 
126  FrameInterpConfig frIntCfg;
127 
129  {
130  frIntCfg.SetUseSpectralShape(true);
134  }
135 
137  "Failed to configure Frame interpolator in SegmentSMSMorph::ConcreteConfigure" );
138 
139  return UpdateControlValueFromBPF(0);
140 }
141 
143 {
145  return true;
146 }
147 
148 void SegmentSMSMorph::UpdateFrameInterpolatorFactors(bool useFrameFactor=false)
149 {
150  if(useFrameFactor)
151  {
153  }
154  else//No Global Factor
155  {
160  }
161 }
162 
163 bool SegmentSMSMorph::Do(const Frame& in1, Frame& out)
164 {
165  TSize nFrames2=mpInput2->GetnFrames();
166  TData synchroTimeFactor=mSynchronizeTime.GetLastValue()*nFrames2;
167 
169  {
170  //it means we are at the boudaries of segment to morph
171  out=in1;
172 
173  return true;
174  }
175 
176  Frame tempFrame2;
177 
178  //With Frame Interpolation
179  if(mConfig.GetInterpolateFrame())
180  {
182  //Morphing
184  mPO_FrameInterpolator.Do(in1,tempFrame2,out);
185 
186  }
187  //Without Frame Interpolation
188  else
189  {
191  mPO_FrameInterpolator.Do(in1,mpInput2->GetFrame(int(synchroTimeFactor)),out);
192 
193  }
194 
195  return true;
196 
197 
198 }
199 
201 {
202  TSize nFrames2=mpInput2->GetnFrames();
203  TData synchroTimeFactor=mSynchronizeTime.GetLastValue()*nFrames2;
204 
205  //Initializes interpolated frame
206  interpolatedFrame=mpInput2->GetFrame(mpInput2->mCurrentFrameIndex);
207  //Interpolation data
208  int frameNo1=int(floor(synchroTimeFactor));
209  int frameNo2=int(ceil(synchroTimeFactor));
210 
211  //Interpolating
212  TData frameFactor=synchroTimeFactor-frameNo1;
213  mHybBPF.DoControl(frameFactor);
215  return mPO_FrameInterpolator.Do(mpInput2->GetFrame(frameNo1) , mpInput2->GetFrame(frameNo2) , interpolatedFrame);
216 }
217 
218 bool SegmentSMSMorph::Do(const Segment& in1, Segment& out)
219 {
220  if(!mHaveInternalSegment) return false;
221  return SegmentTransformation::Do(in1,out);
222 }
223 
224 bool SegmentSMSMorph::Do(const Segment& in1,Segment& in2, Segment& out)
225 {
226  if(!mpInput2)
227  mpInput2 = &in2;
228  mHaveInternalSegment = true;
229  return Do(in1,out);
230 }
231 
233 {
234  bool ret=true;
235 
236  TData globalFactor=0;
237 
238  //Warning, maybe controls that are not used should be initialize to something sensible (does not affect)
239 
240  if(mUseGlobalFactor)
241  {
242  globalFactor=mConfig.GetHybBPF().GetValue(pos);
243  mHybBPF.DoControl(globalFactor);
244  }
245  else
246  ret=false;
248  {
249  pos=mConfig.GetSynchronizeTime().GetValue(pos);
251  }
252 
253  if(mUseSinAmp)
254  mHybSinAmp.DoControl(mConfig.GetHybSinAmp().GetValue(pos));
255  else
256  mHybSinAmp.DoControl(globalFactor);
257 
258  if(mUsePitch)
259  mHybPitch.DoControl(mConfig.GetHybPitch().GetValue(pos));
260  else
261  mHybPitch.DoControl(globalFactor);
262 
263  if(mUseSinFreq)
264  mHybSinFreq.DoControl(mConfig.GetHybSinFreq().GetValue(pos));
265  else
266  mHybSinFreq.DoControl(globalFactor);
267 
268  if(mUseResAmp)
269  mHybResAmp.DoControl(mConfig.GetHybResAmp().GetValue(pos));
270  else
271  mHybResAmp.DoControl(globalFactor);
272 
273  //Updating spectral shapes
275  {
276  mHybResSpectralShape.DoControl(mConfig.GetHybResSpectralShape().GetValue(pos));
278  }
280  {
281  mHybSinSpectralShape.DoControl(mConfig.GetHybSinSpectralShape().GetValue(pos));
283  mConfig.GetHybSinShapeW1(),
284  mConfig.GetHybSinShapeW2(),
286  mSpectralShape );
287  }
288 
289  return ret;
290 }
291 
293 {
294  if(mConfig.HasHybBPF())
295  mUseGlobalFactor=true;
296  if(mConfig.HasSynchronizeTime() && mConfig.GetSynchronizeTime().Size() )
297  mUseSynchronizeTime=true;
298  if(mConfig.HasHybSinAmp() && mConfig.GetHybSinAmp().Size())
299  mUseSinAmp=true;
300  if(mConfig.HasHybPitch() && mConfig.GetHybPitch().Size() )
301  mUsePitch=true;
302  if(mConfig.HasHybSinFreq() && mConfig.GetHybSinFreq().Size())
303  mUseSinFreq=true;
304  if(mConfig.HasHybResAmp() && mConfig.GetHybResAmp().Size() )
305  mUseResAmp=true;
306  if(mConfig.GetUseSpectralShapes())
307  {
309  mUseResAmp=false;
311  mUseSinAmp=false;
312  }
313 
314 }
315 
316 void SegmentSMSMorph::UpdateSpectralShape(const BPF& weightBPF1, const BPF& weightBPF2,TData interpFactor, Spectrum& spectralShape)
317 {
318  BPF spectralShapeBPF;
319  TData spectralRange=spectralShape.GetSpectralRange();
320  //we will always add as many points as possible, so we take weightBPF with maximum points
321  int nPoints;
322  bool usingFirst=false;
323  if(weightBPF1.Size()>weightBPF2.Size())
324  {
325  nPoints=weightBPF1.Size();
326  usingFirst=true;
327  }
328  else nPoints=weightBPF2.Size();
329  int i;
330  TData xValue,yValue;
331  if(usingFirst)
332  {
333  for(i=0;i<nPoints;i++)
334  {
335  xValue=weightBPF1.GetXValue(i);
336  yValue=weightBPF1.GetValueFromIndex(i)*interpFactor+weightBPF2.GetValue(xValue)*(1-interpFactor);
337  xValue*=spectralRange;
338  spectralShapeBPF.Insert(xValue,yValue);
339  }
340  }
341  else
342  {
343  for(i=0;i<nPoints;i++)
344  {
345  xValue=weightBPF2.GetXValue(i);
346  yValue=weightBPF2.GetValueFromIndex(i)*interpFactor+weightBPF1.GetValue(xValue)*(1-interpFactor);
347  xValue*=spectralRange;
348  spectralShapeBPF.Insert(xValue,yValue);
349  }
350  }
351  spectralShape.SetMagBPF(spectralShapeBPF);
352  spectralShape.SetSize(spectralShapeBPF.Size());
353 
354 }
355 
356 bool SegmentSMSMorph::LoadSDIF( std::string fileName, Segment& segment )
357 {
358  SDIFInConfig cfg;
359  cfg.SetMaxNumPeaks( 100 );
360  cfg.SetFileName( fileName );
361  cfg.SetEnableResidual( true );
362  if(!mSDIFReader.Configure( cfg )) return false;//wrong filename or non-existing sdif
363 
364  segment.AddAll( );
365  segment.UpdateData( );
366 
367  try{
368  mSDIFReader.Start( );}
369  catch (Err)
370  {
371  return false;//wrong filename or non-existing sdif
372  }
373  while( mSDIFReader.Do(segment) ) { }
374  mSDIFReader.Stop( );
375 
376  return true;
377 }
378 
380 {
381  mpInput2 = &segmentToMorph;
383 }
384 
385 
386 }
387