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.

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