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.

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