|
CLAM-Development
1.3
|
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 if (not (windowSize&1)) 00060 { 00061 AddConfigErrorMessage("FFT Restriction:"); 00062 AddConfigErrorMessage("Window size should be odd."); 00063 return false; 00064 } 00065 // TODO: Review those restriction 00066 /* 00067 if (windowSize<2*hopSize+1) 00068 { 00069 AddConfigErrorMessage("FFT Restriction:"); 00070 AddConfigErrorMessage("This condition is not met: WindowSize < 2*HopSize+1"); 00071 return false; 00072 } 00073 */ 00074 if (not isPowerOfTwo(mConfig.GetFFTSize())) 00075 { 00076 AddConfigErrorMessage("FFT Restriction:"); 00077 AddConfigErrorMessage("FFT Size should be a power of two."); 00078 return false; 00079 } 00080 WindowGeneratorConfig windowGeneratorConfig; 00081 windowGeneratorConfig.SetSize(windowSize); 00082 windowGeneratorConfig.SetType(windowType); 00083 if (! mWindowGenerator.Configure(windowGeneratorConfig) ) 00084 { 00085 AddConfigErrorMessage("Window Generator configuration failed."); 00086 AddConfigErrorMessage(mWindowGenerator.GetConfigErrorMessage()); 00087 return false; 00088 } 00089 00090 CircularShiftConfig circularShiftConfig; 00091 circularShiftConfig.SetAmount(-((windowSize-1)/TData(2))); 00092 if (! mCircularShift.Configure(circularShiftConfig) ) 00093 { 00094 AddConfigErrorMessage("Circular Shift configuration failed."); 00095 AddConfigErrorMessage(mCircularShift.GetConfigErrorMessage()); 00096 return false; 00097 } 00098 00099 return true; 00100 } 00101 00102 void AudioWindowing::ConfigureData() 00103 { 00104 mInput.SetSize(mConfig.GetWindowSize()-1); 00105 mInput.SetHop(mConfig.GetHopSize()); 00106 00107 mWindow.SetSize(mConfig.GetWindowSize()); 00108 00109 /*Window is generated and data is kept in internal member mWindow*/ 00110 mWindowGenerator.Do(mWindow); 00111 00112 /*Leaving out last sample of odd-sized window*/ 00113 mWindow.SetSize(mWindow.GetSize()-1); 00114 00115 /* Adding zero padding to windowing function */ 00116 mWindow.SetSize(mConfig.GetFFTSize()); 00117 } 00118 00119 void AudioWindowing::AttachChildren() 00120 { 00121 mWindowGenerator.SetParent(this); 00122 mAudioProduct.SetParent(this); 00123 mCircularShift.SetParent(this); 00124 } 00125 00126 bool AudioWindowing::Do(void) 00127 { 00128 bool result = Do(mInput.GetAudio(),mOutput.GetData()); 00129 00130 mInput.Consume(); 00131 mOutput.Produce(); 00132 00133 return result; 00134 } 00135 00136 bool AudioWindowing::Do(const Audio& in,Audio& out) 00137 { 00138 in.GetAudioChunk(0,in.GetSize() ,out,true ); 00139 out.SetSampleRate(mConfig.GetSamplingRate()); 00140 00141 // Zero padding 00142 out.SetSize(mConfig.GetFFTSize()); 00143 00144 // Windowing 00145 if (mConfig.GetWindowType()!=EWindowType::eNone ) 00146 mAudioProduct.Do(out, mWindow, out); 00147 00148 // Half Window Shift 00149 if (mConfig.GetDoHalfWindowShift()) 00150 mCircularShift.Do(out,out); 00151 00152 00153 return true; 00154 } 00155 00156 00157 00158 } // namespace CLAM 00159
1.7.6.1