annotate vamp-support/LoaderRequests.h @ 178:353f842ed73f

Add a more relaxed comparision of JSON responses if the initial comparision fails. Some optional fields aren't populated from the JSON serialisation code.
author Lucas Thompson <dev@lucas.im>
date Tue, 31 Jan 2017 23:01:49 +0000
parents bf8e3e7dd7de
children 3eb00e5c76c4
rev   line source
c@97 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@97 2
c@97 3 /*
c@97 4 Piper C++
c@97 5
c@97 6 An API for audio analysis and feature extraction plugins.
c@97 7
c@97 8 Centre for Digital Music, Queen Mary, University of London.
c@97 9 Copyright 2006-2016 Chris Cannam and QMUL.
c@97 10
c@97 11 Permission is hereby granted, free of charge, to any person
c@97 12 obtaining a copy of this software and associated documentation
c@97 13 files (the "Software"), to deal in the Software without
c@97 14 restriction, including without limitation the rights to use, copy,
c@97 15 modify, merge, publish, distribute, sublicense, and/or sell copies
c@97 16 of the Software, and to permit persons to whom the Software is
c@97 17 furnished to do so, subject to the following conditions:
c@97 18
c@97 19 The above copyright notice and this permission notice shall be
c@97 20 included in all copies or substantial portions of the Software.
c@97 21
c@97 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@97 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@97 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@97 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
c@97 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@97 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@97 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@97 29
c@97 30 Except as contained in this notice, the names of the Centre for
c@97 31 Digital Music; Queen Mary, University of London; and Chris Cannam
c@97 32 shall not be used in advertising or otherwise to promote the sale,
c@97 33 use or other dealings in this Software without prior written
c@97 34 authorization.
c@97 35 */
c@97 36
c@97 37 #ifndef PIPER_LOADER_REQUESTS_H
c@97 38 #define PIPER_LOADER_REQUESTS_H
c@97 39
c@97 40 #include "PluginStaticData.h"
c@97 41 #include "PluginConfiguration.h"
c@97 42
c@97 43 #include <vamp-hostsdk/PluginLoader.h>
c@97 44
c@97 45 #include <map>
c@97 46 #include <string>
c@134 47 #include <iostream>
c@97 48
c@97 49 namespace piper_vamp {
c@97 50
c@97 51 class LoaderRequests
c@97 52 {
c@97 53 public:
c@97 54 ListResponse
c@127 55 listPluginData(ListRequest req) {
c@97 56
c@97 57 auto loader = Vamp::HostExt::PluginLoader::getInstance();
c@134 58
cannam@150 59 // std::cerr << "listPluginData: about to ask loader to list plugins" << std::endl;
c@127 60
c@127 61 std::vector<std::string> keys;
c@127 62 if (req.from.empty()) {
c@127 63 keys = loader->listPlugins();
c@127 64 } else {
c@127 65 keys = loader->listPluginsIn(req.from);
c@127 66 }
c@127 67
cannam@150 68 // std::cerr << "listPluginData: loader listed " << keys.size() << " plugins" << std::endl;
c@134 69
c@97 70 ListResponse response;
c@97 71 for (std::string key: keys) {
cannam@150 72 // std::cerr << "listPluginData: loading plugin and querying static data: " << key << std::endl;
c@97 73 Vamp::Plugin *p = loader->loadPlugin(key, 44100, 0);
c@97 74 if (!p) continue;
c@97 75 auto category = loader->getPluginCategory(key);
c@97 76 response.available.push_back
c@97 77 (PluginStaticData::fromPlugin(key, category, p));
c@97 78 delete p;
c@97 79 }
c@97 80
c@97 81 return response;
c@97 82 }
c@97 83
c@97 84 LoadResponse
c@97 85 loadPlugin(LoadRequest req) {
c@97 86
c@97 87 auto loader = Vamp::HostExt::PluginLoader::getInstance();
c@97 88
c@97 89 Vamp::Plugin *plugin = loader->loadPlugin(req.pluginKey,
c@97 90 req.inputSampleRate,
c@97 91 req.adapterFlags);
c@97 92
c@97 93 LoadResponse response;
c@97 94 response.plugin = plugin;
c@97 95 if (!plugin) return response;
c@97 96
c@97 97 response.plugin = plugin;
c@97 98 response.staticData = PluginStaticData::fromPlugin
c@97 99 (req.pluginKey,
c@97 100 loader->getPluginCategory(req.pluginKey),
c@97 101 plugin);
c@97 102
c@97 103 int defaultChannels = 0;
c@97 104 if (plugin->getMinChannelCount() == plugin->getMaxChannelCount()) {
c@108 105 defaultChannels = int(plugin->getMinChannelCount());
c@97 106 }
c@97 107
c@97 108 response.defaultConfiguration = PluginConfiguration::fromPlugin
c@97 109 (plugin,
c@97 110 defaultChannels,
c@108 111 int(plugin->getPreferredStepSize()),
c@108 112 int(plugin->getPreferredBlockSize()));
c@97 113
c@97 114 return response;
c@97 115 }
c@97 116
c@97 117 ConfigurationResponse
c@97 118 configurePlugin(ConfigurationRequest req) {
c@97 119
c@97 120 for (PluginConfiguration::ParameterMap::const_iterator i =
c@97 121 req.configuration.parameterValues.begin();
c@97 122 i != req.configuration.parameterValues.end(); ++i) {
c@97 123 req.plugin->setParameter(i->first, i->second);
c@97 124 }
c@97 125
c@97 126 if (req.configuration.currentProgram != "") {
c@97 127 req.plugin->selectProgram(req.configuration.currentProgram);
c@97 128 }
c@97 129
c@97 130 ConfigurationResponse response;
c@97 131
c@97 132 response.plugin = req.plugin;
c@97 133
c@97 134 if (req.plugin->initialise(req.configuration.channelCount,
c@97 135 req.configuration.stepSize,
c@97 136 req.configuration.blockSize)) {
c@97 137 response.outputs = req.plugin->getOutputDescriptors();
c@97 138 }
c@97 139
c@97 140 return response;
c@97 141 }
c@97 142 };
c@97 143
c@97 144 }
c@97 145
c@97 146 #endif