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.

697 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 "util/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 * v )
  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. /* return a list of available plugins */
  222. Plugin_Module::Plugin_Info *
  223. Plugin_Module::get_all_plugins ( void )
  224. {
  225. if ( !ladspainfo )
  226. {
  227. if ( ! plugin_discover_thread )
  228. ladspainfo = new LADSPAInfo();
  229. else
  230. plugin_discover_thread->join();
  231. }
  232. std::vector<LADSPAInfo::PluginEntry> plugins = ladspainfo->GetMenuList();
  233. Plugin_Info* pi = new Plugin_Info[plugins.size() + 1];
  234. int j = 0;
  235. for (std::vector<LADSPAInfo::PluginEntry>::iterator i=plugins.begin();
  236. i!=plugins.end(); i++, j++)
  237. {
  238. pi[j].path = i->Name.c_str();
  239. pi[j].id = i->UniqueID;
  240. }
  241. return pi;
  242. }
  243. bool
  244. Plugin_Module::plugin_instances ( unsigned int n )
  245. {
  246. if ( _idata->handle.size() > n )
  247. {
  248. for ( int i = _idata->handle.size() - n; i--; )
  249. {
  250. DMESSAGE( "Destroying plugin instance" );
  251. LADSPA_Handle h = _idata->handle.back();
  252. if ( _idata->descriptor->deactivate )
  253. _idata->descriptor->deactivate( h );
  254. if ( _idata->descriptor->cleanup )
  255. _idata->descriptor->cleanup( h );
  256. _idata->handle.pop_back();
  257. }
  258. }
  259. else if ( _idata->handle.size() < n )
  260. {
  261. for ( int i = n - _idata->handle.size(); i--; )
  262. {
  263. LADSPA_Handle h;
  264. DMESSAGE( "Instantiating plugin..." );
  265. if ( ! (h = _idata->descriptor->instantiate( _idata->descriptor, Engine::sample_rate() ) ) )
  266. {
  267. WARNING( "Failed to instantiate plugin" );
  268. return false;
  269. }
  270. DMESSAGE( "Instantiated: %p", h );
  271. _idata->handle.push_back( h );
  272. DMESSAGE( "Connecting control ports..." );
  273. int ij = 0;
  274. int oj = 0;
  275. for ( unsigned int k = 0; k < _idata->descriptor->PortCount; ++k )
  276. {
  277. if ( LADSPA_IS_PORT_CONTROL( _idata->descriptor->PortDescriptors[k] ) )
  278. {
  279. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[k] ) )
  280. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)control_input[ij++].buffer() );
  281. else if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[k] ) )
  282. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)control_output[oj++].buffer() );
  283. }
  284. }
  285. // connect ports to magic bogus value to aid debugging.
  286. for ( unsigned int k = 0; k < _idata->descriptor->PortCount; ++k )
  287. if ( LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[k] ) )
  288. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)0x42 );
  289. }
  290. }
  291. return true;
  292. }
  293. bool
  294. Plugin_Module::load ( unsigned long id )
  295. {
  296. if ( !ladspainfo )
  297. {
  298. if ( ! plugin_discover_thread )
  299. ladspainfo = new LADSPAInfo();
  300. else
  301. plugin_discover_thread->join();
  302. }
  303. _idata->descriptor = ladspainfo->GetDescriptorByID( id );
  304. label( _idata->descriptor->Name );
  305. _plugin_ins = _plugin_outs = 0;
  306. if ( _idata->descriptor )
  307. {
  308. if ( LADSPA_IS_INPLACE_BROKEN( _idata->descriptor->Properties ) )
  309. {
  310. WARNING( "Cannot use this plugin because it is incapable of processing audio in-place" );
  311. return false;
  312. }
  313. else if ( ! LADSPA_IS_HARD_RT_CAPABLE( _idata->descriptor->Properties ) )
  314. {
  315. WARNING( "Cannot use this plugin because it is incapable of hard real-time operation" );
  316. return false;
  317. }
  318. MESSAGE( "Name: %s", _idata->descriptor->Name );
  319. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  320. {
  321. if ( LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  322. {
  323. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) )
  324. {
  325. add_port( Port( this, Port::INPUT, Port::AUDIO, _idata->descriptor->PortNames[ i ] ) );
  326. _plugin_ins++;
  327. }
  328. else if (LADSPA_IS_PORT_OUTPUT(_idata->descriptor->PortDescriptors[i]))
  329. {
  330. _plugin_outs++;
  331. add_port( Port( this, Port::OUTPUT, Port::AUDIO, _idata->descriptor->PortNames[ i ] ) );
  332. }
  333. }
  334. }
  335. MESSAGE( "Plugin has %i inputs and %i outputs", _plugin_ins, _plugin_outs);
  336. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  337. {
  338. if ( LADSPA_IS_PORT_CONTROL( _idata->descriptor->PortDescriptors[i] ) )
  339. {
  340. Port::Direction d = Port::INPUT;
  341. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) )
  342. {
  343. d = Port::INPUT;
  344. }
  345. else if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[i] ) )
  346. {
  347. d = Port::OUTPUT;
  348. }
  349. Port p( this, d, Port::CONTROL, _idata->descriptor->PortNames[ i ] );
  350. LADSPA_PortRangeHintDescriptor hd = _idata->descriptor->PortRangeHints[i].HintDescriptor;
  351. if ( LADSPA_IS_HINT_BOUNDED_BELOW(hd) )
  352. {
  353. p.hints.ranged = true;
  354. p.hints.minimum = _idata->descriptor->PortRangeHints[i].LowerBound;
  355. }
  356. if ( LADSPA_IS_HINT_BOUNDED_ABOVE(hd) )
  357. {
  358. p.hints.ranged = true;
  359. p.hints.maximum = _idata->descriptor->PortRangeHints[i].UpperBound;
  360. }
  361. if ( LADSPA_IS_HINT_HAS_DEFAULT(hd) )
  362. {
  363. float Max=1.0f, Min=-1.0f, Default=0.0f;
  364. int Port=i;
  365. // Get the bounding hints for the port
  366. LADSPA_PortRangeHintDescriptor HintDesc=_idata->descriptor->PortRangeHints[Port].HintDescriptor;
  367. if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc))
  368. {
  369. Min=_idata->descriptor->PortRangeHints[Port].LowerBound;
  370. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  371. {
  372. Min*=Engine::sample_rate();
  373. }
  374. }
  375. if (LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc))
  376. {
  377. Max=_idata->descriptor->PortRangeHints[Port].UpperBound;
  378. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  379. {
  380. Max*=Engine::sample_rate();
  381. }
  382. }
  383. #ifdef LADSPA_VERSION
  384. // We've got a version of the header that supports port defaults
  385. if (LADSPA_IS_HINT_HAS_DEFAULT(HintDesc)) {
  386. // LADSPA_HINT_DEFAULT_0 is assumed anyway, so we don't check for it
  387. if (LADSPA_IS_HINT_DEFAULT_1(HintDesc)) {
  388. Default = 1.0f;
  389. } else if (LADSPA_IS_HINT_DEFAULT_100(HintDesc)) {
  390. Default = 100.0f;
  391. } else if (LADSPA_IS_HINT_DEFAULT_440(HintDesc)) {
  392. Default = 440.0f;
  393. } else {
  394. // These hints may be affected by SAMPLERATE, LOGARITHMIC and INTEGER
  395. if (LADSPA_IS_HINT_DEFAULT_MINIMUM(HintDesc) &&
  396. LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc)) {
  397. Default=_idata->descriptor->PortRangeHints[Port].LowerBound;
  398. } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(HintDesc) &&
  399. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  400. Default=_idata->descriptor->PortRangeHints[Port].UpperBound;
  401. } else if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc) &&
  402. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  403. // These hints require both upper and lower bounds
  404. float lp = 0.0f, up = 0.0f;
  405. float min = _idata->descriptor->PortRangeHints[Port].LowerBound;
  406. float max = _idata->descriptor->PortRangeHints[Port].UpperBound;
  407. if (LADSPA_IS_HINT_DEFAULT_LOW(HintDesc)) {
  408. lp = 0.75f;
  409. up = 0.25f;
  410. } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(HintDesc)) {
  411. lp = 0.5f;
  412. up = 0.5f;
  413. } else if (LADSPA_IS_HINT_DEFAULT_HIGH(HintDesc)) {
  414. lp = 0.25f;
  415. up = 0.75f;
  416. }
  417. if (LADSPA_IS_HINT_LOGARITHMIC(HintDesc)) {
  418. p.hints.type = Port::Hints::LOGARITHMIC;
  419. if (min==0.0f || max==0.0f) {
  420. // Zero at either end means zero no matter
  421. // where hint is at, since:
  422. // log(n->0) -> Infinity
  423. Default = 0.0f;
  424. } else {
  425. // Catch negatives
  426. bool neg_min = min < 0.0f ? true : false;
  427. bool neg_max = max < 0.0f ? true : false;
  428. if (!neg_min && !neg_max) {
  429. Default = exp(::log(min) * lp + ::log(max) * up);
  430. } else if (neg_min && neg_max) {
  431. Default = -exp(::log(-min) * lp + ::log(-max) * up);
  432. } else {
  433. // Logarithmic range has asymptote
  434. // so just use linear scale
  435. Default = min * lp + max * up;
  436. }
  437. }
  438. } else {
  439. Default = min * lp + max * up;
  440. }
  441. }
  442. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc)) {
  443. Default *= Engine::sample_rate();
  444. }
  445. if (LADSPA_IS_HINT_INTEGER(HintDesc)) {
  446. if ( p.hints.ranged &&
  447. 0 == p.hints.minimum &&
  448. 1 == p.hints.maximum )
  449. p.hints.type = Port::Hints::BOOLEAN;
  450. else
  451. p.hints.type = Port::Hints::INTEGER;
  452. Default = floorf(Default);
  453. }
  454. if (LADSPA_IS_HINT_TOGGLED(HintDesc)){
  455. p.hints.type = Port::Hints::BOOLEAN;
  456. }
  457. }
  458. }
  459. #else
  460. Default = 0.0f;
  461. #endif
  462. p.hints.default_value = Default;
  463. }
  464. float *control_value = new float;
  465. *control_value = p.hints.default_value;
  466. p.connect_to( control_value );
  467. add_port( p );
  468. DMESSAGE( "Plugin has control port \"%s\" (default: %f)", _idata->descriptor->PortNames[ i ], p.hints.default_value );
  469. }
  470. }
  471. }
  472. else
  473. {
  474. WARNING( "Failed to load plugin" );
  475. return false;
  476. }
  477. return plugin_instances( 1 );
  478. }
  479. void
  480. Plugin_Module::set_input_buffer ( int n, void *buf )
  481. {
  482. LADSPA_Handle h;
  483. if ( instances() > 1 )
  484. {
  485. h = _idata->handle[n];
  486. n = 0;
  487. }
  488. else
  489. h = _idata->handle[0];
  490. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  491. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) &&
  492. LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  493. if ( n-- == 0 )
  494. _idata->descriptor->connect_port( h, i, (LADSPA_Data*)buf );
  495. }
  496. void
  497. Plugin_Module::set_output_buffer ( int n, void *buf )
  498. {
  499. LADSPA_Handle h;
  500. if ( instances() > 1 )
  501. {
  502. h = _idata->handle[n];
  503. n = 0;
  504. }
  505. else
  506. h = _idata->handle[0];
  507. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  508. if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[i] ) &&
  509. LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  510. if ( n-- == 0 )
  511. _idata->descriptor->connect_port( h, i, (LADSPA_Data*)buf );
  512. }
  513. void
  514. Plugin_Module::activate ( void )
  515. {
  516. if ( _active )
  517. FATAL( "Attempt to activate already active plugin" );
  518. if ( _idata->descriptor->activate )
  519. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  520. _idata->descriptor->activate( _idata->handle[i] );
  521. _active = true;
  522. }
  523. void
  524. Plugin_Module::deactivate( void )
  525. {
  526. if ( _idata->descriptor->deactivate )
  527. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  528. _idata->descriptor->activate( _idata->handle[i] );
  529. _active = false;
  530. }
  531. void
  532. Plugin_Module::handle_port_connection_change ( void )
  533. {
  534. // DMESSAGE( "Connecting audio ports" );
  535. if ( _crosswire )
  536. {
  537. for ( int i = 0; i < plugin_ins(); ++i )
  538. set_input_buffer( i, audio_input[0].buffer() );
  539. }
  540. else
  541. {
  542. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  543. set_input_buffer( i, audio_input[i].buffer() );
  544. }
  545. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  546. set_output_buffer( i, audio_output[i].buffer() );
  547. }
  548. /**********/
  549. /* Engine */
  550. /**********/
  551. void
  552. Plugin_Module::process ( nframes_t nframes )
  553. {
  554. handle_port_connection_change();
  555. if ( _active )
  556. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  557. _idata->descriptor->run( _idata->handle[i], nframes );
  558. }