Mercurial > hg > svcore
comparison plugin/PiperVampPluginFactory.cpp @ 1230:9ae2ce9190e6 project-file-rework
Merge
author | Chris Cannam |
---|---|
date | Tue, 25 Oct 2016 11:04:26 +0100 |
parents | 5d886b7b4029 |
children | fac1666e429b |
comparison
equal
deleted
inserted
replaced
1228:a2091d148d7f | 1230:9ae2ce9190e6 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2006 Chris Cannam and QMUL. | |
8 | |
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 | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "PiperVampPluginFactory.h" | |
17 #include "PluginIdentifier.h" | |
18 | |
19 #include "system/System.h" | |
20 | |
21 #include "PluginScan.h" | |
22 | |
23 #ifdef _WIN32 | |
24 #undef VOID | |
25 #undef ERROR | |
26 #define CAPNP_LITE 1 | |
27 #endif | |
28 | |
29 #include "vamp-client/AutoPlugin.h" | |
30 | |
31 #include <QDir> | |
32 #include <QFile> | |
33 #include <QFileInfo> | |
34 #include <QTextStream> | |
35 #include <QCoreApplication> | |
36 | |
37 #include <iostream> | |
38 | |
39 #include "base/Profiler.h" | |
40 | |
41 #include "vamp-client/ProcessQtTransport.h" | |
42 #include "vamp-client/CapnpRRClient.h" | |
43 | |
44 using namespace std; | |
45 | |
46 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1 | |
47 | |
48 PiperVampPluginFactory::PiperVampPluginFactory() : | |
49 // No server unless we find one - don't run arbitrary stuff from the path: | |
50 m_serverName() | |
51 { | |
52 // Server must exist either in the same directory as this one or | |
53 // (preferably) a subdirectory called "piper-bin". | |
54 //!!! todo: merge this with plugin scan checker thingy used in main.cpp? | |
55 QString myDir = QCoreApplication::applicationDirPath(); | |
56 QString name = "piper-vamp-server"; | |
57 QString path = myDir + "/piper-bin/" + name; | |
58 QString suffix = ""; | |
59 #ifdef _WIN32 | |
60 suffix = ".exe"; | |
61 #endif | |
62 if (!QFile(path + suffix).exists()) { | |
63 cerr << "NOTE: Piper Vamp server not found at " << (path + suffix) | |
64 << ", trying in my own directory" << endl; | |
65 path = myDir + "/" + name; | |
66 } | |
67 if (!QFile(path + suffix).exists()) { | |
68 cerr << "NOTE: Piper Vamp server not found at " << (path + suffix) | |
69 << endl; | |
70 } else { | |
71 m_serverName = (path + suffix).toStdString(); | |
72 } | |
73 } | |
74 | |
75 vector<QString> | |
76 PiperVampPluginFactory::getPluginIdentifiers(QString &errorMessage) | |
77 { | |
78 Profiler profiler("PiperVampPluginFactory::getPluginIdentifiers"); | |
79 | |
80 QMutexLocker locker(&m_mutex); | |
81 | |
82 if (m_serverName == "") { | |
83 errorMessage = QObject::tr("External plugin host executable does not appear to be installed"); | |
84 return {}; | |
85 } | |
86 | |
87 if (m_pluginData.empty()) { | |
88 populate(errorMessage); | |
89 } | |
90 | |
91 vector<QString> rv; | |
92 | |
93 for (const auto &d: m_pluginData) { | |
94 rv.push_back(QString("vamp:") + QString::fromStdString(d.second.pluginKey)); | |
95 } | |
96 | |
97 return rv; | |
98 } | |
99 | |
100 Vamp::Plugin * | |
101 PiperVampPluginFactory::instantiatePlugin(QString identifier, | |
102 sv_samplerate_t inputSampleRate) | |
103 { | |
104 Profiler profiler("PiperVampPluginFactory::instantiatePlugin"); | |
105 | |
106 auto psd = getPluginStaticData(identifier); | |
107 if (psd.pluginKey == "") { | |
108 return 0; | |
109 } | |
110 | |
111 auto ap = new piper_vamp::client::AutoPlugin | |
112 (m_serverName, psd.pluginKey, float(inputSampleRate), 0); | |
113 if (!ap->isOK()) { | |
114 delete ap; | |
115 return 0; | |
116 } | |
117 | |
118 return ap; | |
119 } | |
120 | |
121 piper_vamp::PluginStaticData | |
122 PiperVampPluginFactory::getPluginStaticData(QString identifier) | |
123 { | |
124 if (m_pluginData.find(identifier) != m_pluginData.end()) { | |
125 return m_pluginData[identifier]; | |
126 } else { | |
127 return {}; | |
128 } | |
129 } | |
130 | |
131 QString | |
132 PiperVampPluginFactory::getPluginCategory(QString identifier) | |
133 { | |
134 if (m_taxonomy.find(identifier) != m_taxonomy.end()) { | |
135 return m_taxonomy[identifier]; | |
136 } else { | |
137 return {}; | |
138 } | |
139 } | |
140 | |
141 void | |
142 PiperVampPluginFactory::populate(QString &errorMessage) | |
143 { | |
144 if (m_serverName == "") return; | |
145 | |
146 piper_vamp::client::ProcessQtTransport transport(m_serverName); | |
147 if (!transport.isOK()) { | |
148 errorMessage = QObject::tr("Could not start external plugin host"); | |
149 return; | |
150 } | |
151 | |
152 piper_vamp::client::CapnpRRClient client(&transport); | |
153 piper_vamp::ListResponse lr = client.listPluginData(); | |
154 | |
155 for (const auto &pd: lr.available) { | |
156 | |
157 QString identifier = | |
158 QString("vamp:") + QString::fromStdString(pd.pluginKey); | |
159 | |
160 m_pluginData[identifier] = pd; | |
161 | |
162 QStringList catlist; | |
163 for (const auto &cs: pd.category) { | |
164 catlist.push_back(QString::fromStdString(cs)); | |
165 } | |
166 | |
167 m_taxonomy[identifier] = catlist.join(" > "); | |
168 } | |
169 } | |
170 |