CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 // Input object checking 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: Float attribute required for Audio object."); 00079 if (out.GetSize() != mSize/2+1 ) { // ALGORITHM DEPENDENT CHECKING 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 (mConfig.HasAudioSize()) { 00116 CLAM_ASSERT(mSize>=0, "Negative Size in FFT configuration"); 00117 mSize = mConfig.GetAudioSize(); 00118 if(mSize>0) 00119 { 00120 mInput.SetSize( mSize ); 00121 mInput.SetHop( mSize ); 00122 } 00123 } 00124 00125 CLAM_ASSERT(mSize>=0, "Negative Size in FFT configuration"); 00126 00127 mState=sOther; 00128 mComplexflags.bComplex=1; 00129 mComplexflags.bMagPhase=0; 00130 if (mSize == 0) 00131 { 00132 fftbuffer = 0; 00133 return false; 00134 } 00135 if (mSize == oldSize) 00136 return true; 00137 00138 delete [] fftbuffer; 00139 fftbuffer = new TData[mSize]; 00140 00141 SpectrumConfig cfg; 00142 SpecTypeFlags fl; 00143 fl.bMagPhase=0; 00144 fl.bComplex=1; 00145 cfg.SetType(fl); 00146 cfg.SetSize(mSize); 00147 mComplexSpectrum.Configure(cfg); 00148 00149 return true; 00150 } 00151 00152 bool FFT_base::UnsetPrototypes() 00153 { 00154 mState=sOther; 00155 return true; 00156 } 00157 00158 void FFT_base::ToOther(Spectrum &out) 00159 { 00160 if(out.HasComplexArray()) { 00161 ToComplex(out); 00162 out.SynchronizeTo(mComplexflags); 00163 } 00164 else { 00165 ToComplex(mComplexSpectrum); 00166 out.SynchronizeTo(mComplexSpectrum); 00167 } 00168 } 00169 };//namespace CLAM 00170