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.

584 lines
15KB

  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 chain. This would all be much simpler if we chose not to
  19. * allow non 1:1 plugins to be mixed in a single chain...
  20. *
  21. * Supporting the mixture requires duplicating some inputs (to satisfy
  22. * stereo input plugins reading mono outputs) and duplicating some
  23. * plugins (to satisfy mono input plugins reading stereo outputs).
  24. *
  25. * Basically, what this means is that the intermediate number of
  26. * buffers need not have any relation to the starting and ending
  27. * buffer count. (Picture an ambisonic panner going into an ambisonic
  28. * decoder (1:6:2).
  29. *
  30. * The chain will allocate enough buffers to hold data from the
  31. * maximum number of channels used by a contained module.
  32. *
  33. * The process thread goes as follows:
  34. *
  35. * 1. Copy inputs to chain buffers.
  36. *
  37. * 2. process() each module in turn (reusing buffers in-place) (inputs
  38. * will be copied or plugins duplicated as necessary)
  39. *
  40. * 3. Copy chain buffers to outputs.
  41. *
  42. * For chains where the number of channels never exceeds the maximum
  43. * of the number of inputs and outputs, the first copy can be
  44. * optimized out.
  45. */
  46. #include "Chain.H"
  47. #include "Module.H"
  48. #include "Meter_Module.H"
  49. #include "JACK_Module.H"
  50. #include "Gain_Module.H"
  51. #include "Plugin_Module.H"
  52. #include <Fl/Fl_Box.H>
  53. #include <FL/Fl_Menu.H>
  54. #include <FL/fl_ask.H>
  55. #include <stdlib.h>
  56. #include "util/debug.h"
  57. #include <stdio.h>
  58. #include <FL/fl_draw.H>
  59. #include "Engine/Engine.H"
  60. #include <FL/Fl_Tabs.H>
  61. #include "FL/Fl_Flowpack.H"
  62. #include "FL/Fl_Scroll.H"
  63. #include <string.h>
  64. Chain::Chain ( int X, int Y, int W, int H, const char *L ) :
  65. Fl_Group( X, Y, W, H, L)
  66. {
  67. _outs = 1;
  68. _ins = 1;
  69. _configure_outputs_callback = NULL;
  70. _name = NULL;
  71. { Fl_Tabs *o = tabs = new Fl_Tabs( X, Y, W, H );
  72. { Fl_Group *o = new Fl_Group( X, Y + 24, W, H - 24, "Chain" );
  73. o->box( FL_FLAT_BOX );
  74. o->labelsize( 9 );
  75. { Fl_Pack *o = modules_pack = new Fl_Pack( X, Y + 24, W, H - 24 );
  76. o->type( Fl_Pack::VERTICAL );
  77. o->spacing( 10 );
  78. o->end();
  79. }
  80. o->end();
  81. }
  82. { Fl_Group *o = new Fl_Group( X, Y + 24, W, H - 24, "Controls" );
  83. o->labelsize( 9 );
  84. o->hide();
  85. { Fl_Scroll *o = new Fl_Scroll( X, Y + 24, W, H - 24 );
  86. o->type( Fl_Scroll::VERTICAL );
  87. { Fl_Flowpack *o = controls_pack = new Fl_Flowpack( X, Y + 24, W, H - 24 );
  88. o->hspacing( 10 );
  89. o->vspacing( 10 );
  90. // o->box( FL_FLAT_BOX );
  91. // o->color( FL_RED );
  92. o->end();
  93. Fl_Group::current()->resizable( o );
  94. }
  95. o->end();
  96. Fl_Group::current()->resizable( o );
  97. }
  98. o->end();
  99. Fl_Group::current()->resizable( o );
  100. }
  101. o->end();
  102. Fl_Group::current()->resizable( o );
  103. }
  104. end();
  105. }
  106. /* Fill this chain with JACK I/O, Gain, and Meter modules. */
  107. void
  108. Chain::initialize_with_default ( void )
  109. {
  110. {
  111. JACK_Module *jm = new JACK_Module( 50, 50, "JACK" );
  112. jm->chain( this );
  113. jm->configure_outputs( 1 );
  114. jm->initialize();
  115. jm->color( FL_BLACK );
  116. insert( NULL, jm );
  117. }
  118. {
  119. JACK_Module *m = new JACK_Module( 50, 50, "JACK" );
  120. m->chain( this );
  121. m->initialize();
  122. m->color( FL_BLACK );
  123. insert( NULL, m );
  124. }
  125. }
  126. void Chain::cb_handle(Fl_Widget* o) {
  127. /* if ( o == head_button ) */
  128. /* { */
  129. /* Module *m = Module::pick_plugin(); */
  130. /* insert_before( (Module*)modules_pack->child( 0 ), m ); */
  131. /* } */
  132. /* else if ( o == tail_button ) */
  133. /* { */
  134. /* Module *m = Module::pick_plugin(); */
  135. /* insert_before( 0, m ); */
  136. /* } */
  137. }
  138. void Chain::cb_handle(Fl_Widget* o, void* v) {
  139. ((Chain*)(v))->cb_handle(o);
  140. }
  141. /* remove a module from the chain. this isn't guaranteed to succeed,
  142. * because removing the module might result in an invalid routing */
  143. void
  144. Chain::remove ( Module *m )
  145. {
  146. int i = modules_pack->find( m );
  147. int ins = 0;
  148. if ( i != 0 )
  149. ins = module( i - 1 )->noutputs();
  150. if ( ! can_configure_outputs( m, ins ) )
  151. {
  152. fl_alert( "Can't remove module at this point because the resultant chain is invalid" );
  153. }
  154. modules_pack->remove( m );
  155. configure_ports();
  156. }
  157. /* determine number of output ports, signal if changed. */
  158. void
  159. Chain::configure_ports ( void )
  160. {
  161. int old_outs = outs();
  162. int nouts = 0;
  163. engine->lock();
  164. for ( int i = 0; i < modules(); ++i )
  165. {
  166. module( i )->configure_inputs( nouts );
  167. nouts = module( i )->noutputs();
  168. }
  169. outs( nouts );
  170. int req_buffers = required_buffers();
  171. if ( outs() != old_outs )
  172. {
  173. if ( configure_outputs_callback() )
  174. configure_outputs_callback()( this, _configure_outputs_userdata );
  175. }
  176. DMESSAGE( "required_buffers = %i", req_buffers );
  177. if ( port.size() != req_buffers )
  178. {
  179. for ( unsigned int i = port.size(); i--; )
  180. delete[] port[i].buffer();
  181. port.clear();
  182. for ( unsigned int i = 0; i < req_buffers; ++i )
  183. {
  184. Module::Port p( NULL, Module::Port::OUTPUT, Module::Port::AUDIO );
  185. p.connect_to( new sample_t[engine->nframes()] );
  186. port.push_back( p );
  187. }
  188. }
  189. build_process_queue();
  190. engine->unlock();
  191. parent()->redraw();
  192. }
  193. /* calculate the minimum number of buffers required to satisfy this chain */
  194. int
  195. Chain::required_buffers ( void )
  196. {
  197. int buffers = 0;
  198. int outs = 0;
  199. for ( int i = 0; i < modules(); ++i )
  200. {
  201. outs = module( i )->can_support_inputs( outs );
  202. if ( outs > buffers )
  203. buffers = outs;
  204. }
  205. return buffers;
  206. }
  207. /* called by a module when it wants to alter the number of its
  208. * outputs. Also used to test for chain validity when inserting /
  209. * removing modules */
  210. bool
  211. Chain::can_configure_outputs ( Module *m, int n ) const
  212. {
  213. /* start at the requesting module */
  214. int outs = n;
  215. int i = modules_pack->find( m );
  216. if ( modules() - 1 == i )
  217. /* last module */
  218. return true;
  219. for ( i++ ; i < modules(); ++i )
  220. {
  221. outs = module( i )->can_support_inputs( outs );
  222. if ( outs < 0 )
  223. return false;
  224. }
  225. return true;
  226. }
  227. /* return true if this chain can be converted to support /n/ input channels */
  228. bool
  229. Chain::can_support_input_channels ( int n )
  230. {
  231. /* FIXME: implement */
  232. return true;
  233. }
  234. /* rename chain... we have to let our modules know our name has
  235. * changed so they can take the appropriate action (in particular the
  236. * JACK module). */
  237. void
  238. Chain::name ( const char *name )
  239. {
  240. _name = name;
  241. for ( int i = 0; i < modules(); ++i )
  242. module( i )->handle_chain_name_changed();
  243. }
  244. #include "FL/menu_popup.H"
  245. bool
  246. Chain::insert ( Module *m, Module *n )
  247. {
  248. engine->lock();
  249. if ( !m )
  250. {
  251. if ( modules() == 0 && n->can_support_inputs( 0 ) >= 0 )
  252. {
  253. n->configure_inputs( 0 );
  254. modules_pack->add( n );
  255. n->chain( this );
  256. }
  257. else if ( n->can_support_inputs( module( modules() - 1 )->noutputs() ) >= 0 )
  258. {
  259. n->configure_inputs( module( modules() - 1 )->noutputs() );
  260. modules_pack->add( n );
  261. n->chain( this );
  262. }
  263. else
  264. goto err;
  265. }
  266. else
  267. {
  268. int i = modules_pack->find( m );
  269. if ( 0 == i )
  270. {
  271. /* inserting to head of chain*/
  272. if ( n->can_support_inputs( 0 ) >= 0 )
  273. n->configure_inputs( 0 );
  274. else
  275. goto err;
  276. }
  277. else
  278. {
  279. if ( n->can_support_inputs( module( i - 1 )->noutputs() ) >= 0 )
  280. {
  281. n->configure_inputs( module( i - 1 )->noutputs() );
  282. m->configure_inputs( n->noutputs() );
  283. for ( int j = i + 1; j < modules(); ++j )
  284. module( j )->configure_inputs( module( j - 1 )->noutputs() );
  285. }
  286. else
  287. goto err;
  288. }
  289. modules_pack->insert( *n, i );
  290. n->chain( this );
  291. }
  292. DMESSAGE( "Module has %i:%i audio and %i:%i control ports",
  293. n->ninputs(),
  294. n->noutputs(),
  295. n->ncontrol_inputs(),
  296. n->ncontrol_outputs() );
  297. configure_ports();
  298. engine->unlock();
  299. return true;
  300. err:
  301. engine->unlock();
  302. return false;
  303. }
  304. /* add a control to the control strip. Assumed to already be connected! */
  305. void
  306. Chain::add_control ( Module *m )
  307. {
  308. controls_pack->add( m );
  309. }
  310. void
  311. Chain::draw_connections ( Module *m )
  312. {
  313. int spacing;
  314. int offset;
  315. Fl_Color c =fl_color_average( FL_WHITE, FL_YELLOW, 0.50 );
  316. fl_color( c );
  317. if ( m->ninputs() )
  318. {
  319. spacing = w() / m->ninputs();
  320. offset = spacing / 2;
  321. for ( int i = m->ninputs(); i--; )
  322. fl_rectf( m->x() + offset + ( spacing * i ), m->y() - 5, 2, 5 );
  323. }
  324. fl_color( fl_darker( c ) );
  325. if ( m->noutputs() )
  326. {
  327. spacing = w() / m->noutputs();
  328. offset = spacing / 2;
  329. for ( int i = m->noutputs(); i--; )
  330. fl_rectf( m->x() + offset + ( spacing * i ), m->y() + m->h(), 2, 5 );
  331. }
  332. }
  333. void
  334. Chain::add_to_process_queue ( Module *m )
  335. {
  336. for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
  337. if ( m == *i )
  338. return;
  339. process_queue.push_back( m );
  340. }
  341. /* run any time the internal connection graph might have
  342. * changed... Tells the process thread what order modules need to be
  343. * run in. */
  344. void
  345. Chain::build_process_queue ( void )
  346. {
  347. process_queue.clear();
  348. for ( int i = 0; i < modules(); ++i )
  349. {
  350. Module *m = (Module*)module( i );
  351. /* controllers */
  352. for ( unsigned int j = 0; j < m->control_input.size(); ++j )
  353. {
  354. if ( m->control_input[j].connected() )
  355. {
  356. add_to_process_queue( m->control_input[j].connected_port()->module() );
  357. }
  358. }
  359. /* audio modules */
  360. add_to_process_queue( m );
  361. /* indicators */
  362. for ( unsigned int j = 0; j < m->control_output.size(); ++j )
  363. {
  364. if ( m->control_output[j].connected() )
  365. {
  366. add_to_process_queue( m->control_output[j].connected_port()->module() );
  367. }
  368. }
  369. }
  370. /* connect all the ports to the buffers */
  371. for ( int i = 0; i < modules(); ++i )
  372. {
  373. Module *m = module( i );
  374. for ( unsigned int j = 0; j < m->audio_input.size(); ++j )
  375. {
  376. m->audio_input[j].connect_to( &port[j] );
  377. }
  378. for ( unsigned int j = 0; j < m->audio_output.size(); ++j )
  379. {
  380. m->audio_output[j].connect_to( &port[j] );
  381. }
  382. }
  383. DMESSAGE( "Process queue looks like:" );
  384. for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
  385. {
  386. const Module* m = *i;
  387. if ( m->audio_input.size() || m->audio_output.size() )
  388. DMESSAGE( "\t%s", (*i)->name() );
  389. else if ( m->control_output.size() )
  390. DMESSAGE( "\t%s -->", (*i)->name() );
  391. else if ( m->control_input.size() )
  392. DMESSAGE( "\t%s <--", (*i)->name() );
  393. }
  394. }
  395. void
  396. Chain::draw ( void )
  397. {
  398. Fl_Group::draw();
  399. if ( 0 == strcmp( "Chain", tabs->value()->label() ) )
  400. for ( int i = 0; i < modules(); ++i )
  401. draw_connections( module( i ) );
  402. }
  403. void
  404. Chain::resize ( int X, int Y, int W, int H )
  405. {
  406. Fl_Group::resize( X, Y, W, H );
  407. /* this won't naturally resize because it's inside of an Fl_Scroll... */
  408. controls_pack->size( W, controls_pack->h() );
  409. }
  410. #include "FL/test_press.H"
  411. int
  412. Chain::handle ( int m )
  413. {
  414. switch ( m )
  415. {
  416. case FL_PUSH:
  417. {
  418. if ( Fl::belowmouse() != this )
  419. {
  420. Module *m = NULL;
  421. for ( int i = 0; i < modules(); ++i )
  422. if ( Fl::event_inside( module( i ) ) )
  423. {
  424. m = module( i );
  425. break;
  426. }
  427. if ( m )
  428. {
  429. if ( test_press( FL_BUTTON3 | FL_CTRL ) )
  430. {
  431. if ( FL_BLACK == m->color() )
  432. {
  433. /* FIXME: hack */
  434. fl_alert( "Cannot delete this module." );
  435. }
  436. else
  437. {
  438. remove( m );
  439. delete m;
  440. redraw();
  441. }
  442. return 1;
  443. }
  444. else if ( test_press( FL_BUTTON1 | FL_SHIFT ) )
  445. {
  446. Module *mod = (Module*)Plugin_Module::pick_plugin();
  447. if ( mod )
  448. {
  449. if ( ! insert( m, mod ) )
  450. fl_alert( "Cannot insert this module at this point in the chain" );
  451. redraw();
  452. }
  453. return 1;
  454. }
  455. else if ( test_press( FL_BUTTON1 | FL_CTRL ) )
  456. {
  457. if ( m->active() )
  458. m->deactivate();
  459. else
  460. m->activate();
  461. return 1;
  462. }
  463. }
  464. }
  465. break;
  466. }
  467. }
  468. return Fl_Group::handle( m );
  469. }
  470. void
  471. Chain::process ( nframes_t nframes )
  472. {
  473. for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
  474. {
  475. Module *m = *i;
  476. m->nframes( nframes );
  477. if ( m->active() )
  478. m->process();
  479. }
  480. }