summaryrefslogtreecommitdiffstats
path: root/src/sound/LADSPAPluginInstance.cpp
blob: fcf35a74300d13fe4b771ceb5c48365c97bf9458 (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
// -*- c-indentation-style:"stroustrup" c-basic-offset: 4 -*-
/*
  Rosegarden
  A sequencer and musical notation editor.
 
  This program is Copyright 2000-2008
  Guillaume Laurent   <glaurent@telegraph-road.org>,
  Chris Cannam        <cannam@all-day-breakfast.com>,
  Richard Bown        <bownie@bownie.com>
 
  The moral right of the authors to claim authorship of this work
  has been asserted.
 
  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.
*/

#include <iostream>
#include <cassert>

#include "LADSPAPluginInstance.h"
#include "LADSPAPluginFactory.h"

#ifdef HAVE_LADSPA

//#define DEBUG_LADSPA 1

namespace Rosegarden
{


LADSPAPluginInstance::LADSPAPluginInstance(PluginFactory *factory,
        InstrumentId instrument,
        TQString identifier,
        int position,
        unsigned long sampleRate,
        size_t blockSize,
        int idealChannelCount,
        const LADSPA_Descriptor* descriptor) :
        RunnablePluginInstance(factory, identifier),
        m_instrument(instrument),
        m_position(position),
        m_instanceCount(0),
        m_descriptor(descriptor),
        m_blockSize(blockSize),
        m_sampleRate(sampleRate),
        m_latencyPort(0),
        m_run(false),
        m_bypassed(false)
{
    init(idealChannelCount);

    m_inputBuffers = new sample_t * [m_instanceCount * m_audioPortsIn.size()];
    m_outputBuffers = new sample_t * [m_instanceCount * m_audioPortsOut.size()];

    for (size_t i = 0; i < m_instanceCount * m_audioPortsIn.size(); ++i) {
        m_inputBuffers[i] = new sample_t[blockSize];
    }
    for (size_t i = 0; i < m_instanceCount * m_audioPortsOut.size(); ++i) {
        m_outputBuffers[i] = new sample_t[blockSize];
    }

    m_ownBuffers = true;

    instantiate(sampleRate);
    if (isOK()) {
        connectPorts();
        activate();
    }
}

LADSPAPluginInstance::LADSPAPluginInstance(PluginFactory *factory,
        InstrumentId instrument,
        TQString identifier,
        int position,
        unsigned long sampleRate,
        size_t blockSize,
        sample_t **inputBuffers,
        sample_t **outputBuffers,
        const LADSPA_Descriptor* descriptor) :
        RunnablePluginInstance(factory, identifier),
        m_instrument(instrument),
        m_position(position),
        m_instanceCount(0),
        m_descriptor(descriptor),
        m_blockSize(blockSize),
        m_inputBuffers(inputBuffers),
        m_outputBuffers(outputBuffers),
        m_ownBuffers(false),
        m_sampleRate(sampleRate),
        m_latencyPort(0),
        m_run(false),
        m_bypassed(false)
{
    init();

    instantiate(sampleRate);
    if (isOK()) {
        connectPorts();
        activate();
    }
}


void
LADSPAPluginInstance::init(int idealChannelCount)
{
#ifdef DEBUG_LADSPA
    std::cerr << "LADSPAPluginInstance::init(" << idealChannelCount << "): plugin has "
    << m_descriptor->PortCount << " ports" << std::endl;
#endif

    // Discover ports numbers and identities
    //
    for (unsigned long i = 0; i < m_descriptor->PortCount; ++i) {
        if (LADSPA_IS_PORT_AUDIO(m_descriptor->PortDescriptors[i])) {
            if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) {
#ifdef DEBUG_LADSPA
                std::cerr << "LADSPAPluginInstance::init: port " << i << " is audio in" << std::endl;
#endif

                m_audioPortsIn.push_back(i);
            } else {
#ifdef DEBUG_LADSPA
                std::cerr << "LADSPAPluginInstance::init: port " << i << " is audio out" << std::endl;
#endif

                m_audioPortsOut.push_back(i);
            }
        } else
            if (LADSPA_IS_PORT_CONTROL(m_descriptor->PortDescriptors[i])) {
                if (LADSPA_IS_PORT_INPUT(m_descriptor->PortDescriptors[i])) {
#ifdef DEBUG_LADSPA
                    std::cerr << "LADSPAPluginInstance::init: port " << i << " is control in" << std::endl;
#endif

                    LADSPA_Data *data = new LADSPA_Data(0.0);
                    m_controlPortsIn.push_back(
                        std::pair<unsigned long, LADSPA_Data*>(i, data));
                } else {
#ifdef DEBUG_LADSPA
                    std::cerr << "LADSPAPluginInstance::init: port " << i << " is control out" << std::endl;
#endif

                    LADSPA_Data *data = new LADSPA_Data(0.0);
                    m_controlPortsOut.push_back(
                        std::pair<unsigned long, LADSPA_Data*>(i, data));
                    if (!strcmp(m_descriptor->PortNames[i], "latency") ||
                            !strcmp(m_descriptor->PortNames[i], "_latency")) {
#ifdef DEBUG_LADSPA
                        std::cerr << "Wooo! We have a latency port!" << std::endl;
#endif

                        m_latencyPort = data;
                    }
                }
            }
#ifdef DEBUG_LADSPA
            else
                std::cerr << "LADSPAPluginInstance::init - "
                << "unrecognised port type" << std::endl;
#endif

    }

    m_instanceCount = 1;

    if (idealChannelCount > 0) {
        if (m_audioPortsIn.size() == 1) {
            // mono plugin: duplicate it if need be
            m_instanceCount = idealChannelCount;
        }
    }
}

size_t
LADSPAPluginInstance::getLatency()
{
    if (m_latencyPort) {
        if (!m_run) {
            for (int i = 0; i < getAudioInputCount(); ++i) {
                for (int j = 0; j < m_blockSize; ++j) {
                    m_inputBuffers[i][j] = 0.f;
                }
            }
            run(RealTime::zeroTime);
	}
        return *m_latencyPort;
    }
    return 0;
}

void
LADSPAPluginInstance::silence()
{
    if (isOK()) {
        deactivate();
        activate();
    }
}

void
LADSPAPluginInstance::setIdealChannelCount(size_t channels)
{
    if (m_audioPortsIn.size() != 1 || channels == m_instanceCount) {
        silence();
        return ;
    }

    if (isOK()) {
        deactivate();
    }

    //!!! don't we need to reallocate inputBuffers and outputBuffers?

    cleanup();
    m_instanceCount = channels;
    instantiate(m_sampleRate);
    if (isOK()) {
        connectPorts();
        activate();
    }
}


LADSPAPluginInstance::~LADSPAPluginInstance()
{
#ifdef DEBUG_LADSPA
    std::cerr << "LADSPAPluginInstance::~LADSPAPluginInstance" << std::endl;
#endif

    if (m_instanceHandles.size() != 0) { // "isOK()"
        deactivate();
    }

    cleanup();

    for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i)
        delete m_controlPortsIn[i].second;

    for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i)
        delete m_controlPortsOut[i].second;

    m_controlPortsIn.clear();
    m_controlPortsOut.clear();

    if (m_ownBuffers) {
        for (size_t i = 0; i < m_audioPortsIn.size(); ++i) {
            delete[] m_inputBuffers[i];
        }
        for (size_t i = 0; i < m_audioPortsOut.size(); ++i) {
            delete[] m_outputBuffers[i];
        }

        delete[] m_inputBuffers;
        delete[] m_outputBuffers;
    }

    m_audioPortsIn.clear();
    m_audioPortsOut.clear();
}


void
LADSPAPluginInstance::instantiate(unsigned long sampleRate)
{
#ifdef DEBUG_LADSPA
    std::cout << "LADSPAPluginInstance::instantiate - plugin unique id = "
    << m_descriptor->UniqueID << std::endl;
#endif

    if (!m_descriptor)
        return ;

    if (!m_descriptor->instantiate) {
        std::cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID
        << ":" << m_descriptor->Label
        << " has no instantiate method!" << std::endl;
        return ;
    }

    for (int i = 0; i < m_instanceCount; ++i) {
        m_instanceHandles.push_back
        (m_descriptor->instantiate(m_descriptor, sampleRate));
    }
}

void
LADSPAPluginInstance::activate()
{
    if (!m_descriptor || !m_descriptor->activate)
        return ;

    for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
            hi != m_instanceHandles.end(); ++hi) {
        m_descriptor->activate(*hi);
    }
}

void
LADSPAPluginInstance::connectPorts()
{
    if (!m_descriptor || !m_descriptor->connect_port)
        return ;

    assert(sizeof(LADSPA_Data) == sizeof(float));
    assert(sizeof(sample_t) == sizeof(float));

    int inbuf = 0, outbuf = 0;

    for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
            hi != m_instanceHandles.end(); ++hi) {

        for (unsigned int i = 0; i < m_audioPortsIn.size(); ++i) {
            m_descriptor->connect_port(*hi,
                                       m_audioPortsIn[i],
                                       (LADSPA_Data *)m_inputBuffers[inbuf]);
            ++inbuf;
        }

        for (unsigned int i = 0; i < m_audioPortsOut.size(); ++i) {
            m_descriptor->connect_port(*hi,
                                       m_audioPortsOut[i],
                                       (LADSPA_Data *)m_outputBuffers[outbuf]);
            ++outbuf;
        }

        // If there is more than one instance, they all share the same
        // control port ins (and outs, for the moment, because we
        // don't actually do anything with the outs anyway -- but they
        // do have to be connected as the plugin can't know if they're
        // not and will write to them anyway).

        for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
            m_descriptor->connect_port(*hi,
                                       m_controlPortsIn[i].first,
                                       m_controlPortsIn[i].second);
        }

        for (unsigned int i = 0; i < m_controlPortsOut.size(); ++i) {
            m_descriptor->connect_port(*hi,
                                       m_controlPortsOut[i].first,
                                       m_controlPortsOut[i].second);
        }
    }
}

void
LADSPAPluginInstance::setPortValue(unsigned int portNumber, float value)
{
    for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
        if (m_controlPortsIn[i].first == portNumber) {
            LADSPAPluginFactory *f = dynamic_cast<LADSPAPluginFactory *>(m_factory);
            if (f) {
                if (value < f->getPortMinimum(m_descriptor, portNumber)) {
                    value = f->getPortMinimum(m_descriptor, portNumber);
                }
                if (value > f->getPortMaximum(m_descriptor, portNumber)) {
                    value = f->getPortMaximum(m_descriptor, portNumber);
                }
            }
            (*m_controlPortsIn[i].second) = value;
        }
    }
}

float
LADSPAPluginInstance::getPortValue(unsigned int portNumber)
{
    for (unsigned int i = 0; i < m_controlPortsIn.size(); ++i) {
        if (m_controlPortsIn[i].first == portNumber) {
            return (*m_controlPortsIn[i].second);
        }
    }

    return 0.0;
}

void
LADSPAPluginInstance::run(const RealTime &)
{
    if (!m_descriptor || !m_descriptor->run)
        return ;

    for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
            hi != m_instanceHandles.end(); ++hi) {
        m_descriptor->run(*hi, m_blockSize);
    }

    m_run = true;
}

void
LADSPAPluginInstance::deactivate()
{
    if (!m_descriptor || !m_descriptor->deactivate)
        return ;

    for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
            hi != m_instanceHandles.end(); ++hi) {
        m_descriptor->deactivate(*hi);
    }
}

void
LADSPAPluginInstance::cleanup()
{
    if (!m_descriptor)
        return ;

    if (!m_descriptor->cleanup) {
        std::cerr << "Bad plugin: plugin id " << m_descriptor->UniqueID
        << ":" << m_descriptor->Label
        << " has no cleanup method!" << std::endl;
        return ;
    }

    for (std::vector<LADSPA_Handle>::iterator hi = m_instanceHandles.begin();
            hi != m_instanceHandles.end(); ++hi) {
        m_descriptor->cleanup(*hi);
    }

    m_instanceHandles.clear();
}



}

#endif // HAVE_LADSPA