annotate plugin/DSSIPluginFactory.cpp @ 14:b101cc2ae1ab

* Introduce potentially-separate read and write ring buffers, so we can swap in a new set when something changes -- thus allowing us to respond quickly when something changes during playback, without losing the long buffers * Some fixes for display & editing
author Chris Cannam
date Fri, 27 Jan 2006 18:04:07 +0000
parents d86891498eef
children 2fb933f88604
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@2 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 /*
Chris@0 11 This is a modified version of a source file from the
Chris@0 12 Rosegarden MIDI and audio sequencer and notation editor.
Chris@0 13 This file copyright 2000-2005 Chris Cannam.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "DSSIPluginFactory.h"
Chris@0 17 #include <iostream>
Chris@0 18
Chris@0 19 #include <QString>
Chris@0 20
Chris@0 21 #include "DSSIPluginInstance.h"
Chris@0 22 #include "PluginIdentifier.h"
Chris@0 23
Chris@0 24 //!!!
Chris@0 25 #include "plugins/SamplePlayer.h"
Chris@0 26
Chris@0 27 #include "base/System.h"
Chris@0 28
Chris@0 29 #ifdef HAVE_LIBLRDF
Chris@0 30 #include "lrdf.h"
Chris@0 31 #endif // HAVE_LIBLRDF
Chris@0 32
Chris@0 33
Chris@0 34 DSSIPluginFactory::DSSIPluginFactory() :
Chris@0 35 LADSPAPluginFactory()
Chris@0 36 {
Chris@0 37 m_hostDescriptor.DSSI_API_Version = 2;
Chris@0 38 m_hostDescriptor.request_transport_information = NULL;
Chris@0 39 m_hostDescriptor.request_midi_send = DSSIPluginInstance::requestMidiSend;
Chris@0 40 m_hostDescriptor.request_non_rt_thread = DSSIPluginInstance::requestNonRTThread;
Chris@0 41 m_hostDescriptor.midi_send = DSSIPluginInstance::midiSend;
Chris@0 42 }
Chris@0 43
Chris@0 44 DSSIPluginFactory::~DSSIPluginFactory()
Chris@0 45 {
Chris@0 46 // nothing else to do here either
Chris@0 47 }
Chris@0 48
Chris@0 49 void
Chris@0 50 DSSIPluginFactory::enumeratePlugins(std::vector<QString> &list)
Chris@0 51 {
Chris@0 52 for (std::vector<QString>::iterator i = m_identifiers.begin();
Chris@0 53 i != m_identifiers.end(); ++i) {
Chris@0 54
Chris@0 55 const DSSI_Descriptor *ddesc = getDSSIDescriptor(*i);
Chris@0 56 if (!ddesc) continue;
Chris@0 57
Chris@0 58 const LADSPA_Descriptor *descriptor = ddesc->LADSPA_Plugin;
Chris@0 59 if (!descriptor) continue;
Chris@0 60
Chris@0 61 // std::cerr << "DSSIPluginFactory::enumeratePlugins: Name " << (descriptor->Name ? descriptor->Name : "NONE" ) << std::endl;
Chris@0 62
Chris@0 63 list.push_back(*i);
Chris@0 64 list.push_back(descriptor->Name);
Chris@0 65 list.push_back(QString("%1").arg(descriptor->UniqueID));
Chris@0 66 list.push_back(descriptor->Label);
Chris@0 67 list.push_back(descriptor->Maker);
Chris@0 68 list.push_back(descriptor->Copyright);
Chris@0 69 list.push_back((ddesc->run_synth || ddesc->run_multiple_synths) ? "true" : "false");
Chris@0 70 list.push_back(ddesc->run_multiple_synths ? "true" : "false");
Chris@0 71 list.push_back(m_taxonomy[descriptor->UniqueID]);
Chris@0 72 list.push_back(QString("%1").arg(descriptor->PortCount));
Chris@0 73
Chris@0 74 for (unsigned long p = 0; p < descriptor->PortCount; ++p) {
Chris@0 75
Chris@0 76 int type = 0;
Chris@0 77 if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[p])) {
Chris@0 78 type |= PortType::Control;
Chris@0 79 } else {
Chris@0 80 type |= PortType::Audio;
Chris@0 81 }
Chris@0 82 if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[p])) {
Chris@0 83 type |= PortType::Input;
Chris@0 84 } else {
Chris@0 85 type |= PortType::Output;
Chris@0 86 }
Chris@0 87
Chris@0 88 list.push_back(QString("%1").arg(p));
Chris@0 89 list.push_back(descriptor->PortNames[p]);
Chris@0 90 list.push_back(QString("%1").arg(type));
Chris@0 91 list.push_back(QString("%1").arg(getPortDisplayHint(descriptor, p)));
Chris@0 92 list.push_back(QString("%1").arg(getPortMinimum(descriptor, p)));
Chris@0 93 list.push_back(QString("%1").arg(getPortMaximum(descriptor, p)));
Chris@0 94 list.push_back(QString("%1").arg(getPortDefault(descriptor, p)));
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 unloadUnusedLibraries();
Chris@0 99 }
Chris@0 100
Chris@0 101 RealTimePluginInstance *
Chris@0 102 DSSIPluginFactory::instantiatePlugin(QString identifier,
Chris@0 103 int instrument,
Chris@0 104 int position,
Chris@0 105 unsigned int sampleRate,
Chris@0 106 unsigned int blockSize,
Chris@0 107 unsigned int channels)
Chris@0 108 {
Chris@0 109 const DSSI_Descriptor *descriptor = getDSSIDescriptor(identifier);
Chris@0 110
Chris@0 111 if (descriptor) {
Chris@0 112
Chris@0 113 DSSIPluginInstance *instance =
Chris@0 114 new DSSIPluginInstance
Chris@0 115 (this, instrument, identifier, position, sampleRate, blockSize, channels,
Chris@0 116 descriptor);
Chris@0 117
Chris@0 118 m_instances.insert(instance);
Chris@0 119
Chris@0 120 return instance;
Chris@0 121 }
Chris@0 122
Chris@0 123 return 0;
Chris@0 124 }
Chris@0 125
Chris@0 126 const DSSI_Descriptor *
Chris@0 127 DSSIPluginFactory::getDSSIDescriptor(QString identifier)
Chris@0 128 {
Chris@0 129 QString type, soname, label;
Chris@0 130 PluginIdentifier::parseIdentifier(identifier, type, soname, label);
Chris@0 131
Chris@0 132 if (soname == PluginIdentifier::BUILTIN_PLUGIN_SONAME) {
Chris@0 133 if (label == "sample_player") {
Chris@0 134 const DSSI_Descriptor *descriptor = SamplePlayer::getDescriptor(0);
Chris@0 135 if (descriptor) {
Chris@0 136 descriptor->receive_host_descriptor(&m_hostDescriptor);
Chris@0 137 }
Chris@0 138 return descriptor;
Chris@0 139 } else {
Chris@0 140 return 0;
Chris@0 141 }
Chris@0 142 }
Chris@0 143
Chris@0 144 bool firstInLibrary = false;
Chris@0 145
Chris@0 146 if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
Chris@0 147 loadLibrary(soname);
Chris@0 148 if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
Chris@0 149 std::cerr << "WARNING: DSSIPluginFactory::getDSSIDescriptor: loadLibrary failed for " << soname.toStdString() << std::endl;
Chris@0 150 return 0;
Chris@0 151 }
Chris@0 152 firstInLibrary = true;
Chris@0 153 }
Chris@0 154
Chris@0 155 void *libraryHandle = m_libraryHandles[soname];
Chris@0 156
Chris@0 157 DSSI_Descriptor_Function fn = (DSSI_Descriptor_Function)
Chris@0 158 DLSYM(libraryHandle, "dssi_descriptor");
Chris@0 159
Chris@0 160 if (!fn) {
Chris@0 161 std::cerr << "WARNING: DSSIPluginFactory::getDSSIDescriptor: No descriptor function in library " << soname.toStdString() << std::endl;
Chris@0 162 return 0;
Chris@0 163 }
Chris@0 164
Chris@0 165 const DSSI_Descriptor *descriptor = 0;
Chris@0 166
Chris@0 167 int index = 0;
Chris@0 168 while ((descriptor = fn(index))) {
Chris@0 169 if (descriptor->LADSPA_Plugin->Label == label) {
Chris@0 170 if (firstInLibrary && (descriptor->DSSI_API_Version >= 2)) {
Chris@0 171 descriptor->receive_host_descriptor(&m_hostDescriptor);
Chris@0 172 }
Chris@0 173 return descriptor;
Chris@0 174 }
Chris@0 175 ++index;
Chris@0 176 }
Chris@0 177
Chris@0 178 std::cerr << "WARNING: DSSIPluginFactory::getDSSIDescriptor: No such plugin as " << label.toStdString() << " in library " << soname.toStdString() << std::endl;
Chris@0 179
Chris@0 180 return 0;
Chris@0 181 }
Chris@0 182
Chris@0 183 const LADSPA_Descriptor *
Chris@0 184 DSSIPluginFactory::getLADSPADescriptor(QString identifier)
Chris@0 185 {
Chris@0 186 const DSSI_Descriptor *dssiDescriptor = getDSSIDescriptor(identifier);
Chris@0 187 if (dssiDescriptor) return dssiDescriptor->LADSPA_Plugin;
Chris@0 188 else return 0;
Chris@0 189 }
Chris@0 190
Chris@0 191
Chris@0 192 std::vector<QString>
Chris@0 193 DSSIPluginFactory::getPluginPath()
Chris@0 194 {
Chris@0 195 std::vector<QString> pathList;
Chris@0 196 std::string path;
Chris@0 197
Chris@0 198 char *cpath = getenv("DSSI_PATH");
Chris@0 199 if (cpath) path = cpath;
Chris@0 200
Chris@0 201 if (path == "") {
Chris@0 202 path = "/usr/local/lib/dssi:/usr/lib/dssi";
Chris@0 203 char *home = getenv("HOME");
Chris@0 204 if (home) path = std::string(home) + "/.dssi:" + path;
Chris@0 205 }
Chris@0 206
Chris@0 207 std::string::size_type index = 0, newindex = 0;
Chris@0 208
Chris@0 209 while ((newindex = path.find(':', index)) < path.size()) {
Chris@0 210 pathList.push_back(path.substr(index, newindex - index).c_str());
Chris@0 211 index = newindex + 1;
Chris@0 212 }
Chris@0 213
Chris@0 214 pathList.push_back(path.substr(index).c_str());
Chris@0 215
Chris@0 216 return pathList;
Chris@0 217 }
Chris@0 218
Chris@0 219
Chris@0 220 #ifdef HAVE_LIBLRDF
Chris@0 221 std::vector<QString>
Chris@0 222 DSSIPluginFactory::getLRDFPath(QString &baseUri)
Chris@0 223 {
Chris@0 224 std::vector<QString> pathList = getPluginPath();
Chris@0 225 std::vector<QString> lrdfPaths;
Chris@0 226
Chris@0 227 lrdfPaths.push_back("/usr/local/share/dssi/rdf");
Chris@0 228 lrdfPaths.push_back("/usr/share/dssi/rdf");
Chris@0 229
Chris@0 230 lrdfPaths.push_back("/usr/local/share/ladspa/rdf");
Chris@0 231 lrdfPaths.push_back("/usr/share/ladspa/rdf");
Chris@0 232
Chris@0 233 for (std::vector<QString>::iterator i = pathList.begin();
Chris@0 234 i != pathList.end(); ++i) {
Chris@0 235 lrdfPaths.push_back(*i + "/rdf");
Chris@0 236 }
Chris@0 237
Chris@0 238 #ifdef DSSI_BASE
Chris@0 239 baseUri = DSSI_BASE;
Chris@0 240 #else
Chris@0 241 baseUri = "http://dssi.sourceforge.net/ontology#";
Chris@0 242 #endif
Chris@0 243
Chris@0 244 return lrdfPaths;
Chris@0 245 }
Chris@0 246 #endif
Chris@0 247
Chris@0 248
Chris@0 249 void
Chris@0 250 DSSIPluginFactory::discoverPlugins(QString soname)
Chris@0 251 {
Chris@0 252 // Note that soname is expected to be a full path at this point,
Chris@0 253 // of a file that is known to exist
Chris@0 254
Chris@0 255 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
Chris@0 256
Chris@0 257 if (!libraryHandle) {
Chris@0 258 std::cerr << "WARNING: DSSIPluginFactory::discoverPlugins: couldn't load plugin library "
Chris@0 259 << soname.toStdString() << " - " << DLERROR() << std::endl;
Chris@0 260 return;
Chris@0 261 }
Chris@0 262
Chris@0 263 DSSI_Descriptor_Function fn = (DSSI_Descriptor_Function)
Chris@0 264 DLSYM(libraryHandle, "dssi_descriptor");
Chris@0 265
Chris@0 266 if (!fn) {
Chris@0 267 std::cerr << "WARNING: DSSIPluginFactory::discoverPlugins: No descriptor function in " << soname.toStdString() << std::endl;
Chris@0 268 return;
Chris@0 269 }
Chris@0 270
Chris@0 271 const DSSI_Descriptor *descriptor = 0;
Chris@0 272
Chris@0 273 int index = 0;
Chris@0 274 while ((descriptor = fn(index))) {
Chris@0 275
Chris@0 276 const LADSPA_Descriptor *ladspaDescriptor = descriptor->LADSPA_Plugin;
Chris@0 277 if (!ladspaDescriptor) {
Chris@0 278 std::cerr << "WARNING: DSSIPluginFactory::discoverPlugins: No LADSPA descriptor for plugin " << index << " in " << soname.toStdString() << std::endl;
Chris@0 279 ++index;
Chris@0 280 continue;
Chris@0 281 }
Chris@0 282
Chris@0 283 #ifdef HAVE_LIBLRDF
Chris@0 284 char *def_uri = 0;
Chris@0 285 lrdf_defaults *defs = 0;
Chris@0 286
Chris@0 287 QString category = m_taxonomy[ladspaDescriptor->UniqueID];
Chris@0 288
Chris@0 289 if (category == "" && ladspaDescriptor->Name != 0) {
Chris@0 290 std::string name = ladspaDescriptor->Name;
Chris@0 291 if (name.length() > 4 &&
Chris@0 292 name.substr(name.length() - 4) == " VST") {
Chris@0 293 if (descriptor->run_synth || descriptor->run_multiple_synths) {
Chris@0 294 category = "VST instruments";
Chris@0 295 } else {
Chris@0 296 category = "VST effects";
Chris@0 297 }
Chris@0 298 m_taxonomy[ladspaDescriptor->UniqueID] = category;
Chris@0 299 }
Chris@0 300 }
Chris@0 301
Chris@0 302 // std::cerr << "Plugin id is " << ladspaDescriptor->UniqueID
Chris@0 303 // << ", category is \"" << (category ? category : QString("(none)"))
Chris@0 304 // << "\", name is " << ladspaDescriptor->Name
Chris@0 305 // << ", label is " << ladspaDescriptor->Label
Chris@0 306 // << std::endl;
Chris@0 307
Chris@0 308 def_uri = lrdf_get_default_uri(ladspaDescriptor->UniqueID);
Chris@0 309 if (def_uri) {
Chris@0 310 defs = lrdf_get_setting_values(def_uri);
Chris@0 311 }
Chris@0 312
Chris@0 313 int controlPortNumber = 1;
Chris@0 314
Chris@0 315 for (unsigned long i = 0; i < ladspaDescriptor->PortCount; i++) {
Chris@0 316
Chris@0 317 if (LADSPA_IS_PORT_CONTROL(ladspaDescriptor->PortDescriptors[i])) {
Chris@0 318
Chris@0 319 if (def_uri && defs) {
Chris@0 320
Chris@0 321 for (int j = 0; j < defs->count; j++) {
Chris@0 322 if (defs->items[j].pid == controlPortNumber) {
Chris@0 323 // 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 " << ladspaDescriptor->PortNames[i] << std::endl;
Chris@0 324 m_portDefaults[ladspaDescriptor->UniqueID][i] =
Chris@0 325 defs->items[j].value;
Chris@0 326 }
Chris@0 327 }
Chris@0 328 }
Chris@0 329
Chris@0 330 ++controlPortNumber;
Chris@0 331 }
Chris@0 332 }
Chris@0 333 #endif // HAVE_LIBLRDF
Chris@0 334
Chris@0 335 QString identifier = PluginIdentifier::createIdentifier
Chris@0 336 ("dssi", soname, ladspaDescriptor->Label);
Chris@0 337 m_identifiers.push_back(identifier);
Chris@0 338
Chris@0 339 ++index;
Chris@0 340 }
Chris@0 341
Chris@0 342 if (DLCLOSE(libraryHandle) != 0) {
Chris@0 343 std::cerr << "WARNING: DSSIPluginFactory::discoverPlugins - can't unload " << libraryHandle << std::endl;
Chris@0 344 return;
Chris@0 345 }
Chris@0 346 }
Chris@0 347
Chris@0 348
Chris@0 349