comparison transform/TransformFactory.cpp @ 107:dd11619b73ba

* Update along with latest Vamp API change. This has not yet been tested.
author Chris Cannam
date Mon, 26 Feb 2007 18:13:07 +0000
parents f4f52566e451
children 58f21cf235c7
comparison
equal deleted inserted replaced
106:f8e362511b2f 107:dd11619b73ba
149 populateFeatureExtractionPlugins(transforms); 149 populateFeatureExtractionPlugins(transforms);
150 populateRealTimePlugins(transforms); 150 populateRealTimePlugins(transforms);
151 151
152 // disambiguate plugins with similar names 152 // disambiguate plugins with similar names
153 153
154 std::map<QString, int> descriptions; 154 std::map<QString, int> names;
155 std::map<QString, QString> pluginSources; 155 std::map<QString, QString> pluginSources;
156 std::map<QString, QString> pluginMakers; 156 std::map<QString, QString> pluginMakers;
157 157
158 for (TransformDescriptionMap::iterator i = transforms.begin(); 158 for (TransformDescriptionMap::iterator i = transforms.begin();
159 i != transforms.end(); ++i) { 159 i != transforms.end(); ++i) {
160 160
161 TransformDesc desc = i->second; 161 TransformDesc desc = i->second;
162 162
163 QString td = desc.description; 163 QString td = desc.name;
164 QString tn = td.section(": ", 0, 0); 164 QString tn = td.section(": ", 0, 0);
165 QString pn = desc.name.section(":", 1, 1); 165 QString pn = desc.identifier.section(":", 1, 1);
166 166
167 if (pluginSources.find(tn) != pluginSources.end()) { 167 if (pluginSources.find(tn) != pluginSources.end()) {
168 if (pluginSources[tn] != pn && pluginMakers[tn] != desc.maker) { 168 if (pluginSources[tn] != pn && pluginMakers[tn] != desc.maker) {
169 ++descriptions[tn]; 169 ++names[tn];
170 } 170 }
171 } else { 171 } else {
172 ++descriptions[tn]; 172 ++names[tn];
173 pluginSources[tn] = pn; 173 pluginSources[tn] = pn;
174 pluginMakers[tn] = desc.maker; 174 pluginMakers[tn] = desc.maker;
175 } 175 }
176 } 176 }
177 177
180 180
181 for (TransformDescriptionMap::iterator i = transforms.begin(); 181 for (TransformDescriptionMap::iterator i = transforms.begin();
182 i != transforms.end(); ++i) { 182 i != transforms.end(); ++i) {
183 183
184 TransformDesc desc = i->second; 184 TransformDesc desc = i->second;
185 QString name = desc.name; 185 QString identifier = desc.identifier;
186 QString maker = desc.maker; 186 QString maker = desc.maker;
187 187
188 QString td = desc.description; 188 QString td = desc.name;
189 QString tn = td.section(": ", 0, 0); 189 QString tn = td.section(": ", 0, 0);
190 QString to = td.section(": ", 1); 190 QString to = td.section(": ", 1);
191 191
192 if (descriptions[tn] > 1) { 192 if (names[tn] > 1) {
193 maker.replace(QRegExp(tr(" [\\(<].*$")), ""); 193 maker.replace(QRegExp(tr(" [\\(<].*$")), "");
194 tn = QString("%1 [%2]").arg(tn).arg(maker); 194 tn = QString("%1 [%2]").arg(tn).arg(maker);
195 } 195 }
196 196
197 if (to != "") { 197 if (to != "") {
198 desc.description = QString("%1: %2").arg(tn).arg(to); 198 desc.name = QString("%1: %2").arg(tn).arg(to);
199 } else { 199 } else {
200 desc.description = tn; 200 desc.name = tn;
201 } 201 }
202 202
203 m_transforms[name] = desc; 203 m_transforms[identifier] = desc;
204 } 204 }
205 } 205 }
206 206
207 void 207 void
208 TransformFactory::populateFeatureExtractionPlugins(TransformDescriptionMap &transforms) 208 TransformFactory::populateFeatureExtractionPlugins(TransformDescriptionMap &transforms)
228 if (!plugin) { 228 if (!plugin) {
229 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl; 229 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl;
230 continue; 230 continue;
231 } 231 }
232 232
233 QString pluginDescription = plugin->getDescription().c_str(); 233 QString pluginName = plugin->getName().c_str();
234 QString category = factory->getPluginCategory(pluginId); 234 QString category = factory->getPluginCategory(pluginId);
235 235
236 Vamp::Plugin::OutputList outputs = 236 Vamp::Plugin::OutputList outputs =
237 plugin->getOutputDescriptors(); 237 plugin->getOutputDescriptors();
238 238
239 for (size_t j = 0; j < outputs.size(); ++j) { 239 for (size_t j = 0; j < outputs.size(); ++j) {
240 240
241 QString transformName = QString("%1:%2") 241 QString transformId = QString("%1:%2")
242 .arg(pluginId).arg(outputs[j].name.c_str()); 242 .arg(pluginId).arg(outputs[j].identifier.c_str());
243 243
244 QString userDescription; 244 QString userName;
245 QString friendlyName; 245 QString friendlyName;
246 QString units = outputs[j].unit.c_str(); 246 QString units = outputs[j].unit.c_str();
247 247
248 if (outputs.size() == 1) { 248 if (outputs.size() == 1) {
249 userDescription = pluginDescription; 249 userName = pluginName;
250 friendlyName = pluginDescription; 250 friendlyName = pluginName;
251 } else { 251 } else {
252 userDescription = QString("%1: %2") 252 userName = QString("%1: %2")
253 .arg(pluginDescription) 253 .arg(pluginName)
254 .arg(outputs[j].description.c_str()); 254 .arg(outputs[j].name.c_str());
255 friendlyName = outputs[j].description.c_str(); 255 friendlyName = outputs[j].name.c_str();
256 } 256 }
257 257
258 bool configurable = (!plugin->getPrograms().empty() || 258 bool configurable = (!plugin->getPrograms().empty() ||
259 !plugin->getParameterDescriptors().empty()); 259 !plugin->getParameterDescriptors().empty());
260 260
261 transforms[transformName] = 261 std::cerr << "Adding feature extraction plugin transform: id = " << transformId.toStdString() << std::endl;
262
263 transforms[transformId] =
262 TransformDesc(tr("Analysis"), 264 TransformDesc(tr("Analysis"),
263 category, 265 category,
264 transformName, 266 transformId,
265 userDescription, 267 userName,
266 friendlyName, 268 friendlyName,
267 plugin->getMaker().c_str(), 269 plugin->getMaker().c_str(),
268 units, 270 units,
269 configurable); 271 configurable);
270 } 272 }
302 //!!! if (descriptor->controlOutputPortCount == 0 || 304 //!!! if (descriptor->controlOutputPortCount == 0 ||
303 // descriptor->audioInputPortCount == 0) continue; 305 // descriptor->audioInputPortCount == 0) continue;
304 306
305 // std::cout << "TransformFactory::populateRealTimePlugins: plugin " << pluginId.toStdString() << " has " << descriptor->controlOutputPortCount << " control output ports, " << descriptor->audioOutputPortCount << " audio outputs, " << descriptor->audioInputPortCount << " audio inputs" << std::endl; 307 // std::cout << "TransformFactory::populateRealTimePlugins: plugin " << pluginId.toStdString() << " has " << descriptor->controlOutputPortCount << " control output ports, " << descriptor->audioOutputPortCount << " audio outputs, " << descriptor->audioInputPortCount << " audio inputs" << std::endl;
306 308
307 QString pluginDescription = descriptor->name.c_str(); 309 QString pluginName = descriptor->name.c_str();
308 QString category = factory->getPluginCategory(pluginId); 310 QString category = factory->getPluginCategory(pluginId);
309 bool configurable = (descriptor->parameterCount > 0); 311 bool configurable = (descriptor->parameterCount > 0);
310 312
311 if (descriptor->audioInputPortCount > 0) { 313 if (descriptor->audioInputPortCount > 0) {
312 314
313 for (size_t j = 0; j < descriptor->controlOutputPortCount; ++j) { 315 for (size_t j = 0; j < descriptor->controlOutputPortCount; ++j) {
314 316
315 QString transformName = QString("%1:%2").arg(pluginId).arg(j); 317 QString transformId = QString("%1:%2").arg(pluginId).arg(j);
316 QString userDescription; 318 QString userName;
317 QString units; 319 QString units;
318 320
319 if (j < descriptor->controlOutputPortNames.size() && 321 if (j < descriptor->controlOutputPortNames.size() &&
320 descriptor->controlOutputPortNames[j] != "") { 322 descriptor->controlOutputPortNames[j] != "") {
321 323
322 QString portName = descriptor->controlOutputPortNames[j].c_str(); 324 QString portName = descriptor->controlOutputPortNames[j].c_str();
323 325
324 userDescription = tr("%1: %2") 326 userName = tr("%1: %2")
325 .arg(pluginDescription) 327 .arg(pluginName)
326 .arg(portName); 328 .arg(portName);
327 329
328 if (unitRE.indexIn(portName) >= 0) { 330 if (unitRE.indexIn(portName) >= 0) {
329 units = unitRE.cap(1); 331 units = unitRE.cap(1);
330 } 332 }
331 333
332 } else if (descriptor->controlOutputPortCount > 1) { 334 } else if (descriptor->controlOutputPortCount > 1) {
333 335
334 userDescription = tr("%1: Output %2") 336 userName = tr("%1: Output %2")
335 .arg(pluginDescription) 337 .arg(pluginName)
336 .arg(j + 1); 338 .arg(j + 1);
337 339
338 } else { 340 } else {
339 341
340 userDescription = pluginDescription; 342 userName = pluginName;
341 } 343 }
342 344
343 345
344 transforms[transformName] = 346 transforms[transformId] =
345 TransformDesc(tr("Effects Data"), 347 TransformDesc(tr("Effects Data"),
346 category, 348 category,
347 transformName, 349 transformId,
348 userDescription, 350 userName,
349 userDescription, 351 userName,
350 descriptor->maker.c_str(), 352 descriptor->maker.c_str(),
351 units, 353 units,
352 configurable); 354 configurable);
353 } 355 }
354 } 356 }
355 357
356 if (!descriptor->isSynth || descriptor->audioInputPortCount > 0) { 358 if (!descriptor->isSynth || descriptor->audioInputPortCount > 0) {
357 359
358 if (descriptor->audioOutputPortCount > 0) { 360 if (descriptor->audioOutputPortCount > 0) {
359 361
360 QString transformName = QString("%1:A").arg(pluginId); 362 QString transformId = QString("%1:A").arg(pluginId);
361 QString type = tr("Effects"); 363 QString type = tr("Effects");
362 if (descriptor->audioInputPortCount == 0) { 364 if (descriptor->audioInputPortCount == 0) {
363 type = tr("Generators"); 365 type = tr("Generators");
364 } 366 }
365 367
366 transforms[transformName] = 368 transforms[transformId] =
367 TransformDesc(type, 369 TransformDesc(type,
368 category, 370 category,
369 transformName, 371 transformId,
370 pluginDescription, 372 pluginName,
371 pluginDescription, 373 pluginName,
372 descriptor->maker.c_str(), 374 descriptor->maker.c_str(),
373 "", 375 "",
374 configurable); 376 configurable);
375 } 377 }
376 } 378 }
377 } 379 }
378 } 380 }
379 381
380 QString 382 QString
381 TransformFactory::getTransformDescription(TransformName name) 383 TransformFactory::getTransformName(TransformId identifier)
382 { 384 {
383 if (m_transforms.find(name) != m_transforms.end()) { 385 if (m_transforms.find(identifier) != m_transforms.end()) {
384 return m_transforms[name].description; 386 return m_transforms[identifier].name;
385 } else return ""; 387 } else return "";
386 } 388 }
387 389
388 QString 390 QString
389 TransformFactory::getTransformFriendlyName(TransformName name) 391 TransformFactory::getTransformFriendlyName(TransformId identifier)
390 { 392 {
391 if (m_transforms.find(name) != m_transforms.end()) { 393 if (m_transforms.find(identifier) != m_transforms.end()) {
392 return m_transforms[name].friendlyName; 394 return m_transforms[identifier].friendlyName;
393 } else return ""; 395 } else return "";
394 } 396 }
395 397
396 QString 398 QString
397 TransformFactory::getTransformUnits(TransformName name) 399 TransformFactory::getTransformUnits(TransformId identifier)
398 { 400 {
399 if (m_transforms.find(name) != m_transforms.end()) { 401 if (m_transforms.find(identifier) != m_transforms.end()) {
400 return m_transforms[name].units; 402 return m_transforms[identifier].units;
401 } else return ""; 403 } else return "";
402 } 404 }
403 405
404 bool 406 bool
405 TransformFactory::isTransformConfigurable(TransformName name) 407 TransformFactory::isTransformConfigurable(TransformId identifier)
406 { 408 {
407 if (m_transforms.find(name) != m_transforms.end()) { 409 if (m_transforms.find(identifier) != m_transforms.end()) {
408 return m_transforms[name].configurable; 410 return m_transforms[identifier].configurable;
409 } else return false; 411 } else return false;
410 } 412 }
411 413
412 bool 414 bool
413 TransformFactory::getTransformChannelRange(TransformName name, 415 TransformFactory::getTransformChannelRange(TransformId identifier,
414 int &min, int &max) 416 int &min, int &max)
415 { 417 {
416 QString id = name.section(':', 0, 2); 418 QString id = identifier.section(':', 0, 2);
417 419
418 if (FeatureExtractionPluginFactory::instanceFor(id)) { 420 if (FeatureExtractionPluginFactory::instanceFor(id)) {
419 421
420 Vamp::Plugin *plugin = 422 Vamp::Plugin *plugin =
421 FeatureExtractionPluginFactory::instanceFor(id)-> 423 FeatureExtractionPluginFactory::instanceFor(id)->
443 445
444 return false; 446 return false;
445 } 447 }
446 448
447 bool 449 bool
448 TransformFactory::getChannelRange(TransformName name, Vamp::PluginBase *plugin, 450 TransformFactory::getChannelRange(TransformId identifier, Vamp::PluginBase *plugin,
449 int &minChannels, int &maxChannels) 451 int &minChannels, int &maxChannels)
450 { 452 {
451 Vamp::Plugin *vp = 0; 453 Vamp::Plugin *vp = 0;
452 if ((vp = dynamic_cast<Vamp::Plugin *>(plugin)) || 454 if ((vp = dynamic_cast<Vamp::Plugin *>(plugin)) ||
453 (vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin))) { 455 (vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin))) {
454 minChannels = vp->getMinChannelCount(); 456 minChannels = vp->getMinChannelCount();
455 maxChannels = vp->getMaxChannelCount(); 457 maxChannels = vp->getMaxChannelCount();
456 return true; 458 return true;
457 } else { 459 } else {
458 return getTransformChannelRange(name, minChannels, maxChannels); 460 return getTransformChannelRange(identifier, minChannels, maxChannels);
459 } 461 }
460 } 462 }
461 463
462 Model * 464 Model *
463 TransformFactory::getConfigurationForTransform(TransformName name, 465 TransformFactory::getConfigurationForTransform(TransformId identifier,
464 const std::vector<Model *> &candidateInputModels, 466 const std::vector<Model *> &candidateInputModels,
465 PluginTransform::ExecutionContext &context, 467 PluginTransform::ExecutionContext &context,
466 QString &configurationXml, 468 QString &configurationXml,
467 AudioCallbackPlaySource *source) 469 AudioCallbackPlaySource *source)
468 { 470 {
483 } 485 }
484 modelMap[modelName] = candidateInputModels[i]; 486 modelMap[modelName] = candidateInputModels[i];
485 candidateModelNames.push_back(modelName); 487 candidateModelNames.push_back(modelName);
486 } 488 }
487 489
488 QString id = name.section(':', 0, 2); 490 QString id = identifier.section(':', 0, 2);
489 QString output = name.section(':', 3); 491 QString output = identifier.section(':', 3);
490 QString outputLabel = ""; 492 QString outputLabel = "";
491 493
492 bool ok = false; 494 bool ok = false;
493 configurationXml = m_lastConfigurations[name]; 495 configurationXml = m_lastConfigurations[identifier];
494 496
495 // std::cerr << "last configuration: " << configurationXml.toStdString() << std::endl; 497 // std::cerr << "last configuration: " << configurationXml.toStdString() << std::endl;
496 498
497 Vamp::PluginBase *plugin = 0; 499 Vamp::PluginBase *plugin = 0;
498 500
513 515
514 std::vector<Vamp::Plugin::OutputDescriptor> od = 516 std::vector<Vamp::Plugin::OutputDescriptor> od =
515 vp->getOutputDescriptors(); 517 vp->getOutputDescriptors();
516 if (od.size() > 1) { 518 if (od.size() > 1) {
517 for (size_t i = 0; i < od.size(); ++i) { 519 for (size_t i = 0; i < od.size(); ++i) {
518 if (od[i].name == output.toStdString()) { 520 if (od[i].identifier == output.toStdString()) {
519 outputLabel = od[i].description.c_str(); 521 outputLabel = od[i].name.c_str();
520 break; 522 break;
521 } 523 }
522 } 524 }
523 } 525 }
524 } 526 }
577 sourceChannels = dynamic_cast<DenseTimeValueModel *>(inputModel) 579 sourceChannels = dynamic_cast<DenseTimeValueModel *>(inputModel)
578 ->getChannelCount(); 580 ->getChannelCount();
579 } 581 }
580 582
581 int minChannels = 1, maxChannels = sourceChannels; 583 int minChannels = 1, maxChannels = sourceChannels;
582 getChannelRange(name, plugin, minChannels, maxChannels); 584 getChannelRange(identifier, plugin, minChannels, maxChannels);
583 585
584 int targetChannels = sourceChannels; 586 int targetChannels = sourceChannels;
585 if (!effect) { 587 if (!effect) {
586 if (sourceChannels < minChannels) targetChannels = minChannels; 588 if (sourceChannels < minChannels) targetChannels = minChannels;
587 if (sourceChannels > maxChannels) targetChannels = maxChannels; 589 if (sourceChannels > maxChannels) targetChannels = maxChannels;
634 } else { 636 } else {
635 delete plugin; 637 delete plugin;
636 } 638 }
637 } 639 }
638 640
639 if (ok) m_lastConfigurations[name] = configurationXml; 641 if (ok) m_lastConfigurations[identifier] = configurationXml;
640 642
641 return ok ? inputModel : 0; 643 return ok ? inputModel : 0;
642 } 644 }
643 645
644 PluginTransform::ExecutionContext 646 PluginTransform::ExecutionContext
645 TransformFactory::getDefaultContextForTransform(TransformName name, 647 TransformFactory::getDefaultContextForTransform(TransformId identifier,
646 Model *inputModel) 648 Model *inputModel)
647 { 649 {
648 PluginTransform::ExecutionContext context(-1); 650 PluginTransform::ExecutionContext context(-1);
649 651
650 QString id = name.section(':', 0, 2); 652 QString id = identifier.section(':', 0, 2);
651 653
652 if (FeatureExtractionPluginFactory::instanceFor(id)) { 654 if (FeatureExtractionPluginFactory::instanceFor(id)) {
653 655
654 Vamp::Plugin *vp = 656 Vamp::Plugin *vp =
655 FeatureExtractionPluginFactory::instanceFor(id)->instantiatePlugin 657 FeatureExtractionPluginFactory::instanceFor(id)->instantiatePlugin
661 663
662 return context; 664 return context;
663 } 665 }
664 666
665 Transform * 667 Transform *
666 TransformFactory::createTransform(TransformName name, Model *inputModel, 668 TransformFactory::createTransform(TransformId identifier, Model *inputModel,
667 const PluginTransform::ExecutionContext &context, 669 const PluginTransform::ExecutionContext &context,
668 QString configurationXml, bool start) 670 QString configurationXml, bool start)
669 { 671 {
670 Transform *transform = 0; 672 Transform *transform = 0;
671 673
672 QString id = name.section(':', 0, 2); 674 QString id = identifier.section(':', 0, 2);
673 QString output = name.section(':', 3); 675 QString output = identifier.section(':', 3);
674 676
675 if (FeatureExtractionPluginFactory::instanceFor(id)) { 677 if (FeatureExtractionPluginFactory::instanceFor(id)) {
676 transform = new FeatureExtractionPluginTransform(inputModel, 678 transform = new FeatureExtractionPluginTransform(inputModel,
677 id, 679 id,
678 context, 680 context,
681 } else if (RealTimePluginFactory::instanceFor(id)) { 683 } else if (RealTimePluginFactory::instanceFor(id)) {
682 transform = new RealTimePluginTransform(inputModel, 684 transform = new RealTimePluginTransform(inputModel,
683 id, 685 id,
684 context, 686 context,
685 configurationXml, 687 configurationXml,
686 getTransformUnits(name), 688 getTransformUnits(identifier),
687 output == "A" ? -1 : 689 output == "A" ? -1 :
688 output.toInt()); 690 output.toInt());
689 } else { 691 } else {
690 std::cerr << "TransformFactory::createTransform: Unknown transform \"" 692 std::cerr << "TransformFactory::createTransform: Unknown transform \""
691 << name.toStdString() << "\"" << std::endl; 693 << identifier.toStdString() << "\"" << std::endl;
692 return transform; 694 return transform;
693 } 695 }
694 696
695 if (start && transform) transform->start(); 697 if (start && transform) transform->start();
696 transform->setObjectName(name); 698 transform->setObjectName(identifier);
697 return transform; 699 return transform;
698 } 700 }
699 701
700 Model * 702 Model *
701 TransformFactory::transform(TransformName name, Model *inputModel, 703 TransformFactory::transform(TransformId identifier, Model *inputModel,
702 const PluginTransform::ExecutionContext &context, 704 const PluginTransform::ExecutionContext &context,
703 QString configurationXml) 705 QString configurationXml)
704 { 706 {
705 Transform *t = createTransform(name, inputModel, context, 707 Transform *t = createTransform(identifier, inputModel, context,
706 configurationXml, false); 708 configurationXml, false);
707 709
708 if (!t) return 0; 710 if (!t) return 0;
709 711
710 connect(t, SIGNAL(finished()), this, SLOT(transformFinished())); 712 connect(t, SIGNAL(finished()), this, SLOT(transformFinished()));
712 t->start(); 714 t->start();
713 Model *model = t->detachOutputModel(); 715 Model *model = t->detachOutputModel();
714 716
715 if (model) { 717 if (model) {
716 QString imn = inputModel->objectName(); 718 QString imn = inputModel->objectName();
717 QString trn = getTransformFriendlyName(name); 719 QString trn = getTransformFriendlyName(identifier);
718 if (imn != "") { 720 if (imn != "") {
719 if (trn != "") { 721 if (trn != "") {
720 model->setObjectName(tr("%1: %2").arg(imn).arg(trn)); 722 model->setObjectName(tr("%1: %2").arg(imn).arg(trn));
721 } else { 723 } else {
722 model->setObjectName(imn); 724 model->setObjectName(imn);