comparison transform/FeatureExtractionPluginTransform.cpp @ 68:8e8c2981a189

* Support plugins returning 0 for preferred block/step size
author Chris Cannam
date Mon, 03 Apr 2006 14:18:40 +0000
parents eb530055ed55
children 57a78e7e4ad3
comparison
equal deleted inserted replaced
67:eb530055ed55 68:8e8c2981a189
37 QString configurationXml, 37 QString configurationXml,
38 QString outputName) : 38 QString outputName) :
39 Transform(inputModel), 39 Transform(inputModel),
40 m_plugin(0), 40 m_plugin(0),
41 m_channel(channel), 41 m_channel(channel),
42 m_stepSize(0),
43 m_blockSize(0),
42 m_descriptor(0), 44 m_descriptor(0),
43 m_outputFeatureNo(0) 45 m_outputFeatureNo(0)
44 { 46 {
45 std::cerr << "FeatureExtractionPluginTransform::FeatureExtractionPluginTransform: plugin " << pluginId.toStdString() << ", outputName " << outputName.toStdString() << std::endl; 47 std::cerr << "FeatureExtractionPluginTransform::FeatureExtractionPluginTransform: plugin " << pluginId.toStdString() << ", outputName " << outputName.toStdString() << std::endl;
46 48
62 } 64 }
63 65
64 if (configurationXml != "") { 66 if (configurationXml != "") {
65 PluginXml(m_plugin).setParametersFromXml(configurationXml); 67 PluginXml(m_plugin).setParametersFromXml(configurationXml);
66 } 68 }
69
70 m_blockSize = m_plugin->getPreferredBlockSize();
71 m_stepSize = m_plugin->getPreferredStepSize();
72
73 if (m_blockSize == 0) m_blockSize = 1024; //!!! todo: ask user
74 if (m_stepSize == 0) m_stepSize = m_blockSize; //!!! likewise
67 75
68 Vamp::Plugin::OutputList outputs = 76 Vamp::Plugin::OutputList outputs =
69 m_plugin->getOutputDescriptors(); 77 m_plugin->getOutputDescriptors();
70 78
71 if (outputs.empty()) { 79 if (outputs.empty()) {
196 return; 204 return;
197 } 205 }
198 206
199 size_t sampleRate = m_input->getSampleRate(); 207 size_t sampleRate = m_input->getSampleRate();
200 208
201 size_t stepSize = m_plugin->getPreferredStepSize(); 209 m_plugin->initialise(channelCount, m_stepSize, m_blockSize);
202 size_t blockSize = m_plugin->getPreferredBlockSize();
203
204 m_plugin->initialise(channelCount, stepSize, blockSize);
205 210
206 float **buffers = new float*[channelCount]; 211 float **buffers = new float*[channelCount];
207 for (size_t ch = 0; ch < channelCount; ++ch) { 212 for (size_t ch = 0; ch < channelCount; ++ch) {
208 buffers[ch] = new float[blockSize]; 213 buffers[ch] = new float[m_blockSize];
209 } 214 }
210 215
211 double *fftInput = 0; 216 double *fftInput = 0;
212 fftw_complex *fftOutput = 0; 217 fftw_complex *fftOutput = 0;
213 fftw_plan fftPlan = 0; 218 fftw_plan fftPlan = 0;
214 Window<double> windower(HanningWindow, blockSize); 219 Window<double> windower(HanningWindow, m_blockSize);
215 220
216 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) { 221 if (m_plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
217 222
218 fftInput = (double *)fftw_malloc(blockSize * sizeof(double)); 223 fftInput = (double *)fftw_malloc(m_blockSize * sizeof(double));
219 fftOutput = (fftw_complex *)fftw_malloc(blockSize * sizeof(fftw_complex)); 224 fftOutput = (fftw_complex *)fftw_malloc(m_blockSize * sizeof(fftw_complex));
220 fftPlan = fftw_plan_dft_r2c_1d(blockSize, fftInput, fftOutput, 225 fftPlan = fftw_plan_dft_r2c_1d(m_blockSize, fftInput, fftOutput,
221 FFTW_ESTIMATE); 226 FFTW_ESTIMATE);
222 if (!fftPlan) { 227 if (!fftPlan) {
223 std::cerr << "ERROR: FeatureExtractionPluginTransform::run(): fftw_plan failed! Results will be garbage" << std::endl; 228 std::cerr << "ERROR: FeatureExtractionPluginTransform::run(): fftw_plan failed! Results will be garbage" << std::endl;
224 } 229 }
225 } 230 }
234 239
235 // std::cerr << "FeatureExtractionPluginTransform::run: blockFrame " 240 // std::cerr << "FeatureExtractionPluginTransform::run: blockFrame "
236 // << blockFrame << std::endl; 241 // << blockFrame << std::endl;
237 242
238 size_t completion = 243 size_t completion =
239 (((blockFrame - startFrame) / stepSize) * 99) / 244 (((blockFrame - startFrame) / m_stepSize) * 99) /
240 ( (endFrame - startFrame) / stepSize); 245 ( (endFrame - startFrame) / m_stepSize);
241 246
242 // channelCount is either m_input->channelCount or 1 247 // channelCount is either m_input->channelCount or 1
243 248
244 size_t got = 0; 249 size_t got = 0;
245 250
246 if (channelCount == 1) { 251 if (channelCount == 1) {
247 got = input->getValues 252 got = input->getValues
248 (m_channel, blockFrame, blockFrame + blockSize, buffers[0]); 253 (m_channel, blockFrame, blockFrame + m_blockSize, buffers[0]);
249 while (got < blockSize) { 254 while (got < m_blockSize) {
250 buffers[0][got++] = 0.0; 255 buffers[0][got++] = 0.0;
251 } 256 }
252 } else { 257 } else {
253 for (size_t ch = 0; ch < channelCount; ++ch) { 258 for (size_t ch = 0; ch < channelCount; ++ch) {
254 got = input->getValues 259 got = input->getValues
255 (ch, blockFrame, blockFrame + blockSize, buffers[ch]); 260 (ch, blockFrame, blockFrame + m_blockSize, buffers[ch]);
256 while (got < blockSize) { 261 while (got < m_blockSize) {
257 buffers[ch][got++] = 0.0; 262 buffers[ch][got++] = 0.0;
258 } 263 }
259 } 264 }
260 } 265 }
261 266
262 if (fftPlan) { 267 if (fftPlan) {
263 for (size_t ch = 0; ch < channelCount; ++ch) { 268 for (size_t ch = 0; ch < channelCount; ++ch) {
264 for (size_t i = 0; i < blockSize; ++i) { 269 for (size_t i = 0; i < m_blockSize; ++i) {
265 fftInput[i] = buffers[ch][i]; 270 fftInput[i] = buffers[ch][i];
266 } 271 }
267 windower.cut(fftInput); 272 windower.cut(fftInput);
268 for (size_t i = 0; i < blockSize/2; ++i) { 273 for (size_t i = 0; i < m_blockSize/2; ++i) {
269 double temp = fftInput[i]; 274 double temp = fftInput[i];
270 fftInput[i] = fftInput[i + blockSize/2]; 275 fftInput[i] = fftInput[i + m_blockSize/2];
271 fftInput[i + blockSize/2] = temp; 276 fftInput[i + m_blockSize/2] = temp;
272 } 277 }
273 fftw_execute(fftPlan); 278 fftw_execute(fftPlan);
274 for (size_t i = 0; i < blockSize/2; ++i) { 279 for (size_t i = 0; i < m_blockSize/2; ++i) {
275 buffers[ch][i*2] = fftOutput[i][0]; 280 buffers[ch][i*2] = fftOutput[i][0];
276 buffers[ch][i*2 + 1] = fftOutput[i][1]; 281 buffers[ch][i*2 + 1] = fftOutput[i][1];
277 } 282 }
278 } 283 }
279 } 284 }
290 if (blockFrame == startFrame || completion > prevCompletion) { 295 if (blockFrame == startFrame || completion > prevCompletion) {
291 setCompletion(completion); 296 setCompletion(completion);
292 prevCompletion = completion; 297 prevCompletion = completion;
293 } 298 }
294 299
295 blockFrame += stepSize; 300 blockFrame += m_stepSize;
296 } 301 }
297 302
298 if (fftPlan) { 303 if (fftPlan) {
299 fftw_destroy_plan(fftPlan); 304 fftw_destroy_plan(fftPlan);
300 fftw_free(fftInput); 305 fftw_free(fftInput);