Mixer: Share (reuse) buffers between all chains.
[nondaw.git] / Mixer / Chain.C
blobd93def34f5fd016ccb05520d20eccd470bf01b9e
2 /*******************************************************************************/
3 /* Copyright (C) 2009 Jonathan Moore Liles                                     */
4 /*                                                                             */
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.                                                  */
9 /*                                                                             */
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   */
13 /* more details.                                                               */
14 /*                                                                             */
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...
22  *
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).
26  *
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
30  * decoder (1:6:2).
31  *
32  * The chain will allocate enough buffers to hold data from the
33  * maximum number of channels used by a contained module.
34  *
35  * The process thread goes as follows:
36  *
37  * 1. Copy inputs to chain buffers.
38  *
39  * 2. process() each module in turn (reusing buffers in-place) (inputs
40  * will be copied or plugins duplicated as necessary)
41  *
42  * 3. Copy chain buffers to outputs.
43  *
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
46  * optimized out.
47  */
49 #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"
57 #include <Fl/Fl_Box.H>
58 #include <FL/Fl_Menu.H>
59 #include <FL/fl_ask.H>
61 #include <stdlib.h>
62 #include "util/debug.h"
64 #include <stdio.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"
72 #include <string.h>
74 #include <dsp.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)
86     _outs = 1;
87     _ins = 1;
89     _configure_outputs_callback = NULL;
91     _name = 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 );
96             o->labelsize( 9 );
97             { Fl_Pack *o = modules_pack = new Fl_Pack( X, Y + 24, W, H - 24 );
98                 o->type( Fl_Pack::VERTICAL );
99                 o->spacing( 10 );
100                 o->end();
101             }
102             o->end();
103         }
104         { Fl_Group *o = new Fl_Group( X, Y + 24, W, H - 24, "Controls" );
105             o->labelsize( 9 );
106             o->hide();
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 );
110                     o->hspacing( 10 );
111                     o->vspacing( 10 );
112 //                    o->box( FL_FLAT_BOX );
113 //                    o->color( FL_RED );
114                     o->end();
115                     Fl_Group::current()->resizable( o );
116                 }
117                 o->end();
118                 Fl_Group::current()->resizable( o );
119             }
120             o->end();
121             Fl_Group::current()->resizable( o );
122         }
123         o->end();
124         Fl_Group::current()->resizable( o );
125     }
127     end();
129     chain.push_back( this );
132 Chain::~Chain ( )
134     chain.remove( this );
137 /* Fill this chain with JACK I/O, Gain, and Meter modules. */
138 void
139 Chain::initialize_with_default ( void )
142     {
143         JACK_Module *jm = new JACK_Module( 50, 50, "JACK" );
144         jm->chain( this );
145         jm->configure_outputs( 1 );
147         jm->initialize();
148         jm->color( FL_BLACK );
149         insert( NULL, jm );
150     }
152     {
153         JACK_Module *m = new JACK_Module( 50, 50, "JACK" );
154         m->chain( this );
155         m->initialize();
156         m->color( FL_BLACK );
157         insert( NULL, m );
158     }
162 void Chain::cb_handle(Fl_Widget* o) {
163     /* if ( o == head_button ) */
164     /* { */
165     /*     Module *m = Module::pick_plugin(); */
167     /*     insert_before( (Module*)modules_pack->child( 0 ), m ); */
168     /* } */
169     /* else if ( o == tail_button ) */
170     /* { */
171     /*     Module *m = Module::pick_plugin(); */
172     /*     insert_before( 0, m ); */
173     /* } */
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 */
183 void
184 Chain::remove ( Module *m )
186     int i = modules_pack->find( m );
188     int ins = 0;
190     if ( i != 0 )
191         ins = module( i - 1 )->noutputs();
193     if ( ! can_configure_outputs( m, ins ) )
194     {
195         fl_alert( "Can't remove module at this point because the resultant chain is invalid" );
196     }
198     modules_pack->remove( m );
200     configure_ports();
203 /* determine number of output ports, signal if changed.  */
204 void
205 Chain::configure_ports ( void )
207     int old_outs = outs();
208     int nouts = 0;
210     engine->lock();
212     for ( int i = 0; i < modules(); ++i )
213     {
214         module( i )->configure_inputs( nouts );
215         nouts = module( i )->noutputs();
216     }
218     outs( nouts );
220     int req_buffers = required_buffers();
222     if ( outs() != old_outs )
223     {
224         if ( configure_outputs_callback() )
225             configure_outputs_callback()( this, _configure_outputs_userdata );
226     }
228     DMESSAGE( "required_buffers = %i", req_buffers );
230     if ( port.size() < req_buffers )
231     {
232         for ( unsigned int i = port.size(); i--; )
233             delete[] (sample_t*)port[i].buffer();
234         port.clear();
236         for ( unsigned int i = 0; i < req_buffers; ++i )
237         {
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() );
241             port.push_back( p );
242         }
243     }
245     build_process_queue();
247     /* let the other chains know we mess with their buffers */
248     for ( std::list<Chain*>::iterator i = chain.begin();
249           i != chain.end();
250           ++i )
251     {
252         if ( *i != this )
253             (*i)->build_process_queue();
254     }
256     engine->unlock();
258     parent()->redraw();
261 /* calculate the minimum number of buffers required to satisfy this chain */
263 Chain::required_buffers ( void )
265     int buffers = 0;
266     int outs = 0;
268     for ( int i = 0; i < modules(); ++i )
269     {
270         outs = module( i )->can_support_inputs( outs );
272         if ( outs > buffers )
273             buffers = outs;
274     }
276     return 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 */
282 bool
283 Chain::can_configure_outputs ( Module *m, int n ) const
285     /* start at the requesting module */
287     int outs = n;
289     int i = modules_pack->find( m );
291     if ( modules() - 1 == i )
292         /* last module */
293         return true;
295     for ( i++ ; i < modules(); ++i )
296     {
297         outs = module( i )->can_support_inputs( outs );
299         if ( outs < 0 )
300             return false;
301     }
303     return true;
306 /* return true if this chain can be converted to support /n/ input channels  */
307 bool
308 Chain::can_support_input_channels ( int n )
310     /* FIXME: implement */
311     return true;
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
316  * JACK module). */
317 void
318 Chain::name ( const char *name )
320     _name = name;
322     for ( int i = 0; i < modules(); ++i )
323         module( i )->handle_chain_name_changed();
330 #include "FL/menu_popup.H"
332 bool
333 Chain::insert ( Module *m, Module *n )
336     engine->lock();
338     if ( !m )
339     {
340         if ( modules() == 0 && n->can_support_inputs( 0 ) >= 0 )
341         {
342             n->configure_inputs( 0 );
343             modules_pack->add( n );
344             n->chain( this );
345         }
346         else if ( n->can_support_inputs( module( modules() - 1 )->noutputs() ) >= 0 )
347         {
348             n->configure_inputs( module( modules() - 1 )->noutputs() );
349             modules_pack->add( n );
350             n->chain( this );
351         }
352         else
353             goto err;
354     }
355     else
356     {
357         int i = modules_pack->find( m );
359         if ( 0 == i )
360         {
361             /* inserting to head of chain*/
362             if ( n->can_support_inputs( 0 ) >= 0 )
363                 n->configure_inputs( 0 );
364             else
365                 goto err;
366         }
367         else
368         {
369             if ( n->can_support_inputs(  module( i - 1 )->noutputs() ) >= 0 )
370             {
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() );
377             }
378             else
379                 goto err;
380         }
382         modules_pack->insert( *n, i );
383         n->chain( this );
384     }
386     DMESSAGE( "Module has %i:%i audio and %i:%i control ports",
387               n->ninputs(),
388               n->noutputs(),
389               n->ncontrol_inputs(),
390               n->ncontrol_outputs() );
392     configure_ports();
394     engine->unlock();
396     return true;
398 err:
400     engine->unlock();
402     return false;
405 /* add a control to the control strip. Assumed to already be connected! */
406 void
407 Chain::add_control ( Module *m )
409     controls_pack->add( m );
412 void
413 Chain::draw_connections ( Module *m )
415     int spacing;
416     int offset;
418     Fl_Color c =fl_color_average( FL_WHITE, FL_YELLOW, 0.50 );
419     fl_color( c );
421     if ( m->ninputs() )
422     {
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 );
428     }
430     fl_color( fl_darker( c ) );
432     if ( m->noutputs() )
433     {
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 );
438     }
441 void
442 Chain::add_to_process_queue ( Module *m )
444     for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
445         if ( m == *i )
446             return;
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
453  * run in. */
454 void
455 Chain::build_process_queue ( void )
457     process_queue.clear();
459     for ( int i = 0; i < modules(); ++i )
460     {
461         Module *m = (Module*)module( i );
463         /* controllers */
464         for ( unsigned int j = 0; j < m->control_input.size(); ++j )
465         {
466             if ( m->control_input[j].connected() )
467             {
468                 add_to_process_queue( m->control_input[j].connected_port()->module() );
469             }
470         }
472         /* audio modules */
473         add_to_process_queue( m );
475         /* indicators */
476         for ( unsigned int j = 0; j < m->control_output.size(); ++j )
477         {
478             if ( m->control_output[j].connected() )
479             {
480                 add_to_process_queue( m->control_output[j].connected_port()->module() );
481             }
482         }
483     }
485     /* connect all the ports to the buffers */
486     for ( int i = 0; i < modules(); ++i )
487     {
488         Module *m = module( i );
489         for ( unsigned int j = 0; j < m->audio_input.size(); ++j )
490         {
491             m->audio_input[j].connect_to( &port[j] );
492         }
493         for ( unsigned int j = 0; j < m->audio_output.size(); ++j )
494         {
495             m->audio_output[j].connect_to( &port[j] );
496         }
498         m->handle_port_connection_change();
499     }
501     DMESSAGE( "Process queue looks like:" );
503     for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
504     {
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() );
514         {
515             char *s = m->describe_inputs();
517             DMESSAGE( "(%s)", s );
519             delete[] s;
520         }
521     }
524 void
525 Chain::draw ( void )
527     Fl_Group::draw();
529     if ( 0 == strcmp( "Chain", tabs->value()->label() ) )
530         for ( int i = 0; i < modules(); ++i )
531             draw_connections( module( i ) );
534 void
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 )
548     switch ( m )
549     {
550         case FL_PUSH:
551         {
552             if ( Fl::belowmouse() != this )
553             {
554                 Module *m = NULL;
556                 for ( int i = 0; i < modules(); ++i )
557                     if ( Fl::event_inside( module( i ) ) )
558                     {
559                         m = module( i );
560                         break;
561                     }
563                 if ( m )
564                 {
565                     if ( test_press( FL_BUTTON3 | FL_CTRL ) )
566                     {
567                         if ( FL_BLACK == m->color() )
568                         {
569                             /* FIXME: hack */
570                             fl_alert( "Cannot delete this module." );
571                         }
572                         else
573                         {
574                             remove( m );
575                             delete m;
576                             redraw();
577                         }
578                         return 1;
579                     }
580                     else if ( test_press( FL_BUTTON1 | FL_SHIFT ) )
581                     {
582                         Module *mod = (Module*)Plugin_Module::pick_plugin();
583                         if ( mod )
584                         {
585                             if ( ! insert( m, mod ) )
586                                 fl_alert( "Cannot insert this module at this point in the chain" );
587                             redraw();
588                         }
589                         return 1;
590                     }
591                     else if ( test_press( FL_BUTTON1 | FL_CTRL ) )
592                     {
593                         if ( m->active() )
594                             m->deactivate();
595                         else
596                             m->activate();
597                         return 1;
598                     }
599                 }
600             }
601             break;
602         }
603     }
605     return Fl_Group::handle( m );
608 void
609 Chain::process ( nframes_t nframes )
611     for ( std::list<Module*>::const_iterator i = process_queue.begin(); i != process_queue.end(); ++i )
612     {
613         Module *m = *i;
615         m->nframes( nframes );
616         if ( m->active() )
617             m->process();
618     }