changeset 351:7263e37d8913

* Add public TransformFactory::instantiatePluginFor()
author Chris Cannam
date Mon, 10 Dec 2007 15:31:32 +0000
parents d7c41483af8f
children f4f9c039c458
files plugin/transform/TransformFactory.cpp plugin/transform/TransformFactory.h
diffstat 2 files changed, 47 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/plugin/transform/TransformFactory.cpp	Fri Dec 07 16:47:31 2007 +0000
+++ b/plugin/transform/TransformFactory.cpp	Mon Dec 10 15:31:32 2007 +0000
@@ -444,7 +444,7 @@
     t.setIdentifier(id);
     if (rate != 0) t.setSampleRate(rate);
 
-    Vamp::PluginBase *plugin = instantiatePluginFor(id, rate);
+    Vamp::PluginBase *plugin = instantiateDefaultPluginFor(id, rate);
 
     if (plugin) {
         setParametersFromPlugin(t, plugin);
@@ -456,7 +456,18 @@
 }
 
 Vamp::PluginBase *
-TransformFactory::instantiatePluginFor(TransformId identifier, size_t rate)
+TransformFactory::instantiatePluginFor(const Transform &transform)
+{
+    Vamp::PluginBase *plugin = instantiateDefaultPluginFor
+        (transform.getIdentifier(), transform.getSampleRate());
+    if (plugin) {
+        setPluginParameters(transform, plugin);
+    }
+    return plugin;
+}
+
+Vamp::PluginBase *
+TransformFactory::instantiateDefaultPluginFor(TransformId identifier, size_t rate)
 {
     Transform t;
     t.setIdentifier(identifier);
@@ -543,7 +554,7 @@
     }
 
     Vamp::Plugin *plugin =
-        downcastVampPlugin(instantiatePluginFor(identifier, 0));
+        downcastVampPlugin(instantiateDefaultPluginFor(identifier, 0));
 
     if (plugin) {
         Vamp::Plugin::InputDomain d = plugin->getInputDomain();
@@ -724,7 +735,8 @@
 {
     QString xml;
 
-    Vamp::PluginBase *plugin = instantiatePluginFor(t.getIdentifier(), 0);
+    Vamp::PluginBase *plugin = instantiateDefaultPluginFor
+        (t.getIdentifier(), 0);
     if (!plugin) {
         std::cerr << "TransformFactory::getPluginConfigurationXml: "
                   << "Unable to instantiate plugin for transform \""
@@ -732,6 +744,8 @@
         return xml;
     }
 
+    setPluginParameters(t, plugin);
+
     QTextStream out(&xml);
     PluginXml(plugin).toXml(out);
     delete plugin;
@@ -743,7 +757,8 @@
 TransformFactory::setParametersFromPluginConfigurationXml(Transform &t,
                                                           QString xml)
 {
-    Vamp::PluginBase *plugin = instantiatePluginFor(t.getIdentifier(), 0);
+    Vamp::PluginBase *plugin = instantiateDefaultPluginFor
+        (t.getIdentifier(), 0);
     if (!plugin) {
         std::cerr << "TransformFactory::setParametersFromPluginConfigurationXml: "
                   << "Unable to instantiate plugin for transform \""
--- a/plugin/transform/TransformFactory.h	Fri Dec 07 16:47:31 2007 +0000
+++ b/plugin/transform/TransformFactory.h	Mon Dec 10 15:31:32 2007 +0000
@@ -83,6 +83,32 @@
                                   int &minChannels, int &maxChannels);
 
     /**
+     * Load an appropriate plugin for the given transform and set the
+     * parameters, program and configuration strings on that plugin
+     * from the Transform object.
+     *
+     * Note that this requires that the transform has a meaningful
+     * sample rate set, as that is used as the rate for the plugin.  A
+     * Transform can legitimately have rate set at zero (= "use the
+     * rate of the input source"), so the caller will need to test for
+     * this case.
+     *
+     * Returns the plugin thus loaded.  This will be a
+     * Vamp::PluginBase, but not necessarily a Vamp::Plugin (only if
+     * the transform was a feature-extraction type -- call
+     * downcastVampPlugin if you only want Vamp::Plugins).  Returns
+     * NULL if no suitable plugin was available.
+     */
+    Vamp::PluginBase *instantiatePluginFor(const Transform &transform);
+
+    /**
+     * Convert a Vamp::PluginBase to a Vamp::Plugin, if it is one.
+     * Return NULL otherwise.  This ill-fitting convenience function
+     * is really just a dynamic_cast wrapper.
+     */
+    Vamp::Plugin *downcastVampPlugin(Vamp::PluginBase *);
+
+    /**
      * Set the plugin parameters, program and configuration strings on
      * the given Transform object from the given plugin instance.
      * Note that no check is made whether the plugin is actually the
@@ -134,8 +160,7 @@
     void populateFeatureExtractionPlugins(TransformDescriptionMap &);
     void populateRealTimePlugins(TransformDescriptionMap &);
 
-    Vamp::PluginBase *instantiatePluginFor(TransformId id, size_t rate);
-    Vamp::Plugin *downcastVampPlugin(Vamp::PluginBase *);
+    Vamp::PluginBase *instantiateDefaultPluginFor(TransformId id, size_t rate);
 
     static TransformFactory *m_instance;
 };