annotate plugin/FeatureExtractionPluginFactory.cpp @ 1224:ab050519c4ba piper

Fix capnp include scenario on Windows
author Chris Cannam
date Thu, 20 Oct 2016 18:31:02 +0100
parents c2207877689d
children 9ae2ce9190e6
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@52 9 This program is free software; you can redistribute it and/or
Chris@52 10 modify it under the terms of the GNU General Public License as
Chris@52 11 published by the Free Software Foundation; either version 2 of the
Chris@52 12 License, or (at your option) any later version. See the file
Chris@52 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "FeatureExtractionPluginFactory.h"
Chris@0 17 #include "PluginIdentifier.h"
Chris@0 18
Chris@150 19 #include "system/System.h"
Chris@66 20
Chris@1179 21 #include "PluginScan.h"
Chris@1179 22
Chris@1224 23 #ifdef _WIN32
Chris@1224 24 #undef VOID
Chris@1224 25 #undef ERROR
Chris@1224 26 #define CAPNP_LITE 1
Chris@1224 27 #endif
Chris@1210 28 #include "vamp-client/AutoPlugin.h"
Chris@1210 29
Chris@66 30 #include <QDir>
Chris@66 31 #include <QFile>
Chris@66 32 #include <QFileInfo>
Chris@165 33 #include <QTextStream>
Chris@66 34
Chris@0 35 #include <iostream>
Chris@0 36
Chris@408 37 #include "base/Profiler.h"
Chris@408 38
Chris@1223 39 #include "vamp-client/ProcessQtTransport.h"
Chris@1223 40 #include "vamp-client/CapnpRRClient.h"
Chris@1223 41
Chris@1164 42 using namespace std;
Chris@1164 43
Chris@249 44 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1
Chris@249 45
Chris@0 46 static FeatureExtractionPluginFactory *_nativeInstance = 0;
Chris@0 47
Chris@0 48 FeatureExtractionPluginFactory *
Chris@0 49 FeatureExtractionPluginFactory::instance(QString pluginType)
Chris@0 50 {
Chris@71 51 if (pluginType == "vamp") {
Chris@0 52 if (!_nativeInstance) {
Chris@690 53 // SVDEBUG << "FeatureExtractionPluginFactory::instance(" << pluginType// << "): creating new FeatureExtractionPluginFactory" << endl;
Chris@0 54 _nativeInstance = new FeatureExtractionPluginFactory();
Chris@0 55 }
Chris@0 56 return _nativeInstance;
Chris@0 57 }
Chris@0 58
Chris@0 59 else return 0;
Chris@0 60 }
Chris@0 61
Chris@0 62 FeatureExtractionPluginFactory *
Chris@0 63 FeatureExtractionPluginFactory::instanceFor(QString identifier)
Chris@0 64 {
Chris@0 65 QString type, soName, label;
Chris@0 66 PluginIdentifier::parseIdentifier(identifier, type, soName, label);
Chris@0 67 return instance(type);
Chris@0 68 }
Chris@0 69
Chris@1209 70 FeatureExtractionPluginFactory::FeatureExtractionPluginFactory() :
Chris@1223 71 m_serverName("piper-cpp/bin/piper-vamp-server") //!!!
Chris@66 72 {
Chris@66 73 }
Chris@66 74
Chris@1164 75 vector<QString>
Chris@0 76 FeatureExtractionPluginFactory::getAllPluginIdentifiers()
Chris@0 77 {
Chris@0 78 FeatureExtractionPluginFactory *factory;
Chris@1164 79 vector<QString> rv;
Chris@0 80
Chris@66 81 factory = instance("vamp");
Chris@0 82 if (factory) {
Chris@1164 83 vector<QString> tmp = factory->getPluginIdentifiers();
Chris@0 84 for (size_t i = 0; i < tmp.size(); ++i) {
Chris@843 85 // cerr << "identifier: " << tmp[i] << endl;
Chris@0 86 rv.push_back(tmp[i]);
Chris@0 87 }
Chris@0 88 }
Chris@0 89
Chris@0 90 // Plugins can change the locale, revert it to default.
Chris@608 91 RestoreStartupLocale();
Chris@608 92
Chris@0 93 return rv;
Chris@0 94 }
Chris@0 95
Chris@1164 96 vector<QString>
Chris@0 97 FeatureExtractionPluginFactory::getPluginIdentifiers()
Chris@0 98 {
Chris@408 99 Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers");
Chris@408 100
Chris@1209 101 QMutexLocker locker(&m_mutex);
Chris@1209 102
Chris@1209 103 if (m_pluginData.empty()) {
Chris@1209 104 populate();
Chris@1209 105 }
Chris@1209 106
Chris@1164 107 vector<QString> rv;
Chris@1179 108
Chris@1209 109 for (const auto &d: m_pluginData) {
Chris@1209 110 rv.push_back(QString("vamp:") + QString::fromStdString(d.pluginKey));
Chris@66 111 }
Chris@66 112
Chris@0 113 return rv;
Chris@0 114 }
Chris@0 115
Chris@66 116 Vamp::Plugin *
Chris@0 117 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
Chris@1040 118 sv_samplerate_t inputSampleRate)
Chris@0 119 {
Chris@408 120 Profiler profiler("FeatureExtractionPluginFactory::instantiatePlugin");
Chris@1210 121
Chris@66 122 QString type, soname, label;
Chris@66 123 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@1210 124 std::string pluginKey = (soname + ":" + label).toStdString();
Chris@0 125
Chris@1210 126 auto ap = new piper_vamp::client::AutoPlugin
Chris@1214 127 (m_serverName, pluginKey, float(inputSampleRate), 0);
Chris@66 128
Chris@1210 129 if (!ap->isOK()) {
Chris@1210 130 delete ap;
Chris@1210 131 return 0;
Chris@1210 132 } else {
Chris@1210 133 return ap;
Chris@1210 134 }
Chris@298 135 }
Chris@298 136
Chris@1223 137 piper_vamp::PluginStaticData
Chris@1223 138 FeatureExtractionPluginFactory::getPluginStaticData(QString identifier)
Chris@1223 139 {
Chris@1223 140 QString type, soname, label;
Chris@1223 141 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@1223 142 std::string pluginKey = (soname + ":" + label).toStdString();
Chris@1223 143
Chris@1223 144 for (const auto &d: m_pluginData) {
Chris@1223 145 if (d.pluginKey == pluginKey) {
Chris@1223 146 return d;
Chris@1223 147 }
Chris@1223 148 }
Chris@1223 149 return {};
Chris@1223 150 }
Chris@1223 151
Chris@165 152 QString
Chris@165 153 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
Chris@165 154 {
Chris@1223 155 if (m_taxonomy.find(identifier) != m_taxonomy.end()) {
Chris@1223 156 return m_taxonomy[identifier];
Chris@1223 157 } else {
Chris@1223 158 return {};
Chris@1223 159 }
Chris@165 160 }
Chris@165 161
Chris@165 162 void
Chris@1209 163 FeatureExtractionPluginFactory::populate()
Chris@165 164 {
Chris@1223 165 piper_vamp::client::ProcessQtTransport transport(m_serverName);
Chris@1223 166 piper_vamp::client::CapnpRRClient client(&transport);
Chris@1223 167 piper_vamp::ListResponse lr = client.listPluginData();
Chris@1209 168 m_pluginData = lr.available;
Chris@1213 169
Chris@1213 170 for (const auto &pd: m_pluginData) {
Chris@1213 171
Chris@1213 172 QString identifier =
Chris@1213 173 QString("vamp:") + QString::fromStdString(pd.pluginKey);
Chris@1213 174
Chris@1213 175 QStringList catlist;
Chris@1213 176 for (const auto &cs: pd.category) {
Chris@1213 177 catlist.push_back(QString::fromStdString(cs));
Chris@1213 178 }
Chris@1223 179
Chris@1213 180 m_taxonomy[identifier] = catlist.join(" > ");
Chris@1213 181 }
Chris@1209 182 }
Chris@165 183