2
* ladspa_effect.cpp - class for processing LADSPA effects
4
* Copyright (c) 2006-2007 Danny McRae <khjklujn/at/users.sourceforge.net>
6
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public
19
* License along with this program (see COPYING); if not, write to the
20
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
* Boston, MA 02110-1301 USA.
26
#include "qt3support.h"
30
#include <QtGui/QMessageBox>
34
#include "qmessagebox.h"
40
#include "ladspa_effect.h"
42
#include "config_mgr.h"
43
#include "audio_device.h"
44
#include "ladspa_control.h"
45
#include "ladspa_subplugin_features.h"
48
#undef SINGLE_SOURCE_COMPILE
55
plugin::descriptor ladspaeffect_plugin_descriptor =
57
STRINGIFY_PLUGIN_NAME( PLUGIN_NAME ),
59
QT_TRANSLATE_NOOP( "pluginBrowser",
60
"plugin for using arbitrary LADSPA-effects "
62
"Danny McRae <khjklujn/at/users.sourceforge.net>",
65
new QPixmap( PLUGIN_NAME::getIconPixmap( "logo" ) ),
66
new ladspaSubPluginFeatures( plugin::Effect )
72
ladspaEffect::ladspaEffect( const descriptor::subPluginFeatures::key * _key ) :
73
effect( &ladspaeffect_plugin_descriptor, _key ),
75
m_key( subPluginKeyToLadspaKey( _key )
76
/* ladspa_key_t( _cdata->settings.attribute( "label" ),
77
_cdata->settings.attribute( "lib" ) )*/ ),
78
m_ladspa( engine::getLADSPAManager() )
80
if( m_ladspa->getDescription( m_key ) == NULL )
82
QMessageBox::warning( 0, "Effect",
83
"Unknown LADSPA plugin requested: " + m_key.first,
84
QMessageBox::Ok, QMessageBox::NoButton );
89
setPublicName( m_ladspa->getShortName( m_key ) );
91
// Calculate how many processing units are needed.
92
const ch_cnt_t lmms_chnls = engine::getMixer()->audioDev()->channels();
93
m_effectChannels = m_ladspa->getDescription( m_key )->inputChannels;
94
setProcessorCount( lmms_chnls / m_effectChannels );
96
// Categorize the ports, and create the buffers.
97
m_portCount = m_ladspa->getPortCount( m_key );
99
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++ )
102
for( Uint16 port = 0; port < m_portCount; port++ )
104
port_desc_t * p = new portDescription;
106
p->name = m_ladspa->getPortName( m_key, port );
111
// Determine the port's category.
112
if( m_ladspa->isPortAudio( m_key, port ) )
114
// Nasty manual memory management--was having difficulty
115
// with some prepackaged plugins that were segfaulting
116
// during cleanup. It was easier to troubleshoot with the
117
// memory management all taking place in one file.
119
new LADSPA_Data[engine::getMixer()->framesPerPeriod()];
121
if( p->name.toUpper().contains( "IN" ) &&
122
m_ladspa->isPortInput( m_key, port ) )
124
p->rate = CHANNEL_IN;
126
else if( p->name.toUpper().contains( "OUT" ) &&
127
m_ladspa->isPortOutput( m_key, port ) )
129
p->rate = CHANNEL_OUT;
131
else if( m_ladspa->isPortInput( m_key, port ) )
133
p->rate = AUDIO_RATE_INPUT;
137
p->rate = AUDIO_RATE_OUTPUT;
142
p->buffer = new LADSPA_Data;
144
if( m_ladspa->isPortInput( m_key, port ) )
146
p->rate = CONTROL_RATE_INPUT;
150
p->rate = CONTROL_RATE_OUTPUT;
155
if( m_ladspa->isPortToggled( m_key, port ) )
157
p->data_type = TOGGLED;
159
else if( m_ladspa->isInteger( m_key, port ) )
161
p->data_type = INTEGER;
163
else if( p->name.toUpper().contains( "(SECONDS)" ) )
167
int loc = p->name.toUpper().indexOf(
169
p->name.replace( loc, 9, "(ms)" );
171
else if( p->name.toUpper().contains( "(S)" ) )
175
int loc = p->name.toUpper().indexOf( "(S)" );
176
p->name.replace( loc, 3, "(ms)" );
178
else if( p->name.toUpper().contains( "(MS)" ) )
181
int loc = p->name.toUpper().indexOf( "(MS)" );
182
p->name.replace( loc, 4, "(ms)" );
186
p->data_type = FLOAT;
189
// Get the range and default values.
190
p->max = m_ladspa->getUpperBound( m_key, port );
191
if( p->max == NOHINT )
193
p->max = p->name.toUpper() == "GAIN" ? 10.0f :
197
if( m_ladspa->areHintsSampleRateDependent(
200
p->max *= engine::getMixer()->sampleRate();
203
p->min = m_ladspa->getLowerBound( m_key, port );
204
if( p->min == NOHINT )
209
if( m_ladspa->areHintsSampleRateDependent(
212
p->min *= engine::getMixer()->sampleRate();
215
p->def = m_ladspa->getDefaultSetting( m_key, port );
216
if( p->def == NOHINT )
218
if( p->data_type != TOGGLED )
220
p->def = ( p->min + p->max ) / 2.0f;
237
// For convenience, keep a separate list of the ports that are used
238
// to control the processors.
239
if( p->rate == AUDIO_RATE_INPUT ||
240
p->rate == CONTROL_RATE_INPUT )
242
p->control_id = m_controls.count();
243
m_controls.append( p );
246
m_ports.append( ports );
249
// Instantiate the processing units.
250
m_descriptor = m_ladspa->getDescriptor( m_key );
251
if( m_descriptor == NULL )
253
QMessageBox::warning( 0, "Effect",
254
"Can't get LADSPA descriptor function: " + m_key.first,
255
QMessageBox::Ok, QMessageBox::NoButton );
259
if( m_descriptor->run == NULL )
261
QMessageBox::warning( 0, "Effect",
262
"Plugin has no processor: " + m_key.first,
263
QMessageBox::Ok, QMessageBox::NoButton );
266
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++ )
268
LADSPA_Handle effect = m_ladspa->instantiate( m_key,
269
engine::getMixer()->sampleRate() );
272
QMessageBox::warning( 0, "Effect",
273
"Can't get LADSPA instance: " + m_key.first,
274
QMessageBox::Ok, QMessageBox::NoButton );
278
m_handles.append( effect );
282
// Connect the ports.
283
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++ )
285
for( Uint16 port = 0; port < m_portCount; port++ )
287
if( !m_ladspa->connectPort( m_key,
290
m_ports[proc][port]->buffer ) )
292
QMessageBox::warning( 0, "Effect",
293
"Failed to connect port: " + m_key.first,
294
QMessageBox::Ok, QMessageBox::NoButton );
301
// Activate the processing units.
302
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++ )
304
m_ladspa->activate( m_key, m_handles[proc] );
311
ladspaEffect::~ladspaEffect()
318
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++ )
320
m_ladspa->deactivate( m_key, m_handles[proc] );
321
m_ladspa->cleanup( m_key, m_handles[proc] );
322
for( Uint16 port = 0; port < m_portCount; port++ )
324
free( m_ports[proc][port]->buffer );
325
free( m_ports[proc][port] );
327
m_ports[proc].clear();
336
bool FASTCALL ladspaEffect::processAudioBuffer( surroundSampleFrame * _buf,
337
const fpp_t _frames )
339
if( !isOkay() || dontRun() || !isRunning() || isBypassed() )
344
// Copy the LMMS audio buffer to the LADSPA input buffer and initialize
345
// the control ports. Need to change this to handle non-in-place-broken
346
// plugins--would speed things up to use the same buffer for both
348
ch_cnt_t channel = 0;
349
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++)
351
for( Uint16 port = 0; port < m_portCount; port++ )
353
switch( m_ports[proc][port]->rate )
356
for( fpp_t frame = 0;
357
frame < _frames; frame++ )
359
m_ports[proc][port]->buffer[frame] =
360
_buf[frame][channel];
364
case AUDIO_RATE_INPUT:
365
m_ports[proc][port]->value =
366
static_cast<LADSPA_Data>(
367
m_ports[proc][port]->control->getValue() /
368
m_ports[proc][port]->scale );
369
// This only supports control rate ports, so the audio rates are
370
// treated as though they were control rate by setting the
371
// port buffer to all the same value.
372
for( fpp_t frame = 0;
373
frame < _frames; frame++ )
375
m_ports[proc][port]->buffer[frame] =
376
m_ports[proc][port]->value;
379
case CONTROL_RATE_INPUT:
380
if( m_ports[proc][port]->control ==
385
m_ports[proc][port]->value =
386
static_cast<LADSPA_Data>(
387
m_ports[proc][port]->control->getValue() /
388
m_ports[proc][port]->scale );
389
m_ports[proc][port]->buffer[0] =
390
m_ports[proc][port]->value;
393
case AUDIO_RATE_OUTPUT:
394
case CONTROL_RATE_OUTPUT:
402
// Process the buffers.
403
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++ )
405
(m_descriptor->run)(m_handles[proc], _frames);
408
// Copy the LADSPA output buffers to the LMMS buffer.
409
double out_sum = 0.0;
411
for( ch_cnt_t proc = 0; proc < getProcessorCount(); proc++)
413
for( Uint16 port = 0; port < m_portCount; port++ )
415
switch( m_ports[proc][port]->rate )
418
case AUDIO_RATE_INPUT:
419
case CONTROL_RATE_INPUT:
422
for( fpp_t frame = 0;
423
frame < _frames; frame++ )
425
_buf[frame][channel] =
427
_buf[frame][channel] +
429
m_ports[proc][port]->buffer[frame];
431
_buf[frame][channel] *
432
_buf[frame][channel];
436
case AUDIO_RATE_OUTPUT:
437
case CONTROL_RATE_OUTPUT:
445
// Check whether we need to continue processing input. Restart the
446
// counter if the threshold has been exceeded.
447
if( out_sum <= getGate() )
449
incrementBufferCount();
450
if( getBufferCount() > getTimeout() )
461
return( isRunning() );
467
void FASTCALL ladspaEffect::setControl( Uint16 _control, LADSPA_Data _value )
473
m_controls[_control]->value = _value;
482
// neccessary for getting instance out of shared lib
483
plugin * lmms_plugin_main( void * _data )
485
return( new ladspaEffect(
486
static_cast<const plugin::descriptor::subPluginFeatures::key *>(