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.

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