CLAM-Development  1.4.0
FFT.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 
23 #include "FFT.hxx"
24 #include "Audio.hxx"
25 #include "Spectrum.hxx"
26 #include "SpectrumConfig.hxx"
27 #include "ProcessingFactory.hxx"
28 
29 namespace CLAM
30 {
31 
32 namespace Hidden
33 {
34  static const char* metadata[] = {
35  "key", "FFT",
36  "category", "Analysis",
37  "description", "FFT",
38  0
39  };
40  static FactoryRegistrator<ProcessingFactory, FFT> reg = metadata;
41 }
42 
43  SpecTypeFlags FFT_base::mComplexflags;
44 
46  mSize(0),
47  mInput("Audio Input",this),
48  mOutput("Spectrum Output",this),
49  fftbuffer( NULL )
50  {
51  };
52 
54  {
55  if (fftbuffer) delete [] fftbuffer;
56  }
57 
59  {
60  CLAM_ASSERT(n>=0,"Wrong (negative) Size in control input.");
61 
62  CLAM_ASSERT(false, "Controls not yet implemented.");
63 
64  }
65 
66  void FFT_base::CheckTypes(const Audio& in, const Spectrum &out) const
67  {
68 
70  // Input object checking
71  if (in.GetSize()!=mSize) {
72  std::stringstream ss;
73  ss << "FFT::Do: Wrong size in FFT Audio input\n"
74  << " Expected: " << mSize << ", used " << in.GetSize();
75  CLAM_ASSERT(0,ss.str().c_str());
76  }
77  if (!in.HasBuffer())
78  CLAM_ASSERT(0,"FFT Do: Received an audio without buffer.");
79  if (out.GetSize() != mSize/2+1 ) { // ALGORITHM DEPENDENT CHECKING
80  std::stringstream ss;
81  ss << "FFT::Do: wrong size Spectrum.\n"
82  << " Expected: " << mSize/2+1 << ", used " << out.GetSize();
83  CLAM_ASSERT(0,ss.str().c_str());
84  }
85 
87  }
88 
89  bool FFT_base::SetPrototypes(const Audio& in,const Spectrum &out)
90  {
91  CheckTypes(in,out);
92 
93  SpecTypeFlags flags;
94  out.GetType(flags);
95 
96  if (flags.bComplex)
97  if (flags.bPolar || flags.bMagPhase || flags.bMagPhaseBPF)
99  else
101  else
102  if (flags.bPolar || flags.bMagPhase || flags.bMagPhaseBPF)
103  mState=sOther;
104  else
105  CLAM_ASSERT(false,"FFT_numrec: SetPrototypes(): Spectrum with no attributes!");
106 
107  return true;
108  }
109 
111  {
112  int oldSize = mSize;
113 
115  if (not mConfig.HasAudioSize())
116  return AddConfigErrorMessage("AudioSize parameter is required");
117  mSize = mConfig.GetAudioSize();
118  if (mSize<=0)
119  return AddConfigErrorMessage("AudioSize should be greater than 0");
120 
121  mInput.SetSize( mSize );
122  mInput.SetHop( mSize );
123 
124  mState=sOther;
127  if (mSize == oldSize)
128  return true;
129  if (fftbuffer) delete [] fftbuffer;
130  fftbuffer = new TData[mSize];
131 
132  SpectrumConfig cfg;
133  SpecTypeFlags fl;
134  fl.bMagPhase=0;
135  fl.bComplex=1;
136  cfg.SetType(fl);
137  cfg.SetSize(mSize);
139 
140  return true;
141  }
142 
144  {
145  mState=sOther;
146  return true;
147  }
148 
150  {
151  if(out.HasComplexArray()) {
152  ToComplex(out);
154  }
155  else {
158  }
159  }
160 };//namespace CLAM
161