CLAM-Development  1.4.0
AudioWindowing.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 "AudioWindowing.hxx"
26 #include "ProcessingFactory.hxx"
27 
28 namespace CLAM
29 {
30 
31 namespace Hidden
32 {
33  static const char* metadata[] = {
34  "key", "AudioWindowing",
35  "category", "Analysis",
36  "description", "AudioWindowing",
37  0
38  };
39  static FactoryRegistrator<ProcessingFactory, AudioWindowing> reg = metadata;
40 }
41 
43 {
44 }
45 
46 bool AudioWindowing::ConcreteConfigure(const ProcessingConfig& cfg)
47 {
48  CopyAsConcreteConfig(mConfig,cfg);
49 
50  if (! ConfigureChildren()) return false;
51  ConfigureData();
52  return true;
53 }
54 
55 bool AudioWindowing::ConfigureChildren()
56 {
57  int windowSize = mConfig.GetWindowSize();
58  EWindowType windowType = mConfig.GetWindowType();
59  if (not (windowSize&1))
60  {
61  AddConfigErrorMessage("FFT Restriction:");
62  AddConfigErrorMessage("Window size should be odd.");
63  return false;
64  }
65  // TODO: Review those restriction
66  /*
67  if (windowSize<2*hopSize+1)
68  {
69  AddConfigErrorMessage("FFT Restriction:");
70  AddConfigErrorMessage("This condition is not met: WindowSize < 2*HopSize+1");
71  return false;
72  }
73  */
74  if (not isPowerOfTwo(mConfig.GetFFTSize()))
75  {
76  AddConfigErrorMessage("FFT Restriction:");
77  AddConfigErrorMessage("FFT Size should be a power of two.");
78  return false;
79  }
80  WindowGeneratorConfig windowGeneratorConfig;
81  windowGeneratorConfig.SetSize(windowSize);
82  windowGeneratorConfig.SetType(windowType);
83  if (! mWindowGenerator.Configure(windowGeneratorConfig) )
84  {
85  AddConfigErrorMessage("Window Generator configuration failed.");
86  AddConfigErrorMessage(mWindowGenerator.GetConfigErrorMessage());
87  return false;
88  }
89 
90  CircularShiftConfig circularShiftConfig;
91  circularShiftConfig.SetAmount(-((windowSize-1)/TData(2)));
92  if (! mCircularShift.Configure(circularShiftConfig) )
93  {
94  AddConfigErrorMessage("Circular Shift configuration failed.");
96  return false;
97  }
98 
99  return true;
100 }
101 
102 void AudioWindowing::ConfigureData()
103 {
104  mInput.SetSize(mConfig.GetWindowSize()-1);
105  mInput.SetHop(mConfig.GetHopSize());
106 
107  mWindow.SetSize(mConfig.GetWindowSize());
108 
109  /*Window is generated and data is kept in internal member mWindow*/
110  mWindowGenerator.Do(mWindow);
111 
112  /*Leaving out last sample of odd-sized window*/
113  mWindow.SetSize(mWindow.GetSize()-1);
114 
115  /* Adding zero padding to windowing function */
116  mWindow.SetSize(mConfig.GetFFTSize());
117 }
118 
119 void AudioWindowing::AttachChildren()
120 {
121  mWindowGenerator.SetParent(this);
122  mAudioProduct.SetParent(this);
123  mCircularShift.SetParent(this);
124 }
125 
127 {
128  bool result = Do(mInput.GetAudio(),mOutput.GetData());
129 
130  mInput.Consume();
131  mOutput.Produce();
132 
133  return result;
134 }
135 
136 bool AudioWindowing::Do(const Audio& in,Audio& out)
137 {
138  in.GetAudioChunk(0,in.GetSize() ,out,true );
139  out.SetSampleRate(mConfig.GetSamplingRate());
140 
141  // Zero padding
142  out.SetSize(mConfig.GetFFTSize());
143 
144  // Windowing
145  if (mConfig.GetWindowType()!=EWindowType::eNone )
146  mAudioProduct.Do(out, mWindow, out);
147 
148  // Half Window Shift
149  if (mConfig.GetDoHalfWindowShift())
150  mCircularShift.Do(out,out);
151 
152 
153  return true;
154 }
155 
156 
157 
158 } // namespace CLAM
159