Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@202: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "FeatureExtractionPluginFactory.h" Chris@0: #include "PluginIdentifier.h" Chris@0: Chris@150: #include "system/System.h" Chris@66: Chris@1179: #include "PluginScan.h" Chris@1179: Chris@1224: #ifdef _WIN32 Chris@1224: #undef VOID Chris@1224: #undef ERROR Chris@1224: #define CAPNP_LITE 1 Chris@1224: #endif Chris@1210: #include "vamp-client/AutoPlugin.h" Chris@1210: Chris@66: #include Chris@66: #include Chris@66: #include Chris@165: #include Chris@66: Chris@0: #include Chris@0: Chris@408: #include "base/Profiler.h" Chris@408: Chris@1223: #include "vamp-client/ProcessQtTransport.h" Chris@1223: #include "vamp-client/CapnpRRClient.h" Chris@1223: Chris@1164: using namespace std; Chris@1164: Chris@249: //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1 Chris@249: Chris@0: static FeatureExtractionPluginFactory *_nativeInstance = 0; Chris@0: Chris@0: FeatureExtractionPluginFactory * Chris@0: FeatureExtractionPluginFactory::instance(QString pluginType) Chris@0: { Chris@71: if (pluginType == "vamp") { Chris@0: if (!_nativeInstance) { Chris@690: // SVDEBUG << "FeatureExtractionPluginFactory::instance(" << pluginType// << "): creating new FeatureExtractionPluginFactory" << endl; Chris@0: _nativeInstance = new FeatureExtractionPluginFactory(); Chris@0: } Chris@0: return _nativeInstance; Chris@0: } Chris@0: Chris@0: else return 0; Chris@0: } Chris@0: Chris@0: FeatureExtractionPluginFactory * Chris@0: FeatureExtractionPluginFactory::instanceFor(QString identifier) Chris@0: { Chris@0: QString type, soName, label; Chris@0: PluginIdentifier::parseIdentifier(identifier, type, soName, label); Chris@0: return instance(type); Chris@0: } Chris@0: Chris@1209: FeatureExtractionPluginFactory::FeatureExtractionPluginFactory() : Chris@1223: m_serverName("piper-cpp/bin/piper-vamp-server") //!!! Chris@66: { Chris@66: } Chris@66: Chris@1164: vector Chris@0: FeatureExtractionPluginFactory::getAllPluginIdentifiers() Chris@0: { Chris@0: FeatureExtractionPluginFactory *factory; Chris@1164: vector rv; Chris@0: Chris@66: factory = instance("vamp"); Chris@0: if (factory) { Chris@1164: vector tmp = factory->getPluginIdentifiers(); Chris@0: for (size_t i = 0; i < tmp.size(); ++i) { Chris@843: // cerr << "identifier: " << tmp[i] << endl; Chris@0: rv.push_back(tmp[i]); Chris@0: } Chris@0: } Chris@0: Chris@0: // Plugins can change the locale, revert it to default. Chris@608: RestoreStartupLocale(); Chris@608: Chris@0: return rv; Chris@0: } Chris@0: Chris@1164: vector Chris@0: FeatureExtractionPluginFactory::getPluginIdentifiers() Chris@0: { Chris@408: Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers"); Chris@408: Chris@1209: QMutexLocker locker(&m_mutex); Chris@1209: Chris@1209: if (m_pluginData.empty()) { Chris@1209: populate(); Chris@1209: } Chris@1209: Chris@1164: vector rv; Chris@1179: Chris@1209: for (const auto &d: m_pluginData) { Chris@1209: rv.push_back(QString("vamp:") + QString::fromStdString(d.pluginKey)); Chris@66: } Chris@66: Chris@0: return rv; Chris@0: } Chris@0: Chris@66: Vamp::Plugin * Chris@0: FeatureExtractionPluginFactory::instantiatePlugin(QString identifier, Chris@1040: sv_samplerate_t inputSampleRate) Chris@0: { Chris@408: Profiler profiler("FeatureExtractionPluginFactory::instantiatePlugin"); Chris@1210: Chris@66: QString type, soname, label; Chris@66: PluginIdentifier::parseIdentifier(identifier, type, soname, label); Chris@1210: std::string pluginKey = (soname + ":" + label).toStdString(); Chris@0: Chris@1210: auto ap = new piper_vamp::client::AutoPlugin Chris@1214: (m_serverName, pluginKey, float(inputSampleRate), 0); Chris@66: Chris@1210: if (!ap->isOK()) { Chris@1210: delete ap; Chris@1210: return 0; Chris@1210: } else { Chris@1210: return ap; Chris@1210: } Chris@298: } Chris@298: Chris@1223: piper_vamp::PluginStaticData Chris@1223: FeatureExtractionPluginFactory::getPluginStaticData(QString identifier) Chris@1223: { Chris@1223: QString type, soname, label; Chris@1223: PluginIdentifier::parseIdentifier(identifier, type, soname, label); Chris@1223: std::string pluginKey = (soname + ":" + label).toStdString(); Chris@1223: Chris@1223: for (const auto &d: m_pluginData) { Chris@1223: if (d.pluginKey == pluginKey) { Chris@1223: return d; Chris@1223: } Chris@1223: } Chris@1223: return {}; Chris@1223: } Chris@1223: Chris@165: QString Chris@165: FeatureExtractionPluginFactory::getPluginCategory(QString identifier) Chris@165: { Chris@1223: if (m_taxonomy.find(identifier) != m_taxonomy.end()) { Chris@1223: return m_taxonomy[identifier]; Chris@1223: } else { Chris@1223: return {}; Chris@1223: } Chris@165: } Chris@165: Chris@165: void Chris@1209: FeatureExtractionPluginFactory::populate() Chris@165: { Chris@1223: piper_vamp::client::ProcessQtTransport transport(m_serverName); Chris@1223: piper_vamp::client::CapnpRRClient client(&transport); Chris@1223: piper_vamp::ListResponse lr = client.listPluginData(); Chris@1209: m_pluginData = lr.available; Chris@1213: Chris@1213: for (const auto &pd: m_pluginData) { Chris@1213: Chris@1213: QString identifier = Chris@1213: QString("vamp:") + QString::fromStdString(pd.pluginKey); Chris@1213: Chris@1213: QStringList catlist; Chris@1213: for (const auto &cs: pd.category) { Chris@1213: catlist.push_back(QString::fromStdString(cs)); Chris@1213: } Chris@1223: Chris@1213: m_taxonomy[identifier] = catlist.join(" > "); Chris@1213: } Chris@1209: } Chris@165: