changeset 287:09753ad777db

Add support for ProgramParameters. In theory this means we can select a program and then re-query the plugin's parameters and get the right values back, just as would happen if we were working with the remote extractor directly as a local Vamp plugin.
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 07 Apr 2020 15:56:02 +0100
parents b3b2e092bd61
children c9f63ca1914d
files test/vamp-client/tst_PluginStub.cpp vamp-client/PiperVampPlugin.h vamp-support/LoaderRequests.h vamp-support/PluginProgramParameters.h vamp-support/PluginStaticData.h vamp-support/RequestResponse.h
diffstat 6 files changed, 128 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/test/vamp-client/tst_PluginStub.cpp	Tue Jan 07 11:20:47 2020 +0000
+++ b/test/vamp-client/tst_PluginStub.cpp	Tue Apr 07 15:56:02 2020 +0100
@@ -91,6 +91,9 @@
     staticData.inputDomain = Vamp::Plugin::InputDomain::TimeDomain;
     staticData.basicOutputInfo = {{"output", "NA", "Not real"}};
 
+    PluginProgramParameters programParameters;
+    // none
+    
     StubClient stub {staticData};
     
     PiperVampPlugin vampPiperAdapter {
@@ -99,7 +102,8 @@
         44100.0, // sample rate
         0, // adapter flags, don't care here
         staticData, 
-        defaultConfig 
+        defaultConfig,
+        programParameters
     };
     
     const auto initWithPreferredFraming = [&]() -> bool {
--- a/vamp-client/PiperVampPlugin.h	Tue Jan 07 11:20:47 2020 +0000
+++ b/vamp-client/PiperVampPlugin.h	Tue Apr 07 15:56:02 2020 +0100
@@ -41,6 +41,7 @@
 
 #include "vamp-support/PluginStaticData.h"
 #include "vamp-support/PluginConfiguration.h"
+#include "vamp-support/PluginProgramParameters.h"
 
 #include "PluginClient.h"
 
@@ -107,7 +108,8 @@
                     float inputSampleRate,
                     int adapterFlags,
                     PluginStaticData psd,
-                    PluginConfiguration defaultConfig) :
+                    PluginConfiguration defaultConfig,
+                    PluginProgramParameters programParameters) :
         Plugin(inputSampleRate),
         m_client(client),
         m_key(pluginKey),
@@ -115,7 +117,8 @@
         m_state(Loaded),
         m_psd(psd),
         m_defaultConfig(defaultConfig),
-        m_config(defaultConfig)
+        m_config(defaultConfig),
+        m_programParameters(programParameters)
     { }
 
     virtual ~PiperVampPlugin() {
@@ -193,11 +196,18 @@
             throw std::logic_error("Can't select program after plugin initialised");
         }
         m_config.currentProgram = program;
+
+        const auto &pp = m_programParameters.programParameters;
+        if (pp.find(program) != pp.end()) {
+            for (auto param: pp.at(program)) {
+                m_config.parameterValues[param.first] = param.second;
+            }
+        }
     }
 
     bool initialise(size_t inputChannels,
-                            size_t stepSize,
-                            size_t blockSize) override {
+                    size_t stepSize,
+                    size_t blockSize) override {
 
         if (m_state == Failed) {
             throw std::logic_error("Plugin is in failed state");
@@ -412,6 +422,7 @@
     OutputList m_outputs;
     PluginConfiguration m_defaultConfig;
     PluginConfiguration m_config;
+    PluginProgramParameters m_programParameters;
 };
 
 }
--- a/vamp-support/LoaderRequests.h	Tue Jan 07 11:20:47 2020 +0000
+++ b/vamp-support/LoaderRequests.h	Tue Apr 07 15:56:02 2020 +0100
@@ -39,7 +39,9 @@
 
 #include "PluginStaticData.h"
 #include "PluginConfiguration.h"
+#include "PluginProgramParameters.h"
 #include "StaticOutputRdf.h"
+#include "RequestResponse.h"
 
 #include <vamp-hostsdk/PluginLoader.h>
 
@@ -116,7 +118,10 @@
 	     defaultChannels,
 	     int(plugin->getPreferredStepSize()),
 	     int(plugin->getPreferredBlockSize()));
-	
+
+        response.programParameters = PluginProgramParameters::fromPlugin
+            (plugin, response.defaultConfiguration);
+        
 	return response;
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vamp-support/PluginProgramParameters.h	Tue Apr 07 15:56:02 2020 +0100
@@ -0,0 +1,93 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Piper C++
+
+    An API for audio analysis and feature extraction plugins.
+
+    Centre for Digital Music, Queen Mary, University of London.
+    Copyright 2006-2019 Chris Cannam and QMUL.
+  
+    Permission is hereby granted, free of charge, to any person
+    obtaining a copy of this software and associated documentation
+    files (the "Software"), to deal in the Software without
+    restriction, including without limitation the rights to use, copy,
+    modify, merge, publish, distribute, sublicense, and/or sell copies
+    of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be
+    included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
+    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+    Except as contained in this notice, the names of the Centre for
+    Digital Music; Queen Mary, University of London; and Chris Cannam
+    shall not be used in advertising or otherwise to promote the sale,
+    use or other dealings in this Software without prior written
+    authorization.
+*/
+
+#ifndef PIPER_PLUGIN_PROGRAM_PARAMETERS_H
+#define PIPER_PLUGIN_PROGRAM_PARAMETERS_H
+
+#include <vamp-hostsdk/Plugin.h>
+
+#include <map>
+#include <string>
+
+namespace piper_vamp {
+
+/**
+ * \class PluginProgramParameters
+ * 
+ * PluginProgramParameters is a structure mapping from program names
+ * to the parameter settings associated with those programs.
+ */
+struct PluginProgramParameters
+{
+    std::map<std::string, std::map<std::string, float>> programParameters;
+
+    /**
+     * Extract the program parameters from the given plugin (without
+     * retaining any persistent reference to the plugin itself).
+     */
+    static PluginProgramParameters
+    fromPlugin(Vamp::Plugin *p, const PluginConfiguration &defaultConfiguration) {
+
+        auto programs = p->getPrograms();
+        if (programs.empty()) return {};
+
+        PluginProgramParameters pp;
+
+        for (auto program: programs) {
+
+            p->selectProgram(program);
+
+            for (auto param: defaultConfiguration.parameterValues) {
+                auto id = param.first;
+                pp.programParameters[program][id] = p->getParameter(id);
+            }
+        }
+
+        if (defaultConfiguration.currentProgram != "") {
+            p->selectProgram(defaultConfiguration.currentProgram);
+        }
+
+        for (auto param: defaultConfiguration.parameterValues) {
+            p->setParameter(param.first, param.second);
+        }
+
+        return pp;
+    }
+};
+
+}
+
+#endif
--- a/vamp-support/PluginStaticData.h	Tue Jan 07 11:20:47 2020 +0000
+++ b/vamp-support/PluginStaticData.h	Tue Apr 07 15:56:02 2020 +0100
@@ -86,7 +86,7 @@
     StaticOutputInfo staticOutputInfo; // not found in the plugin, may
                                        // come from accompanying
                                        // (RDF?) metadata
-
+    
     static PluginStaticData
     fromPlugin(std::string pluginKey,
 	       std::vector<std::string> category,
--- a/vamp-support/RequestResponse.h	Tue Jan 07 11:20:47 2020 +0000
+++ b/vamp-support/RequestResponse.h	Tue Apr 07 15:56:02 2020 +0100
@@ -39,6 +39,7 @@
 
 #include "PluginStaticData.h"
 #include "PluginConfiguration.h"
+#include "PluginProgramParameters.h"
 
 #include <map>
 #include <string>
@@ -161,6 +162,13 @@
      * only valid if plugin is non-0.
      */
     PluginConfiguration defaultConfiguration;
+
+    /**
+     * The parameter values associated with any program settings
+     * available for the plugin. The contents of this structure are
+     * only valid if plugin is non-0.
+     */
+    PluginProgramParameters programParameters;
 };
 
 /**