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.

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