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.

791 lines
19KB

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