2 /*******************************************************************************/
3 /* Copyright (C) 2009 Jonathan Moore Liles */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
20 /* Filter chain. This would all be much simpler if we chose not to
21 * allow non 1:1 plugins to be mixed in a single chain...
23 * Supporting the mixture requires duplicating some inputs (to satisfy
24 * stereo input plugins reading mono outputs) and duplicating some
25 * plugins (to satisfy mono input plugins reading stereo outputs).
27 * Basically, what this means is that the intermediate number of
28 * buffers need not have any relation to the starting and ending
29 * buffer count. (Picture an ambisonic panner going into an ambisonic
32 * The chain will allocate enough buffers to hold data from the
33 * maximum number of channels used by a contained module.
35 * The process thread goes as follows:
37 * 1. Copy inputs to chain buffers.
39 * 2. process() each module in turn (reusing buffers in-place) (inputs
40 * will be copied or plugins duplicated as necessary)
42 * 3. Copy chain buffers to outputs.
44 * For chains where the number of channels never exceeds the maximum
45 * of the number of inputs and outputs, the first copy can be
52 #include "Meter_Module.H"
53 #include "JACK_Module.H"
54 #include "Gain_Module.H"
55 #include "Plugin_Module.H"
57 #include <Fl/Fl_Box.H>
58 #include <FL/Fl_Menu.H>
59 #include <FL/fl_ask.H>
62 #include "util/debug.h"
66 #include <FL/fl_draw.H>
68 #include "Engine/Engine.H"
69 #include <FL/Fl_Tabs.H>
70 #include "FL/Fl_Flowpack.H"
71 #include "FL/Fl_Scroll.H"
78 std::vector <Module::Port> Chain::port;
79 std::list <Chain*> Chain::chain;
83 Chain::Chain ( int X, int Y, int W, int H, const char *L ) :
84 Fl_Group( X, Y, W, H, L)
89 _configure_outputs_callback = NULL;
93 { Fl_Tabs *o = tabs = new Fl_Tabs( X, Y, W, H );
94 { Fl_Group *o = new Fl_Group( X, Y + 24, W, H - 24, "Chain" );
95 o->box( FL_FLAT_BOX );
97 { Fl_Pack *o = modules_pack = new Fl_Pack( X, Y + 24, W, H - 24 );
98 o->type( Fl_Pack::VERTICAL );
104 { Fl_Group *o = new Fl_Group( X, Y + 24, W, H - 24, "Controls" );
107 { Fl_Scroll *o = new Fl_Scroll( X, Y + 24, W, H - 24 );
108 o->type( Fl_Scroll::VERTICAL );
109 { Fl_Flowpack *o = controls_pack = new Fl_Flowpack( X, Y + 24, W, H - 24 );
112 // o->box( FL_FLAT_BOX );
113 // o->color( FL_RED );
115 Fl_Group::current()->resizable( o );
118 Fl_Group::current()->resizable( o );
121 Fl_Group::current()->resizable( o );
124 Fl_Group::current()->resizable( o );
129 chain.push_back( this );
134 chain.remove( this );
137 /* Fill this chain with JACK I/O, Gain, and Meter modules. */
139 Chain::initialize_with_default ( void )
143 JACK_Module *jm = new JACK_Module( 50, 50, "JACK" );
145 jm->configure_outputs( 1 );
148 jm->color( FL_BLACK );
153 JACK_Module *m = new JACK_Module( 50, 50, "JACK" );
156 m->color( FL_BLACK );
162 void Chain::cb_handle(Fl_Widget* o) {
163 /* if ( o == head_button ) */
165 /* Module *m = Module::pick_plugin(); */
167 /* insert_before( (Module*)modules_pack->child( 0 ), m ); */
169 /* else if ( o == tail_button ) */
171 /* Module *m = Module::pick_plugin(); */
172 /* insert_before( 0, m ); */
176 void Chain::cb_handle(Fl_Widget* o, void* v) {
177 ((Chain*)(v))->cb_handle(o);
181 /* remove a module from the chain. this isn't guaranteed to succeed,
182 * because removing the module might result in an invalid routing */
184 Chain::remove ( Module *m )
186 int i = modules_pack->find( m );
191 ins = module( i - 1 )->noutputs();
193 if ( ! can_configure_outputs( m, ins ) )
195 fl_alert( "Can't remove module at this point because the resultant chain is invalid" );
198 modules_pack->remove( m );
203 /* determine number of output ports, signal if changed. */
205 Chain::configure_ports ( void )
207 int old_outs = outs();
212 for ( int i = 0; i < modules(); ++i )
214 module( i )->configure_inputs( nouts );
215 nouts = module( i )->noutputs();
220 int req_buffers = required_buffers();
222 if ( outs() != old_outs )
224 if ( configure_outputs_callback() )
225 configure_outputs_callback()( this, _configure_outputs_userdata );
228 DMESSAGE( "required_buffers = %i", req_buffers );
230 if ( port.size() < req_buffers )
232 for ( unsigned int i = port.size(); i--; )
233 delete[] (sample_t*)port[i].buffer();
236 for ( unsigned int i = 0; i < req_buffers; ++i )
238 Module::Port p( NULL, Module::Port::OUTPUT, Module::Port::AUDIO );
239 p.connect_to( new sample_t[engine->nframes()] );
240 buffer_fill_with_silence( (sample_t*)p.buffer(), engine->nframes() );
245 build_process_queue();
247 /* let the other chains know we mess with their buffers */
248 for ( std::list<Chain*>::iterator i = chain.begin();
253 (*i)->build_process_queue();
261 /* calculate the minimum number of buffers required to satisfy this chain */
263 Chain::required_buffers ( void )
268 for ( int i = 0; i < modules(); ++i )
270 outs = module( i )->can_support_inputs( outs );
272 if ( outs > buffers )
279 /* called by a module when it wants to alter the number of its
280 * outputs. Also used to test for chain validity when inserting /
281 * removing modules */
283 Chain::can_configure_outputs ( Module *m, int n ) const
285 /* start at the requesting module */
289 int i = modules_pack->find( m );
291 if ( modules() - 1 == i )
295 for ( i++ ; i < modules(); ++i )
297 outs = module( i )->can_support_inputs( outs );
306 /* return true if this chain can be converted to support /n/ input channels */
308 Chain::can_support_input_channels ( int n )
310 /* FIXME: implement */
314 /* rename chain... we have to let our modules know our name has
315 * changed so they can take the appropriate action (in particular the
318 Chain::name ( const char *name )
322 for ( int i = 0; i < modules(); ++i )
323 module( i )->handle_chain_name_changed();
330 #include "FL/menu_popup.H"
333 Chain::insert ( Module *m, Module *n )
340 if ( modules() == 0 && n->can_support_inputs( 0 ) >= 0 )
342 n->configure_inputs( 0 );
343 modules_pack->add( n );
346 else if ( n->can_support_inputs( module( modules() - 1 )->noutputs() ) >= 0 )
348 n->configure_inputs( module( modules() - 1 )->noutputs() );
349 modules_pack->add( n );
357 int i = modules_pack->find( m );
361 /* inserting to head of chain*/
362 if ( n->can_support_inputs( 0 ) >= 0 )
363 n->configure_inputs( 0 );
369 if ( n->can_support_inputs( module( i - 1 )->noutputs() ) >= 0 )
371 n->configure_inputs( module( i - 1 )->noutputs() );
373 m->configure_inputs( n->noutputs() );
375 for ( int j = i + 1; j < modules(); ++j )
376 module( j )->configure_inputs( module( j - 1 )->noutputs() );
382 modules_pack->insert( *n, i );
386 DMESSAGE( "Module has %i:%i audio and %i:%i control ports",
389 n->ncontrol_inputs(),
390 n->ncontrol_outputs() );
405 /* add a control to the control strip. Assumed to already be connected! */
407 Chain::add_control ( Module *m )
409 controls_pack->add( m );
413 Chain::draw_connections ( Module *m )
418 Fl_Color c =fl_color_average( FL_WHITE, FL_YELLOW, 0.50 );
423 spacing = w() / m->ninputs();
424 offset = spacing / 2;
426 for ( int i = m->ninputs(); i--; )
427 fl_rectf( m->x() + offset + ( spacing * i ), m->y() - 5, 2, 5 );
430 fl_color( fl_darker( c ) );
434 spacing = w() / m->noutputs();
435 offset = spacing / 2;
436 for ( int i = m->noutputs(); i--; )
437 fl_rectf( m->x() + offset + ( spacing * i ), m->y() + m->h(), 2, 5 );
442 Chain::add_to_process_queue ( Module *m )
444 for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
448 process_queue.push_back( m );
451 /* run any time the internal connection graph might have
452 * changed... Tells the process thread what order modules need to be
455 Chain::build_process_queue ( void )
457 process_queue.clear();
459 for ( int i = 0; i < modules(); ++i )
461 Module *m = (Module*)module( i );
464 for ( unsigned int j = 0; j < m->control_input.size(); ++j )
466 if ( m->control_input[j].connected() )
468 add_to_process_queue( m->control_input[j].connected_port()->module() );
473 add_to_process_queue( m );
476 for ( unsigned int j = 0; j < m->control_output.size(); ++j )
478 if ( m->control_output[j].connected() )
480 add_to_process_queue( m->control_output[j].connected_port()->module() );
485 /* connect all the ports to the buffers */
486 for ( int i = 0; i < modules(); ++i )
488 Module *m = module( i );
489 for ( unsigned int j = 0; j < m->audio_input.size(); ++j )
491 m->audio_input[j].connect_to( &port[j] );
493 for ( unsigned int j = 0; j < m->audio_output.size(); ++j )
495 m->audio_output[j].connect_to( &port[j] );
498 m->handle_port_connection_change();
501 DMESSAGE( "Process queue looks like:" );
503 for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
505 const Module* m = *i;
507 if ( m->audio_input.size() || m->audio_output.size() )
508 DMESSAGE( "\t%s", (*i)->name() );
509 else if ( m->control_output.size() )
510 DMESSAGE( "\t%s -->", (*i)->name() );
511 else if ( m->control_input.size() )
512 DMESSAGE( "\t%s <--", (*i)->name() );
515 char *s = m->describe_inputs();
517 DMESSAGE( "(%s)", s );
529 if ( 0 == strcmp( "Chain", tabs->value()->label() ) )
530 for ( int i = 0; i < modules(); ++i )
531 draw_connections( module( i ) );
535 Chain::resize ( int X, int Y, int W, int H )
537 Fl_Group::resize( X, Y, W, H );
539 /* this won't naturally resize because it's inside of an Fl_Scroll... */
540 controls_pack->size( W, controls_pack->h() );
543 #include "FL/test_press.H"
546 Chain::handle ( int m )
552 if ( Fl::belowmouse() != this )
556 for ( int i = 0; i < modules(); ++i )
557 if ( Fl::event_inside( module( i ) ) )
565 if ( test_press( FL_BUTTON3 | FL_CTRL ) )
567 if ( FL_BLACK == m->color() )
570 fl_alert( "Cannot delete this module." );
580 else if ( test_press( FL_BUTTON1 | FL_SHIFT ) )
582 Module *mod = (Module*)Plugin_Module::pick_plugin();
585 if ( ! insert( m, mod ) )
586 fl_alert( "Cannot insert this module at this point in the chain" );
591 else if ( test_press( FL_BUTTON1 | FL_CTRL ) )
605 return Fl_Group::handle( m );
609 Chain::process ( nframes_t nframes )
611 for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
615 m->nframes( nframes );