-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtractorLadspaPlugin.cpp
558 lines (470 loc) · 14.9 KB
/
qtractorLadspaPlugin.cpp
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
// qtractorLadspaPlugin.cpp
//
/****************************************************************************
Copyright (C) 2005-2014, rncbc aka Rui Nuno Capela. All rights reserved.
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*****************************************************************************/
#include "qtractorAbout.h"
#ifdef CONFIG_LADSPA
#include "qtractorLadspaPlugin.h"
#include <math.h>
//----------------------------------------------------------------------------
// qtractorLadspaPluginType -- LADSPA plugin type instance.
//
// Derived methods.
bool qtractorLadspaPluginType::open (void)
{
// Do we have a descriptor already?
if (m_pLadspaDescriptor == NULL)
m_pLadspaDescriptor = ladspa_descriptor(file(), index());
if (m_pLadspaDescriptor == NULL)
return false;
#ifdef CONFIG_DEBUG
qDebug("qtractorLadspaPluginType[%p]::open() filename=\"%s\" index=%lu",
this, filename().toUtf8().constData(), index());
#endif
// Retrieve plugin type names.
m_sName = m_pLadspaDescriptor->Name;
m_sLabel = m_pLadspaDescriptor->Label;
// Retrieve plugin unique identifier.
m_iUniqueID = m_pLadspaDescriptor->UniqueID;
// Compute and cache port counts...
m_iControlIns = 0;
m_iControlOuts = 0;
m_iAudioIns = 0;
m_iAudioOuts = 0;
m_iMidiIns = 0;
m_iMidiOuts = 0;
for (unsigned long i = 0; i < m_pLadspaDescriptor->PortCount; ++i) {
const LADSPA_PortDescriptor portType
= m_pLadspaDescriptor->PortDescriptors[i];
if (LADSPA_IS_PORT_INPUT(portType)) {
if (LADSPA_IS_PORT_AUDIO(portType))
++m_iAudioIns;
else
if (LADSPA_IS_PORT_CONTROL(portType))
++m_iControlIns;
}
else
if (LADSPA_IS_PORT_OUTPUT(portType)) {
if (LADSPA_IS_PORT_AUDIO(portType))
++m_iAudioOuts;
else
if (LADSPA_IS_PORT_CONTROL(portType))
++m_iControlOuts;
}
}
// Cache flags.
m_bRealtime = LADSPA_IS_HARD_RT_CAPABLE(m_pLadspaDescriptor->Properties);
// Done.
return true;
}
void qtractorLadspaPluginType::close (void)
{
m_pLadspaDescriptor = NULL;
}
// Factory method (static)
qtractorLadspaPluginType *qtractorLadspaPluginType::createType (
qtractorPluginFile *pFile, unsigned long iIndex )
{
// Sanity check...
if (pFile == NULL)
return NULL;
// Retrieve descriptor if any...
const LADSPA_Descriptor *pLadspaDescriptor
= ladspa_descriptor(pFile, iIndex);
if (pLadspaDescriptor == NULL)
return NULL;
// Yep, most probably its a valid plugin descriptor...
return new qtractorLadspaPluginType(pFile, iIndex,
qtractorPluginType::Ladspa, pLadspaDescriptor);
}
// Descriptor method (static)
const LADSPA_Descriptor *qtractorLadspaPluginType::ladspa_descriptor (
qtractorPluginFile *pFile, unsigned long iIndex )
{
// Retrieve the descriptor function, if any...
LADSPA_Descriptor_Function pfnLadspaDescriptor
= (LADSPA_Descriptor_Function) pFile->resolve("ladspa_descriptor");
if (pfnLadspaDescriptor == NULL)
return NULL;
// Retrieve descriptor if any...
return (*pfnLadspaDescriptor)(iIndex);
}
// Instance cached-deferred accesors.
const QString& qtractorLadspaPluginType::aboutText (void)
{
if (m_sAboutText.isEmpty() && m_pLadspaDescriptor) {
if (m_pLadspaDescriptor->Maker) {
if (!m_sAboutText.isEmpty())
m_sAboutText += '\n';
m_sAboutText += QObject::tr("Author: ");
m_sAboutText += m_pLadspaDescriptor->Maker;
}
if (m_pLadspaDescriptor->Copyright) {
if (!m_sAboutText.isEmpty())
m_sAboutText += '\n';
m_sAboutText += QObject::tr("Copyright: ");
m_sAboutText += m_pLadspaDescriptor->Copyright;
}
}
return m_sAboutText;
}
//----------------------------------------------------------------------------
// qtractorLadspaPlugin -- LADSPA plugin instance.
//
// Constructors.
qtractorLadspaPlugin::qtractorLadspaPlugin ( qtractorPluginList *pList,
qtractorLadspaPluginType *pLadspaType )
: qtractorPlugin(pList, pLadspaType), m_phInstances(NULL),
m_piControlOuts(NULL), m_pfControlOuts(NULL),
m_piAudioIns(NULL), m_piAudioOuts(NULL)
{
#ifdef CONFIG_DEBUG
qDebug("qtractorLadspaPlugin[%p] filename=\"%s\" index=%lu typeHint=%d",
this, type()->filename().toUtf8().constData(),
type()->index(), int(type()->typeHint()));
#endif
// Get some structural data first...
const LADSPA_Descriptor *pLadspaDescriptor
= pLadspaType->ladspa_descriptor();
if (pLadspaDescriptor) {
unsigned short iControlOuts = pLadspaType->controlOuts();
unsigned short iAudioIns = pLadspaType->audioIns();
unsigned short iAudioOuts = pLadspaType->audioOuts();
if (iAudioIns > 0)
m_piAudioIns = new unsigned long [iAudioIns];
if (iAudioOuts > 0)
m_piAudioOuts = new unsigned long [iAudioOuts];
if (iControlOuts > 0) {
m_piControlOuts = new unsigned long [iControlOuts];
m_pfControlOuts = new float [iControlOuts];
}
iControlOuts = iAudioIns = iAudioOuts = 0;
for (unsigned long i = 0; i < pLadspaDescriptor->PortCount; ++i) {
const LADSPA_PortDescriptor portType
= pLadspaDescriptor->PortDescriptors[i];
if (LADSPA_IS_PORT_INPUT(portType)) {
if (LADSPA_IS_PORT_AUDIO(portType))
m_piAudioIns[iAudioIns++] = i;
else
if (LADSPA_IS_PORT_CONTROL(portType))
addParam(new qtractorLadspaPluginParam(this, i));
}
else
if (LADSPA_IS_PORT_OUTPUT(portType)) {
if (LADSPA_IS_PORT_AUDIO(portType))
m_piAudioOuts[iAudioOuts++] = i;
else
if (LADSPA_IS_PORT_CONTROL(portType)) {
m_piControlOuts[iControlOuts] = i;
m_pfControlOuts[iControlOuts] = 0.0f;
++iControlOuts;
}
}
}
// FIXME: instantiate each instance properly...
qtractorLadspaPlugin::setChannels(channels());
}
}
// Destructor.
qtractorLadspaPlugin::~qtractorLadspaPlugin (void)
{
// Cleanup all plugin instances...
setChannels(0);
// Free up all the rest...
if (m_piAudioOuts)
delete [] m_piAudioOuts;
if (m_piAudioIns)
delete [] m_piAudioIns;
if (m_piControlOuts)
delete [] m_piControlOuts;
if (m_pfControlOuts)
delete [] m_pfControlOuts;
}
// Channel/instance number accessors.
void qtractorLadspaPlugin::setChannels ( unsigned short iChannels )
{
// Check our type...
qtractorPluginType *pType = type();
if (pType == NULL)
return;
// Estimate the (new) number of instances...
unsigned short iOldInstances = instances();
unsigned short iInstances
= pType->instances(iChannels, list()->isMidi());
// Now see if instance count changed anyhow...
if (iInstances == iOldInstances)
return;
const LADSPA_Descriptor *pLadspaDescriptor = ladspa_descriptor();
if (pLadspaDescriptor == NULL)
return;
// Gotta go for a while...
bool bActivated = isActivated();
setActivated(false);
// Set new instance number...
setInstances(iInstances);
if (m_phInstances) {
if (pLadspaDescriptor->cleanup) {
for (unsigned short i = 0; i < iOldInstances; ++i)
(*pLadspaDescriptor->cleanup)(m_phInstances[i]);
}
delete [] m_phInstances;
m_phInstances = NULL;
}
// Bail out, if none are about to be created...
if (iInstances < 1) {
setActivated(bActivated);
return;
}
#ifdef CONFIG_DEBUG
qDebug("qtractorLadspaPlugin[%p]::setChannels(%u) instances=%u",
this, iChannels, iInstances);
#endif
// We'll need output control (not dummy anymore) port indexes...
unsigned short iControlOuts = pType->controlOuts();
// Allocate new instances...
m_phInstances = new LADSPA_Handle [iInstances];
for (unsigned short i = 0; i < iInstances; ++i) {
// Instantiate them properly first...
LADSPA_Handle handle
= (*pLadspaDescriptor->instantiate)(pLadspaDescriptor, sampleRate());
// Connect all existing input control ports...
const qtractorPlugin::Params& params = qtractorPlugin::params();
qtractorPlugin::Params::ConstIterator param = params.constBegin();
const qtractorPlugin::Params::ConstIterator& param_end = params.constEnd();
for ( ; param != param_end; ++param) {
qtractorPluginParam *pParam = param.value();
// Just in case the plugin decides
// to set the port value at this time...
float *pfValue = pParam->subject()->data();
float fValue = *pfValue;
(*pLadspaDescriptor->connect_port)(handle,
pParam->index(), pfValue);
// Make new one the default and restore port value...
pParam->setDefaultValue(*pfValue);
*pfValue = fValue;
}
// Connect all existing output control ports...
for (unsigned short j = 0; j < iControlOuts; ++j) {
(*pLadspaDescriptor->connect_port)(handle,
m_piControlOuts[j], &m_pfControlOuts[j]);
}
// This is it...
m_phInstances[i] = handle;
}
// (Re)issue all configuration as needed...
realizeConfigs();
realizeValues();
// But won't need it anymore.
releaseConfigs();
releaseValues();
// (Re)activate instance if necessary...
setActivated(bActivated);
}
// Specific accessors.
const LADSPA_Descriptor *qtractorLadspaPlugin::ladspa_descriptor (void) const
{
qtractorLadspaPluginType *pLadspaType
= static_cast<qtractorLadspaPluginType *> (type());
return (pLadspaType ? pLadspaType->ladspa_descriptor() : NULL);
}
LADSPA_Handle qtractorLadspaPlugin::ladspa_handle ( unsigned short iInstance ) const
{
return (m_phInstances ? m_phInstances[iInstance] : NULL);
}
// Do the actual activation.
void qtractorLadspaPlugin::activate (void)
{
const LADSPA_Descriptor *pLadspaDescriptor = ladspa_descriptor();
if (pLadspaDescriptor == NULL)
return;
if (m_phInstances && pLadspaDescriptor->activate) {
for (unsigned short i = 0; i < instances(); ++i)
(*pLadspaDescriptor->activate)(m_phInstances[i]);
}
}
// Do the actual deactivation.
void qtractorLadspaPlugin::deactivate (void)
{
const LADSPA_Descriptor *pLadspaDescriptor = ladspa_descriptor();
if (pLadspaDescriptor == NULL)
return;
if (m_phInstances && pLadspaDescriptor->deactivate) {
for (unsigned short i = 0; i < instances(); ++i)
(*pLadspaDescriptor->deactivate)(m_phInstances[i]);
}
}
// The main plugin processing procedure.
void qtractorLadspaPlugin::process (
float **ppIBuffer, float **ppOBuffer, unsigned int nframes )
{
if (m_phInstances == NULL)
return;
const LADSPA_Descriptor *pLadspaDescriptor = ladspa_descriptor();
if (pLadspaDescriptor == NULL)
return;
// We'll cross channels over instances...
const unsigned short iInstances = instances();
const unsigned short iChannels = channels();
const unsigned short iAudioIns = audioIns();
const unsigned short iAudioOuts = audioOuts();
unsigned short iIChannel = 0;
unsigned short iOChannel = 0;
unsigned short i, j;
// For each plugin instance...
for (i = 0; i < iInstances; ++i) {
LADSPA_Handle handle = m_phInstances[i];
// For each instance audio input port...
for (j = 0; j < iAudioIns; ++j) {
(*pLadspaDescriptor->connect_port)(handle,
m_piAudioIns[j], ppIBuffer[iIChannel]);
if (++iIChannel >= iChannels)
iIChannel = 0;
}
// For each instance audio output port...
for (j = 0; j < iAudioOuts; ++j) {
(*pLadspaDescriptor->connect_port)(handle,
m_piAudioOuts[j], ppOBuffer[iOChannel]);
if (++iOChannel >= iChannels)
iOChannel = 0;
}
// Make it run...
(*pLadspaDescriptor->run)(handle, nframes);
// Wrap channels?...
if (iIChannel < iChannels - 1)
++iIChannel;
if (iOChannel < iChannels - 1)
++iOChannel;
}
}
//----------------------------------------------------------------------------
// qtractorLadspaPluginParam -- LADSPA plugin control input port instance.
//
// Constructors.
qtractorLadspaPluginParam::qtractorLadspaPluginParam (
qtractorLadspaPlugin *pLadspaPlugin, unsigned long iIndex )
: qtractorPluginParam(pLadspaPlugin, iIndex)
{
const LADSPA_Descriptor *pLadspaDescriptor
= pLadspaPlugin->ladspa_descriptor();
const LADSPA_PortRangeHint *pPortRangeHint
= &(pLadspaDescriptor->PortRangeHints[iIndex]);
m_portHints = pPortRangeHint->HintDescriptor;
// Set nominal parameter name...
setName(pLadspaDescriptor->PortNames[iIndex]);
// Initialize range values...
float fMinValue = 0.0f;
if (isBoundedBelow()) {
fMinValue = pPortRangeHint->LowerBound;
if (isSampleRate())
fMinValue *= float(pLadspaPlugin->sampleRate());
}
setMinValue(fMinValue);
float fMaxValue = 1.0f;
if (isBoundedAbove()) {
fMaxValue = pPortRangeHint->UpperBound;
if (isSampleRate())
fMaxValue *= float(pLadspaPlugin->sampleRate());
}
setMaxValue(fMaxValue);
// Port default value...
float fDefaultValue = value();
if (isDefaultValue()) {
switch (m_portHints & LADSPA_HINT_DEFAULT_MASK) {
case LADSPA_HINT_DEFAULT_MINIMUM:
fDefaultValue = fMinValue;
break;
case LADSPA_HINT_DEFAULT_LOW:
if (isLogarithmic()) {
fDefaultValue = ::expf(
::logf(fMinValue) * 0.75f + ::logf(fMaxValue) * 0.25f);
} else {
fDefaultValue = (fMinValue * 0.75f + fMaxValue * 0.25f);
}
break;
case LADSPA_HINT_DEFAULT_MIDDLE:
if (isLogarithmic()) {
fDefaultValue = ::sqrt(fMinValue * fMaxValue);
} else {
fDefaultValue = (fMinValue + fMaxValue) * 0.5f;
}
break;
case LADSPA_HINT_DEFAULT_HIGH:
if (isLogarithmic()) {
fDefaultValue = ::expf(
::logf(fMinValue) * 0.25f + ::logf(fMaxValue) * 0.75f);
} else {
fDefaultValue = (fMinValue * 0.25f + fMaxValue * 0.75f);
}
break;
case LADSPA_HINT_DEFAULT_MAXIMUM:
fDefaultValue = fMaxValue;
break;
case LADSPA_HINT_DEFAULT_0:
fDefaultValue = 0.0f;
break;
case LADSPA_HINT_DEFAULT_1:
fDefaultValue = 1.0f;
break;
case LADSPA_HINT_DEFAULT_100:
fDefaultValue = 100.0f;
break;
case LADSPA_HINT_DEFAULT_440:
fDefaultValue = 440.0f;
break;
}
}
setDefaultValue(fDefaultValue);
// Initialize port value...
reset();
}
// Destructor.
qtractorLadspaPluginParam::~qtractorLadspaPluginParam (void)
{
}
// Port range hints predicate methods.
bool qtractorLadspaPluginParam::isBoundedBelow (void) const
{
return LADSPA_IS_HINT_BOUNDED_BELOW(m_portHints);
}
bool qtractorLadspaPluginParam::isBoundedAbove (void) const
{
return LADSPA_IS_HINT_BOUNDED_ABOVE(m_portHints);
}
bool qtractorLadspaPluginParam::isDefaultValue (void) const
{
return LADSPA_IS_HINT_HAS_DEFAULT(m_portHints);
}
bool qtractorLadspaPluginParam::isLogarithmic (void) const
{
return LADSPA_IS_HINT_LOGARITHMIC(m_portHints);
}
bool qtractorLadspaPluginParam::isSampleRate (void) const
{
return LADSPA_IS_HINT_SAMPLE_RATE(m_portHints);
}
bool qtractorLadspaPluginParam::isInteger (void) const
{
return LADSPA_IS_HINT_INTEGER(m_portHints);
}
bool qtractorLadspaPluginParam::isToggled (void) const
{
return LADSPA_IS_HINT_TOGGLED(m_portHints);
}
bool qtractorLadspaPluginParam::isDisplay (void) const
{
return false;
}
#endif // CONFIG_LADSPA
// end of qtractorLadspaPlugin.cpp