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