Mercurial > hg > svcore
comparison plugin/transform/TransformFactory.cpp @ 384:6f6ab834449d spectrogram-cache-rejig
* Merge from trunk
author | Chris Cannam |
---|---|
date | Wed, 27 Feb 2008 11:59:42 +0000 |
parents | 1afaf98dbf11 |
children |
comparison
equal
deleted
inserted
replaced
337:a6fab10ff9e6 | 384:6f6ab834449d |
---|---|
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(); |
52 i != m_transforms.end(); ++i) { | 53 i != m_transforms.end(); ++i) { |
54 // std::cerr << "inserting transform into set: id = " << i->second.identifier.toStdString() << std::endl; | |
53 dset.insert(i->second); | 55 dset.insert(i->second); |
54 } | 56 } |
55 | 57 |
56 TransformList list; | 58 TransformList list; |
57 for (std::set<TransformDescription>::const_iterator i = dset.begin(); | 59 for (std::set<TransformDescription>::const_iterator i = dset.begin(); |
58 i != dset.end(); ++i) { | 60 i != dset.end(); ++i) { |
61 // std::cerr << "inserting transform into list: id = " << i->identifier.toStdString() << std::endl; | |
59 list.push_back(*i); | 62 list.push_back(*i); |
60 } | 63 } |
61 | 64 |
62 return list; | 65 return list; |
66 } | |
67 | |
68 TransformDescription | |
69 TransformFactory::getTransformDescription(TransformId id) | |
70 { | |
71 if (m_transforms.empty()) populateTransforms(); | |
72 | |
73 if (m_transforms.find(id) == m_transforms.end()) { | |
74 return TransformDescription(); | |
75 } | |
76 | |
77 return m_transforms[id]; | |
63 } | 78 } |
64 | 79 |
65 std::vector<QString> | 80 std::vector<QString> |
66 TransformFactory::getAllTransformTypes() | 81 TransformFactory::getAllTransformTypes() |
67 { | 82 { |
215 std::cerr << "WARNING: TransformFactory::populateTransforms: No feature extraction plugin factory for instance " << pluginId.toLocal8Bit().data() << std::endl; | 230 std::cerr << "WARNING: TransformFactory::populateTransforms: No feature extraction plugin factory for instance " << pluginId.toLocal8Bit().data() << std::endl; |
216 continue; | 231 continue; |
217 } | 232 } |
218 | 233 |
219 Vamp::Plugin *plugin = | 234 Vamp::Plugin *plugin = |
220 factory->instantiatePlugin(pluginId, 48000); | 235 factory->instantiatePlugin(pluginId, 44100); |
221 | 236 |
222 if (!plugin) { | 237 if (!plugin) { |
223 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl; | 238 std::cerr << "WARNING: TransformFactory::populateTransforms: Failed to instantiate plugin " << pluginId.toLocal8Bit().data() << std::endl; |
224 continue; | 239 continue; |
225 } | 240 } |
271 } | 286 } |
272 | 287 |
273 bool configurable = (!plugin->getPrograms().empty() || | 288 bool configurable = (!plugin->getPrograms().empty() || |
274 !plugin->getParameterDescriptors().empty()); | 289 !plugin->getParameterDescriptors().empty()); |
275 | 290 |
276 // std::cerr << "Feature extraction plugin transform: " << transformId.toStdString() << std::endl; | 291 // std::cerr << "Feature extraction plugin transform: " << transformId.toStdString() << " friendly name: " << friendlyName.toStdString() << std::endl; |
277 | 292 |
278 transforms[transformId] = | 293 transforms[transformId] = |
279 TransformDescription(tr("Analysis"), | 294 TransformDescription(tr("Analysis"), |
280 category, | 295 category, |
281 transformId, | 296 transformId, |
421 } | 436 } |
422 } | 437 } |
423 } | 438 } |
424 } | 439 } |
425 | 440 |
441 | |
442 Transform | |
443 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate) | |
444 { | |
445 Transform t; | |
446 t.setIdentifier(id); | |
447 if (rate != 0) t.setSampleRate(rate); | |
448 | |
449 Vamp::PluginBase *plugin = instantiateDefaultPluginFor(id, rate); | |
450 | |
451 if (plugin) { | |
452 t.setPluginVersion(QString("%1").arg(plugin->getPluginVersion())); | |
453 setParametersFromPlugin(t, plugin); | |
454 makeContextConsistentWithPlugin(t, plugin); | |
455 delete plugin; | |
456 } | |
457 | |
458 return t; | |
459 } | |
460 | |
461 Vamp::PluginBase * | |
462 TransformFactory::instantiatePluginFor(const Transform &transform) | |
463 { | |
464 Vamp::PluginBase *plugin = instantiateDefaultPluginFor | |
465 (transform.getIdentifier(), transform.getSampleRate()); | |
466 if (plugin) { | |
467 setPluginParameters(transform, plugin); | |
468 } | |
469 return plugin; | |
470 } | |
471 | |
472 Vamp::PluginBase * | |
473 TransformFactory::instantiateDefaultPluginFor(TransformId identifier, size_t rate) | |
474 { | |
475 Transform t; | |
476 t.setIdentifier(identifier); | |
477 if (rate == 0) rate = 44100; | |
478 QString pluginId = t.getPluginIdentifier(); | |
479 | |
480 Vamp::PluginBase *plugin = 0; | |
481 | |
482 if (t.getType() == Transform::FeatureExtraction) { | |
483 | |
484 FeatureExtractionPluginFactory *factory = | |
485 FeatureExtractionPluginFactory::instanceFor(pluginId); | |
486 | |
487 plugin = factory->instantiatePlugin(pluginId, rate); | |
488 | |
489 } else { | |
490 | |
491 RealTimePluginFactory *factory = | |
492 RealTimePluginFactory::instanceFor(pluginId); | |
493 | |
494 plugin = factory->instantiatePlugin(pluginId, 0, 0, rate, 1024, 1); | |
495 } | |
496 | |
497 return plugin; | |
498 } | |
499 | |
500 Vamp::Plugin * | |
501 TransformFactory::downcastVampPlugin(Vamp::PluginBase *plugin) | |
502 { | |
503 Vamp::Plugin *vp = dynamic_cast<Vamp::Plugin *>(plugin); | |
504 if (!vp) { | |
505 // std::cerr << "makeConsistentWithPlugin: not a Vamp::Plugin" << std::endl; | |
506 vp = dynamic_cast<Vamp::PluginHostAdapter *>(plugin); //!!! why? | |
507 } | |
508 if (!vp) { | |
509 // std::cerr << "makeConsistentWithPlugin: not a Vamp::PluginHostAdapter" << std::endl; | |
510 vp = dynamic_cast<Vamp::HostExt::PluginWrapper *>(plugin); //!!! no, I mean really why? | |
511 } | |
512 if (!vp) { | |
513 // std::cerr << "makeConsistentWithPlugin: not a Vamp::HostExt::PluginWrapper" << std::endl; | |
514 } | |
515 return vp; | |
516 } | |
517 | |
426 bool | 518 bool |
427 TransformFactory::haveTransform(TransformId identifier) | 519 TransformFactory::haveTransform(TransformId identifier) |
428 { | 520 { |
429 if (m_transforms.empty()) populateTransforms(); | 521 if (m_transforms.empty()) populateTransforms(); |
430 return (m_transforms.find(identifier) != m_transforms.end()); | 522 return (m_transforms.find(identifier) != m_transforms.end()); |
452 if (m_transforms.find(identifier) != m_transforms.end()) { | 544 if (m_transforms.find(identifier) != m_transforms.end()) { |
453 return m_transforms[identifier].units; | 545 return m_transforms[identifier].units; |
454 } else return ""; | 546 } else return ""; |
455 } | 547 } |
456 | 548 |
549 Vamp::Plugin::InputDomain | |
550 TransformFactory::getTransformInputDomain(TransformId identifier) | |
551 { | |
552 Transform transform; | |
553 transform.setIdentifier(identifier); | |
554 | |
555 if (transform.getType() != Transform::FeatureExtraction) { | |
556 return Vamp::Plugin::TimeDomain; | |
557 } | |
558 | |
559 Vamp::Plugin *plugin = | |
560 downcastVampPlugin(instantiateDefaultPluginFor(identifier, 0)); | |
561 | |
562 if (plugin) { | |
563 Vamp::Plugin::InputDomain d = plugin->getInputDomain(); | |
564 delete plugin; | |
565 return d; | |
566 } | |
567 | |
568 return Vamp::Plugin::TimeDomain; | |
569 } | |
570 | |
457 bool | 571 bool |
458 TransformFactory::isTransformConfigurable(TransformId identifier) | 572 TransformFactory::isTransformConfigurable(TransformId identifier) |
459 { | 573 { |
460 if (m_transforms.find(identifier) != m_transforms.end()) { | 574 if (m_transforms.find(identifier) != m_transforms.end()) { |
461 return m_transforms[identifier].configurable; | 575 return m_transforms[identifier].configurable; |
470 | 584 |
471 if (FeatureExtractionPluginFactory::instanceFor(id)) { | 585 if (FeatureExtractionPluginFactory::instanceFor(id)) { |
472 | 586 |
473 Vamp::Plugin *plugin = | 587 Vamp::Plugin *plugin = |
474 FeatureExtractionPluginFactory::instanceFor(id)-> | 588 FeatureExtractionPluginFactory::instanceFor(id)-> |
475 instantiatePlugin(id, 48000); | 589 instantiatePlugin(id, 44100); |
476 if (!plugin) return false; | 590 if (!plugin) return false; |
477 | 591 |
478 min = plugin->getMinChannelCount(); | 592 min = plugin->getMinChannelCount(); |
479 max = plugin->getMaxChannelCount(); | 593 max = plugin->getMaxChannelCount(); |
480 delete plugin; | 594 delete plugin; |
481 | 595 |
482 return true; | 596 return true; |
483 | 597 |
484 } else if (RealTimePluginFactory::instanceFor(id)) { | 598 } else if (RealTimePluginFactory::instanceFor(id)) { |
599 | |
600 // don't need to instantiate | |
485 | 601 |
486 const RealTimePluginDescriptor *descriptor = | 602 const RealTimePluginDescriptor *descriptor = |
487 RealTimePluginFactory::instanceFor(id)-> | 603 RealTimePluginFactory::instanceFor(id)-> |
488 getPluginDescriptor(id); | 604 getPluginDescriptor(id); |
489 if (!descriptor) return false; | 605 if (!descriptor) return false; |
501 TransformFactory::setParametersFromPlugin(Transform &transform, | 617 TransformFactory::setParametersFromPlugin(Transform &transform, |
502 Vamp::PluginBase *plugin) | 618 Vamp::PluginBase *plugin) |
503 { | 619 { |
504 Transform::ParameterMap pmap; | 620 Transform::ParameterMap pmap; |
505 | 621 |
622 //!!! record plugin & API version | |
623 | |
624 //!!! check that this is the right plugin! | |
625 | |
506 Vamp::PluginBase::ParameterList parameters = | 626 Vamp::PluginBase::ParameterList parameters = |
507 plugin->getParameterDescriptors(); | 627 plugin->getParameterDescriptors(); |
508 | 628 |
509 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin(); | 629 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin(); |
510 i != parameters.end(); ++i) { | 630 i != parameters.end(); ++i) { |
537 | 657 |
538 transform.setConfiguration(cmap); | 658 transform.setConfiguration(cmap); |
539 } | 659 } |
540 | 660 |
541 void | 661 void |
662 TransformFactory::setPluginParameters(const Transform &transform, | |
663 Vamp::PluginBase *plugin) | |
664 { | |
665 //!!! check plugin & API version (see e.g. PluginXml::setParameters) | |
666 | |
667 //!!! check that this is the right plugin! | |
668 | |
669 RealTimePluginInstance *rtpi = | |
670 dynamic_cast<RealTimePluginInstance *>(plugin); | |
671 | |
672 if (rtpi) { | |
673 const Transform::ConfigurationMap &cmap = transform.getConfiguration(); | |
674 for (Transform::ConfigurationMap::const_iterator i = cmap.begin(); | |
675 i != cmap.end(); ++i) { | |
676 rtpi->configure(i->first.toStdString(), i->second.toStdString()); | |
677 } | |
678 } | |
679 | |
680 if (transform.getProgram() != "") { | |
681 plugin->selectProgram(transform.getProgram().toStdString()); | |
682 } | |
683 | |
684 const Transform::ParameterMap &pmap = transform.getParameters(); | |
685 | |
686 Vamp::PluginBase::ParameterList parameters = | |
687 plugin->getParameterDescriptors(); | |
688 | |
689 for (Vamp::PluginBase::ParameterList::const_iterator i = parameters.begin(); | |
690 i != parameters.end(); ++i) { | |
691 QString key = i->identifier.c_str(); | |
692 Transform::ParameterMap::const_iterator pmi = pmap.find(key); | |
693 if (pmi != pmap.end()) { | |
694 plugin->setParameter(i->identifier, pmi->second); | |
695 } | |
696 } | |
697 } | |
698 | |
699 void | |
542 TransformFactory::makeContextConsistentWithPlugin(Transform &transform, | 700 TransformFactory::makeContextConsistentWithPlugin(Transform &transform, |
543 Vamp::PluginBase *plugin) | 701 Vamp::PluginBase *plugin) |
544 { | 702 { |
545 const Vamp::Plugin *vp = dynamic_cast<const Vamp::Plugin *>(plugin); | 703 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 | 704 |
558 if (!vp) { | 705 if (!vp) { |
559 // time domain input for real-time effects plugin | 706 // time domain input for real-time effects plugin |
560 if (!transform.getBlockSize()) { | 707 if (!transform.getBlockSize()) { |
561 if (!transform.getStepSize()) transform.setStepSize(1024); | 708 if (!transform.getStepSize()) transform.setStepSize(1024); |
584 } | 731 } |
585 } | 732 } |
586 } | 733 } |
587 } | 734 } |
588 | 735 |
589 Transform | 736 QString |
590 TransformFactory::getDefaultTransformFor(TransformId id, size_t rate) | 737 TransformFactory::getPluginConfigurationXml(const Transform &t) |
591 { | 738 { |
592 Transform t; | 739 QString xml; |
593 t.setIdentifier(id); | 740 |
594 | 741 Vamp::PluginBase *plugin = instantiateDefaultPluginFor |
595 if (rate == 0) { | 742 (t.getIdentifier(), 0); |
596 rate = 44100; | 743 if (!plugin) { |
597 } else { | 744 std::cerr << "TransformFactory::getPluginConfigurationXml: " |
598 t.setSampleRate(rate); | 745 << "Unable to instantiate plugin for transform \"" |
599 } | 746 << t.getIdentifier().toStdString() << "\"" << std::endl; |
600 | 747 return xml; |
601 QString pluginId = t.getPluginIdentifier(); | 748 } |
602 | 749 |
603 if (t.getType() == Transform::FeatureExtraction) { | 750 setPluginParameters(t, plugin); |
604 | 751 |
605 FeatureExtractionPluginFactory *factory = | 752 QTextStream out(&xml); |
606 FeatureExtractionPluginFactory::instanceFor(pluginId); | 753 PluginXml(plugin).toXml(out); |
607 | 754 delete plugin; |
608 Vamp::Plugin *plugin = factory->instantiatePlugin | 755 |
609 (pluginId, rate); | 756 return xml; |
610 | 757 } |
611 if (plugin) { | 758 |
612 setParametersFromPlugin(t, plugin); | 759 void |
613 makeContextConsistentWithPlugin(t, plugin); | 760 TransformFactory::setParametersFromPluginConfigurationXml(Transform &t, |
614 delete plugin; | 761 QString xml) |
615 } | 762 { |
616 | 763 Vamp::PluginBase *plugin = instantiateDefaultPluginFor |
617 } else { | 764 (t.getIdentifier(), 0); |
618 | 765 if (!plugin) { |
619 RealTimePluginFactory *factory = | 766 std::cerr << "TransformFactory::setParametersFromPluginConfigurationXml: " |
620 RealTimePluginFactory::instanceFor(pluginId); | 767 << "Unable to instantiate plugin for transform \"" |
621 | 768 << t.getIdentifier().toStdString() << "\"" << std::endl; |
622 RealTimePluginInstance *plugin = factory->instantiatePlugin | 769 return; |
623 (pluginId, 0, 0, rate, 1024, 1); | 770 } |
624 | 771 |
625 if (plugin) { | 772 PluginXml(plugin).setParametersFromXml(xml); |
626 setParametersFromPlugin(t, plugin); | 773 setParametersFromPlugin(t, plugin); |
627 makeContextConsistentWithPlugin(t, plugin); | 774 delete plugin; |
628 delete plugin; | 775 } |
629 } | 776 |
630 } | |
631 | |
632 return t; | |
633 } | |
634 |