File: DSSIPluginFactory.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (441 lines) | stat: -rw-r--r-- 14,052 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

/*
   This is a modified version of a source file from the 
   Rosegarden MIDI and audio sequencer and notation editor.
   This file copyright 2000-2006 Chris Cannam.
*/

#include "DSSIPluginFactory.h"
#include <iostream>

#include <QString>

#include "DSSIPluginInstance.h"
#include "PluginIdentifier.h"

#include <cstdlib>

#include "base/Profiler.h"

//!!!
#include "plugin/plugins/SamplePlayer.h"

#include "system/System.h"

#ifdef HAVE_LRDF
#include "lrdf.h"
#endif // HAVE_LRDF

using std::string;

namespace sv {

DSSIPluginFactory::DSSIPluginFactory() :
    LADSPAPluginFactory()
{
    m_hostDescriptor.DSSI_API_Version = 2;
    m_hostDescriptor.request_transport_information = nullptr;
    m_hostDescriptor.request_midi_send = DSSIPluginInstance::requestMidiSend;
    m_hostDescriptor.request_non_rt_thread = DSSIPluginInstance::requestNonRTThread;
    m_hostDescriptor.midi_send = DSSIPluginInstance::midiSend;
}

DSSIPluginFactory::~DSSIPluginFactory()
{
    // nothing else to do here either
}

void
DSSIPluginFactory::enumeratePlugins(std::vector<QString> &list)
{
    Profiler profiler("DSSIPluginFactory::enumeratePlugins");

    for (std::vector<QString>::iterator i = m_identifiers.begin();
         i != m_identifiers.end(); ++i) {

        const DSSI_Descriptor *ddesc = getDSSIDescriptor(*i);
        if (!ddesc) continue;

        const LADSPA_Descriptor *descriptor = ddesc->LADSPA_Plugin;
        if (!descriptor) continue;
        
//        SVDEBUG << "DSSIPluginFactory::enumeratePlugins: Name " << (descriptor->Name ? descriptor->Name : "NONE" ) << endl;

        list.push_back(*i);
        list.push_back(descriptor->Name);
        list.push_back(QString("%1").arg(descriptor->UniqueID));
        list.push_back(descriptor->Label);
        list.push_back(descriptor->Maker);
        list.push_back(descriptor->Copyright);
        list.push_back((ddesc->run_synth || ddesc->run_multiple_synths) ? "true" : "false");
        list.push_back(ddesc->run_multiple_synths ? "true" : "false");
        list.push_back(m_taxonomy[*i]);
        list.push_back(QString("%1").arg(descriptor->PortCount));

        for (int p = 0; p < (int)descriptor->PortCount; ++p) {

            int type = 0;
            if (LADSPA_IS_PORT_CONTROL(descriptor->PortDescriptors[p])) {
                type |= PortType::Control;
            } else {
                type |= PortType::Audio;
            }
            if (LADSPA_IS_PORT_INPUT(descriptor->PortDescriptors[p])) {
                type |= PortType::Input;
            } else {
                type |= PortType::Output;
            }

            list.push_back(QString("%1").arg(p));
            list.push_back(descriptor->PortNames[p]);
            list.push_back(QString("%1").arg(type));
            list.push_back(QString("%1").arg(getPortDisplayHint(descriptor, p)));
            list.push_back(QString("%1").arg(getPortMinimum(descriptor, p)));
            list.push_back(QString("%1").arg(getPortMaximum(descriptor, p)));
            list.push_back(QString("%1").arg(getPortDefault(descriptor, p)));
        }
    }

    unloadUnusedLibraries();
}
        
std::shared_ptr<RealTimePluginInstance> 
DSSIPluginFactory::instantiatePlugin(QString identifier,
                                     int instrument,
                                     int position,
                                     sv_samplerate_t sampleRate,
                                     int blockSize,
                                     int channels)
{
    Profiler profiler("DSSIPluginFactory::instantiatePlugin");

    const DSSI_Descriptor *descriptor = getDSSIDescriptor(identifier);

    if (descriptor) {

        auto instance =
            std::shared_ptr<RealTimePluginInstance>
            (new DSSIPluginInstance
             (this, instrument, identifier, position,
              sampleRate, blockSize, channels, descriptor));

        m_instances.insert(instance);

        return instance;
    }

    return nullptr;
}

const DSSI_Descriptor *
DSSIPluginFactory::getDSSIDescriptor(QString identifier)
{
    QString type, soname, label;
    PluginIdentifier::parseIdentifier(identifier, type, soname, label);

    if (soname == PluginIdentifier::BUILTIN_PLUGIN_SONAME) {
        if (label == "sample_player") {
            const DSSI_Descriptor *descriptor = SamplePlayer::getDescriptor(0);
            if (descriptor) {
                descriptor->receive_host_descriptor(&m_hostDescriptor);
            }
            return descriptor;
        } else {
            return nullptr;
        }
    }
    
    bool firstInLibrary = false;

    if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
        loadLibrary(soname);
        if (m_libraryHandles.find(soname) == m_libraryHandles.end()) {
            SVCERR << "WARNING: DSSIPluginFactory::getDSSIDescriptor: loadLibrary failed for " << soname << endl;
            return nullptr;
        }
        firstInLibrary = true;
    }

    void *libraryHandle = m_libraryHandles[soname];

    DSSI_Descriptor_Function fn = (DSSI_Descriptor_Function)
        DLSYM(libraryHandle, "dssi_descriptor");

    if (!fn) {
        SVCERR << "WARNING: DSSIPluginFactory::getDSSIDescriptor: No descriptor function in library " << soname << endl;
        return nullptr;
    }

    const DSSI_Descriptor *descriptor = nullptr;
    
    int index = 0;
    while ((descriptor = fn(index))) {
        if (descriptor->LADSPA_Plugin->Label == label) {
            if (firstInLibrary && (descriptor->DSSI_API_Version >= 2)) {
                descriptor->receive_host_descriptor(&m_hostDescriptor);
            }
            return descriptor;
        }
        ++index;
    }

    SVCERR << "WARNING: DSSIPluginFactory::getDSSIDescriptor: No such plugin as " << label << " in library " << soname << endl;

    return nullptr;
}

const LADSPA_Descriptor *
DSSIPluginFactory::getLADSPADescriptor(QString identifier)
{
    const DSSI_Descriptor *dssiDescriptor = getDSSIDescriptor(identifier);
    if (dssiDescriptor) return dssiDescriptor->LADSPA_Plugin;
    else return nullptr;
}


std::vector<QString>
DSSIPluginFactory::getPluginPath()
{
    std::vector<QString> pathList;
    string path;

    (void)getEnvUtf8("DSSI_PATH", path);

    if (path == "") {

        path = DEFAULT_DSSI_PATH;

        string home;
        if (getEnvUtf8("HOME", home)) {
            string::size_type f;
            while ((f = path.find("$HOME")) != string::npos &&
                   f < path.length()) {
                path.replace(f, 5, home);
            }
        }

#ifdef _WIN32
        string pfiles;
        if (!getEnvUtf8("ProgramFiles", pfiles)) {
            pfiles = "C:\\Program Files";
        }

        string::size_type f;
        while ((f = path.find("%ProgramFiles%")) != string::npos &&
               f < path.length()) {
            path.replace(f, 14, pfiles);
        }
#endif
    }

    string::size_type index = 0, newindex = 0;

    while ((newindex = path.find(PATH_SEPARATOR, index)) < path.size()) {
        pathList.push_back(path.substr(index, newindex - index).c_str());
        index = newindex + 1;
    }
    
    pathList.push_back(path.substr(index).c_str());

    return pathList;
}


std::vector<QString>
DSSIPluginFactory::getLRDFPath(QString &baseUri)
{
    std::vector<QString> lrdfPaths;

#ifdef HAVE_LRDF
    std::vector<QString> pathList = getPluginPath();

    lrdfPaths.push_back("/usr/local/share/dssi/rdf");
    lrdfPaths.push_back("/usr/share/dssi/rdf");

    lrdfPaths.push_back("/usr/local/share/ladspa/rdf");
    lrdfPaths.push_back("/usr/share/ladspa/rdf");

    for (std::vector<QString>::iterator i = pathList.begin();
         i != pathList.end(); ++i) {
        lrdfPaths.push_back(*i + "/rdf");
    }

#ifdef DSSI_BASE
    baseUri = DSSI_BASE;
#else
    baseUri = "http://dssi.sourceforge.net/ontology#";
#endif
#else
    // avoid unused parameter
    baseUri = "";
#endif

    return lrdfPaths;
}    


void
DSSIPluginFactory::discoverPluginsFrom(QString soname)
{
    Profiler profiler("DSSIPluginFactory::discoverPlugins");

    // Note that soname is expected to be a full path at this point,
    // of a file that is known to exist

    void *libraryHandle = DLOPEN(soname, RTLD_LAZY);

    if (!libraryHandle) {
        SVCERR << "WARNING: DSSIPluginFactory::discoverPlugins: couldn't load plugin library "
                  << soname << " - " << DLERROR() << endl;
        return;
    }

    DSSI_Descriptor_Function fn = (DSSI_Descriptor_Function)
        DLSYM(libraryHandle, "dssi_descriptor");

    if (!fn) {
        SVCERR << "WARNING: DSSIPluginFactory::discoverPlugins: No descriptor function in " << soname << endl;
        return;
    }

    const DSSI_Descriptor *descriptor = nullptr;
    
    int index = 0;
    while ((descriptor = fn(index))) {

        const LADSPA_Descriptor *ladspaDescriptor = descriptor->LADSPA_Plugin;
        if (!ladspaDescriptor) {
            SVCERR << "WARNING: DSSIPluginFactory::discoverPlugins: No LADSPA descriptor for plugin " << index << " in " << soname << endl;
            ++index;
            continue;
        }

        RealTimePluginDescriptor rtd;
        rtd.name = ladspaDescriptor->Name;
        rtd.label = ladspaDescriptor->Label;
        rtd.maker = ladspaDescriptor->Maker;
        rtd.copyright = ladspaDescriptor->Copyright;
        rtd.category = "";
        rtd.isSynth = (descriptor->run_synth ||
                        descriptor->run_multiple_synths);
        rtd.parameterCount = 0;
        rtd.audioInputPortCount = 0;
        rtd.audioOutputPortCount = 0;
        rtd.controlOutputPortCount = 0;

        QString identifier = PluginIdentifier::createIdentifier
            ("dssi", soname, ladspaDescriptor->Label);

#ifdef HAVE_LRDF
        char *def_uri = nullptr;
        lrdf_defaults *defs = nullptr;
                
        QString category = m_taxonomy[identifier];

        if (category == "" && m_lrdfTaxonomy[ladspaDescriptor->UniqueID] != "") {
            m_taxonomy[identifier] = m_lrdfTaxonomy[ladspaDescriptor->UniqueID];
            category = m_taxonomy[identifier];
        }

        if (category == "") {
            string name = rtd.name;
            if (name.length() > 4 &&
                name.substr(name.length() - 4) == " VST") {
                if (descriptor->run_synth || descriptor->run_multiple_synths) {
                    category = "VST instruments";
                } else {
                    category = "VST effects";
                }
                m_taxonomy[identifier] = category;
            }
        }

        rtd.category = category.toStdString();
        
//        SVCERR << "Plugin id is " << ladspaDescriptor->UniqueID
//                  << ", identifier is \"" << identifier
//                  << "\", category is \"" << category
//                  << "\", name is " << ladspaDescriptor->Name
//                  << ", label is " << ladspaDescriptor->Label
//                  << endl;
        
        def_uri = lrdf_get_default_uri(ladspaDescriptor->UniqueID);
        if (def_uri) {
            defs = lrdf_get_setting_values(def_uri);
        }
        
        unsigned int controlPortNumber = 1;
        
        for (int i = 0; i < (int)ladspaDescriptor->PortCount; i++) {
            
            if (LADSPA_IS_PORT_CONTROL(ladspaDescriptor->PortDescriptors[i])) {
                
                if (def_uri && defs) {
                    
                    for (int j = 0; j < (int)defs->count; j++) {
                        if (defs->items[j].pid == controlPortNumber) {
//                            SVCERR << "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] << endl;
                            m_portDefaults[ladspaDescriptor->UniqueID][i] =
                                defs->items[j].value;
                        }
                    }
                }
                
                ++controlPortNumber;
            }
        }
#endif // HAVE_LRDF

        for (unsigned long i = 0; i < ladspaDescriptor->PortCount; i++) {
            if (LADSPA_IS_PORT_CONTROL(ladspaDescriptor->PortDescriptors[i])) {
                if (LADSPA_IS_PORT_INPUT(ladspaDescriptor->PortDescriptors[i])) {
                    ++rtd.parameterCount;
                } else {
                    if (strcmp(ladspaDescriptor->PortNames[i], "latency") &&
                        strcmp(ladspaDescriptor->PortNames[i], "_latency")) {
                        ++rtd.controlOutputPortCount;
                        rtd.controlOutputPortNames.push_back
                            (ladspaDescriptor->PortNames[i]);
                    }
                }
            } else {
                if (LADSPA_IS_PORT_INPUT(ladspaDescriptor->PortDescriptors[i])) {
                    ++rtd.audioInputPortCount;
                } else if (LADSPA_IS_PORT_OUTPUT(ladspaDescriptor->PortDescriptors[i])) {
                    ++rtd.audioOutputPortCount;
                }
            }
        }

        m_identifiers.push_back(identifier);

        m_libraries[identifier] = soname;

        m_rtDescriptors[identifier] = rtd;

        ++index;
    }

    if (DLCLOSE(libraryHandle) != 0) {
        SVCERR << "WARNING: DSSIPluginFactory::discoverPlugins - can't unload " << libraryHandle << endl;
        return;
    }
}

} // end namespace sv