annotate plugin/FeatureExtractionPluginFactory.cpp @ 1222:771a17925576 piper

Merge from branch 3.0-integration
author Chris Cannam
date Thu, 20 Oct 2016 11:19:34 +0100
parents 77320e522253
children c2207877689d
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@1164 34 using namespace std;
Chris@1164 35
Chris@249 36 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1
Chris@249 37
Chris@0 38 static FeatureExtractionPluginFactory *_nativeInstance = 0;
Chris@0 39
Chris@0 40 FeatureExtractionPluginFactory *
Chris@0 41 FeatureExtractionPluginFactory::instance(QString pluginType)
Chris@0 42 {
Chris@71 43 if (pluginType == "vamp") {
Chris@0 44 if (!_nativeInstance) {
Chris@690 45 // SVDEBUG << "FeatureExtractionPluginFactory::instance(" << pluginType// << "): creating new FeatureExtractionPluginFactory" << endl;
Chris@0 46 _nativeInstance = new FeatureExtractionPluginFactory();
Chris@0 47 }
Chris@0 48 return _nativeInstance;
Chris@0 49 }
Chris@0 50
Chris@0 51 else return 0;
Chris@0 52 }
Chris@0 53
Chris@0 54 FeatureExtractionPluginFactory *
Chris@0 55 FeatureExtractionPluginFactory::instanceFor(QString identifier)
Chris@0 56 {
Chris@0 57 QString type, soName, label;
Chris@0 58 PluginIdentifier::parseIdentifier(identifier, type, soName, label);
Chris@0 59 return instance(type);
Chris@0 60 }
Chris@0 61
Chris@1209 62 FeatureExtractionPluginFactory::FeatureExtractionPluginFactory() :
Chris@1210 63 m_serverName("piper-cpp/bin/piper-vamp-server"),
Chris@1210 64 m_transport(m_serverName),
Chris@1209 65 m_client(&m_transport)
Chris@66 66 {
Chris@66 67 }
Chris@66 68
Chris@1164 69 vector<QString>
Chris@0 70 FeatureExtractionPluginFactory::getAllPluginIdentifiers()
Chris@0 71 {
Chris@0 72 FeatureExtractionPluginFactory *factory;
Chris@1164 73 vector<QString> rv;
Chris@0 74
Chris@66 75 factory = instance("vamp");
Chris@0 76 if (factory) {
Chris@1164 77 vector<QString> tmp = factory->getPluginIdentifiers();
Chris@0 78 for (size_t i = 0; i < tmp.size(); ++i) {
Chris@843 79 // cerr << "identifier: " << tmp[i] << endl;
Chris@0 80 rv.push_back(tmp[i]);
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 // Plugins can change the locale, revert it to default.
Chris@608 85 RestoreStartupLocale();
Chris@608 86
Chris@0 87 return rv;
Chris@0 88 }
Chris@0 89
Chris@1164 90 vector<QString>
Chris@0 91 FeatureExtractionPluginFactory::getPluginIdentifiers()
Chris@0 92 {
Chris@408 93 Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers");
Chris@408 94
Chris@1209 95 QMutexLocker locker(&m_mutex);
Chris@1209 96
Chris@1209 97 if (m_pluginData.empty()) {
Chris@1209 98 populate();
Chris@1209 99 }
Chris@1209 100
Chris@1164 101 vector<QString> rv;
Chris@1179 102
Chris@1209 103 for (const auto &d: m_pluginData) {
Chris@1209 104 rv.push_back(QString("vamp:") + QString::fromStdString(d.pluginKey));
Chris@66 105 }
Chris@66 106
Chris@0 107 return rv;
Chris@0 108 }
Chris@0 109
Chris@66 110 Vamp::Plugin *
Chris@0 111 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
Chris@1040 112 sv_samplerate_t inputSampleRate)
Chris@0 113 {
Chris@408 114 Profiler profiler("FeatureExtractionPluginFactory::instantiatePlugin");
Chris@1210 115
Chris@66 116 QString type, soname, label;
Chris@66 117 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@1210 118 std::string pluginKey = (soname + ":" + label).toStdString();
Chris@0 119
Chris@1210 120 auto ap = new piper_vamp::client::AutoPlugin
Chris@1214 121 (m_serverName, pluginKey, float(inputSampleRate), 0);
Chris@66 122
Chris@1210 123 if (!ap->isOK()) {
Chris@1210 124 delete ap;
Chris@1210 125 return 0;
Chris@1210 126 } else {
Chris@1210 127 return ap;
Chris@1210 128 }
Chris@298 129 }
Chris@298 130
Chris@165 131 QString
Chris@165 132 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
Chris@165 133 {
Chris@1213 134 return m_taxonomy[identifier];
Chris@165 135 }
Chris@165 136
Chris@165 137 void
Chris@1209 138 FeatureExtractionPluginFactory::populate()
Chris@165 139 {
Chris@1209 140 piper_vamp::ListResponse lr = m_client.listPluginData();
Chris@1209 141 m_pluginData = lr.available;
Chris@1213 142
Chris@1213 143 for (const auto &pd: m_pluginData) {
Chris@1213 144
Chris@1213 145 QString identifier =
Chris@1213 146 QString("vamp:") + QString::fromStdString(pd.pluginKey);
Chris@1213 147
Chris@1213 148 QStringList catlist;
Chris@1213 149 for (const auto &cs: pd.category) {
Chris@1213 150 catlist.push_back(QString::fromStdString(cs));
Chris@1213 151 }
Chris@1213 152 m_taxonomy[identifier] = catlist.join(" > ");
Chris@1213 153 }
Chris@1209 154 }
Chris@165 155