Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

703 lines
21KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2009 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. /* Filter module. Can host LADPSA Plugins, or can be inherited from to make internal
  19. modules with special features and appearance. */
  20. #include "const.h"
  21. #include <string.h>
  22. #include <vector>
  23. #include <string>
  24. #include <ladspa.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <FL/fl_draw.H>
  28. #include <FL/Fl_Group.H>
  29. #include <FL/Fl_Menu_Button.H>
  30. #include "Plugin_Module.H"
  31. #include "debug.h"
  32. #define HAVE_LIBLRDF 1
  33. #include "LADSPAInfo.h"
  34. #include "Engine/Engine.H"
  35. static LADSPAInfo *ladspainfo;
  36. Thread* Plugin_Module::plugin_discover_thread;
  37. /* keep this out of the header to avoid spreading ladspa.h dependency */
  38. struct Plugin_Module::ImplementationData
  39. {
  40. const LADSPA_Descriptor *descriptor;
  41. // std::vector<LADSPA_Data*> m_LADSPABufVec;
  42. std::vector<LADSPA_Handle> handle;
  43. };
  44. Plugin_Module::Plugin_Module ( ) : Module( 50, 35, name() )
  45. {
  46. init();
  47. end();
  48. log_create();
  49. }
  50. Plugin_Module::~Plugin_Module ( )
  51. {
  52. log_destroy();
  53. plugin_instances( 0 );
  54. }
  55. void
  56. Plugin_Module::get ( Log_Entry &e ) const
  57. {
  58. // char s[512];
  59. // snprintf( s, sizeof( s ), "ladspa:%lu", _idata->descriptor->UniqueID );
  60. e.add( ":plugin_id", _idata->descriptor->UniqueID );
  61. Module::get( e );
  62. }
  63. void
  64. Plugin_Module::set ( Log_Entry &e )
  65. {
  66. for ( int i = 0; i < e.size(); ++i )
  67. {
  68. const char *s, *v;
  69. e.get( i, &s, &v );
  70. if ( ! strcmp( s, ":plugin_id" ) )
  71. {
  72. load( (unsigned long) atoll ( v ) );
  73. }
  74. }
  75. Module::set( e );
  76. }
  77. void
  78. Plugin_Module::add_plugins_to_menu ( Fl_Menu_Button *menu )
  79. {
  80. Plugin_Module::Plugin_Info *pia = Plugin_Module::get_all_plugins();
  81. char path[1024];
  82. for ( Plugin_Module::Plugin_Info *pi = pia; pi->path; ++pi )
  83. {
  84. snprintf( path, sizeof( path ), "Plugin/%s", pi->path );
  85. menu->add(path, 0, NULL, new unsigned long( pi->id ), 0 );
  86. }
  87. delete[] pia;
  88. }
  89. /* allow the user to pick a plugin */
  90. Plugin_Module *
  91. Plugin_Module::pick_plugin ( void )
  92. {
  93. /**************/
  94. /* build menu */
  95. /**************/
  96. Fl_Menu_Button *menu = new Fl_Menu_Button( 0, 0, 400, 400 );
  97. menu->type( Fl_Menu_Button::POPUP3 );
  98. Plugin_Module::Plugin_Info *pia = Plugin_Module::get_all_plugins();
  99. for ( Plugin_Module::Plugin_Info *pi = pia; pi->path; ++pi )
  100. {
  101. menu->add(pi->path, 0, NULL, pi, 0 );
  102. }
  103. menu->popup();
  104. if ( menu->value() <= 0 )
  105. return NULL;
  106. /************************/
  107. /* load selected plugin */
  108. /************************/
  109. Plugin_Module::Plugin_Info *pi = (Plugin_Module::Plugin_Info*)menu->menu()[ menu->value() ].user_data();
  110. if ( ! pi )
  111. return NULL;
  112. Plugin_Module *m = new Plugin_Module();
  113. m->load( pi->id );
  114. delete[] pia;
  115. return m;
  116. }
  117. void
  118. Plugin_Module::init ( void )
  119. {
  120. _idata = new Plugin_Module::ImplementationData();
  121. _idata->handle.clear();
  122. _active = false;
  123. _crosswire = false;
  124. align( (Fl_Align)FL_ALIGN_CENTER | FL_ALIGN_INSIDE );
  125. color( (Fl_Color)fl_color_average( FL_BLUE, FL_GREEN, 0.5f ) );
  126. int tw, th, tx, ty;
  127. bbox( tx, ty, tw, th );
  128. }
  129. int
  130. Plugin_Module::can_support_inputs ( int n )
  131. {
  132. /* this is the simple case */
  133. if ( plugin_ins() == n )
  134. return plugin_outs();
  135. /* e.g. MONO going into STEREO */
  136. /* we'll duplicate our inputs */
  137. else if ( n < plugin_ins() &&
  138. 1 == n )
  139. {
  140. return plugin_outs();
  141. }
  142. /* e.g. STEREO going into MONO */
  143. /* we'll run multiple instances of the plugin */
  144. else if ( n > plugin_ins() &&
  145. ( plugin_ins() == 1 && plugin_outs() == 1 ) )
  146. {
  147. return n;
  148. }
  149. return -1;
  150. }
  151. bool
  152. Plugin_Module::configure_inputs( int n )
  153. {
  154. int inst = _idata->handle.size();
  155. if ( ninputs() != n )
  156. {
  157. _crosswire = false;
  158. if ( n != ninputs() )
  159. {
  160. if ( 1 == n && plugin_ins() > 1 )
  161. {
  162. DMESSAGE( "Cross-wiring plugin inputs" );
  163. _crosswire = true;
  164. audio_input.clear();
  165. for ( int i = n; i--; )
  166. audio_input.push_back( Port( this, Port::INPUT, Port::AUDIO ) );
  167. }
  168. else if ( n >= plugin_ins() &&
  169. ( plugin_ins() == 1 && plugin_outs() == 1 ) )
  170. {
  171. DMESSAGE( "Running multiple instances of plugin" );
  172. audio_input.clear();
  173. audio_output.clear();
  174. for ( int i = n; i--; )
  175. {
  176. add_port( Port( this, Port::INPUT, Port::AUDIO ) );
  177. add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
  178. }
  179. inst = n;
  180. }
  181. else if ( n == plugin_ins() )
  182. {
  183. DMESSAGE( "Plugin input configuration is a perfect match" );
  184. }
  185. else
  186. {
  187. DMESSAGE( "Unsupported input configuration" );
  188. return false;
  189. }
  190. }
  191. }
  192. if ( _active )
  193. deactivate();
  194. if ( plugin_instances( inst ) )
  195. instances( inst );
  196. else
  197. return false;
  198. if ( ! _active )
  199. activate();
  200. return true;
  201. }
  202. void *
  203. Plugin_Module::discover_thread ( void * )
  204. {
  205. THREAD_ASSERT( Plugin_Discover );
  206. DMESSAGE( "Discovering plugins in the background" );
  207. ladspainfo = new LADSPAInfo();
  208. return NULL;
  209. }
  210. /* Spawn a background thread for plugin discovery */
  211. void
  212. Plugin_Module::spawn_discover_thread ( void )
  213. {
  214. if ( plugin_discover_thread )
  215. {
  216. FATAL( "Plugin discovery thread is already running or has completed" );
  217. }
  218. plugin_discover_thread = new Thread( "Plugin_Discover" );
  219. plugin_discover_thread->clone( &Plugin_Module::discover_thread, NULL );
  220. }
  221. void
  222. Plugin_Module::join_discover_thread ( void )
  223. {
  224. plugin_discover_thread->join();
  225. }
  226. /* return a list of available plugins */
  227. Plugin_Module::Plugin_Info *
  228. Plugin_Module::get_all_plugins ( void )
  229. {
  230. if ( !ladspainfo )
  231. {
  232. if ( ! plugin_discover_thread )
  233. ladspainfo = new LADSPAInfo();
  234. else
  235. plugin_discover_thread->join();
  236. }
  237. std::vector<LADSPAInfo::PluginEntry> plugins = ladspainfo->GetMenuList();
  238. Plugin_Info* pi = new Plugin_Info[plugins.size() + 1];
  239. int j = 0;
  240. for (std::vector<LADSPAInfo::PluginEntry>::iterator i=plugins.begin();
  241. i!=plugins.end(); i++, j++)
  242. {
  243. pi[j].path = i->Name.c_str();
  244. pi[j].id = i->UniqueID;
  245. }
  246. return pi;
  247. }
  248. bool
  249. Plugin_Module::plugin_instances ( unsigned int n )
  250. {
  251. if ( _idata->handle.size() > n )
  252. {
  253. for ( int i = _idata->handle.size() - n; i--; )
  254. {
  255. DMESSAGE( "Destroying plugin instance" );
  256. LADSPA_Handle h = _idata->handle.back();
  257. if ( _idata->descriptor->deactivate )
  258. _idata->descriptor->deactivate( h );
  259. if ( _idata->descriptor->cleanup )
  260. _idata->descriptor->cleanup( h );
  261. _idata->handle.pop_back();
  262. }
  263. }
  264. else if ( _idata->handle.size() < n )
  265. {
  266. for ( int i = n - _idata->handle.size(); i--; )
  267. {
  268. LADSPA_Handle h;
  269. DMESSAGE( "Instantiating plugin..." );
  270. if ( ! (h = _idata->descriptor->instantiate( _idata->descriptor, Engine::sample_rate() ) ) )
  271. {
  272. WARNING( "Failed to instantiate plugin" );
  273. return false;
  274. }
  275. DMESSAGE( "Instantiated: %p", h );
  276. _idata->handle.push_back( h );
  277. DMESSAGE( "Connecting control ports..." );
  278. int ij = 0;
  279. int oj = 0;
  280. for ( unsigned int k = 0; k < _idata->descriptor->PortCount; ++k )
  281. {
  282. if ( LADSPA_IS_PORT_CONTROL( _idata->descriptor->PortDescriptors[k] ) )
  283. {
  284. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[k] ) )
  285. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)control_input[ij++].buffer() );
  286. else if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[k] ) )
  287. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)control_output[oj++].buffer() );
  288. }
  289. }
  290. // connect ports to magic bogus value to aid debugging.
  291. for ( unsigned int k = 0; k < _idata->descriptor->PortCount; ++k )
  292. if ( LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[k] ) )
  293. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)0x42 );
  294. }
  295. }
  296. return true;
  297. }
  298. bool
  299. Plugin_Module::load ( unsigned long id )
  300. {
  301. if ( !ladspainfo )
  302. {
  303. if ( ! plugin_discover_thread )
  304. ladspainfo = new LADSPAInfo();
  305. else
  306. plugin_discover_thread->join();
  307. }
  308. _idata->descriptor = ladspainfo->GetDescriptorByID( id );
  309. label( _idata->descriptor->Name );
  310. _plugin_ins = _plugin_outs = 0;
  311. if ( _idata->descriptor )
  312. {
  313. if ( LADSPA_IS_INPLACE_BROKEN( _idata->descriptor->Properties ) )
  314. {
  315. WARNING( "Cannot use this plugin because it is incapable of processing audio in-place" );
  316. return false;
  317. }
  318. else if ( ! LADSPA_IS_HARD_RT_CAPABLE( _idata->descriptor->Properties ) )
  319. {
  320. WARNING( "Cannot use this plugin because it is incapable of hard real-time operation" );
  321. return false;
  322. }
  323. MESSAGE( "Name: %s", _idata->descriptor->Name );
  324. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  325. {
  326. if ( LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  327. {
  328. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) )
  329. {
  330. add_port( Port( this, Port::INPUT, Port::AUDIO, _idata->descriptor->PortNames[ i ] ) );
  331. _plugin_ins++;
  332. }
  333. else if (LADSPA_IS_PORT_OUTPUT(_idata->descriptor->PortDescriptors[i]))
  334. {
  335. _plugin_outs++;
  336. add_port( Port( this, Port::OUTPUT, Port::AUDIO, _idata->descriptor->PortNames[ i ] ) );
  337. }
  338. }
  339. }
  340. MESSAGE( "Plugin has %i inputs and %i outputs", _plugin_ins, _plugin_outs);
  341. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  342. {
  343. if ( LADSPA_IS_PORT_CONTROL( _idata->descriptor->PortDescriptors[i] ) )
  344. {
  345. Port::Direction d = Port::INPUT;
  346. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) )
  347. {
  348. d = Port::INPUT;
  349. }
  350. else if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[i] ) )
  351. {
  352. d = Port::OUTPUT;
  353. }
  354. Port p( this, d, Port::CONTROL, _idata->descriptor->PortNames[ i ] );
  355. LADSPA_PortRangeHintDescriptor hd = _idata->descriptor->PortRangeHints[i].HintDescriptor;
  356. if ( LADSPA_IS_HINT_BOUNDED_BELOW(hd) )
  357. {
  358. p.hints.ranged = true;
  359. p.hints.minimum = _idata->descriptor->PortRangeHints[i].LowerBound;
  360. }
  361. if ( LADSPA_IS_HINT_BOUNDED_ABOVE(hd) )
  362. {
  363. p.hints.ranged = true;
  364. p.hints.maximum = _idata->descriptor->PortRangeHints[i].UpperBound;
  365. }
  366. if ( LADSPA_IS_HINT_HAS_DEFAULT(hd) )
  367. {
  368. float Max=1.0f, Min=-1.0f, Default=0.0f;
  369. int Port=i;
  370. // Get the bounding hints for the port
  371. LADSPA_PortRangeHintDescriptor HintDesc=_idata->descriptor->PortRangeHints[Port].HintDescriptor;
  372. if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc))
  373. {
  374. Min=_idata->descriptor->PortRangeHints[Port].LowerBound;
  375. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  376. {
  377. Min*=Engine::sample_rate();
  378. }
  379. }
  380. if (LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc))
  381. {
  382. Max=_idata->descriptor->PortRangeHints[Port].UpperBound;
  383. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  384. {
  385. Max*=Engine::sample_rate();
  386. }
  387. }
  388. #ifdef LADSPA_VERSION
  389. // We've got a version of the header that supports port defaults
  390. if (LADSPA_IS_HINT_HAS_DEFAULT(HintDesc)) {
  391. // LADSPA_HINT_DEFAULT_0 is assumed anyway, so we don't check for it
  392. if (LADSPA_IS_HINT_DEFAULT_1(HintDesc)) {
  393. Default = 1.0f;
  394. } else if (LADSPA_IS_HINT_DEFAULT_100(HintDesc)) {
  395. Default = 100.0f;
  396. } else if (LADSPA_IS_HINT_DEFAULT_440(HintDesc)) {
  397. Default = 440.0f;
  398. } else {
  399. // These hints may be affected by SAMPLERATE, LOGARITHMIC and INTEGER
  400. if (LADSPA_IS_HINT_DEFAULT_MINIMUM(HintDesc) &&
  401. LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc)) {
  402. Default=_idata->descriptor->PortRangeHints[Port].LowerBound;
  403. } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(HintDesc) &&
  404. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  405. Default=_idata->descriptor->PortRangeHints[Port].UpperBound;
  406. } else if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc) &&
  407. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  408. // These hints require both upper and lower bounds
  409. float lp = 0.0f, up = 0.0f;
  410. float min = _idata->descriptor->PortRangeHints[Port].LowerBound;
  411. float max = _idata->descriptor->PortRangeHints[Port].UpperBound;
  412. if (LADSPA_IS_HINT_DEFAULT_LOW(HintDesc)) {
  413. lp = 0.75f;
  414. up = 0.25f;
  415. } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(HintDesc)) {
  416. lp = 0.5f;
  417. up = 0.5f;
  418. } else if (LADSPA_IS_HINT_DEFAULT_HIGH(HintDesc)) {
  419. lp = 0.25f;
  420. up = 0.75f;
  421. }
  422. if (LADSPA_IS_HINT_LOGARITHMIC(HintDesc)) {
  423. p.hints.type = Port::Hints::LOGARITHMIC;
  424. if (min==0.0f || max==0.0f) {
  425. // Zero at either end means zero no matter
  426. // where hint is at, since:
  427. // log(n->0) -> Infinity
  428. Default = 0.0f;
  429. } else {
  430. // Catch negatives
  431. bool neg_min = min < 0.0f ? true : false;
  432. bool neg_max = max < 0.0f ? true : false;
  433. if (!neg_min && !neg_max) {
  434. Default = exp(::log(min) * lp + ::log(max) * up);
  435. } else if (neg_min && neg_max) {
  436. Default = -exp(::log(-min) * lp + ::log(-max) * up);
  437. } else {
  438. // Logarithmic range has asymptote
  439. // so just use linear scale
  440. Default = min * lp + max * up;
  441. }
  442. }
  443. } else {
  444. Default = min * lp + max * up;
  445. }
  446. }
  447. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc)) {
  448. Default *= Engine::sample_rate();
  449. }
  450. if (LADSPA_IS_HINT_INTEGER(HintDesc)) {
  451. if ( p.hints.ranged &&
  452. 0 == p.hints.minimum &&
  453. 1 == p.hints.maximum )
  454. p.hints.type = Port::Hints::BOOLEAN;
  455. else
  456. p.hints.type = Port::Hints::INTEGER;
  457. Default = floorf(Default);
  458. }
  459. if (LADSPA_IS_HINT_TOGGLED(HintDesc)){
  460. p.hints.type = Port::Hints::BOOLEAN;
  461. }
  462. }
  463. }
  464. #else
  465. Default = 0.0f;
  466. #endif
  467. p.hints.default_value = Default;
  468. }
  469. float *control_value = new float;
  470. *control_value = p.hints.default_value;
  471. p.connect_to( control_value );
  472. add_port( p );
  473. DMESSAGE( "Plugin has control port \"%s\" (default: %f)", _idata->descriptor->PortNames[ i ], p.hints.default_value );
  474. }
  475. }
  476. }
  477. else
  478. {
  479. WARNING( "Failed to load plugin" );
  480. return false;
  481. }
  482. return plugin_instances( 1 );
  483. }
  484. void
  485. Plugin_Module::set_input_buffer ( int n, void *buf )
  486. {
  487. LADSPA_Handle h;
  488. if ( instances() > 1 )
  489. {
  490. h = _idata->handle[n];
  491. n = 0;
  492. }
  493. else
  494. h = _idata->handle[0];
  495. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  496. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) &&
  497. LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  498. if ( n-- == 0 )
  499. _idata->descriptor->connect_port( h, i, (LADSPA_Data*)buf );
  500. }
  501. void
  502. Plugin_Module::set_output_buffer ( int n, void *buf )
  503. {
  504. LADSPA_Handle h;
  505. if ( instances() > 1 )
  506. {
  507. h = _idata->handle[n];
  508. n = 0;
  509. }
  510. else
  511. h = _idata->handle[0];
  512. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  513. if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[i] ) &&
  514. LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  515. if ( n-- == 0 )
  516. _idata->descriptor->connect_port( h, i, (LADSPA_Data*)buf );
  517. }
  518. void
  519. Plugin_Module::activate ( void )
  520. {
  521. if ( _active )
  522. FATAL( "Attempt to activate already active plugin" );
  523. if ( _idata->descriptor->activate )
  524. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  525. _idata->descriptor->activate( _idata->handle[i] );
  526. _active = true;
  527. }
  528. void
  529. Plugin_Module::deactivate( void )
  530. {
  531. if ( _idata->descriptor->deactivate )
  532. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  533. _idata->descriptor->activate( _idata->handle[i] );
  534. _active = false;
  535. }
  536. void
  537. Plugin_Module::handle_port_connection_change ( void )
  538. {
  539. // DMESSAGE( "Connecting audio ports" );
  540. if ( _crosswire )
  541. {
  542. for ( int i = 0; i < plugin_ins(); ++i )
  543. set_input_buffer( i, audio_input[0].buffer() );
  544. }
  545. else
  546. {
  547. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  548. set_input_buffer( i, audio_input[i].buffer() );
  549. }
  550. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  551. set_output_buffer( i, audio_output[i].buffer() );
  552. }
  553. /**********/
  554. /* Engine */
  555. /**********/
  556. void
  557. Plugin_Module::process ( nframes_t nframes )
  558. {
  559. handle_port_connection_change();
  560. if ( _active )
  561. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  562. _idata->descriptor->run( _idata->handle[i], nframes );
  563. }