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.

743 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. /* rename chain... we have to let our modules know our name has
  322. * changed so they can take the appropriate action (in particular the
  323. * JACK module). */
  324. void
  325. Chain::name ( const char *name )
  326. {
  327. char ename[512];
  328. snprintf( ename, sizeof(ename), "%s/%s", APP_NAME, name );
  329. if ( ! _engine )
  330. {
  331. _engine = new Engine( &Chain::process, this );
  332. engine()->buffer_size_callback( &Chain::buffer_size, this );
  333. engine()->init( ename );
  334. }
  335. else
  336. {
  337. DMESSAGE( "Renaming JACK client from \"%s\" to \"%s\"", _name, ename );
  338. _name = engine()->name( ename );
  339. /* FIXME: discarding the name jack picked is technically wrong! */
  340. }
  341. _name = name;
  342. for ( int i = 0; i < modules(); ++i )
  343. module( i )->handle_chain_name_changed();
  344. }
  345. bool
  346. Chain::add ( Module *m )
  347. {
  348. /* FIXME: hacky */
  349. if ( !strcmp( m->name(), "Controller" ) )
  350. return false;
  351. else
  352. return insert( NULL, m );
  353. }
  354. bool
  355. Chain::add ( Controller_Module *m )
  356. {
  357. DMESSAGE( "Adding control" );
  358. add_control(m);
  359. return true;
  360. }
  361. bool
  362. Chain::insert ( Module *m, Module *n )
  363. {
  364. engine()->lock();
  365. if ( !m )
  366. {
  367. if ( modules() == 0 && n->can_support_inputs( 0 ) >= 0 )
  368. {
  369. n->configure_inputs( 0 );
  370. modules_pack->add( n );
  371. n->chain( this );
  372. }
  373. else if ( n->can_support_inputs( module( modules() - 1 )->noutputs() ) >= 0 )
  374. {
  375. n->configure_inputs( module( modules() - 1 )->noutputs() );
  376. modules_pack->add( n );
  377. n->chain( this );
  378. }
  379. else
  380. {
  381. DMESSAGE( "Module says it can't support %i inputs", module( modules() - 1 )->noutputs() );
  382. goto err;
  383. }
  384. }
  385. else
  386. {
  387. int i = modules_pack->find( m );
  388. if ( 0 == i )
  389. {
  390. /* inserting to head of chain*/
  391. if ( n->can_support_inputs( 0 ) >= 0 )
  392. n->configure_inputs( 0 );
  393. else
  394. goto err;
  395. }
  396. else
  397. {
  398. if ( n->can_support_inputs( module( i - 1 )->noutputs() ) >= 0 )
  399. {
  400. n->configure_inputs( module( i - 1 )->noutputs() );
  401. m->configure_inputs( n->noutputs() );
  402. for ( int j = i + 1; j < modules(); ++j )
  403. module( j )->configure_inputs( module( j - 1 )->noutputs() );
  404. }
  405. else
  406. goto err;
  407. }
  408. modules_pack->insert( *n, i );
  409. n->chain( this );
  410. }
  411. DMESSAGE( "Module \"%s\" has %i:%i audio and %i:%i control ports",
  412. n->name(),
  413. n->ninputs(),
  414. n->noutputs(),
  415. n->ncontrol_inputs(),
  416. n->ncontrol_outputs() );
  417. strip()->handle_module_added( n );
  418. configure_ports();
  419. engine()->unlock();
  420. return true;
  421. err:
  422. DMESSAGE( "Insert failed" );
  423. engine()->unlock();
  424. return false;
  425. }
  426. /* add a control to the control strip. Assumed to already be connected! */
  427. void
  428. Chain::add_control ( Controller_Module *m )
  429. {
  430. engine()->lock();
  431. controls_pack->add( m );
  432. configure_ports();
  433. engine()->unlock();
  434. controls_pack->redraw();
  435. }
  436. void
  437. Chain::draw_connections ( Module *m )
  438. {
  439. int spacing;
  440. int offset;
  441. int X, Y, W, H;
  442. ((Fl_Packscroller*)chain_tab->child( 0 ))->bbox( X, Y, W, H );
  443. fl_push_clip( X, Y, W, H );
  444. Fl_Color c =fl_color_average( FL_WHITE, FL_YELLOW, 0.50 );
  445. fl_color( c );
  446. if ( m->ninputs() )
  447. {
  448. spacing = w() / m->ninputs();
  449. offset = spacing / 2;
  450. for ( int i = m->ninputs(); i--; )
  451. fl_rectf( m->x() + offset + ( spacing * i ), m->y() - 5, 2, 5 );
  452. }
  453. fl_color( fl_darker( c ) );
  454. if ( m->noutputs() )
  455. {
  456. spacing = w() / m->noutputs();
  457. offset = spacing / 2;
  458. for ( int i = m->noutputs(); i--; )
  459. fl_rectf( m->x() + offset + ( spacing * i ), m->y() + m->h(), 2, 5 );
  460. }
  461. fl_pop_clip();
  462. }
  463. void
  464. Chain::add_to_process_queue ( Module *m )
  465. {
  466. for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
  467. if ( m == *i )
  468. return;
  469. process_queue.push_back( m );
  470. }
  471. /* run any time the internal connection graph might have
  472. * changed... Tells the process thread what order modules need to be
  473. * run in. */
  474. void
  475. Chain::build_process_queue ( void )
  476. {
  477. process_queue.clear();
  478. for ( int i = 0; i < modules(); ++i )
  479. {
  480. Module *m = (Module*)module( i );
  481. /* controllers */
  482. for ( unsigned int j = 0; j < m->control_input.size(); ++j )
  483. {
  484. if ( m->control_input[j].connected() )
  485. {
  486. add_to_process_queue( m->control_input[j].connected_port()->module() );
  487. }
  488. }
  489. /* audio modules */
  490. add_to_process_queue( m );
  491. /* indicators */
  492. for ( unsigned int j = 0; j < m->control_output.size(); ++j )
  493. {
  494. if ( m->control_output[j].connected() )
  495. {
  496. add_to_process_queue( m->control_output[j].connected_port()->module() );
  497. }
  498. }
  499. }
  500. /* connect all the ports to the buffers */
  501. for ( int i = 0; i < modules(); ++i )
  502. {
  503. Module *m = module( i );
  504. for ( unsigned int j = 0; j < m->audio_input.size(); ++j )
  505. {
  506. m->audio_input[j].connect_to( &scratch_port[j] );
  507. }
  508. for ( unsigned int j = 0; j < m->audio_output.size(); ++j )
  509. {
  510. m->audio_output[j].connect_to( &scratch_port[j] );
  511. }
  512. m->handle_port_connection_change();
  513. }
  514. /* DMESSAGE( "Process queue looks like:" ); */
  515. /* for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i ) */
  516. /* { */
  517. /* const Module* m = *i; */
  518. /* if ( m->audio_input.size() || m->audio_output.size() ) */
  519. /* DMESSAGE( "\t%s", (*i)->name() ); */
  520. /* else if ( m->control_output.size() ) */
  521. /* DMESSAGE( "\t%s -->", (*i)->name() ); */
  522. /* else if ( m->control_input.size() ) */
  523. /* DMESSAGE( "\t%s <--", (*i)->name() ); */
  524. /* { */
  525. /* char *s = m->get_parameters(); */
  526. /* DMESSAGE( "(%s)", s ); */
  527. /* delete[] s; */
  528. /* } */
  529. /* } */
  530. }
  531. void
  532. Chain::strip ( Mixer_Strip * ms )
  533. {
  534. _strip = ms;
  535. }
  536. void
  537. Chain::draw ( void )
  538. {
  539. Fl_Group::draw();
  540. /* if ( 0 == strcmp( "Chain", tabs->value()->label() ) ) */
  541. if ( chain_tab->visible() )
  542. for ( int i = 0; i < modules(); ++i )
  543. draw_connections( module( i ) );
  544. }
  545. void
  546. Chain::resize ( int X, int Y, int W, int H )
  547. {
  548. Fl_Group::resize( X, Y, W, H );
  549. /* this won't naturally resize because it's inside of an Fl_Scroll... */
  550. controls_pack->size( W, controls_pack->h() );
  551. }
  552. /**********/
  553. /* Engine */
  554. /**********/
  555. void
  556. Chain::process ( nframes_t nframes, void *v )
  557. {
  558. ((Chain*)v)->process( nframes );
  559. }
  560. void
  561. Chain::process ( nframes_t nframes )
  562. {
  563. for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
  564. {
  565. Module *m = *i;
  566. if ( ! m->bypass() )
  567. m->process( nframes );
  568. }
  569. }
  570. void
  571. Chain::buffer_size ( nframes_t nframes, void *v )
  572. {
  573. ((Chain*)v)->buffer_size( nframes );
  574. }
  575. void
  576. Chain::buffer_size ( nframes_t nframes )
  577. {
  578. for ( unsigned int i = scratch_port.size(); i--; )
  579. delete[] (sample_t*)scratch_port[i].buffer();
  580. scratch_port.clear();
  581. configure_ports();
  582. for ( int i = 0; i < modules(); ++i )
  583. {
  584. Module *m = module(i);
  585. m->resize_buffers( nframes );
  586. }
  587. }