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.

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