comparison plugin/transform/TransformFactory.cpp @ 350:d7c41483af8f

* Merge from transforms branch -- switch over to using Transform object properly
author Chris Cannam
date Fri, 07 Dec 2007 16:47:31 +0000
parents 1afaf98dbf11
children 7263e37d8913
comparison
equal deleted inserted replaced
348:edda24bb85fc 350:d7c41483af8f
26 26
27 #include <iostream> 27 #include <iostream>
28 #include <set> 28 #include <set>
29 29
30 #include <QRegExp> 30 #include <QRegExp>
31 #include <QTextStream>
31 32
32 TransformFactory * 33 TransformFactory *
33 TransformFactory::m_instance = new TransformFactory; 34 TransformFactory::m_instance = new TransformFactory;
34 35
35 TransformFactory * 36 TransformFactory *
41 TransformFactory::~TransformFactory() 42 TransformFactory::~TransformFactory()
42 { 43 {
43 } 44 }
44 45
45 TransformList 46 TransformList
46 TransformFactory::getAllTransforms() 47 TransformFactory::getAllTransformDescriptions()
47 { 48 {
48 if (m_transforms.empty()) populateTransforms(); 49 if (m_transforms.empty()) populateTransforms();
49 50
50 std::set<TransformDescription> dset; 51 std::set<TransformDescription> dset;
51 for (TransformDescriptionMap::const_iterator i = m_transforms.begin(); 52 for (TransformDescriptionMap::const_iterator i = m_transforms.begin();
58 i != dset.end(); ++i) { 59 i != dset.end(); ++i) {
59 list.push_back(*i); 60 list.push_back(*i);
60 } 61 }
61 62
62 return list; 63 return list;
64 }
65
66 TransformDescription
67 TransformFactory::getTransformDescription(TransformId id)
68 {
69 if (m_transforms.empty()) populateTransforms();
70
71 if (m_transforms.find(id) == m_transforms.end()) {
72 return TransformDescription();
73 }
74
75 return m_transforms[id];
63 } 76 }
64 77
65 std::vector<QString> 78 std::vector<QString>
66 TransformFactory::getAllTransformTypes() 79 TransformFactory::getAllTransformTypes()
67 { 80 {
215 std::cerr << "WARNING: TransformFactory::populateTransforms: No feature extraction plugin factory for instance " << pluginId.toLocal8Bit().data() << std::endl; 228 std::cerr << "WARNING: TransformFactory::populateTransforms: No feature extraction plugin factory for instance " << pluginId.toLocal8Bit().data() << std::endl;
216 continue; 229 continue;
217 } 230 }
218 231
219 Vamp::Plugin *plugin = 232 Vamp::Plugin *plugin =
220 factory->instantiatePlugin(pluginId, 48000); 233 factory->instantiatePlugin(pluginId, 44100);
221 234
222 if (!plugin) { 235 if (!plugin) {
223 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl; 236 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl;
224 continue; 237 continue;
225 } 238 }
421 } 434 }
422 } 435 }
423 } 436 }
424 } 437 }
425 438
439
440 Transform
441 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate)
442 {
443 Transform t;
444 t.setIdentifier(id);
445 if (rate != 0) t.setSampleRate(rate);
446
447 Vamp::PluginBase *plugin = instantiatePluginFor(id, rate);
448
449 if (plugin) {
450 setParametersFromPlugin(t, plugin);
451 makeContextConsistentWithPlugin(t, plugin);
452 delete plugin;
453 }
454
455 return t;
456 }
457
458 Vamp::PluginBase *
459 TransformFactory::instantiatePluginFor(TransformId identifier, size_t rate)
460 {
461 Transform t;
462 t.setIdentifier(identifier);
463 if (rate == 0) rate = 44100;
464 QString pluginId = t.getPluginIdentifier();
465
466 Vamp::PluginBase *plugin = 0;
467
468 if (t.getType() == Transform::FeatureExtraction) {
469
470 FeatureExtractionPluginFactory *factory =
471 FeatureExtractionPluginFactory::instanceFor(pluginId);
472
473 plugin = factory->instantiatePlugin(pluginId, rate);
474
475 } else {
476
477 RealTimePluginFactory *factory =
478 RealTimePluginFactory::instanceFor(pluginId);
479
480 plugin = factory->instantiatePlugin(pluginId, 0, 0, rate, 1024, 1);
481 }
482
483 return plugin;
484 }
485
486 Vamp::Plugin *
487 TransformFactory::downcastVampPlugin(Vamp::PluginBase *plugin)
488 {
489 Vamp::Plugin *vp = dynamic_cast<Vamp::Plugin *>(plugin);
490 if (!vp) {
491 // std::cerr << "makeConsistentWithPlugin: not a Vamp::Plugin" << std::endl;
492 vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin); //!!! why?
493 }
494 if (!vp) {
495 // std::cerr << "makeConsistentWithPlugin: not a Vamp::PluginHostAdapter" << std::endl;
496 vp = dynamic_cast<Vamp::HostExt::PluginWrapper *>(plugin); //!!! no, I mean really why?
497 }
498 if (!vp) {
499 // std::cerr << "makeConsistentWithPlugin: not a Vamp::HostExt::PluginWrapper" << std::endl;
500 }
501 return vp;
502 }
503
426 bool 504 bool
427 TransformFactory::haveTransform(TransformId identifier) 505 TransformFactory::haveTransform(TransformId identifier)
428 { 506 {
429 if (m_transforms.empty()) populateTransforms(); 507 if (m_transforms.empty()) populateTransforms();
430 return (m_transforms.find(identifier) != m_transforms.end()); 508 return (m_transforms.find(identifier) != m_transforms.end());
452 if (m_transforms.find(identifier) != m_transforms.end()) { 530 if (m_transforms.find(identifier) != m_transforms.end()) {
453 return m_transforms[identifier].units; 531 return m_transforms[identifier].units;
454 } else return ""; 532 } else return "";
455 } 533 }
456 534
535 Vamp::Plugin::InputDomain
536 TransformFactory::getTransformInputDomain(TransformId identifier)
537 {
538 Transform transform;
539 transform.setIdentifier(identifier);
540
541 if (transform.getType() != Transform::FeatureExtraction) {
542 return Vamp::Plugin::TimeDomain;
543 }
544
545 Vamp::Plugin *plugin =
546 downcastVampPlugin(instantiatePluginFor(identifier, 0));
547
548 if (plugin) {
549 Vamp::Plugin::InputDomain d = plugin->getInputDomain();
550 delete plugin;
551 return d;
552 }
553
554 return Vamp::Plugin::TimeDomain;
555 }
556
457 bool 557 bool
458 TransformFactory::isTransformConfigurable(TransformId identifier) 558 TransformFactory::isTransformConfigurable(TransformId identifier)
459 { 559 {
460 if (m_transforms.find(identifier) != m_transforms.end()) { 560 if (m_transforms.find(identifier) != m_transforms.end()) {
461 return m_transforms[identifier].configurable; 561 return m_transforms[identifier].configurable;
470 570
471 if (FeatureExtractionPluginFactory::instanceFor(id)) { 571 if (FeatureExtractionPluginFactory::instanceFor(id)) {
472 572
473 Vamp::Plugin *plugin = 573 Vamp::Plugin *plugin =
474 FeatureExtractionPluginFactory::instanceFor(id)-> 574 FeatureExtractionPluginFactory::instanceFor(id)->
475 instantiatePlugin(id, 48000); 575 instantiatePlugin(id, 44100);
476 if (!plugin) return false; 576 if (!plugin) return false;
477 577
478 min = plugin->getMinChannelCount(); 578 min = plugin->getMinChannelCount();
479 max = plugin->getMaxChannelCount(); 579 max = plugin->getMaxChannelCount();
480 delete plugin; 580 delete plugin;
481 581
482 return true; 582 return true;
483 583
484 } else if (RealTimePluginFactory::instanceFor(id)) { 584 } else if (RealTimePluginFactory::instanceFor(id)) {
585
586 // don't need to instantiate
485 587
486 const RealTimePluginDescriptor *descriptor = 588 const RealTimePluginDescriptor *descriptor =
487 RealTimePluginFactory::instanceFor(id)-> 589 RealTimePluginFactory::instanceFor(id)->
488 getPluginDescriptor(id); 590 getPluginDescriptor(id);
489 if (!descriptor) return false; 591 if (!descriptor) return false;
501 TransformFactory::setParametersFromPlugin(Transform &transform, 603 TransformFactory::setParametersFromPlugin(Transform &transform,
502 Vamp::PluginBase *plugin) 604 Vamp::PluginBase *plugin)
503 { 605 {
504 Transform::ParameterMap pmap; 606 Transform::ParameterMap pmap;
505 607
608 //!!! record plugin & API version
609
610 //!!! check that this is the right plugin!
611
506 Vamp::PluginBase::ParameterList parameters = 612 Vamp::PluginBase::ParameterList parameters =
507 plugin->getParameterDescriptors(); 613 plugin->getParameterDescriptors();
508 614
509 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin(); 615 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
510 i != parameters.end(); ++i) { 616 i != parameters.end(); ++i) {
537 643
538 transform.setConfiguration(cmap); 644 transform.setConfiguration(cmap);
539 } 645 }
540 646
541 void 647 void
648 TransformFactory::setPluginParameters(const Transform &transform,
649 Vamp::PluginBase *plugin)
650 {
651 //!!! check plugin & API version (see e.g. PluginXml::setParameters)
652
653 //!!! check that this is the right plugin!
654
655 RealTimePluginInstance *rtpi =
656 dynamic_cast<RealTimePluginInstance *>(plugin);
657
658 if (rtpi) {
659 const Transform::ConfigurationMap &cmap = transform.getConfiguration();
660 for (Transform::ConfigurationMap::const_iterator i = cmap.begin();
661 i != cmap.end(); ++i) {
662 rtpi->configure(i->first.toStdString(), i->second.toStdString());
663 }
664 }
665
666 if (transform.getProgram() != "") {
667 plugin->selectProgram(transform.getProgram().toStdString());
668 }
669
670 const Transform::ParameterMap &pmap = transform.getParameters();
671
672 Vamp::PluginBase::ParameterList parameters =
673 plugin->getParameterDescriptors();
674
675 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin();
676 i != parameters.end(); ++i) {
677 QString key = i->identifier.c_str();
678 Transform::ParameterMap::const_iterator pmi = pmap.find(key);
679 if (pmi != pmap.end()) {
680 plugin->setParameter(i->identifier, pmi->second);
681 }
682 }
683 }
684
685 void
542 TransformFactory::makeContextConsistentWithPlugin(Transform &transform, 686 TransformFactory::makeContextConsistentWithPlugin(Transform &transform,
543 Vamp::PluginBase *plugin) 687 Vamp::PluginBase *plugin)
544 { 688 {
545 const Vamp::Plugin *vp = dynamic_cast<const Vamp::Plugin *>(plugin); 689 const Vamp::Plugin *vp = downcastVampPlugin(plugin);
546 if (!vp) {
547 // std::cerr << "makeConsistentWithPlugin: not a Vamp::Plugin" << std::endl;
548 vp = dynamic_cast<const Vamp::PluginHostAdapter *>(plugin); //!!! why?
549 }
550 if (!vp) {
551 // std::cerr << "makeConsistentWithPlugin: not a Vamp::PluginHostAdapter" << std::endl;
552 vp = dynamic_cast<const Vamp::HostExt::PluginWrapper *>(plugin); //!!! no, I mean really why?
553 }
554 if (!vp) {
555 // std::cerr << "makeConsistentWithPlugin: not a Vamp::HostExt::PluginWrapper" << std::endl;
556 }
557 690
558 if (!vp) { 691 if (!vp) {
559 // time domain input for real-time effects plugin 692 // time domain input for real-time effects plugin
560 if (!transform.getBlockSize()) { 693 if (!transform.getBlockSize()) {
561 if (!transform.getStepSize()) transform.setStepSize(1024); 694 if (!transform.getStepSize()) transform.setStepSize(1024);
584 } 717 }
585 } 718 }
586 } 719 }
587 } 720 }
588 721
589 Transform 722 QString
590 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate) 723 TransformFactory::getPluginConfigurationXml(const Transform &t)
591 { 724 {
592 Transform t; 725 QString xml;
593 t.setIdentifier(id); 726
594 727 Vamp::PluginBase *plugin = instantiatePluginFor(t.getIdentifier(), 0);
595 if (rate == 0) { 728 if (!plugin) {
596 rate = 44100; 729 std::cerr << "TransformFactory::getPluginConfigurationXml: "
597 } else { 730 << "Unable to instantiate plugin for transform \""
598 t.setSampleRate(rate); 731 << t.getIdentifier().toStdString() << "\"" << std::endl;
599 } 732 return xml;
600 733 }
601 QString pluginId = t.getPluginIdentifier(); 734
602 735 QTextStream out(&xml);
603 if (t.getType() == Transform::FeatureExtraction) { 736 PluginXml(plugin).toXml(out);
604 737 delete plugin;
605 FeatureExtractionPluginFactory *factory = 738
606 FeatureExtractionPluginFactory::instanceFor(pluginId); 739 return xml;
607 740 }
608 Vamp::Plugin *plugin = factory->instantiatePlugin 741
609 (pluginId, rate); 742 void
610 743 TransformFactory::setParametersFromPluginConfigurationXml(Transform &t,
611 if (plugin) { 744 QString xml)
612 setParametersFromPlugin(t, plugin); 745 {
613 makeContextConsistentWithPlugin(t, plugin); 746 Vamp::PluginBase *plugin = instantiatePluginFor(t.getIdentifier(), 0);
614 delete plugin; 747 if (!plugin) {
615 } 748 std::cerr << "TransformFactory::setParametersFromPluginConfigurationXml: "
616 749 << "Unable to instantiate plugin for transform \""
617 } else { 750 << t.getIdentifier().toStdString() << "\"" << std::endl;
618 751 return;
619 RealTimePluginFactory *factory = 752 }
620 RealTimePluginFactory::instanceFor(pluginId); 753
621 754 PluginXml(plugin).setParametersFromXml(xml);
622 RealTimePluginInstance *plugin = factory->instantiatePlugin 755 setParametersFromPlugin(t, plugin);
623 (pluginId, 0, 0, rate, 1024, 1); 756 delete plugin;
624 757 }
625 if (plugin) { 758
626 setParametersFromPlugin(t, plugin);
627 makeContextConsistentWithPlugin(t, plugin);
628 delete plugin;
629 }
630 }
631
632 return t;
633 }
634