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.

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