annotate plugin/FeatureExtractionPluginFactory.cpp @ 1223:c2207877689d piper

Avoid instantiating all plugins (in piper client) on startup, using plugin static data instead. Problem of where to get the units field from is still pending.
author Chris Cannam
date Thu, 20 Oct 2016 14:06:58 +0100
parents 77320e522253
children ab050519c4ba
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@1210 23 #include "vamp-client/AutoPlugin.h"
Chris@1210 24
Chris@66 25 #include <QDir>
Chris@66 26 #include <QFile>
Chris@66 27 #include <QFileInfo>
Chris@165 28 #include <QTextStream>
Chris@66 29
Chris@0 30 #include <iostream>
Chris@0 31
Chris@408 32 #include "base/Profiler.h"
Chris@408 33
Chris@1223 34 #include "vamp-client/ProcessQtTransport.h"
Chris@1223 35 #include "vamp-client/CapnpRRClient.h"
Chris@1223 36
Chris@1164 37 using namespace std;
Chris@1164 38
Chris@249 39 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1
Chris@249 40
Chris@0 41 static FeatureExtractionPluginFactory *_nativeInstance = 0;
Chris@0 42
Chris@0 43 FeatureExtractionPluginFactory *
Chris@0 44 FeatureExtractionPluginFactory::instance(QString pluginType)
Chris@0 45 {
Chris@71 46 if (pluginType == "vamp") {
Chris@0 47 if (!_nativeInstance) {
Chris@690 48 // SVDEBUG << "FeatureExtractionPluginFactory::instance(" << pluginType// << "): creating new FeatureExtractionPluginFactory" << endl;
Chris@0 49 _nativeInstance = new FeatureExtractionPluginFactory();
Chris@0 50 }
Chris@0 51 return _nativeInstance;
Chris@0 52 }
Chris@0 53
Chris@0 54 else return 0;
Chris@0 55 }
Chris@0 56
Chris@0 57 FeatureExtractionPluginFactory *
Chris@0 58 FeatureExtractionPluginFactory::instanceFor(QString identifier)
Chris@0 59 {
Chris@0 60 QString type, soName, label;
Chris@0 61 PluginIdentifier::parseIdentifier(identifier, type, soName, label);
Chris@0 62 return instance(type);
Chris@0 63 }
Chris@0 64
Chris@1209 65 FeatureExtractionPluginFactory::FeatureExtractionPluginFactory() :
Chris@1223 66 m_serverName("piper-cpp/bin/piper-vamp-server") //!!!
Chris@66 67 {
Chris@66 68 }
Chris@66 69
Chris@1164 70 vector<QString>
Chris@0 71 FeatureExtractionPluginFactory::getAllPluginIdentifiers()
Chris@0 72 {
Chris@0 73 FeatureExtractionPluginFactory *factory;
Chris@1164 74 vector<QString> rv;
Chris@0 75
Chris@66 76 factory = instance("vamp");
Chris@0 77 if (factory) {
Chris@1164 78 vector<QString> tmp = factory->getPluginIdentifiers();
Chris@0 79 for (size_t i = 0; i < tmp.size(); ++i) {
Chris@843 80 // cerr << "identifier: " << tmp[i] << endl;
Chris@0 81 rv.push_back(tmp[i]);
Chris@0 82 }
Chris@0 83 }
Chris@0 84
Chris@0 85 // Plugins can change the locale, revert it to default.
Chris@608 86 RestoreStartupLocale();
Chris@608 87
Chris@0 88 return rv;
Chris@0 89 }
Chris@0 90
Chris@1164 91 vector<QString>
Chris@0 92 FeatureExtractionPluginFactory::getPluginIdentifiers()
Chris@0 93 {
Chris@408 94 Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers");
Chris@408 95
Chris@1209 96 QMutexLocker locker(&m_mutex);
Chris@1209 97
Chris@1209 98 if (m_pluginData.empty()) {
Chris@1209 99 populate();
Chris@1209 100 }
Chris@1209 101
Chris@1164 102 vector<QString> rv;
Chris@1179 103
Chris@1209 104 for (const auto &d: m_pluginData) {
Chris@1209 105 rv.push_back(QString("vamp:") + QString::fromStdString(d.pluginKey));
Chris@66 106 }
Chris@66 107
Chris@0 108 return rv;
Chris@0 109 }
Chris@0 110
Chris@66 111 Vamp::Plugin *
Chris@0 112 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
Chris@1040 113 sv_samplerate_t inputSampleRate)
Chris@0 114 {
Chris@408 115 Profiler profiler("FeatureExtractionPluginFactory::instantiatePlugin");
Chris@1210 116
Chris@66 117 QString type, soname, label;
Chris@66 118 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@1210 119 std::string pluginKey = (soname + ":" + label).toStdString();
Chris@0 120
Chris@1210 121 auto ap = new piper_vamp::client::AutoPlugin
Chris@1214 122 (m_serverName, pluginKey, float(inputSampleRate), 0);
Chris@66 123
Chris@1210 124 if (!ap->isOK()) {
Chris@1210 125 delete ap;
Chris@1210 126 return 0;
Chris@1210 127 } else {
Chris@1210 128 return ap;
Chris@1210 129 }
Chris@298 130 }
Chris@298 131
Chris@1223 132 piper_vamp::PluginStaticData
Chris@1223 133 FeatureExtractionPluginFactory::getPluginStaticData(QString identifier)
Chris@1223 134 {
Chris@1223 135 QString type, soname, label;
Chris@1223 136 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@1223 137 std::string pluginKey = (soname + ":" + label).toStdString();
Chris@1223 138
Chris@1223 139 for (const auto &d: m_pluginData) {
Chris@1223 140 if (d.pluginKey == pluginKey) {
Chris@1223 141 return d;
Chris@1223 142 }
Chris@1223 143 }
Chris@1223 144 return {};
Chris@1223 145 }
Chris@1223 146
Chris@165 147 QString
Chris@165 148 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
Chris@165 149 {
Chris@1223 150 if (m_taxonomy.find(identifier) != m_taxonomy.end()) {
Chris@1223 151 return m_taxonomy[identifier];
Chris@1223 152 } else {
Chris@1223 153 return {};
Chris@1223 154 }
Chris@165 155 }
Chris@165 156
Chris@165 157 void
Chris@1209 158 FeatureExtractionPluginFactory::populate()
Chris@165 159 {
Chris@1223 160 piper_vamp::client::ProcessQtTransport transport(m_serverName);
Chris@1223 161 piper_vamp::client::CapnpRRClient client(&transport);
Chris@1223 162 piper_vamp::ListResponse lr = client.listPluginData();
Chris@1209 163 m_pluginData = lr.available;
Chris@1213 164
Chris@1213 165 for (const auto &pd: m_pluginData) {
Chris@1213 166
Chris@1213 167 QString identifier =
Chris@1213 168 QString("vamp:") + QString::fromStdString(pd.pluginKey);
Chris@1213 169
Chris@1213 170 QStringList catlist;
Chris@1213 171 for (const auto &cs: pd.category) {
Chris@1213 172 catlist.push_back(QString::fromStdString(cs));
Chris@1213 173 }
Chris@1223 174
Chris@1213 175 m_taxonomy[identifier] = catlist.join(" > ");
Chris@1213 176 }
Chris@1209 177 }
Chris@165 178