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.

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