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.

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