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.

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