FFT.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "FFT.hxx"
00024 #include "Audio.hxx"
00025 #include "Spectrum.hxx"
00026 #include "SpectrumConfig.hxx"
00027 #include "ProcessingFactory.hxx"
00028
00029 namespace CLAM
00030 {
00031
00032 namespace Hidden
00033 {
00034 static const char* metadata[] = {
00035 "key", "FFT",
00036 "category", "Analysis",
00037 "description", "FFT",
00038 0
00039 };
00040 static FactoryRegistrator<ProcessingFactory, FFT> reg = metadata;
00041 }
00042
00043 SpecTypeFlags FFT_base::mComplexflags;
00044
00045 FFT_base::FFT_base() :
00046 mSize(0),
00047 mInput("Audio Input",this),
00048 mOutput("Spectrum Output",this),
00049 fftbuffer( NULL )
00050 {
00051 };
00052
00053 FFT_base::~FFT_base()
00054 {
00055 if (fftbuffer) delete [] fftbuffer;
00056 }
00057
00058 void FFT_base::ChangeSize(int n)
00059 {
00060 CLAM_ASSERT(n>=0,"Wrong (negative) Size in control input.");
00061
00062 CLAM_ASSERT(false, "Controls not yet implemented.");
00063
00064 }
00065
00066 void FFT_base::CheckTypes(const Audio& in, const Spectrum &out) const
00067 {
00068
00069 CLAM_BEGIN_CHECK
00070
00071 if (in.GetSize()!=mSize) {
00072 std::stringstream ss;
00073 ss << "FFT::Do: Wrong size in FFT Audio input\n"
00074 << " Expected: " << mSize << ", used " << in.GetSize();
00075 CLAM_ASSERT(0,ss.str().c_str());
00076 }
00077 if (!in.HasBuffer())
00078 CLAM_ASSERT(0,"FFT Do: Received an audio without buffer.");
00079 if (out.GetSize() != mSize/2+1 ) {
00080 std::stringstream ss;
00081 ss << "FFT::Do: wrong size Spectrum.\n"
00082 << " Expected: " << mSize/2+1 << ", used " << out.GetSize();
00083 CLAM_ASSERT(0,ss.str().c_str());
00084 }
00085
00086 CLAM_END_CHECK
00087 }
00088
00089 bool FFT_base::SetPrototypes(const Audio& in,const Spectrum &out)
00090 {
00091 CheckTypes(in,out);
00092
00093 SpecTypeFlags flags;
00094 out.GetType(flags);
00095
00096 if (flags.bComplex)
00097 if (flags.bPolar || flags.bMagPhase || flags.bMagPhaseBPF)
00098 mState=sComplexSync;
00099 else
00100 mState=sComplex;
00101 else
00102 if (flags.bPolar || flags.bMagPhase || flags.bMagPhaseBPF)
00103 mState=sOther;
00104 else
00105 CLAM_ASSERT(false,"FFT_numrec: SetPrototypes(): Spectrum with no attributes!");
00106
00107 return true;
00108 }
00109
00110 bool FFT_base::ConcreteConfigure(const ProcessingConfig& c)
00111 {
00112 int oldSize = mSize;
00113
00114 CopyAsConcreteConfig(mConfig, c);
00115 if (not mConfig.HasAudioSize())
00116 return AddConfigErrorMessage("AudioSize parameter is required");
00117 mSize = mConfig.GetAudioSize();
00118 if (mSize<=0)
00119 return AddConfigErrorMessage("AudioSize should be greater than 0");
00120
00121 mInput.SetSize( mSize );
00122 mInput.SetHop( mSize );
00123
00124 mState=sOther;
00125 mComplexflags.bComplex=1;
00126 mComplexflags.bMagPhase=0;
00127 if (mSize == oldSize)
00128 return true;
00129 if (fftbuffer) delete [] fftbuffer;
00130 fftbuffer = new TData[mSize];
00131
00132 SpectrumConfig cfg;
00133 SpecTypeFlags fl;
00134 fl.bMagPhase=0;
00135 fl.bComplex=1;
00136 cfg.SetType(fl);
00137 cfg.SetSize(mSize);
00138 mComplexSpectrum.Configure(cfg);
00139
00140 return true;
00141 }
00142
00143 bool FFT_base::UnsetPrototypes()
00144 {
00145 mState=sOther;
00146 return true;
00147 }
00148
00149 void FFT_base::ToOther(Spectrum &out)
00150 {
00151 if(out.HasComplexArray()) {
00152 ToComplex(out);
00153 out.SynchronizeTo(mComplexflags);
00154 }
00155 else {
00156 ToComplex(mComplexSpectrum);
00157 out.SynchronizeTo(mComplexSpectrum);
00158 }
00159 }
00160 };
00161