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.

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