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 #include "Segment.hxx" 00023 #include "Frame.hxx" 00024 #include "SpectrumConfig.hxx" 00025 #include "AudioWindowing.hxx" 00026 #include "ProcessingFactory.hxx" 00027 00028 namespace CLAM 00029 { 00030 00031 namespace Hidden 00032 { 00033 static const char* metadata[] = { 00034 "key", "AudioWindowing", 00035 "category", "Analysis", 00036 "description", "AudioWindowing", 00037 0 00038 }; 00039 static FactoryRegistrator<ProcessingFactory, AudioWindowing> reg = metadata; 00040 } 00041 00042 AudioWindowing::~AudioWindowing() 00043 { 00044 } 00045 00046 bool AudioWindowing::ConcreteConfigure(const ProcessingConfig& cfg) 00047 { 00048 CopyAsConcreteConfig(mConfig,cfg); 00049 00050 if (! ConfigureChildren()) return false; 00051 ConfigureData(); 00052 return true; 00053 } 00054 00055 bool AudioWindowing::ConfigureChildren() 00056 { 00057 int windowSize = mConfig.GetWindowSize(); 00058 EWindowType windowType = mConfig.GetWindowType(); 00059 int hopSize = mConfig.GetHopSize(); 00060 int sampleRate = mConfig.GetSamplingRate(); 00061 if (not (windowSize&1)) 00062 { 00063 AddConfigErrorMessage("FFT Restriction:"); 00064 AddConfigErrorMessage("Window size should be odd."); 00065 return false; 00066 } 00067 // TODO: Review those restriction 00068 /* 00069 if (windowSize<2*hopSize+1) 00070 { 00071 AddConfigErrorMessage("FFT Restriction:"); 00072 AddConfigErrorMessage("This condition is not met: WindowSize < 2*HopSize+1"); 00073 return false; 00074 } 00075 */ 00076 if (not isPowerOfTwo(mConfig.GetFFTSize())) 00077 { 00078 AddConfigErrorMessage("FFT Restriction:"); 00079 AddConfigErrorMessage("FFT Size should be a power of two."); 00080 return false; 00081 } 00082 WindowGeneratorConfig windowGeneratorConfig; 00083 windowGeneratorConfig.SetSize(windowSize); 00084 windowGeneratorConfig.SetType(windowType); 00085 if (! mWindowGenerator.Configure(windowGeneratorConfig) ) 00086 { 00087 AddConfigErrorMessage("Window Generator configuration failed."); 00088 AddConfigErrorMessage(mWindowGenerator.GetConfigErrorMessage()); 00089 return false; 00090 } 00091 00092 CircularShiftConfig circularShiftConfig; 00093 circularShiftConfig.SetAmount(-((windowSize-1)/TData(2))); 00094 if (! mCircularShift.Configure(circularShiftConfig) ) 00095 { 00096 AddConfigErrorMessage("Circular Shift configuration failed."); 00097 AddConfigErrorMessage(mCircularShift.GetConfigErrorMessage()); 00098 return false; 00099 } 00100 00101 return true; 00102 } 00103 00104 void AudioWindowing::ConfigureData() 00105 { 00106 mInput.SetSize(mConfig.GetWindowSize()-1); 00107 mInput.SetHop(mConfig.GetHopSize()); 00108 00109 mWindow.SetSize(mConfig.GetWindowSize()); 00110 00111 /*Window is generated and data is kept in internal member mWindow*/ 00112 mWindowGenerator.Do(mWindow); 00113 00114 /*Leaving out last sample of odd-sized window*/ 00115 mWindow.SetSize(mWindow.GetSize()-1); 00116 00117 /* Adding zero padding to windowing function */ 00118 mWindow.SetSize(mConfig.GetFFTSize()); 00119 } 00120 00121 void AudioWindowing::AttachChildren() 00122 { 00123 mWindowGenerator.SetParent(this); 00124 mAudioProduct.SetParent(this); 00125 mCircularShift.SetParent(this); 00126 } 00127 00128 bool AudioWindowing::Do(void) 00129 { 00130 bool result = Do(mInput.GetAudio(),mOutput.GetData()); 00131 00132 mInput.Consume(); 00133 mOutput.Produce(); 00134 00135 return result; 00136 } 00137 00138 bool AudioWindowing::Do(const Audio& in,Audio& out) 00139 { 00140 in.GetAudioChunk(0,in.GetSize() ,out,true ); 00141 out.SetSampleRate(mConfig.GetSamplingRate()); 00142 00143 // Zero padding 00144 out.SetSize(mConfig.GetFFTSize()); 00145 00146 // Windowing 00147 if (mConfig.GetWindowType()!=EWindowType::eNone ) 00148 mAudioProduct.Do(out, mWindow, out); 00149 00150 // Half Window Shift 00151 if (mConfig.GetDoHalfWindowShift()) 00152 mCircularShift.Do(out,out); 00153 00154 00155 return true; 00156 } 00157 00158 00159 00160 } // namespace CLAM 00161