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.

620 lines
16KB

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