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.

701 lines
22KB

  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 "Plugin_Module.H"
  22. #include <Fl/fl_draw.H>
  23. #include <FL/Fl_Group.H>
  24. #include "util/debug.h"
  25. #include <string.h>
  26. #define HAVE_LIBLRDF 1
  27. #include "LADSPAInfo.h"
  28. #include <vector>
  29. #include <string>
  30. #include <ladspa.h>
  31. #include <stdlib.h>
  32. #include <math.h>
  33. #include "Engine/Engine.H"
  34. static LADSPAInfo *ladspainfo;
  35. /* keep this out of the header to avoid spreading ladspa.h dependency */
  36. struct Plugin_Module::ImplementationData
  37. {
  38. const LADSPA_Descriptor *descriptor;
  39. // std::vector<LADSPA_Data*> m_LADSPABufVec;
  40. std::vector<LADSPA_Handle> handle;
  41. };
  42. Plugin_Module::Plugin_Module ( ) : Module( 50, 35, name() )
  43. {
  44. init();
  45. end();
  46. log_create();
  47. }
  48. Plugin_Module::~Plugin_Module ( )
  49. {
  50. log_destroy();
  51. plugin_instances( 0 );
  52. }
  53. void
  54. Plugin_Module::get ( Log_Entry &e ) const
  55. {
  56. // char s[512];
  57. // snprintf( s, sizeof( s ), "ladspa:%lu", _idata->descriptor->UniqueID );
  58. e.add( ":plugin_id", _idata->descriptor->UniqueID );
  59. Module::get( e );
  60. }
  61. void
  62. Plugin_Module::set ( Log_Entry &e )
  63. {
  64. for ( int i = 0; i < e.size(); ++i )
  65. {
  66. const char *s, *v;
  67. e.get( i, &s, &v );
  68. if ( ! strcmp( s, ":plugin_id" ) )
  69. {
  70. load( (unsigned long) atoll ( v ) );
  71. }
  72. }
  73. Module::set( e );
  74. }
  75. #include <FL/Fl_Menu_Button.H>
  76. void
  77. Plugin_Module::add_plugins_to_menu ( Fl_Menu_Button *menu )
  78. {
  79. Plugin_Module::Plugin_Info *pia = Plugin_Module::discover();
  80. char path[1024];
  81. for ( Plugin_Module::Plugin_Info *pi = pia; pi->path; ++pi )
  82. {
  83. snprintf( path, sizeof( path ), "%s/%s", "Plugin", pi->path );
  84. menu->add(path, 0, NULL, new unsigned long( pi->id ), 0 );
  85. }
  86. delete[] pia;
  87. }
  88. /* allow the user to pick a plugin */
  89. Plugin_Module *
  90. Plugin_Module::pick_plugin ( void )
  91. {
  92. /**************/
  93. /* build menu */
  94. /**************/
  95. Fl_Menu_Button *menu = new Fl_Menu_Button( 0, 0, 400, 400 );
  96. menu->type( Fl_Menu_Button::POPUP3 );
  97. Plugin_Module::Plugin_Info *pia = Plugin_Module::discover();
  98. for ( Plugin_Module::Plugin_Info *pi = pia; pi->path; ++pi )
  99. {
  100. menu->add(pi->path, 0, NULL, pi, 0 );
  101. }
  102. menu->popup();
  103. if ( menu->value() <= 0 )
  104. return NULL;
  105. /************************/
  106. /* load selected plugin */
  107. /************************/
  108. Plugin_Module::Plugin_Info *pi = (Plugin_Module::Plugin_Info*)menu->menu()[ menu->value() ].user_data();
  109. if ( ! pi )
  110. return NULL;
  111. Plugin_Module *m = new Plugin_Module();
  112. m->load( pi->id );
  113. delete[] pia;
  114. return m;
  115. }
  116. void
  117. Plugin_Module::init ( void )
  118. {
  119. _idata = new Plugin_Module::ImplementationData();
  120. _idata->handle.clear();
  121. _active = false;
  122. _crosswire = false;
  123. align( (Fl_Align)FL_ALIGN_CENTER | FL_ALIGN_INSIDE );
  124. color( (Fl_Color)fl_color_average( FL_BLUE, FL_GREEN, 0.5f ) );
  125. int tw, th, tx, ty;
  126. bbox( tx, ty, tw, th );
  127. }
  128. #include "FL/test_press.H"
  129. int
  130. Plugin_Module::handle ( int m )
  131. {
  132. switch ( m )
  133. {
  134. case FL_ENTER:
  135. case FL_LEAVE:
  136. redraw();
  137. return 1;
  138. break;
  139. default:
  140. return Module::handle( m );
  141. }
  142. return 0;
  143. }
  144. /* There are two possible adaptations that can be made at Plugin_Module input to account for a mismatch
  145. between channel configurations.
  146. The two scenarios are as follows.
  147. 1. The preceding module has fewer outputs than this module has inputs. If
  148. the preceding module has 1 output (MONO) then it will be duplicated
  149. for this module's addition inputs. If the preceding module has more
  150. than one output, then the chain is in error.
  151. 2. The preceding module has more outputs than this module has inputs
  152. If this module has 1 output (MONO) then it will create the required number of
  153. instances of its plugin.
  154. Stereo plugins are never run with more than one instance. Mono
  155. plugins will have their outputs brought up to stereo for plugins with
  156. stereo input.
  157. */
  158. int
  159. Plugin_Module::can_support_inputs ( int n )
  160. {
  161. /* this is the simple case */
  162. if ( plugin_ins() == n )
  163. return plugin_outs();
  164. /* e.g. MONO going into STEREO */
  165. /* we'll duplicate our inputs */
  166. else if ( n < plugin_ins() &&
  167. 1 == n )
  168. {
  169. return plugin_outs();
  170. }
  171. /* e.g. STEREO going into MONO */
  172. /* we'll run multiple instances of the plugin */
  173. else if ( n > plugin_ins() &&
  174. ( plugin_ins() == 1 && plugin_outs() == 1 ) )
  175. {
  176. return n;
  177. }
  178. return -1;
  179. }
  180. bool
  181. Plugin_Module::configure_inputs( int n )
  182. {
  183. int inst = _idata->handle.size();
  184. if ( ninputs() != n )
  185. {
  186. _crosswire = false;
  187. if ( n != ninputs() )
  188. {
  189. if ( 1 == n && plugin_ins() > 1 )
  190. {
  191. DMESSAGE( "Cross-wiring plugin inputs" );
  192. _crosswire = true;
  193. audio_input.clear();
  194. for ( int i = n; i--; )
  195. audio_input.push_back( Port( this, Port::INPUT, Port::AUDIO ) );
  196. }
  197. else if ( n >= plugin_ins() &&
  198. ( plugin_ins() == 1 && plugin_outs() == 1 ) )
  199. {
  200. DMESSAGE( "Running multiple instances of plugin" );
  201. audio_input.clear();
  202. audio_output.clear();
  203. for ( int i = n; i--; )
  204. {
  205. add_port( Port( this, Port::INPUT, Port::AUDIO ) );
  206. add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
  207. }
  208. inst = n;
  209. }
  210. else if ( n == plugin_ins() )
  211. {
  212. DMESSAGE( "Plugin input configuration is a perfect match" );
  213. }
  214. else
  215. {
  216. DMESSAGE( "Unsupported input configuration" );
  217. return false;
  218. }
  219. }
  220. }
  221. if ( _active )
  222. deactivate();
  223. if ( plugin_instances( inst ) )
  224. instances( inst );
  225. else
  226. return false;
  227. if ( ! _active )
  228. activate();
  229. return true;
  230. }
  231. /* return a list of available plugins */
  232. Plugin_Module::Plugin_Info *
  233. Plugin_Module::discover ( void )
  234. {
  235. if ( !ladspainfo )
  236. ladspainfo = new LADSPAInfo();
  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. ladspainfo = new LADSPAInfo();
  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. void
  549. Plugin_Module::process ( )
  550. {
  551. handle_port_connection_change();
  552. if ( _active )
  553. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  554. {
  555. volatile int n = i;
  556. _idata->descriptor->run( _idata->handle[n], nframes() );
  557. }
  558. }