LadspaEffect.cpp 14 KB

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