CLAM-Development  1.4.0
SpectralAnalysis.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 "Segment.hxx"
23 #include "Frame.hxx"
24 #include "SpectrumConfig.hxx"
25 #include "SpectralAnalysis.hxx"
26 #include "ProcessingFactory.hxx"
27 
28 namespace CLAM
29 {
30 
31 namespace Hidden
32 {
33  static const char * metadata[] = {
34  "key", "SpectralAnalysis",
35  "category", "Analysis",
36  "description", "SpectralAnalysis",
37  0
38  };
39  static FactoryRegistrator<ProcessingFactory, SpectralAnalysis> reg = metadata;
40 }
41 
43  : mInput("Input",this ),
44  mOutput("Output",this )
45 {
46  AttachChildren();
48 }
49 
51  : mInput("Input",this),
52  mOutput("Output",this)
53 {
54  AttachChildren();
55  Configure(cfg);
56 }
57 
59 {
60 }
61 
62 bool SpectralAnalysis::ConcreteConfigure(const ProcessingConfig& cfg)
63 {
64  CopyAsConcreteConfig(mConfig,cfg);
65 
66  mConfig.Sync();
67  ConfigureChildren();
68  ConfigureData();
69  return true;
70 }
71 
72 
73 bool SpectralAnalysis::ConfigureChildren()
74 {
75  mPO_WinGen.Configure(mConfig.GetWindowGenerator());
76  mPO_CShift.Configure(mConfig.GetCircularShift());
77  mPO_FFT.Configure(mConfig.GetFFT());
78  return true;
79 
80 }
81 
82 void SpectralAnalysis::ConfigureData()
83 {
84  TData samplingRate=mConfig.GetSamplingRate();
85 
86  mInput.SetSize(mConfig.GetWindowSize());
87  mInput.SetHop(mConfig.GetHopSize());
88 
89  mAudioFrame.SetSize(mConfig.GetprFFTSize());
90  mAudioFrame.SetSampleRate(mConfig.GetSamplingRate());
91  mWindow.SetSize(mConfig.GetWindowSize());
92 
93  /*Window is generated and data is kept in internal member mWindow*/
94  mPO_WinGen.Do(mWindow);
95 
96  /*Leaving out last sample of odd-sized window*/
97  mWindow.SetSize(mWindow.GetSize()-1);
98 
99  /* Adding zero padding to windowing function */
100  mWindow.SetSize(mConfig.GetprFFTSize());
101 
102  /* Spectrum used only for initializing a frame */
103  SpectrumConfig scfg;
104  scfg.SetSize(mConfig.GetprFFTSize()/2+1);
105  mSpec.Configure(scfg);
106 
107 
108  /*Setting prototypes in the FFT*/
109  mPO_FFT.SetPrototypes (mWindow, mSpec);
110  mInput.SetSampleRate( samplingRate );
111 }
112 
113 void SpectralAnalysis::AttachChildren()
114 {
115  mPO_WinGen.SetParent(this);
116  mPO_AProduct.SetParent(this);
117  mPO_CShift.SetParent(this);
118  mPO_FFT.SetParent(this);
119 }
120 
122 {
123  mOutput.GetData().SetSize( mConfig.GetFFT().GetAudioSize()/2+1);
124  mOutput.GetData().SetSpectralRange( mInput.GetAudio().GetSampleRate()/2);
125 
126  bool result = Do(mInput.GetAudio(),mOutput.GetData());
127 
128  mInput.Consume();
129  mOutput.Produce();
130 
131  return result;
132 }
133 
134 bool SpectralAnalysis::Do(const Audio& in,Spectrum& outSp)
135 {
136 
137  /* mAudioFrame is used as a helper audio copy where all windowing is done */
138  in.GetAudioChunk(0,in.GetSize()-1 ,mAudioFrame,true );
139 
140  // TODO: it is wrong
141  mAudioFrame.SetSize(mConfig.GetWindowSize()-1);
142 
143 
144  /* Zero padding is added to audioframe */
145  mAudioFrame.SetSize(mConfig.GetprFFTSize());
146 
147  /* Windowing function is now applied */
148  mPO_AProduct.Do(mAudioFrame, mWindow, mAudioFrame);
149 
150  /* Finally, we do the circular shift */
151  mPO_CShift.Do(mAudioFrame,mAudioFrame);
152 
153  /* and now the FFT can be performed */
154  mPO_FFT.Do(mAudioFrame, outSp);
155 
156  return true;
157 }
158 
159 
161 {
162  return Do(in.GetAudioFrame(),in.GetSpectrum());
163 }
164 
166 {
167  return Do(in.GetFrame(in.mCurrentFrameIndex++));
168 }
169 
170 
171 } // namespace CLAM
172