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.

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