CLAM-Development  1.4.0
SMSPitchShift.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 "SMSPitchShift.hxx"
23 #include "ProcessingFactory.hxx"
24 
25 
26 namespace CLAM
27 {
28 
29 namespace Hidden
30 {
31  static const char * metadata[] = {
32  "key", "SMSPitchShift",
33  "category", "SMS Transformations",
34  "description", "SMSPitchShift",
35  0
36  };
37  static FactoryRegistrator<ProcessingFactory, SMSPitchShift> reg = metadata;
38 }
39 
41  const Fundamental& inFund,
42  const Spectrum& inRes,
43  SpectralPeakArray& outPeaks,
44  Fundamental& outFund,
45  Spectrum& outRes)
46 {
47  bool ignoreResidual = mIgnoreResidual.GetLastValue()>0.;
48  if (!mConfig.GetPreserveOuts()) //TODO big cludge for streaming
49  {
50  outPeaks = inPeaks;
51  outFund = inFund;
52  if (!ignoreResidual)
53  outRes = inRes;
54  }
55 
56  TData amount = mPitchSteps.GetLastValue();
57  if(amount==1)//no pitch shift
58  return true;
59 
60  TData spectralRange = inRes.GetSpectralRange();
61  TData fundamental = inFund.GetFreq(0);
62  bool isHarmonic = fundamental > .5;
63  mIsHarmonic.DoControl( isHarmonic );
64 
65  for (int i=0;i<inFund.GetnCandidates();i++)
66  outFund.SetFreq(i,inFund.GetFreq(i)*amount);
67 
68  mSpectralEnvelope.SetSpectralRange(spectralRange);
69 
70  bool haveEnvelope=false;
71 
72  //First extract spectral envelope
73  if(isHarmonic)
74  haveEnvelope = mSpectralEnvelopeExtract.Do(inPeaks,mSpectralEnvelope);
75 
76  if(&outPeaks!=&inPeaks)//TODO: this is already solved inPeaks new DT
77  outPeaks=inPeaks;
78  DataArray& iFreqArray=inPeaks.GetFreqBuffer();
79  DataArray& oFreqArray=outPeaks.GetFreqBuffer();
80  DataArray& iBinPosArray=inPeaks.GetBinPosBuffer();
81  DataArray& oBinPosArray=outPeaks.GetBinPosBuffer();
82  TSize nPeaks=inPeaks.GetnPeaks();
83 
84  //Shift all peaks
85  for(int i=0;i<nPeaks;i++)
86  {
87  TData newFreq=iFreqArray[i]*amount;
88  if (newFreq>=spectralRange)
89  {
90  outPeaks.SetnPeaks(i);
91  break;
92  }
93  oFreqArray[i]=newFreq;
94  oBinPosArray[i]=iBinPosArray[i]*amount;
95  }
96 
97  //Apply original spectral shape and comb filter the residual
98  if (isHarmonic)
99  {
100  if (haveEnvelope)
101  mSpectralEnvelopeApply.Do(outPeaks,mSpectralEnvelope,outPeaks);
102  if (false && !ignoreResidual)
103  {
104  mFDCombFilter.mFreq.DoControl(fundamental*amount);
105  mFDCombFilter.Do(inRes,outRes);
106  }
107  }
108  return true;
109 }
110 
111 bool SMSPitchShift::Do(const Frame& in, Frame& out)
112 {
113  return Do( in.GetSpectralPeakArray(),
114  in.GetFundamental(),
115  in.GetResidualSpec(),
116  out.GetSpectralPeakArray(),
117  out.GetFundamental(),
118  out.GetResidualSpec());
119 }
120 
121 
122 }
123