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@0
|
7
|
Chris@52
|
8 This program is free software; you can redistribute it and/or
|
Chris@52
|
9 modify it under the terms of the GNU General Public License as
|
Chris@52
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@52
|
11 License, or (at your option) any later version. See the file
|
Chris@52
|
12 COPYING included with this distribution for more information.
|
Chris@0
|
13 */
|
Chris@0
|
14
|
Chris@0
|
15 /*
|
Chris@0
|
16 This is a modified version of a source file from the
|
Chris@0
|
17 Rosegarden MIDI and audio sequencer and notation editor.
|
Chris@17
|
18 This file copyright 2000-2006 Chris Cannam and Richard Bown.
|
Chris@0
|
19 */
|
Chris@0
|
20
|
Chris@0
|
21 #include "LADSPAPluginFactory.h"
|
Chris@0
|
22 #include <iostream>
|
Chris@0
|
23
|
Chris@0
|
24 #include <QDir>
|
Chris@0
|
25 #include <QFile>
|
Chris@0
|
26 #include <QTextStream>
|
Chris@0
|
27
|
Chris@0
|
28 #include <cmath>
|
Chris@0
|
29
|
Chris@0
|
30 #include "LADSPAPluginInstance.h"
|
Chris@0
|
31 #include "PluginIdentifier.h"
|
Chris@0
|
32
|
Chris@150
|
33 #include "system/System.h"
|
Chris@145
|
34 #include "base/Preferences.h"
|
Chris@0
|
35
|
Chris@35
|
36 #ifdef HAVE_LRDF
|
Chris@0
|
37 #include "lrdf.h"
|
Chris@35
|
38 #endif // HAVE_LRDF
|
Chris@0
|
39
|
Chris@0
|
40
|
Chris@0
|
41 LADSPAPluginFactory::LADSPAPluginFactory()
|
Chris@0
|
42 {
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 LADSPAPluginFactory::~LADSPAPluginFactory()
|
Chris@0
|
46 {
|
Chris@0
|
47 for (std::set<RealTimePluginInstance *>::iterator i = m_instances.begin();
|
Chris@0
|
48 i != m_instances.end(); ++i) {
|
Chris@0
|
49 (*i)->setFactory(0);
|
Chris@0
|
50 delete *i;
|
Chris@0
|
51 }
|
Chris@0
|
52 m_instances.clear();
|
Chris@0
|
53 unloadUnusedLibraries();
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 const std::vector<QString> &
|
Chris@0
|
57 LADSPAPluginFactory::getPluginIdentifiers() const
|
Chris@0
|
58 {
|
Chris@0
|
59 return m_identifiers;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 void
|
Chris@0
|
63 LADSPAPluginFactory::enumeratePlugins(std::vector<QString> &list)
|
Chris@0
|
64 {
|
Chris@0
|
65 for (std::vector<QString>::iterator i = m_identifiers.begin();
|
Chris@0
|
66 i != m_identifiers.end(); ++i) {
|
Chris@0
|
67
|
Chris@0
|
68 const LADSPA_Descriptor *descriptor = getLADSPADescriptor(*i);
|
Chris@0
|
69
|
Chris@0
|
70 if (!descriptor) {
|
Chris@0
|
71 std::cerr << "WARNING: LADSPAPluginFactory::enumeratePlugins: couldn't get descriptor for identifier " << i->toStdString() << std::endl;
|
Chris@0
|
72 continue;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 list.push_back(*i);
|
Chris@0
|
76 list.push_back(descriptor->Name);
|
Chris@0
|
77 list.push_back(QString("%1").arg(descriptor->UniqueID));
|
Chris@0
|
78 list.push_back(descriptor->Label);
|
Chris@0
|
79 list.push_back(descriptor->Maker);
|
Chris@0
|
80 list.push_back(descriptor->Copyright);
|
Chris@0
|
81 list.push_back("false"); // is synth
|
Chris@0
|
82 list.push_back("false"); // is grouped
|
Chris@0
|
83
|
Chris@0
|
84 if (m_taxonomy.find(descriptor->UniqueID) != m_taxonomy.end() &&
|
Chris@0
|
85 m_taxonomy[descriptor->UniqueID] != "") {
|
Chris@0
|
86 // std::cerr << "LADSPAPluginFactory: cat for " << i->toStdString()<< " found in taxonomy as " << m_taxonomy[descriptor->UniqueID] << std::endl;
|
Chris@0
|
87 list.push_back(m_taxonomy[descriptor->UniqueID]);
|
Chris@0
|
88
|
Chris@0
|
89 } else if (m_fallbackCategories.find(*i) !=
|
Chris@0
|
90 m_fallbackCategories.end()) {
|
Chris@0
|
91 list.push_back(m_fallbackCategories[*i]);
|
Chris@0
|
92 // std::cerr << "LADSPAPluginFactory: cat for " << i->toStdString() <<" found in fallbacks as " << m_fallbackCategories[*i] << std::endl;
|
Chris@0
|
93
|
Chris@0
|
94 } else {
|
Chris@0
|
95 list.push_back("");
|
Chris@0
|
96 // std::cerr << "LADSPAPluginFactory: cat for " << i->toStdString() << " not found (despite having " << m_fallbackCategories.size() << " fallbacks)" << std::endl;
|
Chris@0
|
97
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 list.push_back(QString("%1").arg(descriptor->PortCount));
|
Chris@0
|
101
|
Chris@0
|
102 for (unsigned long p = 0; p < descriptor->PortCount; ++p) {
|
Chris@0
|
103
|
Chris@0
|
104 int type = 0;
|
Chris@0
|
105 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[p])) {
|
Chris@0
|
106 type |= PortType::Control;
|
Chris@0
|
107 } else {
|
Chris@0
|
108 type |= PortType::Audio;
|
Chris@0
|
109 }
|
Chris@0
|
110 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[p])) {
|
Chris@0
|
111 type |= PortType::Input;
|
Chris@0
|
112 } else {
|
Chris@0
|
113 type |= PortType::Output;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 list.push_back(QString("%1").arg(p));
|
Chris@0
|
117 list.push_back(descriptor->PortNames[p]);
|
Chris@0
|
118 list.push_back(QString("%1").arg(type));
|
Chris@0
|
119 list.push_back(QString("%1").arg(getPortDisplayHint(descriptor, p)));
|
Chris@0
|
120 list.push_back(QString("%1").arg(getPortMinimum(descriptor, p)));
|
Chris@0
|
121 list.push_back(QString("%1").arg(getPortMaximum(descriptor, p)));
|
Chris@0
|
122 list.push_back(QString("%1").arg(getPortDefault(descriptor, p)));
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 unloadUnusedLibraries();
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@60
|
129 const RealTimePluginDescriptor *
|
Chris@60
|
130 LADSPAPluginFactory::getPluginDescriptor(QString identifier) const
|
Chris@60
|
131 {
|
Chris@60
|
132 std::map<QString, RealTimePluginDescriptor *>::const_iterator i =
|
Chris@60
|
133 m_rtDescriptors.find(identifier);
|
Chris@60
|
134
|
Chris@60
|
135 if (i != m_rtDescriptors.end()) {
|
Chris@60
|
136 return i->second;
|
Chris@60
|
137 }
|
Chris@60
|
138
|
Chris@60
|
139 return 0;
|
Chris@60
|
140 }
|
Chris@60
|
141
|
Chris@0
|
142 float
|
Chris@0
|
143 LADSPAPluginFactory::getPortMinimum(const LADSPA_Descriptor *descriptor, int port)
|
Chris@0
|
144 {
|
Chris@0
|
145 LADSPA_PortRangeHintDescriptor d =
|
Chris@0
|
146 descriptor->PortRangeHints[port].HintDescriptor;
|
Chris@0
|
147
|
Chris@0
|
148 float minimum = 0.0;
|
Chris@0
|
149
|
Chris@0
|
150 if (LADSPA_IS_HINT_BOUNDED_BELOW(d)) {
|
Chris@0
|
151 float lb = descriptor->PortRangeHints[port].LowerBound;
|
Chris@0
|
152 minimum = lb;
|
Chris@0
|
153 } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
|
Chris@0
|
154 float ub = descriptor->PortRangeHints[port].UpperBound;
|
Chris@0
|
155 minimum = std::min(0.0, ub - 1.0);
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 if (LADSPA_IS_HINT_SAMPLE_RATE(d)) {
|
Chris@0
|
159 minimum *= m_sampleRate;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 return minimum;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 float
|
Chris@0
|
166 LADSPAPluginFactory::getPortMaximum(const LADSPA_Descriptor *descriptor, int port)
|
Chris@0
|
167 {
|
Chris@0
|
168 LADSPA_PortRangeHintDescriptor d =
|
Chris@0
|
169 descriptor->PortRangeHints[port].HintDescriptor;
|
Chris@0
|
170
|
Chris@0
|
171 float maximum = 1.0;
|
Chris@0
|
172
|
Chris@0
|
173 if (LADSPA_IS_HINT_BOUNDED_ABOVE(d)) {
|
Chris@0
|
174 float ub = descriptor->PortRangeHints[port].UpperBound;
|
Chris@0
|
175 maximum = ub;
|
Chris@0
|
176 } else {
|
Chris@0
|
177 float lb = descriptor->PortRangeHints[port].LowerBound;
|
Chris@0
|
178 maximum = lb + 1.0;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 if (LADSPA_IS_HINT_SAMPLE_RATE(d)) {
|
Chris@0
|
182 maximum *= m_sampleRate;
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 return maximum;
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 float
|
Chris@0
|
189 LADSPAPluginFactory::getPortDefault(const LADSPA_Descriptor *descriptor, int port)
|
Chris@0
|
190 {
|
Chris@0
|
191 float minimum = getPortMinimum(descriptor, port);
|
Chris@0
|
192 float maximum = getPortMaximum(descriptor, port);
|
Chris@0
|
193 float deft;
|
Chris@0
|
194
|
Chris@0
|
195 if (m_portDefaults.find(descriptor->UniqueID) !=
|
Chris@0
|
196 m_portDefaults.end()) {
|
Chris@0
|
197 if (m_portDefaults[descriptor->UniqueID].find(port) !=
|
Chris@0
|
198 m_portDefaults[descriptor->UniqueID].end()) {
|
Chris@0
|
199
|
Chris@0
|
200 deft = m_portDefaults[descriptor->UniqueID][port];
|
Chris@0
|
201 if (deft < minimum) deft = minimum;
|
Chris@0
|
202 if (deft > maximum) deft = maximum;
|
Chris@0
|
203 return deft;
|
Chris@0
|
204 }
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 LADSPA_PortRangeHintDescriptor d =
|
Chris@0
|
208 descriptor->PortRangeHints[port].HintDescriptor;
|
Chris@0
|
209
|
Chris@0
|
210 bool logarithmic = LADSPA_IS_HINT_LOGARITHMIC(d);
|
Chris@0
|
211
|
Chris@0
|
212 if (!LADSPA_IS_HINT_HAS_DEFAULT(d)) {
|
Chris@0
|
213
|
Chris@0
|
214 deft = minimum;
|
Chris@0
|
215
|
Chris@0
|
216 } else if (LADSPA_IS_HINT_DEFAULT_MINIMUM(d)) {
|
Chris@0
|
217
|
Chris@0
|
218 deft = minimum;
|
Chris@0
|
219
|
Chris@0
|
220 } else if (LADSPA_IS_HINT_DEFAULT_LOW(d)) {
|
Chris@0
|
221
|
Chris@0
|
222 if (logarithmic) {
|
Chris@0
|
223 deft = powf(10, log10(minimum) * 0.75 +
|
Chris@0
|
224 log10(maximum) * 0.25);
|
Chris@0
|
225 } else {
|
Chris@0
|
226 deft = minimum * 0.75 + maximum * 0.25;
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(d)) {
|
Chris@0
|
230
|
Chris@0
|
231 if (logarithmic) {
|
Chris@0
|
232 deft = powf(10, log10(minimum) * 0.5 +
|
Chris@0
|
233 log10(maximum) * 0.5);
|
Chris@0
|
234 } else {
|
Chris@0
|
235 deft = minimum * 0.5 + maximum * 0.5;
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 } else if (LADSPA_IS_HINT_DEFAULT_HIGH(d)) {
|
Chris@0
|
239
|
Chris@0
|
240 if (logarithmic) {
|
Chris@0
|
241 deft = powf(10, log10(minimum) * 0.25 +
|
Chris@0
|
242 log10(maximum) * 0.75);
|
Chris@0
|
243 } else {
|
Chris@0
|
244 deft = minimum * 0.25 + maximum * 0.75;
|
Chris@0
|
245 }
|
Chris@0
|
246
|
Chris@0
|
247 } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(d)) {
|
Chris@0
|
248
|
Chris@0
|
249 deft = maximum;
|
Chris@0
|
250
|
Chris@0
|
251 } else if (LADSPA_IS_HINT_DEFAULT_0(d)) {
|
Chris@0
|
252
|
Chris@0
|
253 deft = 0.0;
|
Chris@0
|
254
|
Chris@0
|
255 } else if (LADSPA_IS_HINT_DEFAULT_1(d)) {
|
Chris@0
|
256
|
Chris@0
|
257 deft = 1.0;
|
Chris@0
|
258
|
Chris@0
|
259 } else if (LADSPA_IS_HINT_DEFAULT_100(d)) {
|
Chris@0
|
260
|
Chris@0
|
261 deft = 100.0;
|
Chris@0
|
262
|
Chris@0
|
263 } else if (LADSPA_IS_HINT_DEFAULT_440(d)) {
|
Chris@0
|
264
|
Chris@145
|
265 // deft = 440.0;
|
Chris@145
|
266 deft = Preferences::getInstance()->getTuningFrequency();
|
Chris@0
|
267
|
Chris@0
|
268 } else {
|
Chris@0
|
269
|
Chris@0
|
270 deft = minimum;
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 if (LADSPA_IS_HINT_SAMPLE_RATE(d)) {
|
Chris@0
|
274 deft *= m_sampleRate;
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 return deft;
|
Chris@0
|
278 }
|
Chris@0
|
279
|
Chris@57
|
280 float
|
Chris@57
|
281 LADSPAPluginFactory::getPortQuantization(const LADSPA_Descriptor *descriptor, int port)
|
Chris@57
|
282 {
|
Chris@57
|
283 int displayHint = getPortDisplayHint(descriptor, port);
|
Chris@57
|
284 if (displayHint & PortHint::Toggled) {
|
Chris@57
|
285 return lrintf(getPortMaximum(descriptor, port)) -
|
Chris@57
|
286 lrintf(getPortMinimum(descriptor, port));
|
Chris@57
|
287 }
|
Chris@57
|
288 if (displayHint & PortHint::Integer) {
|
Chris@57
|
289 return 1.0;
|
Chris@57
|
290 }
|
Chris@57
|
291 return 0.0;
|
Chris@57
|
292 }
|
Chris@57
|
293
|
Chris@0
|
294 int
|
Chris@0
|
295 LADSPAPluginFactory::getPortDisplayHint(const LADSPA_Descriptor *descriptor, int port)
|
Chris@0
|
296 {
|
Chris@0
|
297 LADSPA_PortRangeHintDescriptor d =
|
Chris@0
|
298 descriptor->PortRangeHints[port].HintDescriptor;
|
Chris@0
|
299 int hint = PortHint::NoHint;
|
Chris@0
|
300
|
Chris@0
|
301 if (LADSPA_IS_HINT_TOGGLED(d)) hint |= PortHint::Toggled;
|
Chris@0
|
302 if (LADSPA_IS_HINT_INTEGER(d)) hint |= PortHint::Integer;
|
Chris@0
|
303 if (LADSPA_IS_HINT_LOGARITHMIC(d)) hint |= PortHint::Logarithmic;
|
Chris@0
|
304
|
Chris@0
|
305 return hint;
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308
|
Chris@0
|
309 RealTimePluginInstance *
|
Chris@0
|
310 LADSPAPluginFactory::instantiatePlugin(QString identifier,
|
Chris@0
|
311 int instrument,
|
Chris@0
|
312 int position,
|
Chris@0
|
313 unsigned int sampleRate,
|
Chris@0
|
314 unsigned int blockSize,
|
Chris@0
|
315 unsigned int channels)
|
Chris@0
|
316 {
|
Chris@0
|
317 const LADSPA_Descriptor *descriptor = getLADSPADescriptor(identifier);
|
Chris@0
|
318
|
Chris@0
|
319 if (descriptor) {
|
Chris@0
|
320
|
Chris@0
|
321 LADSPAPluginInstance *instance =
|
Chris@0
|
322 new LADSPAPluginInstance
|
Chris@0
|
323 (this, instrument, identifier, position, sampleRate, blockSize, channels,
|
Chris@0
|
324 descriptor);
|
Chris@0
|
325
|
Chris@0
|
326 m_instances.insert(instance);
|
Chris@0
|
327
|
Chris@78
|
328 std::cerr << "LADSPAPluginFactory::instantiatePlugin("
|
Chris@78
|
329 << identifier.toStdString() << ": now have " << m_instances.size() << " instances" << std::endl;
|
Chris@78
|
330
|
Chris@0
|
331 return instance;
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 return 0;
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 void
|
Chris@0
|
338 LADSPAPluginFactory::releasePlugin(RealTimePluginInstance *instance,
|
Chris@0
|
339 QString identifier)
|
Chris@0
|
340 {
|
Chris@0
|
341 if (m_instances.find(instance) == m_instances.end()) {
|
Chris@0
|
342 std::cerr << "WARNING: LADSPAPluginFactory::releasePlugin: Not one of mine!"
|
Chris@0
|
343 << std::endl;
|
Chris@0
|
344 return;
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 QString type, soname, label;
|
Chris@0
|
348 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
|
Chris@0
|
349
|
Chris@0
|
350 m_instances.erase(instance);
|
Chris@0
|
351
|
Chris@0
|
352 bool stillInUse = false;
|
Chris@0
|
353
|
Chris@0
|
354 for (std::set<RealTimePluginInstance *>::iterator ii = m_instances.begin();
|
Chris@0
|
355 ii != m_instances.end(); ++ii) {
|
Chris@0
|
356 QString itype, isoname, ilabel;
|
Chris@0
|
357 PluginIdentifier::parseIdentifier((*ii)->getIdentifier(), itype, isoname, ilabel);
|
Chris@0
|
358 if (isoname == soname) {
|
Chris@78
|
359 std::cerr << "LADSPAPluginFactory::releasePlugin: dll " << soname.toStdString() << " is still in use for plugin " << ilabel.toStdString() << std::endl;
|
Chris@0
|
360 stillInUse = true;
|
Chris@0
|
361 break;
|
Chris@0
|
362 }
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 if (!stillInUse) {
|
Chris@118
|
366 if (soname != PluginIdentifier::BUILTIN_PLUGIN_SONAME) {
|
Chris@118
|
367 std::cerr << "LADSPAPluginFactory::releasePlugin: dll " << soname.toStdString() << " no longer in use, unloading" << std::endl;
|
Chris@118
|
368 unloadLibrary(soname);
|
Chris@118
|
369 }
|
Chris@0
|
370 }
|
Chris@78
|
371
|
Chris@78
|
372 std::cerr << "LADSPAPluginFactory::releasePlugin("
|
Chris@78
|
373 << identifier.toStdString() << ": now have " << m_instances.size() << " instances" << std::endl;
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 const LADSPA_Descriptor *
|
Chris@0
|
377 LADSPAPluginFactory::getLADSPADescriptor(QString identifier)
|
Chris@0
|
378 {
|
Chris@0
|
379 QString type, soname, label;
|
Chris@0
|
380 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
|
Chris@0
|
381
|
Chris@0
|
382 if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
|
Chris@0
|
383 loadLibrary(soname);
|
Chris@0
|
384 if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
|
Chris@0
|
385 std::cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: loadLibrary failed for " << soname.toStdString() << std::endl;
|
Chris@0
|
386 return 0;
|
Chris@0
|
387 }
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 void *libraryHandle = m_libraryHandles[soname];
|
Chris@0
|
391
|
Chris@0
|
392 LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function)
|
Chris@0
|
393 DLSYM(libraryHandle, "ladspa_descriptor");
|
Chris@0
|
394
|
Chris@0
|
395 if (!fn) {
|
Chris@0
|
396 std::cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No descriptor function in library " << soname.toStdString() << std::endl;
|
Chris@0
|
397 return 0;
|
Chris@0
|
398 }
|
Chris@0
|
399
|
Chris@0
|
400 const LADSPA_Descriptor *descriptor = 0;
|
Chris@0
|
401
|
Chris@0
|
402 int index = 0;
|
Chris@0
|
403 while ((descriptor = fn(index))) {
|
Chris@0
|
404 if (descriptor->Label == label) return descriptor;
|
Chris@0
|
405 ++index;
|
Chris@0
|
406 }
|
Chris@0
|
407
|
Chris@0
|
408 std::cerr << "WARNING: LADSPAPluginFactory::getLADSPADescriptor: No such plugin as " << label.toStdString() << " in library " << soname.toStdString() << std::endl;
|
Chris@0
|
409
|
Chris@0
|
410 return 0;
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 void
|
Chris@0
|
414 LADSPAPluginFactory::loadLibrary(QString soName)
|
Chris@0
|
415 {
|
Chris@0
|
416 void *libraryHandle = DLOPEN(soName, RTLD_NOW);
|
Chris@106
|
417 if (libraryHandle) {
|
Chris@106
|
418 m_libraryHandles[soName] = libraryHandle;
|
Chris@106
|
419 std::cerr << "LADSPAPluginFactory::loadLibrary: Loaded library \"" << soName.toStdString() << "\"" << std::endl;
|
Chris@106
|
420 return;
|
Chris@106
|
421 }
|
Chris@106
|
422
|
Chris@106
|
423 if (QFileInfo(soName).exists()) {
|
Chris@106
|
424 DLERROR();
|
Chris@106
|
425 std::cerr << "LADSPAPluginFactory::loadLibrary: Library \"" << soName.toStdString() << "\" exists, but failed to load it" << std::endl;
|
Chris@106
|
426 return;
|
Chris@106
|
427 }
|
Chris@106
|
428
|
Chris@106
|
429 std::vector<QString> pathList = getPluginPath();
|
Chris@106
|
430
|
Chris@106
|
431 QString fileName = QFile(soName).fileName();
|
Chris@106
|
432 QString base = QFileInfo(soName).baseName();
|
Chris@106
|
433
|
Chris@106
|
434 for (std::vector<QString>::iterator i = pathList.begin();
|
Chris@106
|
435 i != pathList.end(); ++i) {
|
Chris@106
|
436
|
Chris@106
|
437 std::cerr << "Looking at: " << (*i).toStdString() << std::endl;
|
Chris@106
|
438
|
Chris@106
|
439 QDir dir(*i, PLUGIN_GLOB,
|
Chris@106
|
440 QDir::Name | QDir::IgnoreCase,
|
Chris@106
|
441 QDir::Files | QDir::Readable);
|
Chris@106
|
442
|
Chris@106
|
443 if (QFileInfo(dir.filePath(fileName)).exists()) {
|
Chris@106
|
444 std::cerr << "Loading: " << fileName.toStdString() << std::endl;
|
Chris@106
|
445 libraryHandle = DLOPEN(dir.filePath(fileName), RTLD_NOW);
|
Chris@106
|
446 if (libraryHandle) m_libraryHandles[soName] = libraryHandle;
|
Chris@106
|
447 return;
|
Chris@106
|
448 }
|
Chris@106
|
449
|
Chris@106
|
450 for (unsigned int j = 0; j < dir.count(); ++j) {
|
Chris@106
|
451 QString file = dir.filePath(dir[j]);
|
Chris@106
|
452 if (QFileInfo(file).baseName() == base) {
|
Chris@106
|
453 std::cerr << "Loading: " << file.toStdString() << std::endl;
|
Chris@106
|
454 libraryHandle = DLOPEN(file, RTLD_NOW);
|
Chris@106
|
455 if (libraryHandle) m_libraryHandles[soName] = libraryHandle;
|
Chris@106
|
456 return;
|
Chris@106
|
457 }
|
Chris@106
|
458 }
|
Chris@106
|
459 }
|
Chris@106
|
460
|
Chris@106
|
461 std::cerr << "LADSPAPluginFactory::loadLibrary: Failed to locate plugin library \"" << soName.toStdString() << "\"" << std::endl;
|
Chris@0
|
462 }
|
Chris@0
|
463
|
Chris@0
|
464 void
|
Chris@0
|
465 LADSPAPluginFactory::unloadLibrary(QString soName)
|
Chris@0
|
466 {
|
Chris@0
|
467 LibraryHandleMap::iterator li = m_libraryHandles.find(soName);
|
Chris@0
|
468 if (li != m_libraryHandles.end()) {
|
Chris@0
|
469 // std::cerr << "unloading " << soname.toStdString() << std::endl;
|
Chris@0
|
470 DLCLOSE(m_libraryHandles[soName]);
|
Chris@0
|
471 m_libraryHandles.erase(li);
|
Chris@0
|
472 }
|
Chris@0
|
473 }
|
Chris@0
|
474
|
Chris@0
|
475 void
|
Chris@0
|
476 LADSPAPluginFactory::unloadUnusedLibraries()
|
Chris@0
|
477 {
|
Chris@0
|
478 std::vector<QString> toUnload;
|
Chris@0
|
479
|
Chris@0
|
480 for (LibraryHandleMap::iterator i = m_libraryHandles.begin();
|
Chris@0
|
481 i != m_libraryHandles.end(); ++i) {
|
Chris@0
|
482
|
Chris@0
|
483 bool stillInUse = false;
|
Chris@0
|
484
|
Chris@0
|
485 for (std::set<RealTimePluginInstance *>::iterator ii = m_instances.begin();
|
Chris@0
|
486 ii != m_instances.end(); ++ii) {
|
Chris@0
|
487
|
Chris@0
|
488 QString itype, isoname, ilabel;
|
Chris@0
|
489 PluginIdentifier::parseIdentifier((*ii)->getIdentifier(), itype, isoname, ilabel);
|
Chris@0
|
490 if (isoname == i->first) {
|
Chris@0
|
491 stillInUse = true;
|
Chris@0
|
492 break;
|
Chris@0
|
493 }
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 if (!stillInUse) toUnload.push_back(i->first);
|
Chris@0
|
497 }
|
Chris@0
|
498
|
Chris@0
|
499 for (std::vector<QString>::iterator i = toUnload.begin();
|
Chris@0
|
500 i != toUnload.end(); ++i) {
|
Chris@118
|
501 if (*i != PluginIdentifier::BUILTIN_PLUGIN_SONAME) {
|
Chris@118
|
502 unloadLibrary(*i);
|
Chris@118
|
503 }
|
Chris@0
|
504 }
|
Chris@0
|
505 }
|
Chris@0
|
506
|
Chris@0
|
507
|
Chris@0
|
508 // It is only later, after they've gone,
|
Chris@0
|
509 // I realize they have delivered a letter.
|
Chris@0
|
510 // It's a letter from my wife. "What are you doing
|
Chris@0
|
511 // there?" my wife asks. "Are you drinking?"
|
Chris@0
|
512 // I study the postmark for hours. Then it, too, begins to fade.
|
Chris@0
|
513 // I hope someday to forget all this.
|
Chris@0
|
514
|
Chris@0
|
515
|
Chris@0
|
516 std::vector<QString>
|
Chris@0
|
517 LADSPAPluginFactory::getPluginPath()
|
Chris@0
|
518 {
|
Chris@0
|
519 std::vector<QString> pathList;
|
Chris@0
|
520 std::string path;
|
Chris@0
|
521
|
Chris@0
|
522 char *cpath = getenv("LADSPA_PATH");
|
Chris@0
|
523 if (cpath) path = cpath;
|
Chris@0
|
524
|
Chris@0
|
525 if (path == "") {
|
Chris@0
|
526 path = "/usr/local/lib/ladspa:/usr/lib/ladspa";
|
Chris@0
|
527 char *home = getenv("HOME");
|
Chris@0
|
528 if (home) path = std::string(home) + "/.ladspa:" + path;
|
Chris@0
|
529 }
|
Chris@0
|
530
|
Chris@0
|
531 std::string::size_type index = 0, newindex = 0;
|
Chris@0
|
532
|
Chris@0
|
533 while ((newindex = path.find(':', index)) < path.size()) {
|
Chris@0
|
534 pathList.push_back(path.substr(index, newindex - index).c_str());
|
Chris@0
|
535 index = newindex + 1;
|
Chris@0
|
536 }
|
Chris@0
|
537
|
Chris@0
|
538 pathList.push_back(path.substr(index).c_str());
|
Chris@0
|
539
|
Chris@0
|
540 return pathList;
|
Chris@0
|
541 }
|
Chris@0
|
542
|
Chris@0
|
543
|
Chris@0
|
544 std::vector<QString>
|
Chris@0
|
545 LADSPAPluginFactory::getLRDFPath(QString &baseUri)
|
Chris@0
|
546 {
|
Chris@150
|
547 std::vector<QString> lrdfPaths;
|
Chris@150
|
548
|
Chris@150
|
549 #ifdef HAVE_LRDF
|
Chris@0
|
550 std::vector<QString> pathList = getPluginPath();
|
Chris@0
|
551
|
Chris@0
|
552 lrdfPaths.push_back("/usr/local/share/ladspa/rdf");
|
Chris@0
|
553 lrdfPaths.push_back("/usr/share/ladspa/rdf");
|
Chris@0
|
554
|
Chris@0
|
555 for (std::vector<QString>::iterator i = pathList.begin();
|
Chris@0
|
556 i != pathList.end(); ++i) {
|
Chris@0
|
557 lrdfPaths.push_back(*i + "/rdf");
|
Chris@0
|
558 }
|
Chris@0
|
559
|
Chris@0
|
560 baseUri = LADSPA_BASE;
|
Chris@150
|
561 #endif
|
Chris@150
|
562
|
Chris@0
|
563 return lrdfPaths;
|
Chris@0
|
564 }
|
Chris@0
|
565
|
Chris@0
|
566 void
|
Chris@0
|
567 LADSPAPluginFactory::discoverPlugins()
|
Chris@0
|
568 {
|
Chris@0
|
569 std::vector<QString> pathList = getPluginPath();
|
Chris@0
|
570
|
Chris@0
|
571 // std::cerr << "LADSPAPluginFactory::discoverPlugins - "
|
Chris@0
|
572 // << "discovering plugins; path is ";
|
Chris@0
|
573 for (std::vector<QString>::iterator i = pathList.begin();
|
Chris@0
|
574 i != pathList.end(); ++i) {
|
Chris@0
|
575 std::cerr << "[" << i->toStdString() << "] ";
|
Chris@0
|
576 }
|
Chris@0
|
577 std::cerr << std::endl;
|
Chris@0
|
578
|
Chris@35
|
579 #ifdef HAVE_LRDF
|
Chris@0
|
580 // Initialise liblrdf and read the description files
|
Chris@0
|
581 //
|
Chris@0
|
582 lrdf_init();
|
Chris@0
|
583
|
Chris@0
|
584 QString baseUri;
|
Chris@0
|
585 std::vector<QString> lrdfPaths = getLRDFPath(baseUri);
|
Chris@0
|
586
|
Chris@0
|
587 bool haveSomething = false;
|
Chris@0
|
588
|
Chris@0
|
589 for (size_t i = 0; i < lrdfPaths.size(); ++i) {
|
Chris@0
|
590 QDir dir(lrdfPaths[i], "*.rdf;*.rdfs");
|
Chris@0
|
591 for (unsigned int j = 0; j < dir.count(); ++j) {
|
Chris@0
|
592 if (!lrdf_read_file(QString("file:" + lrdfPaths[i] + "/" + dir[j]).toStdString().c_str())) {
|
Chris@0
|
593 // std::cerr << "LADSPAPluginFactory: read RDF file " << (lrdfPaths[i] + "/" + dir[j]) << std::endl;
|
Chris@0
|
594 haveSomething = true;
|
Chris@0
|
595 }
|
Chris@0
|
596 }
|
Chris@0
|
597 }
|
Chris@0
|
598
|
Chris@0
|
599 if (haveSomething) {
|
Chris@0
|
600 generateTaxonomy(baseUri + "Plugin", "");
|
Chris@0
|
601 }
|
Chris@35
|
602 #endif // HAVE_LRDF
|
Chris@0
|
603
|
Chris@0
|
604 generateFallbackCategories();
|
Chris@0
|
605
|
Chris@0
|
606 for (std::vector<QString>::iterator i = pathList.begin();
|
Chris@0
|
607 i != pathList.end(); ++i) {
|
Chris@0
|
608
|
Chris@0
|
609 QDir pluginDir(*i, PLUGIN_GLOB);
|
Chris@0
|
610
|
Chris@0
|
611 for (unsigned int j = 0; j < pluginDir.count(); ++j) {
|
Chris@0
|
612 discoverPlugins(QString("%1/%2").arg(*i).arg(pluginDir[j]));
|
Chris@0
|
613 }
|
Chris@0
|
614 }
|
Chris@0
|
615
|
Chris@35
|
616 #ifdef HAVE_LRDF
|
Chris@0
|
617 // Cleanup after the RDF library
|
Chris@0
|
618 //
|
Chris@0
|
619 lrdf_cleanup();
|
Chris@35
|
620 #endif // HAVE_LRDF
|
Chris@0
|
621 }
|
Chris@0
|
622
|
Chris@0
|
623 void
|
Chris@0
|
624 LADSPAPluginFactory::discoverPlugins(QString soname)
|
Chris@0
|
625 {
|
Chris@0
|
626 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
|
Chris@0
|
627
|
Chris@0
|
628 if (!libraryHandle) {
|
Chris@0
|
629 std::cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: couldn't load plugin library "
|
Chris@0
|
630 << soname.toStdString() << " - " << DLERROR() << std::endl;
|
Chris@0
|
631 return;
|
Chris@0
|
632 }
|
Chris@0
|
633
|
Chris@0
|
634 LADSPA_Descriptor_Function fn = (LADSPA_Descriptor_Function)
|
Chris@0
|
635 DLSYM(libraryHandle, "ladspa_descriptor");
|
Chris@0
|
636
|
Chris@0
|
637 if (!fn) {
|
Chris@0
|
638 std::cerr << "WARNING: LADSPAPluginFactory::discoverPlugins: No descriptor function in " << soname.toStdString() << std::endl;
|
Chris@0
|
639 return;
|
Chris@0
|
640 }
|
Chris@0
|
641
|
Chris@0
|
642 const LADSPA_Descriptor *descriptor = 0;
|
Chris@0
|
643
|
Chris@0
|
644 int index = 0;
|
Chris@0
|
645 while ((descriptor = fn(index))) {
|
Chris@0
|
646
|
Chris@60
|
647 RealTimePluginDescriptor *rtd = new RealTimePluginDescriptor;
|
Chris@60
|
648 rtd->name = descriptor->Name;
|
Chris@60
|
649 rtd->label = descriptor->Label;
|
Chris@60
|
650 rtd->maker = descriptor->Maker;
|
Chris@60
|
651 rtd->copyright = descriptor->Copyright;
|
Chris@60
|
652 rtd->category = "";
|
Chris@60
|
653 rtd->isSynth = false;
|
Chris@60
|
654 rtd->parameterCount = 0;
|
Chris@60
|
655 rtd->audioInputPortCount = 0;
|
Chris@60
|
656 rtd->controlOutputPortCount = 0;
|
Chris@60
|
657
|
Chris@35
|
658 #ifdef HAVE_LRDF
|
Chris@0
|
659 char *def_uri = 0;
|
Chris@0
|
660 lrdf_defaults *defs = 0;
|
Chris@0
|
661
|
Chris@0
|
662 QString category = m_taxonomy[descriptor->UniqueID];
|
Chris@0
|
663
|
Chris@0
|
664 if (category == "" && descriptor->Name != 0) {
|
Chris@0
|
665 std::string name = descriptor->Name;
|
Chris@0
|
666 if (name.length() > 4 &&
|
Chris@0
|
667 name.substr(name.length() - 4) == " VST") {
|
Chris@0
|
668 category = "VST effects";
|
Chris@0
|
669 m_taxonomy[descriptor->UniqueID] = category;
|
Chris@0
|
670 }
|
Chris@0
|
671 }
|
Chris@0
|
672
|
Chris@60
|
673 rtd->category = category.toStdString();
|
Chris@60
|
674
|
Chris@0
|
675 // std::cerr << "Plugin id is " << descriptor->UniqueID
|
Chris@0
|
676 // << ", category is \"" << (category ? category : QString("(none)"))
|
Chris@0
|
677 // << "\", name is " << descriptor->Name
|
Chris@0
|
678 // << ", label is " << descriptor->Label
|
Chris@0
|
679 // << std::endl;
|
Chris@0
|
680
|
Chris@0
|
681 def_uri = lrdf_get_default_uri(descriptor->UniqueID);
|
Chris@0
|
682 if (def_uri) {
|
Chris@0
|
683 defs = lrdf_get_setting_values(def_uri);
|
Chris@0
|
684 }
|
Chris@0
|
685
|
Chris@0
|
686 int controlPortNumber = 1;
|
Chris@0
|
687
|
Chris@0
|
688 for (unsigned long i = 0; i < descriptor->PortCount; i++) {
|
Chris@0
|
689
|
Chris@0
|
690 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) {
|
Chris@0
|
691
|
Chris@0
|
692 if (def_uri && defs) {
|
Chris@0
|
693
|
Chris@0
|
694 for (int j = 0; j < defs->count; j++) {
|
Chris@0
|
695 if (defs->items[j].pid == controlPortNumber) {
|
Chris@0
|
696 // std::cerr << "Default for this port (" << defs->items[j].pid << ", " << defs->items[j].label << ") is " << defs->items[j].value << "; applying this to port number " << i << " with name " << descriptor->PortNames[i] << std::endl;
|
Chris@0
|
697 m_portDefaults[descriptor->UniqueID][i] =
|
Chris@0
|
698 defs->items[j].value;
|
Chris@0
|
699 }
|
Chris@0
|
700 }
|
Chris@0
|
701 }
|
Chris@0
|
702
|
Chris@0
|
703 ++controlPortNumber;
|
Chris@0
|
704 }
|
Chris@0
|
705 }
|
Chris@35
|
706 #endif // HAVE_LRDF
|
Chris@0
|
707
|
Chris@60
|
708 for (unsigned long i = 0; i < descriptor->PortCount; i++) {
|
Chris@60
|
709 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[i])) {
|
Chris@60
|
710 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
|
Chris@60
|
711 ++rtd->parameterCount;
|
Chris@60
|
712 } else {
|
Chris@60
|
713 if (strcmp(descriptor->PortNames[i], "latency") &&
|
Chris@60
|
714 strcmp(descriptor->PortNames[i], "_latency")) {
|
Chris@60
|
715 ++rtd->controlOutputPortCount;
|
Chris@60
|
716 rtd->controlOutputPortNames.push_back
|
Chris@60
|
717 (descriptor->PortNames[i]);
|
Chris@60
|
718 }
|
Chris@60
|
719 }
|
Chris@60
|
720 } else {
|
Chris@60
|
721 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[i])) {
|
Chris@60
|
722 ++rtd->audioInputPortCount;
|
Chris@60
|
723 }
|
Chris@60
|
724 }
|
Chris@60
|
725 }
|
Chris@60
|
726
|
Chris@0
|
727 QString identifier = PluginIdentifier::createIdentifier
|
Chris@0
|
728 ("ladspa", soname, descriptor->Label);
|
Chris@0
|
729 m_identifiers.push_back(identifier);
|
Chris@0
|
730
|
Chris@60
|
731 m_rtDescriptors[identifier] = rtd;
|
Chris@60
|
732
|
Chris@0
|
733 ++index;
|
Chris@0
|
734 }
|
Chris@0
|
735
|
Chris@0
|
736 if (DLCLOSE(libraryHandle) != 0) {
|
Chris@0
|
737 std::cerr << "WARNING: LADSPAPluginFactory::discoverPlugins - can't unload " << libraryHandle << std::endl;
|
Chris@0
|
738 return;
|
Chris@0
|
739 }
|
Chris@0
|
740 }
|
Chris@0
|
741
|
Chris@0
|
742 void
|
Chris@0
|
743 LADSPAPluginFactory::generateFallbackCategories()
|
Chris@0
|
744 {
|
Chris@0
|
745 std::vector<QString> pluginPath = getPluginPath();
|
Chris@0
|
746 std::vector<QString> path;
|
Chris@0
|
747
|
Chris@0
|
748 for (size_t i = 0; i < pluginPath.size(); ++i) {
|
Chris@0
|
749 if (pluginPath[i].contains("/lib/")) {
|
Chris@0
|
750 QString p(pluginPath[i]);
|
Chris@0
|
751 p.replace("/lib/", "/share/");
|
Chris@0
|
752 path.push_back(p);
|
Chris@0
|
753 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: path element " << p << std::endl;
|
Chris@0
|
754 }
|
Chris@0
|
755 path.push_back(pluginPath[i]);
|
Chris@0
|
756 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: path element " << pluginPath[i] << std::endl;
|
Chris@0
|
757 }
|
Chris@0
|
758
|
Chris@0
|
759 for (size_t i = 0; i < path.size(); ++i) {
|
Chris@0
|
760
|
Chris@0
|
761 QDir dir(path[i], "*.cat");
|
Chris@0
|
762
|
Chris@0
|
763 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: directory " << path[i] << " has " << dir.count() << " .cat files" << std::endl;
|
Chris@0
|
764 for (unsigned int j = 0; j < dir.count(); ++j) {
|
Chris@0
|
765
|
Chris@0
|
766 QFile file(path[i] + "/" + dir[j]);
|
Chris@0
|
767
|
Chris@0
|
768 // std::cerr << "LADSPAPluginFactory::generateFallbackCategories: about to open " << (path[i] + "/" + dir[j]) << std::endl;
|
Chris@0
|
769
|
Chris@0
|
770 if (file.open(QIODevice::ReadOnly)) {
|
Chris@0
|
771 // std::cerr << "...opened" << std::endl;
|
Chris@0
|
772 QTextStream stream(&file);
|
Chris@0
|
773 QString line;
|
Chris@0
|
774
|
Chris@0
|
775 while (!stream.atEnd()) {
|
Chris@0
|
776 line = stream.readLine();
|
Chris@0
|
777 // std::cerr << "line is: \"" << line << "\"" << std::endl;
|
Chris@0
|
778 QString id = line.section("::", 0, 0);
|
Chris@0
|
779 QString cat = line.section("::", 1, 1);
|
Chris@0
|
780 m_fallbackCategories[id] = cat;
|
Chris@0
|
781 // std::cerr << "set id \"" << id << "\" to cat \"" << cat << "\"" << std::endl;
|
Chris@0
|
782 }
|
Chris@0
|
783 }
|
Chris@0
|
784 }
|
Chris@0
|
785 }
|
Chris@0
|
786 }
|
Chris@0
|
787
|
Chris@0
|
788 void
|
Chris@0
|
789 LADSPAPluginFactory::generateTaxonomy(QString uri, QString base)
|
Chris@0
|
790 {
|
Chris@35
|
791 #ifdef HAVE_LRDF
|
Chris@0
|
792 lrdf_uris *uris = lrdf_get_instances(uri.toStdString().c_str());
|
Chris@0
|
793
|
Chris@0
|
794 if (uris != NULL) {
|
Chris@0
|
795 for (int i = 0; i < uris->count; ++i) {
|
Chris@0
|
796 m_taxonomy[lrdf_get_uid(uris->items[i])] = base;
|
Chris@0
|
797 }
|
Chris@0
|
798 lrdf_free_uris(uris);
|
Chris@0
|
799 }
|
Chris@0
|
800
|
Chris@0
|
801 uris = lrdf_get_subclasses(uri.toStdString().c_str());
|
Chris@0
|
802
|
Chris@0
|
803 if (uris != NULL) {
|
Chris@0
|
804 for (int i = 0; i < uris->count; ++i) {
|
Chris@0
|
805 char *label = lrdf_get_label(uris->items[i]);
|
Chris@0
|
806 generateTaxonomy(uris->items[i],
|
Chris@0
|
807 base + (base.length() > 0 ? " > " : "") + label);
|
Chris@0
|
808 }
|
Chris@0
|
809 lrdf_free_uris(uris);
|
Chris@0
|
810 }
|
Chris@0
|
811 #endif
|
Chris@0
|
812 }
|
Chris@0
|
813
|
Chris@0
|
814
|