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.

677 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 "Plugin_Module.H"
  21. #include <Fl/fl_draw.H>
  22. #include <FL/Fl_Group.H>
  23. #include "util/debug.h"
  24. #include <string.h>
  25. #define HAVE_LIBLRDF 1
  26. #include "LADSPAInfo.h"
  27. #include <vector>
  28. #include <string>
  29. #include <ladspa.h>
  30. #include <stdlib.h>
  31. #include <math.h>
  32. #include "Engine/Engine.H"
  33. #include "Chain.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, 50, 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. /* allow the user to pick a plugin */
  77. Plugin_Module *
  78. Plugin_Module::pick_plugin ( void )
  79. {
  80. /**************/
  81. /* build menu */
  82. /**************/
  83. Fl_Menu_Button *menu = new Fl_Menu_Button( 0, 0, 400, 400 );
  84. menu->type( Fl_Menu_Button::POPUP3 );
  85. Plugin_Module::Plugin_Info *pia = Plugin_Module::discover();
  86. for ( Plugin_Module::Plugin_Info *pi = pia; pi->path; ++pi )
  87. {
  88. menu->add(pi->path, 0, NULL, pi, 0 );
  89. }
  90. menu->popup();
  91. if ( menu->value() <= 0 )
  92. return NULL;
  93. /************************/
  94. /* load selected plugin */
  95. /************************/
  96. Plugin_Module::Plugin_Info *pi = (Plugin_Module::Plugin_Info*)menu->menu()[ menu->value() ].user_data();
  97. Plugin_Module *m = new Plugin_Module();
  98. m->load( pi->id );
  99. delete[] pia;
  100. return m;
  101. }
  102. void
  103. Plugin_Module::init ( void )
  104. {
  105. _idata = new Plugin_Module::ImplementationData();
  106. _idata->handle.clear();
  107. _active = false;
  108. _crosswire = false;
  109. align( (Fl_Align)FL_ALIGN_CENTER | FL_ALIGN_INSIDE );
  110. color( (Fl_Color)fl_color_average( FL_BLUE, FL_GREEN, 0.5f ) );
  111. int tw, th, tx, ty;
  112. bbox( tx, ty, tw, th );
  113. }
  114. #include "FL/test_press.H"
  115. int
  116. Plugin_Module::handle ( int m )
  117. {
  118. switch ( m )
  119. {
  120. case FL_ENTER:
  121. case FL_LEAVE:
  122. redraw();
  123. return 1;
  124. break;
  125. default:
  126. return Module::handle( m );
  127. }
  128. return 0;
  129. }
  130. /* There are two possible adaptations that can be made at Plugin_Module input to account for a mismatch
  131. between channel configurations.
  132. The two scenarios are as follows.
  133. 1. The preceding module has fewer outputs than this module has inputs. If
  134. the preceding module has 1 output (MONO) then it will be duplicated
  135. for this module's addition inputs. If the preceding module has more
  136. than one output, then the chain is in error.
  137. 2. The preceding module has more outputs than this module has inputs
  138. If this module has 1 output (MONO) then it will create the required number of
  139. instances of its plugin.
  140. Stereo plugins are never run with more than one instance. Mono
  141. plugins will have their outputs brought up to stereo for plugins with
  142. stereo input.
  143. */
  144. int
  145. Plugin_Module::can_support_inputs ( int n )
  146. {
  147. /* this is the simple case */
  148. if ( plugin_ins() == n )
  149. return plugin_outs();
  150. /* e.g. MONO going into STEREO */
  151. /* we'll duplicate our inputs */
  152. else if ( n < plugin_ins() &&
  153. 1 == n )
  154. {
  155. return plugin_outs();
  156. }
  157. /* e.g. STEREO going into MONO */
  158. /* we'll run multiple instances of the plugin */
  159. else if ( n > plugin_ins() &&
  160. ( plugin_ins() == 1 && plugin_outs() == 1 ) )
  161. {
  162. return n;
  163. }
  164. return -1;
  165. }
  166. bool
  167. Plugin_Module::configure_inputs( int n )
  168. {
  169. if ( ninputs() == n )
  170. return true;
  171. int inst = 1;
  172. _crosswire = false;
  173. if ( 1 == n && plugin_ins() > 1 )
  174. {
  175. DMESSAGE( "Cross-wiring plugin inputs" );
  176. _crosswire = true;
  177. audio_input.clear();
  178. for ( int i = n; i--; )
  179. audio_input.push_back( Port( this, Port::INPUT, Port::AUDIO ) );
  180. }
  181. else if ( n >= plugin_ins() &&
  182. ( plugin_ins() == 1 && plugin_outs() == 1 ) )
  183. {
  184. DMESSAGE( "Running multiple instances of plugin" );
  185. audio_input.clear();
  186. audio_output.clear();
  187. for ( int i = n; i--; )
  188. {
  189. add_port( Port( this, Port::INPUT, Port::AUDIO ) );
  190. add_port( Port( this, Port::OUTPUT, Port::AUDIO ) );
  191. }
  192. inst = n;
  193. }
  194. if ( n == plugin_ins() )
  195. {
  196. }
  197. else
  198. {
  199. // unsupported configuration
  200. return false;
  201. }
  202. if ( _active )
  203. deactivate();
  204. if ( plugin_instances( inst ) )
  205. instances( inst );
  206. else
  207. return false;
  208. if ( ! _active )
  209. activate();
  210. return true;
  211. }
  212. /* return a list of available plugins */
  213. Plugin_Module::Plugin_Info *
  214. Plugin_Module::discover ( void )
  215. {
  216. if ( !ladspainfo )
  217. ladspainfo = new LADSPAInfo();
  218. std::vector<LADSPAInfo::PluginEntry> plugins = ladspainfo->GetMenuList();
  219. Plugin_Info* pi = new Plugin_Info[plugins.size() + 1];
  220. int j = 0;
  221. for (std::vector<LADSPAInfo::PluginEntry>::iterator i=plugins.begin();
  222. i!=plugins.end(); i++, j++)
  223. {
  224. pi[j].path = i->Name.c_str();
  225. pi[j].id = i->UniqueID;
  226. }
  227. return pi;
  228. }
  229. bool
  230. Plugin_Module::plugin_instances ( unsigned int n )
  231. {
  232. if ( _idata->handle.size() > n )
  233. {
  234. for ( int i = _idata->handle.size() - n; i--; )
  235. {
  236. DMESSAGE( "Destroying plugin instance" );
  237. LADSPA_Handle h = _idata->handle.back();
  238. if ( _idata->descriptor->deactivate )
  239. _idata->descriptor->deactivate( h );
  240. if ( _idata->descriptor->cleanup )
  241. _idata->descriptor->cleanup( h );
  242. _idata->handle.pop_back();
  243. }
  244. }
  245. else if ( _idata->handle.size() < n )
  246. {
  247. for ( int i = n - _idata->handle.size(); i--; )
  248. {
  249. LADSPA_Handle h;
  250. DMESSAGE( "Instantiating plugin..." );
  251. if ( ! (h = _idata->descriptor->instantiate( _idata->descriptor, chain()->engine()->sample_rate() ) ) )
  252. {
  253. WARNING( "Failed to instantiate plugin" );
  254. return false;
  255. }
  256. DMESSAGE( "Instantiated: %p", h );
  257. _idata->handle.push_back( h );
  258. DMESSAGE( "Connecting control ports..." );
  259. int ij = 0;
  260. int oj = 0;
  261. for ( unsigned int k = 0; k < _idata->descriptor->PortCount; ++k )
  262. {
  263. if ( LADSPA_IS_PORT_CONTROL( _idata->descriptor->PortDescriptors[k] ) )
  264. {
  265. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[k] ) )
  266. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)control_input[ij++].buffer() );
  267. else if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[k] ) )
  268. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)control_output[oj++].buffer() );
  269. }
  270. }
  271. // connect ports to magic bogus value to aid debugging.
  272. for ( unsigned int k = 0; k < _idata->descriptor->PortCount; ++k )
  273. if ( LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[k] ) )
  274. _idata->descriptor->connect_port( h, k, (LADSPA_Data*)0x42 );
  275. }
  276. }
  277. return true;
  278. }
  279. bool
  280. Plugin_Module::load ( unsigned long id )
  281. {
  282. if ( !ladspainfo )
  283. ladspainfo = new LADSPAInfo();
  284. _idata->descriptor = ladspainfo->GetDescriptorByID( id );
  285. label( _idata->descriptor->Name );
  286. _plugin_ins = _plugin_outs = 0;
  287. if ( _idata->descriptor )
  288. {
  289. if ( LADSPA_IS_INPLACE_BROKEN( _idata->descriptor->Properties ) )
  290. {
  291. WARNING( "Cannot use this plugin because it is incapable of processing audio in-place" );
  292. return false;
  293. }
  294. else if ( ! LADSPA_IS_HARD_RT_CAPABLE( _idata->descriptor->Properties ) )
  295. {
  296. WARNING( "Cannot use this plugin because it is incapable of hard real-time operation" );
  297. return false;
  298. }
  299. MESSAGE( "Name: %s", _idata->descriptor->Name );
  300. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  301. {
  302. if ( LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  303. {
  304. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) )
  305. {
  306. add_port( Port( this, Port::INPUT, Port::AUDIO, _idata->descriptor->PortNames[ i ] ) );
  307. _plugin_ins++;
  308. }
  309. else if (LADSPA_IS_PORT_OUTPUT(_idata->descriptor->PortDescriptors[i]))
  310. {
  311. _plugin_outs++;
  312. add_port( Port( this, Port::OUTPUT, Port::AUDIO, _idata->descriptor->PortNames[ i ] ) );
  313. }
  314. }
  315. }
  316. MESSAGE( "Plugin has %i inputs and %i outputs", _plugin_ins, _plugin_outs);
  317. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  318. {
  319. if ( LADSPA_IS_PORT_CONTROL( _idata->descriptor->PortDescriptors[i] ) )
  320. {
  321. Port::Direction d = Port::INPUT;
  322. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) )
  323. {
  324. d = Port::INPUT;
  325. }
  326. else if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[i] ) )
  327. {
  328. d = Port::OUTPUT;
  329. }
  330. Port p( this, d, Port::CONTROL, _idata->descriptor->PortNames[ i ] );
  331. LADSPA_PortRangeHintDescriptor hd = _idata->descriptor->PortRangeHints[i].HintDescriptor;
  332. if ( LADSPA_IS_HINT_BOUNDED_BELOW(hd) )
  333. {
  334. p.hints.ranged = true;
  335. p.hints.minimum = _idata->descriptor->PortRangeHints[i].LowerBound;
  336. }
  337. if ( LADSPA_IS_HINT_BOUNDED_ABOVE(hd) )
  338. {
  339. p.hints.ranged = true;
  340. p.hints.maximum = _idata->descriptor->PortRangeHints[i].UpperBound;
  341. }
  342. if ( LADSPA_IS_HINT_HAS_DEFAULT(hd) )
  343. {
  344. float Max=1.0f, Min=-1.0f, Default=0.0f;
  345. int Port=i;
  346. // Get the bounding hints for the port
  347. LADSPA_PortRangeHintDescriptor HintDesc=_idata->descriptor->PortRangeHints[Port].HintDescriptor;
  348. if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc))
  349. {
  350. Min=_idata->descriptor->PortRangeHints[Port].LowerBound;
  351. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  352. {
  353. Min*=chain()->engine()->sample_rate();
  354. }
  355. }
  356. if (LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc))
  357. {
  358. Max=_idata->descriptor->PortRangeHints[Port].UpperBound;
  359. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  360. {
  361. Max*=chain()->engine()->sample_rate();
  362. }
  363. }
  364. #ifdef LADSPA_VERSION
  365. // We've got a version of the header that supports port defaults
  366. if (LADSPA_IS_HINT_HAS_DEFAULT(HintDesc)) {
  367. // LADSPA_HINT_DEFAULT_0 is assumed anyway, so we don't check for it
  368. if (LADSPA_IS_HINT_DEFAULT_1(HintDesc)) {
  369. Default = 1.0f;
  370. } else if (LADSPA_IS_HINT_DEFAULT_100(HintDesc)) {
  371. Default = 100.0f;
  372. } else if (LADSPA_IS_HINT_DEFAULT_440(HintDesc)) {
  373. Default = 440.0f;
  374. } else {
  375. // These hints may be affected by SAMPLERATE, LOGARITHMIC and INTEGER
  376. if (LADSPA_IS_HINT_DEFAULT_MINIMUM(HintDesc) &&
  377. LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc)) {
  378. Default=_idata->descriptor->PortRangeHints[Port].LowerBound;
  379. } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(HintDesc) &&
  380. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  381. Default=_idata->descriptor->PortRangeHints[Port].UpperBound;
  382. } else if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc) &&
  383. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  384. // These hints require both upper and lower bounds
  385. float lp = 0.0f, up = 0.0f;
  386. float min = _idata->descriptor->PortRangeHints[Port].LowerBound;
  387. float max = _idata->descriptor->PortRangeHints[Port].UpperBound;
  388. if (LADSPA_IS_HINT_DEFAULT_LOW(HintDesc)) {
  389. lp = 0.75f;
  390. up = 0.25f;
  391. } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(HintDesc)) {
  392. lp = 0.5f;
  393. up = 0.5f;
  394. } else if (LADSPA_IS_HINT_DEFAULT_HIGH(HintDesc)) {
  395. lp = 0.25f;
  396. up = 0.75f;
  397. }
  398. if (LADSPA_IS_HINT_LOGARITHMIC(HintDesc)) {
  399. p.hints.type = Port::Hints::LOGARITHMIC;
  400. if (min==0.0f || max==0.0f) {
  401. // Zero at either end means zero no matter
  402. // where hint is at, since:
  403. // log(n->0) -> Infinity
  404. Default = 0.0f;
  405. } else {
  406. // Catch negatives
  407. bool neg_min = min < 0.0f ? true : false;
  408. bool neg_max = max < 0.0f ? true : false;
  409. if (!neg_min && !neg_max) {
  410. Default = exp(::log(min) * lp + ::log(max) * up);
  411. } else if (neg_min && neg_max) {
  412. Default = -exp(::log(-min) * lp + ::log(-max) * up);
  413. } else {
  414. // Logarithmic range has asymptote
  415. // so just use linear scale
  416. Default = min * lp + max * up;
  417. }
  418. }
  419. } else {
  420. Default = min * lp + max * up;
  421. }
  422. }
  423. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc)) {
  424. Default *= chain()->engine()->sample_rate();
  425. }
  426. if (LADSPA_IS_HINT_INTEGER(HintDesc)) {
  427. if ( p.hints.ranged &&
  428. 0 == p.hints.minimum &&
  429. 1 == p.hints.maximum )
  430. p.hints.type = Port::Hints::BOOLEAN;
  431. else
  432. p.hints.type = Port::Hints::INTEGER;
  433. Default = floorf(Default);
  434. }
  435. if (LADSPA_IS_HINT_TOGGLED(HintDesc)){
  436. p.hints.type = Port::Hints::BOOLEAN;
  437. }
  438. }
  439. }
  440. #else
  441. Default = 0.0f;
  442. #endif
  443. p.hints.default_value = Default;
  444. }
  445. float *control_value = new float;
  446. *control_value = p.hints.default_value;
  447. p.connect_to( control_value );
  448. add_port( p );
  449. DMESSAGE( "Plugin has control port \"%s\" (default: %f)", _idata->descriptor->PortNames[ i ], p.hints.default_value );
  450. }
  451. }
  452. }
  453. else
  454. {
  455. WARNING( "Failed to load plugin" );
  456. return false;
  457. }
  458. return plugin_instances( 1 );
  459. }
  460. void
  461. Plugin_Module::set_input_buffer ( int n, void *buf )
  462. {
  463. LADSPA_Handle h;
  464. if ( instances() > 1 )
  465. {
  466. h = _idata->handle[n];
  467. n = 0;
  468. }
  469. else
  470. h = _idata->handle[0];
  471. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  472. if ( LADSPA_IS_PORT_INPUT( _idata->descriptor->PortDescriptors[i] ) &&
  473. LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  474. if ( n-- == 0 )
  475. _idata->descriptor->connect_port( h, i, (LADSPA_Data*)buf );
  476. }
  477. void
  478. Plugin_Module::set_output_buffer ( int n, void *buf )
  479. {
  480. LADSPA_Handle h;
  481. if ( instances() > 1 )
  482. {
  483. h = _idata->handle[n];
  484. n = 0;
  485. }
  486. else
  487. h = _idata->handle[0];
  488. for ( unsigned int i = 0; i < _idata->descriptor->PortCount; ++i )
  489. if ( LADSPA_IS_PORT_OUTPUT( _idata->descriptor->PortDescriptors[i] ) &&
  490. LADSPA_IS_PORT_AUDIO( _idata->descriptor->PortDescriptors[i] ) )
  491. if ( n-- == 0 )
  492. _idata->descriptor->connect_port( h, i, (LADSPA_Data*)buf );
  493. }
  494. void
  495. Plugin_Module::activate ( void )
  496. {
  497. if ( _active )
  498. FATAL( "Attempt to activate already active plugin" );
  499. if ( _idata->descriptor->activate )
  500. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  501. _idata->descriptor->activate( _idata->handle[i] );
  502. _active = true;
  503. }
  504. void
  505. Plugin_Module::deactivate( void )
  506. {
  507. if ( _idata->descriptor->deactivate )
  508. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  509. _idata->descriptor->activate( _idata->handle[i] );
  510. _active = false;
  511. }
  512. void
  513. Plugin_Module::handle_port_connection_change ( void )
  514. {
  515. // DMESSAGE( "Connecting audio ports" );
  516. if ( _crosswire )
  517. {
  518. for ( int i = 0; i < plugin_ins(); ++i )
  519. set_input_buffer( i, audio_input[0].buffer() );
  520. }
  521. else
  522. {
  523. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  524. set_input_buffer( i, audio_input[i].buffer() );
  525. }
  526. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  527. set_output_buffer( i, audio_output[i].buffer() );
  528. }
  529. void
  530. Plugin_Module::process ( )
  531. {
  532. handle_port_connection_change();
  533. if ( _active )
  534. for ( unsigned int i = 0; i < _idata->handle.size(); ++i )
  535. {
  536. volatile int n = i;
  537. _idata->descriptor->run( _idata->handle[n], nframes() );
  538. }
  539. }