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.

731 lines
18KB

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