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.

700 lines
17KB

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