Update NTK.
[nondaw.git] / mixer / src / Controller_Module.C
blob37d59728fcbc1dbc8682b003149233af030c4595
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 /*******************************************************************************/
19 #include "const.h"
21 #include "Controller_Module.H"
23 #include <stdio.h>
25 #include <FL/Fl.H>
26 #include <FL/Fl_Box.H>
27 #include <FL/fl_ask.H>
28 #include <FL/Fl_Counter.H>
29 #include <FL/Fl_Menu_Item.H>
30 #include <FL/Fl_Menu_Button.H>
31 #include <FL/Fl_Menu_.H>
32 #include <FL/Fl_Light_Button.H>
33 #include <FL/fl_draw.H>
34 #include "FL/Fl_Dial.H"
35 #include "FL/Fl_Labelpad_Group.H"
36 #include "FL/Fl_Value_SliderX.H"
37 #include "Panner.H"
38 #include "FL/test_press.H"
39 #include "FL/menu_popup.H"
41 #include "Engine/Engine.H"
42 #include "Chain.H"
43 #include "OSC/Endpoint.H"
45 // needed for mixer->endpoint
46 #include "Mixer.H"
50 const float CONTROL_UPDATE_FREQ = 0.2f;
54 Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
56 //    label( "" );
57     box( FL_NO_BOX );
59     _pad = true;
60     control = 0;
61     control_value =0.0f;
63     add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
65     _mode = GUI;
67 //    mode( GUI );
68 //    mode( CV );
69 //    configure_inputs( 1 );
71     end();
73     Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
75     log_create();
78 Controller_Module::~Controller_Module ( )
80     Fl::remove_timeout( update_cb, this );
82     log_destroy();
84     /* shutdown JACK port, if we have one */
85     mode( GUI );
88 void
89 Controller_Module::handle_chain_name_changed()
91 //    change_osc_path( generate_osc_path() );
94 void
95 Controller_Module::disconnect ( void )
97     for ( std::vector<Module::Port>::iterator i = control_output.begin();
98           i != control_output.end();
99           ++i )
100     {
101         (*i).disconnect();
102     }
107 void
108 Controller_Module::get ( Log_Entry &e ) const
110     Module::get( e );
112     Port *p = control_output[0].connected_port();
114     if ( !p )
115     {
116         e.add( ":module", "" );
117         e.add( ":port", "" );
118         e.add( ":mode", "" );
119     }
120     else
121     {
122         Module *m = p->module();
123         
124         e.add( ":module", m );
125         e.add( ":port", m->control_input_port_index( p ) );
126         e.add( ":mode", mode() );
127     }
130 void
131 Controller_Module::set ( Log_Entry &e )
133     Module::set( e );
135     int port = -1;
136     Module *module = NULL;
138     for ( int i = 0; i < e.size(); ++i )
139     {
140         const char *s, *v;
142         e.get( i, &s, &v );
144         if ( ! strcmp( s, ":port" ) )
145         {
146             port = atoi( v );
147         }
148         else if ( ! strcmp( s, ":module" ) )
149         {
150             int i;
151             sscanf( v, "%X", &i );
152             Module *t = (Module*)Loggable::find( i );
154             assert( t );
156             module = t;
157         }
158     }
160     if ( port >= 0 && module )
161     {
162         connect_to( &module->control_input[port] );
163         module->chain()->add_control( this );
164         label( module->control_input[port].name() );
165     }
167     for ( int i = 0; i < e.size(); ++i )
168     {
169         const char *s, *v;
171         e.get( i, &s, &v );
173         if ( ! strcmp( s, ":mode" ) )
174         {
175             mode( (Mode)atoi( v ) );
176         }
177     }
183 void
184 Controller_Module::mode ( Mode m )
187     if( mode() != CV && m == CV )
188     {
189         if ( control_output[0].connected() )
190         {
191             chain()->engine()->lock();
193             Port *p = control_output[0].connected_port();
195             JACK::Port po( chain()->engine(), JACK::Port::Input, p->name(), 0, "CV" );
197             if ( ! po.activate() )
198             {
199                 fl_alert( "Could not activate JACK port \"%s\"", po.name() );
200                 chain()->engine()->unlock();
201                 return;
202             }
204             if ( po.valid() )
205             {
206                 jack_input.push_back( po );
207             }
209             chain()->engine()->unlock();
210         }
211     }
212     else if ( mode() == CV && m != CV )
213     {
214         chain()->engine()->lock();
216         jack_input.back().shutdown();
217         jack_input.pop_back();
219         chain()->engine()->unlock();
220     }
222     _mode = m ;
225 /** attempt to transform this controller into a spatialization
226     controller and connect to the given module's spatialization
227     control inputs. Returns true on success, false if given module
228     does not accept spatialization inputs. */
229 bool
230 Controller_Module::connect_spatializer_to ( Module *m )
232     /* these are for detecting related parameter groups which can be
233        better represented by a single control */
234     Port *azimuth_port = NULL;
235     float azimuth_value = 0.0f;
236     Port *elevation_port = NULL;
237     float elevation_value = 0.0f;
239     for ( unsigned int i = 0; i < m->control_input.size(); ++i )
240     {
241         Port *p = &m->control_input[i];
243         if ( !strcasecmp( "Azimuth", p->name() ) &&
244              180.0f == p->hints.maximum &&
245              -180.0f == p->hints.minimum )
246         {
247             azimuth_port = p;
248             azimuth_value = p->control_value();
249             continue;
250         }
251         else if ( !strcasecmp( "Elevation", p->name() ) &&
252                   90.0f == p->hints.maximum &&
253                   -90.0f == p->hints.minimum )
254         {
255             elevation_port = p;
256             elevation_value = p->control_value();
257             continue;
258         }
259     }
261     if ( ! ( azimuth_port && elevation_port ) )
262         return false;
264     control_output.clear();
265     add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
266     add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
268     control_output[0].connect_to( azimuth_port );
269     control_output[1].connect_to( elevation_port );
271     {
272         clear();
274         Panner *o = new Panner( 0,0, 100, 100 );
276         o->box(FL_THIN_UP_BOX);
277         o->color(FL_GRAY0);
278         o->selection_color(FL_BACKGROUND_COLOR);
279         o->labeltype(FL_NORMAL_LABEL);
280         o->labelfont(0);
281         o->labelcolor(FL_FOREGROUND_COLOR);
282         o->align(FL_ALIGN_TOP);
283         o->when(FL_WHEN_CHANGED);
284         o->label( "Spatialization" );
286         o->align(FL_ALIGN_TOP);
287         o->labelsize( 10 );
288 //        o->callback( cb_panner_value_handle, new callback_data( this, azimuth_port_number, elevation_port_number ) );
290         o->point( 0 )->azimuth( azimuth_value );
291         o->point( 0 )->elevation( elevation_value );
293         o->callback( cb_spatializer_handle, this );
295         control = (Fl_Valuator*)o;
297         if ( _pad )
298         {
299             Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( o );
300             flg->position( x(), y() );
301             flg->set_visible_focus();
302             size( flg->w(), flg->h() );
303             add( flg );
304         }
305         else
306         {
307             o->resize( x(), y(), w(), h() );
308             add( o );
309             resizable( o );
310             init_sizes();
311         }
313         _type = SPATIALIZATION;
314         return true;
315     }
318 void
319 Controller_Module::connect_to ( Port *p )
321     control_output[0].connect_to( p );
323     clear();
325     Fl_Widget *w;
327     if ( p->hints.type == Module::Port::Hints::BOOLEAN )
328     {
329         Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
330         w = o;
331         o->value( p->control_value() );
333         _type = TOGGLE;
335         /* FIXME: hack */
336         control = (Fl_Valuator*)o;
337     }
338     else if ( p->hints.type == Module::Port::Hints::INTEGER )
339     {
341         Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
342         control = o;
343         w = o;
345         o->type(1);
346         o->step(1);
348         if ( p->hints.ranged )
349         {
350             o->minimum( p->hints.minimum );
351             o->maximum( p->hints.maximum );
352         }
354         _type = SPINNER;
356         o->value( p->control_value() );
357     }
358     else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
359     {
360         Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
361         control = o;
362         w = o;
364         o->type(4);
365         o->color(FL_BACKGROUND_COLOR);
366         o->selection_color((Fl_Color)1);
367         o->minimum(1.5);
368         o->maximum(0);
369         o->value(1);
370         o->textsize(6);
372         if ( p->hints.ranged )
373         {
374             o->minimum( p->hints.maximum );
375             o->maximum( p->hints.minimum );
376         }
378         o->value( p->control_value() );
380         _type = SLIDER;
381     }
382     else
383     {
384         { Fl_Dial *o = new Fl_Dial( 0, 0, 50, 50, p->name() );
385             w = o;
386             control = o;
388             if ( p->hints.ranged )
389             {
390                 DMESSAGE( "Min: %f, max: %f", p->hints.minimum, p->hints.maximum );
391                 o->minimum( p->hints.minimum );
392                 o->maximum( p->hints.maximum );
393             }
394             
395             o->color( fl_darker( FL_GRAY ) );
396             o->selection_color( FL_WHITE );
397             o->value( p->control_value() );
398         }
400         _type = KNOB;
401     }
403     control_value = p->control_value();
405     w->set_visible_focus();
406     w->align(FL_ALIGN_TOP);
407     w->labelsize( 10 );
408     w->callback( cb_handle, this );
410     if ( _pad )
411     {
412         Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
413         flg->set_visible_focus();
414         size( flg->w(), flg->h() );
415         flg->position( x(), y() );
416         add( flg );
417     }
418     else
419     {
420         /* HACK: hide label */
421         w->labeltype( FL_NO_LABEL );
422         w->resize( x(), y(), this->w(), h() );
423         add( w );
424         resizable( w );
425 //       init_sizes();
426     }
429 void
430 Controller_Module::update_cb ( void *v )
432     ((Controller_Module*)v)->update_cb();
435 void
436 Controller_Module::update_cb ( void )
438     Fl::repeat_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
440     /* we only need this in CV (JACK) mode, because with other forms
441      * of control the change happens in the GUI thread and we know it */
442     if ( mode() != CV )
443         return;
445     /* ensures that port value change callbacks are run */
446     if ( control && control_output.size() > 0 && control_output[0].connected() )
447         control_output[0].connected_port()->control_value( control_value );
450 void
451 Controller_Module::cb_handle ( Fl_Widget *w, void *v )
453     ((Controller_Module*)v)->cb_handle( w );
456 void
457 Controller_Module::cb_handle ( Fl_Widget *w )
459     if ( type() == TOGGLE )
460     {
461         control_value = ((Fl_Button*)w)->value();
462     }
463     else
464         control_value = ((Fl_Valuator*)w)->value();
466     if ( control_output[0].connected() )
467         control_output[0].connected_port()->control_value( control_value );
471 void
472 Controller_Module::cb_spatializer_handle ( Fl_Widget *w, void *v )
474     ((Controller_Module*)v)->cb_spatializer_handle( w );
477 void
478 Controller_Module::cb_spatializer_handle ( Fl_Widget *w )
480     Panner *pan = (Panner*)w;
482     if ( control_output[0].connected() &&
483          control_output[1].connected() )
484     {
485         control_output[0].connected_port()->control_value( pan->point( 0 )->azimuth() );
486         control_output[1].connected_port()->control_value( pan->point( 0 )->elevation() );
487     }
490 void
491 Controller_Module::menu_cb ( Fl_Widget *w, void *v )
493     ((Controller_Module*)v)->menu_cb( (Fl_Menu_*) w );
496 void
497 Controller_Module::menu_cb ( const Fl_Menu_ *m )
499     char picked[256];
501     m->item_pathname( picked, sizeof( picked ) );
503     Logger log( this );
505     if ( ! strcmp( picked, "Mode/GUI + OSC" ) )
506         mode( GUI );
507     else if ( ! strcmp( picked, "Mode/Control Voltage (JACK)" ) )
508         mode( CV );
509     else if ( ! strcmp( picked, "/Remove" ) )
510         command_remove();
513 /** build the context menu for this control */
514 Fl_Menu_Button &
515 Controller_Module::menu ( void )
517     static Fl_Menu_Button m( 0, 0, 0, 0, "Controller" );
519     Fl_Menu_Item items[] =
520         {
521             { "Mode",             0, 0, 0,  FL_SUBMENU    },
522             { "GUI + OSC",       0, 0, 0,  FL_MENU_RADIO | ( mode() == GUI ? FL_MENU_VALUE : 0 ) },
523             { "Control Voltage (JACK)",           0, 0, 0,  FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ) },
524             { 0 },
525             { "Remove", 0, 0, 0, 0 },
526             { 0 },
527         };
529     menu_set_callback( items, &Controller_Module::menu_cb, (void*)this );
531     m.copy( items, (void*)this );
533     return m;
537 Controller_Module::handle ( int m )
540     switch ( m )
541     {
542         case FL_PUSH:
543         {
544             if ( test_press( FL_BUTTON3 ) )
545             {
546                 /* context menu */
547                 if ( type() != SPATIALIZATION )
548                     menu_popup( &menu() );
550                 return 1;
551             }
552             else
553                 return Fl_Group::handle( m );
554         }
555     }
557     return Fl_Group::handle( m );
560 void
561 Controller_Module::handle_control_changed ( Port *p )
563     /* ignore changes initiated while mouse is over widget */
564     if ( contains( Fl::pushed() ) )
565         return;
567     if ( p )
568         control_value = p->control_value();
570     if ( control->value() == control_value )
571         return;
573     /* if ( control->value() != control_value ) */
574     /* { */
575     /*     redraw(); */
576     /* } */
578     if ( type() == SPATIALIZATION )
579     {
580         Panner *pan = (Panner*)control;
582         pan->point( 0 )->azimuth( control_output[0].control_value() );
583         pan->point( 0 )->elevation( control_output[1].control_value() );
585         pan->redraw();
586     }
587     else
588     {
589         if ( type() == TOGGLE )
590             ((Fl_Button*)control)->value(control_value);
591         else
592             control->value(control_value);
593     }
596 void
597 Controller_Module::command_remove ( void )
599     if ( is_default() )
600         fl_alert( "Default modules may not be deleted." );
601     else
602     {
603         chain()->remove( this );
604         Fl::delete_widget( this );
605     }
608 /**********/
609 /* Engine */
610 /**********/
612 void
613 Controller_Module::process ( nframes_t nframes )
615     THREAD_ASSERT( RT );
617     if ( type() == SPATIALIZATION )
618     {
619         return;
620     }
622     if (  control_output[0].connected() )
623     {
624         float f = control_value;
626         if ( mode() == CV )
627         {
628             f = *((float*)jack_input[0].buffer( nframes ));
630             const Port *p = control_output[0].connected_port();
632             if (p->hints.ranged )
633             {
634                 // scale value to range.
635                 // we assume that CV values are between 0 and 1
637                 float scale = p->hints.maximum - p->hints.minimum;
638                 float offset = p->hints.minimum;
640                 f = ( f * scale ) + offset;
641             }
642         }
643 //        else
644 //            f =  *((float*)control_output[0].buffer());
646         *((float*)control_output[0].buffer()) = f;
648         control_value = f;
649     }