1 /*******************************************************************************/
2 /* Copyright (C) 2009 Jonathan Moore Liles */
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. */
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 */
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 /*******************************************************************************/
21 #include "Controller_Module.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"
38 #include "FL/test_press.H"
39 #include "FL/menu_popup.H"
41 #include "Engine/Engine.H"
43 #include "OSC/Endpoint.H"
45 // needed for mixer->endpoint
50 const float CONTROL_UPDATE_FREQ = 0.2f;
54 Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
63 add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
69 // configure_inputs( 1 );
73 Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
78 Controller_Module::~Controller_Module ( )
80 Fl::remove_timeout( update_cb, this );
84 /* shutdown JACK port, if we have one */
89 Controller_Module::handle_chain_name_changed()
91 // change_osc_path( generate_osc_path() );
95 Controller_Module::disconnect ( void )
97 for ( std::vector<Module::Port>::iterator i = control_output.begin();
98 i != control_output.end();
108 Controller_Module::get ( Log_Entry &e ) const
112 Port *p = control_output[0].connected_port();
116 e.add( ":module", "" );
117 e.add( ":port", "" );
118 e.add( ":mode", "" );
122 Module *m = p->module();
124 e.add( ":module", m );
125 e.add( ":port", m->control_input_port_index( p ) );
126 e.add( ":mode", mode() );
131 Controller_Module::set ( Log_Entry &e )
136 Module *module = NULL;
138 for ( int i = 0; i < e.size(); ++i )
144 if ( ! strcmp( s, ":port" ) )
148 else if ( ! strcmp( s, ":module" ) )
151 sscanf( v, "%X", &i );
152 Module *t = (Module*)Loggable::find( i );
160 if ( port >= 0 && module )
162 connect_to( &module->control_input[port] );
163 module->chain()->add_control( this );
164 label( module->control_input[port].name() );
167 for ( int i = 0; i < e.size(); ++i )
173 if ( ! strcmp( s, ":mode" ) )
175 mode( (Mode)atoi( v ) );
184 Controller_Module::mode ( Mode m )
187 if( mode() != CV && m == CV )
189 if ( control_output[0].connected() )
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() )
199 fl_alert( "Could not activate JACK port \"%s\"", po.name() );
200 chain()->engine()->unlock();
206 jack_input.push_back( po );
209 chain()->engine()->unlock();
212 else if ( mode() == CV && m != CV )
214 chain()->engine()->lock();
216 jack_input.back().shutdown();
217 jack_input.pop_back();
219 chain()->engine()->unlock();
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. */
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 )
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 )
248 azimuth_value = p->control_value();
251 else if ( !strcasecmp( "Elevation", p->name() ) &&
252 90.0f == p->hints.maximum &&
253 -90.0f == p->hints.minimum )
256 elevation_value = p->control_value();
261 if ( ! ( azimuth_port && elevation_port ) )
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 );
274 Panner *o = new Panner( 0,0, 100, 100 );
276 o->box(FL_THIN_UP_BOX);
278 o->selection_color(FL_BACKGROUND_COLOR);
279 o->labeltype(FL_NORMAL_LABEL);
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);
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;
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() );
307 o->resize( x(), y(), w(), h() );
313 _type = SPATIALIZATION;
319 Controller_Module::connect_to ( Port *p )
321 control_output[0].connect_to( p );
327 if ( p->hints.type == Module::Port::Hints::BOOLEAN )
329 Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
331 o->value( p->control_value() );
336 control = (Fl_Valuator*)o;
338 else if ( p->hints.type == Module::Port::Hints::INTEGER )
341 Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
348 if ( p->hints.ranged )
350 o->minimum( p->hints.minimum );
351 o->maximum( p->hints.maximum );
356 o->value( p->control_value() );
358 else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
360 Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
365 o->color(FL_BACKGROUND_COLOR);
366 o->selection_color((Fl_Color)1);
372 if ( p->hints.ranged )
374 o->minimum( p->hints.maximum );
375 o->maximum( p->hints.minimum );
378 o->value( p->control_value() );
384 { Fl_Dial *o = new Fl_Dial( 0, 0, 50, 50, p->name() );
388 if ( p->hints.ranged )
390 DMESSAGE( "Min: %f, max: %f", p->hints.minimum, p->hints.maximum );
391 o->minimum( p->hints.minimum );
392 o->maximum( p->hints.maximum );
395 o->color( fl_darker( FL_GRAY ) );
396 o->selection_color( FL_WHITE );
397 o->value( p->control_value() );
403 control_value = p->control_value();
405 w->set_visible_focus();
406 w->align(FL_ALIGN_TOP);
408 w->callback( cb_handle, this );
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() );
420 /* HACK: hide label */
421 w->labeltype( FL_NO_LABEL );
422 w->resize( x(), y(), this->w(), h() );
430 Controller_Module::update_cb ( void *v )
432 ((Controller_Module*)v)->update_cb();
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 */
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 );
451 Controller_Module::cb_handle ( Fl_Widget *w, void *v )
453 ((Controller_Module*)v)->cb_handle( w );
457 Controller_Module::cb_handle ( Fl_Widget *w )
459 if ( type() == TOGGLE )
461 control_value = ((Fl_Button*)w)->value();
464 control_value = ((Fl_Valuator*)w)->value();
466 if ( control_output[0].connected() )
467 control_output[0].connected_port()->control_value( control_value );
472 Controller_Module::cb_spatializer_handle ( Fl_Widget *w, void *v )
474 ((Controller_Module*)v)->cb_spatializer_handle( w );
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() )
485 control_output[0].connected_port()->control_value( pan->point( 0 )->azimuth() );
486 control_output[1].connected_port()->control_value( pan->point( 0 )->elevation() );
491 Controller_Module::menu_cb ( Fl_Widget *w, void *v )
493 ((Controller_Module*)v)->menu_cb( (Fl_Menu_*) w );
497 Controller_Module::menu_cb ( const Fl_Menu_ *m )
501 m->item_pathname( picked, sizeof( picked ) );
505 if ( ! strcmp( picked, "Mode/GUI + OSC" ) )
507 else if ( ! strcmp( picked, "Mode/Control Voltage (JACK)" ) )
509 else if ( ! strcmp( picked, "/Remove" ) )
513 /** build the context menu for this control */
515 Controller_Module::menu ( void )
517 static Fl_Menu_Button m( 0, 0, 0, 0, "Controller" );
519 Fl_Menu_Item items[] =
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 ) },
525 { "Remove", 0, 0, 0, 0 },
529 menu_set_callback( items, &Controller_Module::menu_cb, (void*)this );
531 m.copy( items, (void*)this );
537 Controller_Module::handle ( int m )
544 if ( test_press( FL_BUTTON3 ) )
547 if ( type() != SPATIALIZATION )
548 menu_popup( &menu() );
553 return Fl_Group::handle( m );
557 return Fl_Group::handle( m );
561 Controller_Module::handle_control_changed ( Port *p )
563 /* ignore changes initiated while mouse is over widget */
564 if ( contains( Fl::pushed() ) )
568 control_value = p->control_value();
570 if ( control->value() == control_value )
573 /* if ( control->value() != control_value ) */
578 if ( type() == SPATIALIZATION )
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() );
589 if ( type() == TOGGLE )
590 ((Fl_Button*)control)->value(control_value);
592 control->value(control_value);
597 Controller_Module::command_remove ( void )
600 fl_alert( "Default modules may not be deleted." );
603 chain()->remove( this );
604 Fl::delete_widget( this );
613 Controller_Module::process ( nframes_t nframes )
617 if ( type() == SPATIALIZATION )
622 if ( control_output[0].connected() )
624 float f = control_value;
628 f = *((float*)jack_input[0].buffer( nframes ));
630 const Port *p = control_output[0].connected_port();
632 if (p->hints.ranged )
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;
644 // f = *((float*)control_output[0].buffer());
646 *((float*)control_output[0].buffer()) = f;