summaryrefslogtreecommitdiff
path: root/distrho/src/DistrhoPluginLADSPA+DSSI.cpp
blob: 4ac645bd3ff6a23609fcb87f17edd08c1da5e592 (plain)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * DISTRHO Plugin Framework (DPF)
 * Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any purpose with
 * or without fee is hereby granted, provided that the above copyright notice and this
 * permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "DistrhoPluginInternal.hpp"

#if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
# error Cannot use MIDI Output with LADSPA or DSSI
#endif

#ifdef DISTRHO_PLUGIN_TARGET_DSSI
# include "dssi/dssi.h"
# if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
#  error DSSI does not support MIDI output
# endif
#else
# include "ladspa/ladspa.h"
# if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
#  error Cannot use MIDI with LADSPA
# endif
# if DISTRHO_PLUGIN_WANT_STATE
#  warning LADSPA cannot handle states
# endif
#endif

#if DISTRHO_PLUGIN_WANT_TIMEPOS
# warning LADSPA/DSSI does not support TimePos
#endif

START_NAMESPACE_DISTRHO

// -----------------------------------------------------------------------

class PluginLadspaDssi
{
public:
    PluginLadspaDssi()
        : fPlugin(nullptr, nullptr),
          fPortControls(nullptr),
          fLastControlValues(nullptr)
    {
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
        for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
            fPortAudioIns[i] = nullptr;
#else
        fPortAudioIns = nullptr;
#endif

#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
        for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
            fPortAudioOuts[i] = nullptr;
#else
        fPortAudioOuts = nullptr;
#endif

        if (const uint32_t count = fPlugin.getParameterCount())
        {
            fPortControls      = new LADSPA_Data*[count];
            fLastControlValues = new LADSPA_Data[count];

            for (uint32_t i=0; i < count; ++i)
            {
                fPortControls[i] = nullptr;
                fLastControlValues[i] = fPlugin.getParameterValue(i);
            }
        }
        else
        {
            fPortControls = nullptr;
            fLastControlValues = nullptr;
        }

#if DISTRHO_PLUGIN_WANT_LATENCY
        fPortLatency = nullptr;
#endif
    }

    ~PluginLadspaDssi() noexcept
    {
        if (fPortControls != nullptr)
        {
            delete[] fPortControls;
            fPortControls = nullptr;
        }

        if (fLastControlValues != nullptr)
        {
            delete[] fLastControlValues;
            fLastControlValues = nullptr;
        }
    }

    // -------------------------------------------------------------------

    void ladspa_activate()
    {
        fPlugin.activate();
    }

    void ladspa_deactivate()
    {
        fPlugin.deactivate();
    }

    // -------------------------------------------------------------------

    void ladspa_connect_port(const ulong port, LADSPA_Data* const dataLocation) noexcept
    {
        ulong index = 0;

#if DISTRHO_PLUGIN_NUM_INPUTS > 0
        for (ulong i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
        {
            if (port == index++)
            {
                fPortAudioIns[i] = dataLocation;
                return;
            }
        }
#endif

#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
        for (ulong i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
        {
            if (port == index++)
            {
                fPortAudioOuts[i] = dataLocation;
                return;
            }
        }
#endif

#if DISTRHO_PLUGIN_WANT_LATENCY
        if (port == index++)
        {
            fPortLatency = dataLocation;
            return;
        }
#endif

        for (ulong i=0, count=fPlugin.getParameterCount(); i < count; ++i)
        {
            if (port == index++)
            {
                fPortControls[i] = dataLocation;
                return;
            }
        }
    }

    // -------------------------------------------------------------------

#ifdef DISTRHO_PLUGIN_TARGET_DSSI
    void ladspa_run(const ulong sampleCount)
    {
        dssi_run_synth(sampleCount, nullptr, 0);
    }

    void dssi_run_synth(const ulong sampleCount, snd_seq_event_t* const events, const ulong eventCount)
#else
    void ladspa_run(const ulong sampleCount)
#endif
    {
        // pre-roll
        if (sampleCount == 0)
            return updateParameterOutputsAndTriggers();

        // Check for updated parameters
        float curValue;

        for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
        {
            if (fPortControls[i] == nullptr)
                continue;

            curValue = *fPortControls[i];

            if (fLastControlValues[i] != curValue && ! fPlugin.isParameterOutput(i))
            {
                fLastControlValues[i] = curValue;
                fPlugin.setParameterValue(i, curValue);
            }
        }

#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
        // Get MIDI Events
        uint32_t  midiEventCount = 0;
        MidiEvent midiEvents[eventCount];

        for (uint32_t i=0, j; i < eventCount; ++i)
        {
            const snd_seq_event_t& seqEvent(events[i]);

            // FIXME
            if (seqEvent.data.note.channel > 0xF || seqEvent.data.control.channel > 0xF)
                continue;

            switch (seqEvent.type)
            {
            case SND_SEQ_EVENT_NOTEOFF:
                j = midiEventCount++;
                midiEvents[j].frame   = seqEvent.time.tick;
                midiEvents[j].size    = 3;
                midiEvents[j].data[0] = 0x80 + seqEvent.data.note.channel;
                midiEvents[j].data[1] = seqEvent.data.note.note;
                midiEvents[j].data[2] = 0;
                midiEvents[j].data[3] = 0;
                break;
            case SND_SEQ_EVENT_NOTEON:
                j = midiEventCount++;
                midiEvents[j].frame   = seqEvent.time.tick;
                midiEvents[j].size    = 3;
                midiEvents[j].data[0] = 0x90 + seqEvent.data.note.channel;
                midiEvents[j].data[1] = seqEvent.data.note.note;
                midiEvents[j].data[2] = seqEvent.data.note.velocity;
                midiEvents[j].data[3] = 0;
                break;
            case SND_SEQ_EVENT_KEYPRESS:
                j = midiEventCount++;
                midiEvents[j].frame   = seqEvent.time.tick;
                midiEvents[j].size    = 3;
                midiEvents[j].data[0] = 0xA0 + seqEvent.data.note.channel;
                midiEvents[j].data[1] = seqEvent.data.note.note;
                midiEvents[j].data[2] = seqEvent.data.note.velocity;
                midiEvents[j].data[3] = 0;
                break;
            case SND_SEQ_EVENT_CONTROLLER:
                j = midiEventCount++;
                midiEvents[j].frame   = seqEvent.time.tick;
                midiEvents[j].size    = 3;
                midiEvents[j].data[0] = 0xB0 + seqEvent.data.control.channel;
                midiEvents[j].data[1] = seqEvent.data.control.param;
                midiEvents[j].data[2] = seqEvent.data.control.value;
                midiEvents[j].data[3] = 0;
                break;
            case SND_SEQ_EVENT_CHANPRESS:
                j = midiEventCount++;
                midiEvents[j].frame   = seqEvent.time.tick;
                midiEvents[j].size    = 2;
                midiEvents[j].data[0] = 0xD0 + seqEvent.data.control.channel;
                midiEvents[j].data[1] = seqEvent.data.control.value;
                midiEvents[j].data[2] = 0;
                midiEvents[j].data[3] = 0;
                break;
            case SND_SEQ_EVENT_PITCHBEND:
                j = midiEventCount++;
                midiEvents[j].frame   = seqEvent.time.tick;
                midiEvents[j].size    = 3;
                midiEvents[j].data[0] = 0xE0 + seqEvent.data.control.channel;
                uint16_t tempvalue = seqEvent.data.control.value + 8192;
                midiEvents[j].data[1] = tempvalue & 0x7F;
                midiEvents[j].data[2] = tempvalue >> 7;
                midiEvents[j].data[3] = 0;
                break;
            }
        }

        fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount, midiEvents, midiEventCount);
#else
        fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount);
#endif

        updateParameterOutputsAndTriggers();

#if defined(DISTRHO_PLUGIN_TARGET_DSSI) && ! DISTRHO_PLUGIN_WANT_MIDI_INPUT
        return; // unused
        (void)events; (void)eventCount;
#endif
    }

    // -------------------------------------------------------------------

#ifdef DISTRHO_PLUGIN_TARGET_DSSI
# if DISTRHO_PLUGIN_WANT_STATE
    char* dssi_configure(const char* const key, const char* const value)
    {
        if (std::strncmp(key, DSSI_RESERVED_CONFIGURE_PREFIX, std::strlen(DSSI_RESERVED_CONFIGURE_PREFIX) == 0))
            return nullptr;
        if (std::strncmp(key, DSSI_GLOBAL_CONFIGURE_PREFIX, std::strlen(DSSI_GLOBAL_CONFIGURE_PREFIX) == 0))
            return nullptr;

        fPlugin.setState(key, value);
        return nullptr;
    }
# endif

# if DISTRHO_PLUGIN_WANT_PROGRAMS
    const DSSI_Program_Descriptor* dssi_get_program(const ulong index)
    {
        if (index >= fPlugin.getProgramCount())
            return nullptr;

        static DSSI_Program_Descriptor desc;

        desc.Bank    = index / 128;
        desc.Program = index % 128;
        desc.Name    = fPlugin.getProgramName(index);

        return &desc;
    }

    void dssi_select_program(const ulong bank, const ulong program)
    {
        const ulong realProgram(bank * 128 + program);

        DISTRHO_SAFE_ASSERT_RETURN(realProgram < fPlugin.getProgramCount(),);

        fPlugin.loadProgram(realProgram);

        // Update control inputs
        for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
        {
            if (fPlugin.isParameterOutput(i))
                continue;

            fLastControlValues[i] = fPlugin.getParameterValue(i);

            if (fPortControls[i] != nullptr)
                *fPortControls[i] = fLastControlValues[i];
        }
    }
# endif

    int dssi_get_midi_controller_for_port(const ulong port) noexcept
    {
        const uint32_t parameterOffset = fPlugin.getParameterOffset();

        if (port > parameterOffset)
            return DSSI_NONE;

        const uint8_t midiCC = fPlugin.getParameterMidiCC(port-parameterOffset);

        if (midiCC == 0 || midiCC == 32 || midiCC >= 0x78)
            return DSSI_NONE;

        return DSSI_CC(midiCC);
    }
#endif

    // -------------------------------------------------------------------

private:
    PluginExporter fPlugin;

    // LADSPA ports
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
    const LADSPA_Data*  fPortAudioIns[DISTRHO_PLUGIN_NUM_INPUTS];
#else
    const LADSPA_Data** fPortAudioIns;
#endif
#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
    LADSPA_Data*  fPortAudioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
#else
    LADSPA_Data** fPortAudioOuts;
#endif
    LADSPA_Data** fPortControls;
#if DISTRHO_PLUGIN_WANT_LATENCY
    LADSPA_Data*  fPortLatency;
#endif

    // Temporary data
    LADSPA_Data* fLastControlValues;

    // -------------------------------------------------------------------

    void updateParameterOutputsAndTriggers()
    {
        float value;

        for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
        {
            if (fPlugin.isParameterOutput(i))
            {
                value = fLastControlValues[i] = fPlugin.getParameterValue(i);

                if (fPortControls[i] != nullptr)
                    *fPortControls[i] = value;
            }
            else if ((fPlugin.getParameterHints(i) & kParameterIsTrigger) == kParameterIsTrigger)
            {
                // NOTE: no trigger support in LADSPA control ports, simulate it here
                const float value = fPlugin.getParameterRanges(i).def;

                if (d_isEqual(value, fPlugin.getParameterValue(i)))
                    continue;

                fLastControlValues[i] = value;
                fPlugin.setParameterValue(i, value);

                if (fPortControls[i] != nullptr)
                    *fPortControls[i] = value;
            }
        }

#if DISTRHO_PLUGIN_WANT_LATENCY
        if (fPortLatency != nullptr)
            *fPortLatency = fPlugin.getLatency();
#endif
    }
};

// -----------------------------------------------------------------------

static LADSPA_Handle ladspa_instantiate(const LADSPA_Descriptor*, ulong sampleRate)
{
    if (d_lastBufferSize == 0)
        d_lastBufferSize = 2048;
    d_lastSampleRate = sampleRate;

    return new PluginLadspaDssi();
}

#define instancePtr ((PluginLadspaDssi*)instance)

static void ladspa_connect_port(LADSPA_Handle instance, ulong port, LADSPA_Data* dataLocation)
{
    instancePtr->ladspa_connect_port(port, dataLocation);
}

static void ladspa_activate(LADSPA_Handle instance)
{
    instancePtr->ladspa_activate();
}

static void ladspa_run(LADSPA_Handle instance, ulong sampleCount)
{
    instancePtr->ladspa_run(sampleCount);
}

static void ladspa_deactivate(LADSPA_Handle instance)
{
    instancePtr->ladspa_deactivate();
}

static void ladspa_cleanup(LADSPA_Handle instance)
{
    delete instancePtr;
}

#ifdef DISTRHO_PLUGIN_TARGET_DSSI
# if DISTRHO_PLUGIN_WANT_STATE
static char* dssi_configure(LADSPA_Handle instance, const char* key, const char* value)
{
    return instancePtr->dssi_configure(key, value);
}
# endif

# if DISTRHO_PLUGIN_WANT_PROGRAMS
static const DSSI_Program_Descriptor* dssi_get_program(LADSPA_Handle instance, ulong index)
{
    return instancePtr->dssi_get_program(index);
}

static void dssi_select_program(LADSPA_Handle instance, ulong bank, ulong program)
{
    instancePtr->dssi_select_program(bank, program);
}
# endif

static int dssi_get_midi_controller_for_port(LADSPA_Handle instance, ulong port)
{
    return instancePtr->dssi_get_midi_controller_for_port(port);
}

# if DISTRHO_PLUGIN_WANT_MIDI_INPUT
static void dssi_run_synth(LADSPA_Handle instance, ulong sampleCount, snd_seq_event_t* events, ulong eventCount)
{
    instancePtr->dssi_run_synth(sampleCount, events, eventCount);
}
# endif
#endif

#undef instancePtr

// -----------------------------------------------------------------------

static LADSPA_Descriptor sLadspaDescriptor = {
    /* UniqueID   */ 0,
    /* Label      */ nullptr,
#if DISTRHO_PLUGIN_IS_RT_SAFE
    /* Properties */ LADSPA_PROPERTY_HARD_RT_CAPABLE,
#else
    /* Properties */ 0x0,
#endif
    /* Name       */ nullptr,
    /* Maker      */ nullptr,
    /* Copyright  */ nullptr,
    /* PortCount  */ 0,
    /* PortDescriptors    */ nullptr,
    /* PortNames          */ nullptr,
    /* PortRangeHints     */ nullptr,
    /* ImplementationData */ nullptr,
    ladspa_instantiate,
    ladspa_connect_port,
    ladspa_activate,
    ladspa_run,
    /* run_adding */          nullptr,
    /* set_run_adding_gain */ nullptr,
    ladspa_deactivate,
    ladspa_cleanup
};

#ifdef DISTRHO_PLUGIN_TARGET_DSSI
static DSSI_Descriptor sDssiDescriptor = {
    1,
    &sLadspaDescriptor,
# if DISTRHO_PLUGIN_WANT_STATE
    dssi_configure,
# else
    /* configure                    */ nullptr,
# endif
# if DISTRHO_PLUGIN_WANT_PROGRAMS
    dssi_get_program,
    dssi_select_program,
# else
    /* get_program                  */ nullptr,
    /* select_program               */ nullptr,
# endif
    dssi_get_midi_controller_for_port,
# if DISTRHO_PLUGIN_WANT_MIDI_INPUT
    dssi_run_synth,
# else
    /* run_synth                    */ nullptr,
# endif
    /* run_synth_adding             */ nullptr,
    /* run_multiple_synths          */ nullptr,
    /* run_multiple_synths_adding   */ nullptr,
    nullptr, nullptr
};
#endif

// -----------------------------------------------------------------------

class DescriptorInitializer
{
public:
    DescriptorInitializer()
    {
        // Create dummy plugin to get data from
        d_lastBufferSize = 512;
        d_lastSampleRate = 44100.0;
        PluginExporter plugin(nullptr, nullptr);
        d_lastBufferSize = 0;
        d_lastSampleRate = 0.0;

        // Get port count, init
        ulong port = 0;
        ulong portCount = DISTRHO_PLUGIN_NUM_INPUTS + DISTRHO_PLUGIN_NUM_OUTPUTS + plugin.getParameterCount();
#if DISTRHO_PLUGIN_WANT_LATENCY
        portCount += 1;
#endif
        const char** const     portNames       = new const char*[portCount];
        LADSPA_PortDescriptor* portDescriptors = new LADSPA_PortDescriptor[portCount];
        LADSPA_PortRangeHint*  portRangeHints  = new LADSPA_PortRangeHint [portCount];

        // Set ports
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
        for (ulong i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i, ++port)
        {
            const AudioPort& aport(plugin.getAudioPort(true, i));

            portNames[port]       = strdup(aport.name);
            portDescriptors[port] = LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT;

            portRangeHints[port].HintDescriptor = 0x0;
            portRangeHints[port].LowerBound = 0.0f;
            portRangeHints[port].UpperBound = 1.0f;
        }
#endif

#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
        for (ulong i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i, ++port)
        {
            const AudioPort& aport(plugin.getAudioPort(false, i));

            portNames[port]       = strdup(aport.name);
            portDescriptors[port] = LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT;

            portRangeHints[port].HintDescriptor = 0x0;
            portRangeHints[port].LowerBound = 0.0f;
            portRangeHints[port].UpperBound = 1.0f;
        }
#endif

#if DISTRHO_PLUGIN_WANT_LATENCY
        // Set latency port
        portNames[port]       = strdup("_latency");
        portDescriptors[port] = LADSPA_PORT_CONTROL | LADSPA_PORT_OUTPUT;
        portRangeHints[port].HintDescriptor = LADSPA_HINT_SAMPLE_RATE|LADSPA_HINT_INTEGER;
        portRangeHints[port].LowerBound     = 0.0f;
        portRangeHints[port].UpperBound     = 1.0f;
        ++port;
#endif

        for (ulong i=0, count=plugin.getParameterCount(); i < count; ++i, ++port)
        {
            portNames[port]       = strdup((const char*)plugin.getParameterName(i));
            portDescriptors[port] = LADSPA_PORT_CONTROL;

            if (plugin.isParameterOutput(i))
                portDescriptors[port] |= LADSPA_PORT_OUTPUT;
            else
                portDescriptors[port] |= LADSPA_PORT_INPUT;

            {
                const ParameterRanges& ranges(plugin.getParameterRanges(i));
                const float defValue(ranges.def);

                portRangeHints[port].HintDescriptor = LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
                portRangeHints[port].LowerBound     = ranges.min;
                portRangeHints[port].UpperBound     = ranges.max;

                if (defValue == 0.0f)
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_0;
                else if (defValue == 1.0f)
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_1;
                else if (defValue == 100.0f)
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_100;
                else if (defValue == 440.0f)
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_440;
                else if (ranges.min == defValue)
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_MINIMUM;
                else if (ranges.max == defValue)
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_MAXIMUM;
                else
                {
                    const float middleValue =  ranges.min/2.0f + ranges.max/2.0f;
                    const float middleLow   = (ranges.min/2.0f + middleValue/2.0f)/2.0f + middleValue/2.0f;
                    const float middleHigh  = (ranges.max/2.0f + middleValue/2.0f)/2.0f + middleValue/2.0f;

                    if (defValue < middleLow)
                        portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_LOW;
                    else if (defValue > middleHigh)
                        portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_HIGH;
                    else
                        portRangeHints[port].HintDescriptor |= LADSPA_HINT_DEFAULT_MIDDLE;
                }
            }

            {
                const uint32_t hints(plugin.getParameterHints(i));

                if (hints & kParameterIsBoolean)
                {
                    portRangeHints[port].HintDescriptor |= LADSPA_HINT_TOGGLED;
                }
                else
                {
                    if (hints & kParameterIsInteger)
                        portRangeHints[port].HintDescriptor |= LADSPA_HINT_INTEGER;
                    if (hints & kParameterIsLogarithmic)
                        portRangeHints[port].HintDescriptor |= LADSPA_HINT_LOGARITHMIC;
                }
            }
        }

        // Set data
        sLadspaDescriptor.UniqueID  = plugin.getUniqueId();
        sLadspaDescriptor.Label     = strdup(plugin.getLabel());
        sLadspaDescriptor.Name      = strdup(plugin.getName());
        sLadspaDescriptor.Maker     = strdup(plugin.getMaker());
        sLadspaDescriptor.Copyright = strdup(plugin.getLicense());
        sLadspaDescriptor.PortCount = portCount;
        sLadspaDescriptor.PortNames = portNames;
        sLadspaDescriptor.PortDescriptors = portDescriptors;
        sLadspaDescriptor.PortRangeHints  = portRangeHints;
    }

    ~DescriptorInitializer()
    {
        if (sLadspaDescriptor.Label != nullptr)
        {
            std::free((void*)sLadspaDescriptor.Label);
            sLadspaDescriptor.Label = nullptr;
        }

        if (sLadspaDescriptor.Name != nullptr)
        {
            std::free((void*)sLadspaDescriptor.Name);
            sLadspaDescriptor.Name = nullptr;
        }

        if (sLadspaDescriptor.Maker != nullptr)
        {
            std::free((void*)sLadspaDescriptor.Maker);
            sLadspaDescriptor.Maker = nullptr;
        }

        if (sLadspaDescriptor.Copyright != nullptr)
        {
            std::free((void*)sLadspaDescriptor.Copyright);
            sLadspaDescriptor.Copyright = nullptr;
        }

        if (sLadspaDescriptor.PortDescriptors != nullptr)
        {
            delete[] sLadspaDescriptor.PortDescriptors;
            sLadspaDescriptor.PortDescriptors = nullptr;
        }

        if (sLadspaDescriptor.PortRangeHints != nullptr)
        {
            delete[] sLadspaDescriptor.PortRangeHints;
            sLadspaDescriptor.PortRangeHints = nullptr;
        }

        if (sLadspaDescriptor.PortNames != nullptr)
        {
            for (ulong i=0; i < sLadspaDescriptor.PortCount; ++i)
            {
                if (sLadspaDescriptor.PortNames[i] != nullptr)
                    std::free((void*)sLadspaDescriptor.PortNames[i]);
            }

            delete[] sLadspaDescriptor.PortNames;
            sLadspaDescriptor.PortNames = nullptr;
        }
    }
};

static DescriptorInitializer sDescInit;

// -----------------------------------------------------------------------

END_NAMESPACE_DISTRHO

DISTRHO_PLUGIN_EXPORT
const LADSPA_Descriptor* ladspa_descriptor(ulong index)
{
    USE_NAMESPACE_DISTRHO
    return (index == 0) ? &sLadspaDescriptor : nullptr;
}

#ifdef DISTRHO_PLUGIN_TARGET_DSSI
DISTRHO_PLUGIN_EXPORT
const DSSI_Descriptor* dssi_descriptor(ulong index)
{
    USE_NAMESPACE_DISTRHO
    return (index == 0) ? &sDssiDescriptor : nullptr;
}
#endif

// -----------------------------------------------------------------------