LadspaEffect.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * LadspaEffect.cpp - class for processing LADSPA effects
  3. *
  4. * Copyright (c) 2006-2008 Danny McRae <khjklujn/at/users.sourceforge.net>
  5. * Copyright (c) 2009-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include <QMessageBox>
  26. #include "LadspaEffect.h"
  27. #include "DataFile.h"
  28. #include "AudioDevice.h"
  29. #include "ConfigManager.h"
  30. #include "Ladspa2LMMS.h"
  31. #include "LadspaControl.h"
  32. #include "LadspaSubPluginFeatures.h"
  33. #include "Mixer.h"
  34. #include "EffectChain.h"
  35. #include "AutomationPattern.h"
  36. #include "ControllerConnection.h"
  37. #include "MemoryManager.h"
  38. #include "ValueBuffer.h"
  39. #include "Song.h"
  40. #include "embed.cpp"
  41. extern "C"
  42. {
  43. Plugin::Descriptor PLUGIN_EXPORT ladspaeffect_plugin_descriptor =
  44. {
  45. STRINGIFY( PLUGIN_NAME ),
  46. "LADSPA",
  47. QT_TRANSLATE_NOOP( "pluginBrowser",
  48. "plugin for using arbitrary LADSPA-effects "
  49. "inside LMMS." ),
  50. "Danny McRae <khjklujn/at/users.sourceforge.net>",
  51. 0x0100,
  52. Plugin::Effect,
  53. new PluginPixmapLoader( "logo" ),
  54. NULL,
  55. new LadspaSubPluginFeatures( Plugin::Effect )
  56. } ;
  57. }
  58. LadspaEffect::LadspaEffect( Model * _parent,
  59. const Descriptor::SubPluginFeatures::Key * _key ) :
  60. Effect( &ladspaeffect_plugin_descriptor, _parent, _key ),
  61. m_controls( NULL ),
  62. m_maxSampleRate( 0 ),
  63. m_key( LadspaSubPluginFeatures::subPluginKeyToLadspaKey( _key ) )
  64. {
  65. Ladspa2LMMS * manager = Engine::getLADSPAManager();
  66. if( manager->getDescription( m_key ) == NULL )
  67. {
  68. Engine::getSong()->collectError(tr( "Unknown LADSPA plugin %1 requested." ).arg(
  69. m_key.second ) );
  70. setOkay( false );
  71. return;
  72. }
  73. setDisplayName( manager->getShortName( m_key ) );
  74. pluginInstantiation();
  75. connect( Engine::mixer(), SIGNAL( sampleRateChanged() ),
  76. this, SLOT( changeSampleRate() ) );
  77. }
  78. LadspaEffect::~LadspaEffect()
  79. {
  80. pluginDestruction();
  81. }
  82. void LadspaEffect::changeSampleRate()
  83. {
  84. DataFile dataFile( DataFile::EffectSettings );
  85. m_controls->saveState( dataFile, dataFile.content() );
  86. LadspaControls * controls = m_controls;
  87. m_controls = NULL;
  88. m_pluginMutex.lock();
  89. pluginDestruction();
  90. pluginInstantiation();
  91. m_pluginMutex.unlock();
  92. controls->effectModelChanged( m_controls );
  93. delete controls;
  94. m_controls->restoreState( dataFile.content().firstChild().toElement() );
  95. // the IDs of re-created controls have been saved and now need to be
  96. // resolved again
  97. AutomationPattern::resolveAllIDs();
  98. }
  99. bool LadspaEffect::processAudioBuffer( sampleFrame * _buf,
  100. const fpp_t _frames )
  101. {
  102. m_pluginMutex.lock();
  103. if( !isOkay() || dontRun() || !isRunning() || !isEnabled() )
  104. {
  105. m_pluginMutex.unlock();
  106. return( false );
  107. }
  108. int frames = _frames;
  109. sampleFrame * o_buf = NULL;
  110. sampleFrame sBuf [_frames];
  111. if( m_maxSampleRate < Engine::mixer()->processingSampleRate() )
  112. {
  113. o_buf = _buf;
  114. _buf = &sBuf[0];
  115. sampleDown( o_buf, _buf, m_maxSampleRate );
  116. frames = _frames * m_maxSampleRate /
  117. Engine::mixer()->processingSampleRate();
  118. }
  119. // Copy the LMMS audio buffer to the LADSPA input buffer and initialize
  120. // the control ports.
  121. ch_cnt_t channel = 0;
  122. for( ch_cnt_t proc = 0; proc < processorCount(); ++proc )
  123. {
  124. for( int port = 0; port < m_portCount; ++port )
  125. {
  126. port_desc_t * pp = m_ports.at( proc ).at( port );
  127. switch( pp->rate )
  128. {
  129. case CHANNEL_IN:
  130. for( fpp_t frame = 0;
  131. frame < frames; ++frame )
  132. {
  133. pp->buffer[frame] =
  134. _buf[frame][channel];
  135. }
  136. ++channel;
  137. break;
  138. case AUDIO_RATE_INPUT:
  139. {
  140. ValueBuffer * vb = pp->control->valueBuffer();
  141. if( vb )
  142. {
  143. memcpy( pp->buffer, vb->values(), frames * sizeof(float) );
  144. }
  145. else
  146. {
  147. pp->value = static_cast<LADSPA_Data>(
  148. pp->control->value() / pp->scale );
  149. // This only supports control rate ports, so the audio rates are
  150. // treated as though they were control rate by setting the
  151. // port buffer to all the same value.
  152. for( fpp_t frame = 0;
  153. frame < frames; ++frame )
  154. {
  155. pp->buffer[frame] =
  156. pp->value;
  157. }
  158. }
  159. break;
  160. }
  161. case CONTROL_RATE_INPUT:
  162. if( pp->control == NULL )
  163. {
  164. break;
  165. }
  166. pp->value = static_cast<LADSPA_Data>(
  167. pp->control->value() / pp->scale );
  168. pp->buffer[0] =
  169. pp->value;
  170. break;
  171. case CHANNEL_OUT:
  172. case AUDIO_RATE_OUTPUT:
  173. case CONTROL_RATE_OUTPUT:
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. }
  180. // Process the buffers.
  181. for( ch_cnt_t proc = 0; proc < processorCount(); ++proc )
  182. {
  183. (m_descriptor->run)( m_handles[proc], frames );
  184. }
  185. // Copy the LADSPA output buffers to the LMMS buffer.
  186. double out_sum = 0.0;
  187. channel = 0;
  188. const float d = dryLevel();
  189. const float w = wetLevel();
  190. for( ch_cnt_t proc = 0; proc < processorCount(); ++proc )
  191. {
  192. for( int port = 0; port < m_portCount; ++port )
  193. {
  194. port_desc_t * pp = m_ports.at( proc ).at( port );
  195. switch( pp->rate )
  196. {
  197. case CHANNEL_IN:
  198. case AUDIO_RATE_INPUT:
  199. case CONTROL_RATE_INPUT:
  200. break;
  201. case CHANNEL_OUT:
  202. for( fpp_t frame = 0;
  203. frame < frames; ++frame )
  204. {
  205. _buf[frame][channel] = d * _buf[frame][channel] + w * pp->buffer[frame];
  206. out_sum += _buf[frame][channel] * _buf[frame][channel];
  207. }
  208. ++channel;
  209. break;
  210. case AUDIO_RATE_OUTPUT:
  211. case CONTROL_RATE_OUTPUT:
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. }
  218. if( o_buf != NULL )
  219. {
  220. sampleBack( _buf, o_buf, m_maxSampleRate );
  221. }
  222. checkGate( out_sum / frames );
  223. bool is_running = isRunning();
  224. m_pluginMutex.unlock();
  225. return( is_running );
  226. }
  227. void LadspaEffect::setControl( int _control, LADSPA_Data _value )
  228. {
  229. if( !isOkay() )
  230. {
  231. return;
  232. }
  233. m_portControls[_control]->value = _value;
  234. }
  235. void LadspaEffect::pluginInstantiation()
  236. {
  237. m_maxSampleRate = maxSamplerate( displayName() );
  238. Ladspa2LMMS * manager = Engine::getLADSPAManager();
  239. // Calculate how many processing units are needed.
  240. const ch_cnt_t lmms_chnls = Engine::mixer()->audioDev()->channels();
  241. int effect_channels = manager->getDescription( m_key )->inputChannels;
  242. setProcessorCount( lmms_chnls / effect_channels );
  243. // get inPlaceBroken property
  244. m_inPlaceBroken = manager->isInplaceBroken( m_key );
  245. // Categorize the ports, and create the buffers.
  246. m_portCount = manager->getPortCount( m_key );
  247. int inputch = 0;
  248. int outputch = 0;
  249. LADSPA_Data * inbuf [2];
  250. inbuf[0] = NULL;
  251. inbuf[1] = NULL;
  252. for( ch_cnt_t proc = 0; proc < processorCount(); proc++ )
  253. {
  254. multi_proc_t ports;
  255. for( int port = 0; port < m_portCount; port++ )
  256. {
  257. port_desc_t * p = new PortDescription;
  258. p->name = manager->getPortName( m_key, port );
  259. p->proc = proc;
  260. p->port_id = port;
  261. p->control = NULL;
  262. p->buffer = NULL;
  263. // Determine the port's category.
  264. if( manager->isPortAudio( m_key, port ) )
  265. {
  266. if( p->name.toUpper().contains( "IN" ) &&
  267. manager->isPortInput( m_key, port ) )
  268. {
  269. p->rate = CHANNEL_IN;
  270. p->buffer = MM_ALLOC( LADSPA_Data, Engine::mixer()->framesPerPeriod() );
  271. inbuf[ inputch ] = p->buffer;
  272. inputch++;
  273. }
  274. else if( p->name.toUpper().contains( "OUT" ) &&
  275. manager->isPortOutput( m_key, port ) )
  276. {
  277. p->rate = CHANNEL_OUT;
  278. if( ! m_inPlaceBroken && inbuf[ outputch ] )
  279. {
  280. p->buffer = inbuf[ outputch ];
  281. outputch++;
  282. }
  283. else
  284. {
  285. p->buffer = MM_ALLOC( LADSPA_Data, Engine::mixer()->framesPerPeriod() );
  286. m_inPlaceBroken = true;
  287. }
  288. }
  289. else if( manager->isPortInput( m_key, port ) )
  290. {
  291. p->rate = AUDIO_RATE_INPUT;
  292. p->buffer = MM_ALLOC( LADSPA_Data, Engine::mixer()->framesPerPeriod() );
  293. }
  294. else
  295. {
  296. p->rate = AUDIO_RATE_OUTPUT;
  297. p->buffer = MM_ALLOC( LADSPA_Data, Engine::mixer()->framesPerPeriod() );
  298. }
  299. }
  300. else
  301. {
  302. p->buffer = MM_ALLOC( LADSPA_Data, 1 );
  303. if( manager->isPortInput( m_key, port ) )
  304. {
  305. p->rate = CONTROL_RATE_INPUT;
  306. }
  307. else
  308. {
  309. p->rate = CONTROL_RATE_OUTPUT;
  310. }
  311. }
  312. p->scale = 1.0f;
  313. if( manager->isPortToggled( m_key, port ) )
  314. {
  315. p->data_type = TOGGLED;
  316. }
  317. else if( manager->isInteger( m_key, port ) )
  318. {
  319. p->data_type = INTEGER;
  320. }
  321. else if( p->name.toUpper().contains( "(SECONDS)" ) )
  322. {
  323. p->data_type = TIME;
  324. p->scale = 1000.0f;
  325. int loc = p->name.toUpper().indexOf(
  326. "(SECONDS)" );
  327. p->name.replace( loc, 9, "(ms)" );
  328. }
  329. else if( p->name.toUpper().contains( "(S)" ) )
  330. {
  331. p->data_type = TIME;
  332. p->scale = 1000.0f;
  333. int loc = p->name.toUpper().indexOf( "(S)" );
  334. p->name.replace( loc, 3, "(ms)" );
  335. }
  336. else if( p->name.toUpper().contains( "(MS)" ) )
  337. {
  338. p->data_type = TIME;
  339. int loc = p->name.toUpper().indexOf( "(MS)" );
  340. p->name.replace( loc, 4, "(ms)" );
  341. }
  342. else
  343. {
  344. p->data_type = FLOATING;
  345. }
  346. // Get the range and default values.
  347. p->max = manager->getUpperBound( m_key, port );
  348. if( p->max == NOHINT )
  349. {
  350. p->max = p->name.toUpper() == "GAIN" ? 10.0f :
  351. 1.0f;
  352. }
  353. if( manager->areHintsSampleRateDependent(
  354. m_key, port ) )
  355. {
  356. p->max *= m_maxSampleRate;
  357. }
  358. p->min = manager->getLowerBound( m_key, port );
  359. if( p->min == NOHINT )
  360. {
  361. p->min = 0.0f;
  362. }
  363. if( manager->areHintsSampleRateDependent(
  364. m_key, port ) )
  365. {
  366. p->min *= m_maxSampleRate;
  367. }
  368. p->def = manager->getDefaultSetting( m_key, port );
  369. if( p->def == NOHINT )
  370. {
  371. if( p->data_type != TOGGLED )
  372. {
  373. p->def = ( p->min + p->max ) / 2.0f;
  374. }
  375. else
  376. {
  377. p->def = 1.0f;
  378. }
  379. }
  380. else if( manager->areHintsSampleRateDependent( m_key, port ) )
  381. {
  382. p->def *= m_maxSampleRate;
  383. }
  384. p->max *= p->scale;
  385. p->min *= p->scale;
  386. p->def *= p->scale;
  387. p->value = p->def;
  388. p->suggests_logscale = manager->isLogarithmic( m_key, port );
  389. ports.append( p );
  390. // For convenience, keep a separate list of the ports that are used
  391. // to control the processors.
  392. if( p->rate == AUDIO_RATE_INPUT ||
  393. p->rate == CONTROL_RATE_INPUT )
  394. {
  395. p->control_id = m_portControls.count();
  396. m_portControls.append( p );
  397. }
  398. }
  399. m_ports.append( ports );
  400. }
  401. // Instantiate the processing units.
  402. m_descriptor = manager->getDescriptor( m_key );
  403. if( m_descriptor == NULL )
  404. {
  405. QMessageBox::warning( 0, "Effect",
  406. "Can't get LADSPA descriptor function: " + m_key.second,
  407. QMessageBox::Ok, QMessageBox::NoButton );
  408. setOkay( false );
  409. return;
  410. }
  411. if( m_descriptor->run == NULL )
  412. {
  413. QMessageBox::warning( 0, "Effect",
  414. "Plugin has no processor: " + m_key.second,
  415. QMessageBox::Ok, QMessageBox::NoButton );
  416. setDontRun( true );
  417. }
  418. for( ch_cnt_t proc = 0; proc < processorCount(); proc++ )
  419. {
  420. LADSPA_Handle effect = manager->instantiate( m_key,
  421. m_maxSampleRate );
  422. if( effect == NULL )
  423. {
  424. QMessageBox::warning( 0, "Effect",
  425. "Can't get LADSPA instance: " + m_key.second,
  426. QMessageBox::Ok, QMessageBox::NoButton );
  427. setOkay( false );
  428. return;
  429. }
  430. m_handles.append( effect );
  431. }
  432. // Connect the ports.
  433. for( ch_cnt_t proc = 0; proc < processorCount(); proc++ )
  434. {
  435. for( int port = 0; port < m_portCount; port++ )
  436. {
  437. port_desc_t * pp = m_ports.at( proc ).at( port );
  438. if( !manager->connectPort( m_key,
  439. m_handles[proc],
  440. port,
  441. pp->buffer ) )
  442. {
  443. QMessageBox::warning( 0, "Effect",
  444. "Failed to connect port: " + m_key.second,
  445. QMessageBox::Ok, QMessageBox::NoButton );
  446. setDontRun( true );
  447. return;
  448. }
  449. }
  450. }
  451. // Activate the processing units.
  452. for( ch_cnt_t proc = 0; proc < processorCount(); proc++ )
  453. {
  454. manager->activate( m_key, m_handles[proc] );
  455. }
  456. m_controls = new LadspaControls( this );
  457. }
  458. void LadspaEffect::pluginDestruction()
  459. {
  460. if( !isOkay() )
  461. {
  462. return;
  463. }
  464. delete m_controls;
  465. for( ch_cnt_t proc = 0; proc < processorCount(); proc++ )
  466. {
  467. Ladspa2LMMS * manager = Engine::getLADSPAManager();
  468. manager->deactivate( m_key, m_handles[proc] );
  469. manager->cleanup( m_key, m_handles[proc] );
  470. for( int port = 0; port < m_portCount; port++ )
  471. {
  472. port_desc_t * pp = m_ports.at( proc ).at( port );
  473. if( m_inPlaceBroken || pp->rate != CHANNEL_OUT )
  474. {
  475. if( pp->buffer) MM_FREE( pp->buffer );
  476. }
  477. delete pp;
  478. }
  479. m_ports[proc].clear();
  480. }
  481. m_ports.clear();
  482. m_handles.clear();
  483. m_portControls.clear();
  484. }
  485. static QMap<QString, sample_rate_t> __buggy_plugins;
  486. sample_rate_t LadspaEffect::maxSamplerate( const QString & _name )
  487. {
  488. if( __buggy_plugins.isEmpty() )
  489. {
  490. __buggy_plugins["C* AmpVTS"] = 88200;
  491. __buggy_plugins["Chorus2"] = 44100;
  492. __buggy_plugins["Notch Filter"] = 96000;
  493. __buggy_plugins["TAP Reflector"] = 192000;
  494. }
  495. if( __buggy_plugins.contains( _name ) )
  496. {
  497. return( __buggy_plugins[_name] );
  498. }
  499. return( Engine::mixer()->processingSampleRate() );
  500. }
  501. extern "C"
  502. {
  503. // necessary for getting instance out of shared lib
  504. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model * _parent, void * _data )
  505. {
  506. return new LadspaEffect( _parent,
  507. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>(
  508. _data ) );
  509. }
  510. }