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.

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