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.

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