annotate plugin/FeatureExtractionPluginFactory.cpp @ 1211:5a1198083d9a piper

Pull out model creation into the transformer thread run(), so that all communications with the plugin server happen on a single thread. Then make the model accessor wait for them to be created (which still happens right at the start of processing) before returning.
author Chris Cannam
date Mon, 17 Oct 2016 14:18:23 +0100
parents 584b2d7d7cd9
children 8dc7ab20e847
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@475 19 #include <vamp-hostsdk/PluginHostAdapter.h>
Chris@475 20 #include <vamp-hostsdk/PluginWrapper.h>
Chris@66 21
Chris@150 22 #include "system/System.h"
Chris@66 23
Chris@1179 24 #include "PluginScan.h"
Chris@1179 25
Chris@1210 26 #include "vamp-client/AutoPlugin.h"
Chris@1210 27
Chris@66 28 #include <QDir>
Chris@66 29 #include <QFile>
Chris@66 30 #include <QFileInfo>
Chris@165 31 #include <QTextStream>
Chris@66 32
Chris@0 33 #include <iostream>
Chris@0 34
Chris@408 35 #include "base/Profiler.h"
Chris@408 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@1210 66 m_serverName("piper-cpp/bin/piper-vamp-server"),
Chris@1210 67 m_transport(m_serverName),
Chris@1209 68 m_client(&m_transport)
Chris@66 69 {
Chris@66 70 }
Chris@66 71
Chris@1164 72 vector<QString>
Chris@0 73 FeatureExtractionPluginFactory::getAllPluginIdentifiers()
Chris@0 74 {
Chris@0 75 FeatureExtractionPluginFactory *factory;
Chris@1164 76 vector<QString> rv;
Chris@0 77
Chris@66 78 factory = instance("vamp");
Chris@0 79 if (factory) {
Chris@1164 80 vector<QString> tmp = factory->getPluginIdentifiers();
Chris@0 81 for (size_t i = 0; i < tmp.size(); ++i) {
Chris@843 82 // cerr << "identifier: " << tmp[i] << endl;
Chris@0 83 rv.push_back(tmp[i]);
Chris@0 84 }
Chris@0 85 }
Chris@0 86
Chris@0 87 // Plugins can change the locale, revert it to default.
Chris@608 88 RestoreStartupLocale();
Chris@608 89
Chris@0 90 return rv;
Chris@0 91 }
Chris@0 92
Chris@1164 93 vector<QString>
Chris@0 94 FeatureExtractionPluginFactory::getPluginIdentifiers()
Chris@0 95 {
Chris@408 96 Profiler profiler("FeatureExtractionPluginFactory::getPluginIdentifiers");
Chris@408 97
Chris@1209 98 QMutexLocker locker(&m_mutex);
Chris@1209 99
Chris@1209 100 if (m_pluginData.empty()) {
Chris@1209 101 populate();
Chris@1209 102 }
Chris@1209 103
Chris@1164 104 vector<QString> rv;
Chris@1179 105
Chris@1209 106 for (const auto &d: m_pluginData) {
Chris@1209 107 rv.push_back(QString("vamp:") + QString::fromStdString(d.pluginKey));
Chris@66 108 }
Chris@66 109
Chris@0 110 return rv;
Chris@0 111 }
Chris@0 112
Chris@66 113 Vamp::Plugin *
Chris@0 114 FeatureExtractionPluginFactory::instantiatePlugin(QString identifier,
Chris@1040 115 sv_samplerate_t inputSampleRate)
Chris@0 116 {
Chris@408 117 Profiler profiler("FeatureExtractionPluginFactory::instantiatePlugin");
Chris@1210 118
Chris@66 119 QString type, soname, label;
Chris@66 120 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@1210 121 std::string pluginKey = (soname + ":" + label).toStdString();
Chris@0 122
Chris@1210 123 auto ap = new piper_vamp::client::AutoPlugin
Chris@1210 124 (m_serverName, pluginKey, inputSampleRate, 0);
Chris@66 125
Chris@1210 126 if (!ap->isOK()) {
Chris@1210 127 delete ap;
Chris@1210 128 return 0;
Chris@1210 129 } else {
Chris@1210 130 return ap;
Chris@1210 131 }
Chris@298 132 }
Chris@298 133
Chris@165 134 QString
Chris@165 135 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
Chris@165 136 {
Chris@1209 137 //!!! (re)implement
Chris@1209 138 // return m_taxonomy[identifier];
Chris@1209 139 return QString();
Chris@165 140 }
Chris@165 141
Chris@165 142 void
Chris@1209 143 FeatureExtractionPluginFactory::populate()
Chris@165 144 {
Chris@1209 145 piper_vamp::ListResponse lr = m_client.listPluginData();
Chris@1209 146 m_pluginData = lr.available;
Chris@1209 147 }
Chris@165 148