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.

798 lines
20KB

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