00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00068
00069
00070
00071
00072
00073
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
00112 mWindowGenerator.Do(mWindow);
00113
00114
00115 mWindow.SetSize(mWindow.GetSize()-1);
00116
00117
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
00144 out.SetSize(mConfig.GetFFTSize());
00145
00146
00147 if (mConfig.GetWindowType()!=EWindowType::eNone )
00148 mAudioProduct.Do(out, mWindow, out);
00149
00150
00151 if (mConfig.GetDoHalfWindowShift())
00152 mCircularShift.Do(out,out);
00153
00154
00155 return true;
00156 }
00157
00158
00159
00160 }
00161