Non reinvents the DAW. Powerful enough to form a complete studio, fast and light enough to run on low-end hardware like the eeePC or Raspberry Pi, and so reliable that it can be used live https://non.tuxfamily.org/
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.

976 lines
24KB

  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. #include "const.h"
  19. #include "Controller_Module.H"
  20. #include <stdio.h>
  21. #include <FL/Fl.H>
  22. #include <FL/Fl_Box.H>
  23. #include <FL/fl_ask.H>
  24. #include <FL/Fl_Counter.H>
  25. #include <FL/Fl_Menu_Item.H>
  26. #include <FL/Fl_Menu_Button.H>
  27. #include <FL/Fl_Menu_.H>
  28. #include <FL/Fl_Light_Button.H>
  29. #include <FL/fl_draw.H>
  30. #include "FL/Fl_DialX.H"
  31. #include "FL/Fl_Labelpad_Group.H"
  32. #include "FL/Fl_Value_SliderX.H"
  33. #include "Panner.H"
  34. #include "FL/test_press.H"
  35. #include "FL/menu_popup.H"
  36. #include "Chain.H"
  37. #include "OSC/Endpoint.H"
  38. // needed for mixer->endpoint
  39. #include "Mixer.H"
  40. #include "Spatialization_Console.H"
  41. #include "string_util.h"
  42. bool Controller_Module::learn_by_number = false;
  43. bool Controller_Module::_learn_mode = false;
  44. Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
  45. {
  46. // label( "" );
  47. box( FL_NO_BOX );
  48. _horizontal = true;
  49. _pad = true;
  50. control = 0;
  51. control_value =0.0f;
  52. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  53. _mode = GUI;
  54. // mode( GUI );
  55. // mode( CV );
  56. // configure_inputs( 1 );
  57. end();
  58. log_create();
  59. }
  60. Controller_Module::~Controller_Module ( )
  61. {
  62. log_destroy();
  63. /* shutdown JACK port, if we have one */
  64. mode( GUI );
  65. // disconnect();
  66. }
  67. void
  68. Controller_Module::handle_chain_name_changed()
  69. {
  70. if ( type() == SPATIALIZATION )
  71. {
  72. if ( Mixer::spatialization_console )
  73. Mixer::spatialization_console->update();
  74. }
  75. // change_osc_path( generate_osc_path() );
  76. }
  77. void
  78. Controller_Module::handle_control_disconnect ( Module::Port *p )
  79. {
  80. if ( type() == SPATIALIZATION )
  81. {
  82. if ( Mixer::spatialization_console )
  83. Mixer::spatialization_console->update();
  84. }
  85. }
  86. void
  87. Controller_Module::disconnect ( void )
  88. {
  89. for ( unsigned int i = 0; i < control_output.size(); ++i )
  90. control_output[i].disconnect();
  91. }
  92. void
  93. Controller_Module::get ( Log_Entry &e ) const
  94. {
  95. Module::get( e );
  96. Port *p = control_output[0].connected_port();
  97. if ( !p )
  98. {
  99. e.add( ":module", "" );
  100. e.add( ":port", "" );
  101. e.add( ":mode", "" );
  102. }
  103. else
  104. {
  105. Module *m = p->module();
  106. e.add( ":module", m );
  107. e.add( ":port", m->control_input_port_index( p ) );
  108. e.add( ":mode", mode() );
  109. }
  110. }
  111. void
  112. Controller_Module::set ( Log_Entry &e )
  113. {
  114. Module::set( e );
  115. int port = -1;
  116. Module *module = NULL;
  117. for ( int i = 0; i < e.size(); ++i )
  118. {
  119. const char *s, *v;
  120. e.get( i, &s, &v );
  121. if ( ! strcmp( s, ":port" ) )
  122. {
  123. port = atoi( v );
  124. }
  125. else if ( ! strcmp( s, ":module" ) )
  126. {
  127. int i;
  128. sscanf( v, "%X", &i );
  129. Module *t = (Module*)Loggable::find( i );
  130. assert( t );
  131. module = t;
  132. }
  133. }
  134. if ( port >= 0 && module )
  135. {
  136. connect_to( &module->control_input[port] );
  137. module->chain()->add_control( this );
  138. label( module->control_input[port].name() );
  139. }
  140. for ( int i = 0; i < e.size(); ++i )
  141. {
  142. const char *s, *v;
  143. e.get( i, &s, &v );
  144. if ( ! strcmp( s, ":mode" ) )
  145. {
  146. mode( (Mode)atoi( v ) );
  147. }
  148. }
  149. }
  150. void
  151. Controller_Module::mode ( Mode m )
  152. {
  153. if( mode() != CV && m == CV )
  154. {
  155. if ( control_output[0].connected() )
  156. {
  157. chain()->client()->lock();
  158. Port *p = control_output[0].connected_port();
  159. char prefix[512];
  160. snprintf( prefix, sizeof(prefix), "CV-%s", p->name() );
  161. add_aux_audio_input( prefix, 0 );
  162. chain()->client()->unlock();
  163. }
  164. }
  165. else if ( mode() == CV && m != CV )
  166. {
  167. chain()->client()->lock();
  168. delete aux_audio_input.back().jack_port();
  169. aux_audio_input.pop_back();
  170. chain()->client()->unlock();
  171. }
  172. _mode = m ;
  173. }
  174. bool
  175. Controller_Module::connect_spatializer_radius_to ( Module *m )
  176. {
  177. Port *radius_port = NULL;
  178. float radius_value = 0.0f;
  179. for ( unsigned int i = 0; i < m->control_input.size(); ++i )
  180. {
  181. Port *p = &m->control_input[i];
  182. if ( !strcasecmp( "Radius", p->name() ) )
  183. /* 90.0f == p->hints.maximum && */
  184. /* -90.0f == p->hints.minimum ) */
  185. {
  186. radius_port = p;
  187. radius_value = p->control_value();
  188. continue;
  189. }
  190. }
  191. if ( ! radius_port )
  192. return false;
  193. if ( control_output.size() != 3 )
  194. {
  195. control_output.clear();
  196. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  197. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  198. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  199. }
  200. control_output[2].connect_to( radius_port );
  201. maybe_create_panner();
  202. Panner *o = (Panner*)control;
  203. o->point( 0 )->radius( radius_value );
  204. if ( Mixer::spatialization_console )
  205. Mixer::spatialization_console->update();
  206. return true;
  207. }
  208. void
  209. Controller_Module::maybe_create_panner ( void )
  210. {
  211. if ( _type != SPATIALIZATION )
  212. {
  213. clear();
  214. Panner *o = new Panner( 0,0, 92,92 );
  215. o->box(FL_FLAT_BOX);
  216. o->color(FL_GRAY0);
  217. o->selection_color(FL_BACKGROUND_COLOR);
  218. o->labeltype(FL_NORMAL_LABEL);
  219. o->labelfont(0);
  220. o->labelcolor(FL_FOREGROUND_COLOR);
  221. o->align(FL_ALIGN_TOP);
  222. o->when(FL_WHEN_CHANGED);
  223. label( "Spatialization" );
  224. o->align(FL_ALIGN_TOP);
  225. o->labelsize( 10 );
  226. // o->callback( cb_panner_value_handle, new callback_data( this, azimuth_port_number, elevation_port_number ) );
  227. o->callback( cb_spatializer_handle, this );
  228. control = (Fl_Valuator*)o;
  229. if ( _pad )
  230. {
  231. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( o );
  232. flg->position( x(), y() );
  233. flg->set_visible_focus();
  234. size( flg->w(), flg->h() );
  235. add( flg );
  236. }
  237. else
  238. {
  239. o->resize( x(), y(), w(), h() );
  240. add( o );
  241. resizable( o );
  242. init_sizes();
  243. }
  244. _type = SPATIALIZATION;
  245. }
  246. }
  247. /** attempt to transform this controller into a spatialization
  248. controller and connect to the given module's spatialization
  249. control inputs. Returns true on success, false if given module
  250. does not accept spatialization inputs. */
  251. bool
  252. Controller_Module::connect_spatializer_to ( Module *m )
  253. {
  254. connect_spatializer_radius_to( m );
  255. /* these are for detecting related parameter groups which can be
  256. better represented by a single control */
  257. Port *azimuth_port = NULL;
  258. float azimuth_value = 0.0f;
  259. Port *elevation_port = NULL;
  260. float elevation_value = 0.0f;
  261. for ( unsigned int i = 0; i < m->control_input.size(); ++i )
  262. {
  263. Port *p = &m->control_input[i];
  264. if ( !strcasecmp( "Azimuth", p->name() ) &&
  265. 180.0f == p->hints.maximum &&
  266. -180.0f == p->hints.minimum )
  267. {
  268. azimuth_port = p;
  269. azimuth_value = p->control_value();
  270. continue;
  271. }
  272. else if ( !strcasecmp( "Elevation", p->name() ) &&
  273. 90.0f == p->hints.maximum &&
  274. -90.0f == p->hints.minimum )
  275. {
  276. elevation_port = p;
  277. elevation_value = p->control_value();
  278. continue;
  279. }
  280. }
  281. if ( ! ( azimuth_port && elevation_port ) )
  282. return false;
  283. if ( control_output.size() != 3 )
  284. {
  285. control_output.clear();
  286. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  287. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  288. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  289. }
  290. control_output[0].connect_to( azimuth_port );
  291. control_output[1].connect_to( elevation_port );
  292. maybe_create_panner();
  293. Panner *o = (Panner*)control;
  294. o->point( 0 )->azimuth( azimuth_value );
  295. o->point( 0 )->elevation( elevation_value );
  296. if ( Mixer::spatialization_console )
  297. Mixer::spatialization_console->update();
  298. return true;
  299. }
  300. void
  301. Controller_Module::connect_to ( Port *p )
  302. {
  303. control_output[0].connect_to( p );
  304. clear();
  305. Fl_Widget *w;
  306. if ( p->hints.type == Module::Port::Hints::BOOLEAN )
  307. {
  308. Fl_Button *o = new Fl_Button( 0, 0, 40, 40, p->name() );
  309. w = o;
  310. o->type( FL_TOGGLE_BUTTON );
  311. o->value( p->control_value() );
  312. o->selection_color( fl_color_average( FL_GRAY, FL_CYAN, 0.5 ) );
  313. _type = TOGGLE;
  314. /* FIXME: hack */
  315. control = (Fl_Valuator*)o;
  316. }
  317. else if ( p->hints.type == Module::Port::Hints::INTEGER )
  318. {
  319. Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
  320. control = o;
  321. w = o;
  322. o->type(1);
  323. o->step(1);
  324. if ( p->hints.ranged )
  325. {
  326. o->minimum( p->hints.minimum );
  327. o->maximum( p->hints.maximum );
  328. }
  329. _type = SPINNER;
  330. o->value( p->control_value() );
  331. }
  332. // else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
  333. else
  334. {
  335. Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
  336. control = o;
  337. w = o;
  338. if ( ! _horizontal )
  339. {
  340. o->size( 30, 250 );
  341. o->type(FL_VERT_NICE_SLIDER);
  342. }
  343. else
  344. {
  345. o->size(250,20);
  346. o->type(FL_HOR_NICE_SLIDER);
  347. }
  348. // o->type(4);
  349. o->color( FL_BACKGROUND2_COLOR );
  350. o->selection_color( fl_color_average( FL_GRAY, FL_CYAN, 0.5 ) );
  351. o->minimum(1.5);
  352. o->maximum(0);
  353. o->value(1);
  354. // o->textsize(9);
  355. if ( p->hints.ranged )
  356. {
  357. if ( ! _horizontal )
  358. {
  359. o->minimum( p->hints.maximum );
  360. o->maximum( p->hints.minimum );
  361. }
  362. else
  363. {
  364. o->minimum( p->hints.minimum );
  365. o->maximum( p->hints.maximum );
  366. }
  367. }
  368. o->value( p->control_value() );
  369. _type = SLIDER;
  370. }
  371. /* else */
  372. /* { */
  373. /* { Fl_DialX *o = new Fl_DialX( 0, 0, 50, 50, p->name() ); */
  374. /* w = o; */
  375. /* control = o; */
  376. /* if ( p->hints.ranged ) */
  377. /* { */
  378. /* DMESSAGE( "Min: %f, max: %f", p->hints.minimum, p->hints.maximum ); */
  379. /* o->minimum( p->hints.minimum ); */
  380. /* o->maximum( p->hints.maximum ); */
  381. /* } */
  382. /* o->color( fl_darker( FL_GRAY ) ); */
  383. /* o->selection_color( FL_WHITE ); */
  384. /* o->value( p->control_value() ); */
  385. /* } */
  386. /* _type = KNOB; */
  387. /* } */
  388. control_value = p->control_value();
  389. w->set_visible_focus();
  390. w->align(FL_ALIGN_TOP);
  391. w->labelsize( 10 );
  392. w->callback( cb_handle, this );
  393. if ( _pad )
  394. {
  395. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
  396. flg->set_visible_focus();
  397. size( flg->w(), flg->h() );
  398. flg->position( x(), y() );
  399. add( flg );
  400. resizable(flg);
  401. // init_sizes();
  402. }
  403. else
  404. {
  405. /* HACK: hide label */
  406. if ( _type == TOGGLE )
  407. {
  408. w->align( FL_ALIGN_INSIDE );
  409. }
  410. else
  411. {
  412. w->labeltype( FL_NO_LABEL );
  413. }
  414. w->resize( x(), y(), this->w(), h() );
  415. add( w );
  416. resizable( w );
  417. init_sizes();
  418. }
  419. }
  420. void
  421. Controller_Module::update ( void )
  422. {
  423. /* we only need this in CV (JACK) mode, because with other forms
  424. * of control the change happens in the GUI thread and we know it */
  425. if ( mode() != CV )
  426. return;
  427. /* ensures that port value change callbacks are run */
  428. if ( control && control_output.size() > 0 && control_output[0].connected() )
  429. control_output[0].connected_port()->control_value( control_value );
  430. }
  431. void
  432. Controller_Module::cb_handle ( Fl_Widget *w, void *v )
  433. {
  434. ((Controller_Module*)v)->cb_handle( w );
  435. }
  436. void
  437. Controller_Module::cb_handle ( Fl_Widget *w )
  438. {
  439. if ( type() == TOGGLE )
  440. {
  441. control_value = ((Fl_Button*)w)->value();
  442. }
  443. else
  444. control_value = ((Fl_Valuator*)w)->value();
  445. if ( control_output[0].connected() )
  446. control_output[0].connected_port()->control_value( control_value );
  447. }
  448. void
  449. Controller_Module::cb_spatializer_handle ( Fl_Widget *w, void *v )
  450. {
  451. ((Controller_Module*)v)->cb_spatializer_handle( w );
  452. }
  453. void
  454. Controller_Module::cb_spatializer_handle ( Fl_Widget *w )
  455. {
  456. Panner *pan = (Panner*)w;
  457. if ( control_output[0].connected() &&
  458. control_output[1].connected() )
  459. {
  460. control_output[0].connected_port()->control_value( pan->point( 0 )->azimuth() );
  461. control_output[1].connected_port()->control_value( pan->point( 0 )->elevation() );
  462. }
  463. if ( control_output[2].connected() )
  464. {
  465. control_output[2].connected_port()->control_value( pan->point( 0 )->radius() );
  466. }
  467. }
  468. void
  469. Controller_Module::menu_cb ( Fl_Widget *w, void *v )
  470. {
  471. ((Controller_Module*)v)->menu_cb( (Fl_Menu_*) w );
  472. }
  473. void
  474. Controller_Module::menu_cb ( const Fl_Menu_ *m )
  475. {
  476. char picked[256];
  477. m->item_pathname( picked, sizeof( picked ) );
  478. Logger log( this );
  479. if ( ! strcmp( picked, "Mode/GUI + OSC" ) )
  480. mode( GUI );
  481. else if ( ! strcmp( picked, "Mode/Control Voltage (JACK)" ) )
  482. mode( CV );
  483. else if ( ! strcmp( picked, "/Remove" ) )
  484. command_remove();
  485. else if ( ! strncmp( picked, "Connect To/", strlen( "Connect To/" ) ) )
  486. {
  487. char *peer_name = index( picked, '/' ) + 1;
  488. *index( peer_name, '/' ) = 0;
  489. // OSC::Signal s = (OSC::Signal*)m->mvalue()->user_data();
  490. const char *path = ((OSC::Signal*)m->mvalue()->user_data())->path();
  491. /* if ( ! _osc_output()->is_connected_to( ((OSC::Signal*)m->mvalue()->user_data()) ) ) */
  492. /* { */
  493. /* _persistent_osc_connections.push_back( strdup(path) ); */
  494. Port *p = control_output[0].connected_port();
  495. if ( learn_by_number )
  496. mixer->osc_endpoint->add_translation( path, p->osc_number_path());
  497. else
  498. mixer->osc_endpoint->add_translation( path, p->osc_path() );
  499. }
  500. else if ( ! strncmp( picked, "Disconnect From/", strlen( "Disconnect From/" ) ) )
  501. {
  502. /* char *peer_name = index( picked, '/' ) + 1; */
  503. /* *index( peer_name, '/' ) = 0; */
  504. // OSC::Signal s = (OSC::Signal*)m->mvalue()->user_data();
  505. const char *path = (const char*)m->mvalue()->user_data();
  506. /* if ( ! _osc_output()->is_connected_to( ((OSC::Signal*)m->mvalue()->user_data()) ) ) */
  507. /* { */
  508. /* _persistent_osc_connections.push_back( strdup(path) ); */
  509. // Port *p = control_output[0].connected_port();
  510. mixer->osc_endpoint->del_translation( path );
  511. /* if ( learn_by_number ) */
  512. /* { */
  513. /* char *our_path = p->osc_number_path(); */
  514. /* mixer->osc_endpoint->add_translation( path, our_path ); */
  515. /* free(our_path); */
  516. /* } */
  517. /* else */
  518. /* mixer->osc_endpoint->add_translation( path, p->osc_path() ); */
  519. }
  520. /* } */
  521. /* else */
  522. /* { */
  523. /* /\* timeline->osc->disconnect_signal( _osc_output(), path ); *\/ */
  524. /* /\* for ( std::list<char*>::iterator i = _persistent_osc_connections.begin(); *\/ */
  525. /* /\* i != _persistent_osc_connections.end(); *\/ */
  526. /* /\* ++i ) *\/ */
  527. /* /\* { *\/ */
  528. /* /\* if ( !strcmp( *i, path ) ) *\/ */
  529. /* /\* { *\/ */
  530. /* /\* free( *i ); *\/ */
  531. /* /\* i = _persistent_osc_connections.erase( i ); *\/ */
  532. /* /\* break; *\/ */
  533. /* /\* } *\/ */
  534. /* /\* } *\/ */
  535. /* //free( path ); */
  536. /* } */
  537. }
  538. static Fl_Menu_Button *peer_menu;
  539. static const char *peer_prefix;
  540. void
  541. Controller_Module::peer_callback( OSC::Signal *sig, OSC::Signal::State state, void *v )
  542. {
  543. char *s;
  544. DMESSAGE( "Paramter limits: %f %f", sig->parameter_limits().min, sig->parameter_limits().max );
  545. /* only show outputs */
  546. if ( sig->direction() != OSC::Signal::Output )
  547. return;
  548. /* only list CV signals for now */
  549. if ( ! ( sig->parameter_limits().min == 0.0 &&
  550. sig->parameter_limits().max == 1.0 ) )
  551. return;
  552. if ( ! v )
  553. {
  554. /* if( state == OSC::Signal::Created ) */
  555. /* timeline->connect_osc(); */
  556. /* else */
  557. /* timeline->update_osc_connection_state(); */
  558. }
  559. else
  560. {
  561. /* building menu */
  562. // const char *name = sig->peer_name();
  563. assert( sig->path() );
  564. char *path = strdup( sig->path() );
  565. unescape_url( path );
  566. asprintf( &s, "%s/%s", peer_prefix, path );
  567. peer_menu->add( s, 0, NULL, (void*)( sig ), 0 );
  568. /* FL_MENU_TOGGLE | */
  569. /* ( ((Controller_Module*)v)->_osc_output()->is_connected_to( sig ) ? FL_MENU_VALUE : 0 ) ); */
  570. free( path );
  571. free( s );
  572. }
  573. }
  574. void
  575. Controller_Module::add_osc_peers_to_menu ( Fl_Menu_Button *m, const char *prefix )
  576. {
  577. mixer->osc_endpoint->peer_signal_notification_callback( &Controller_Module::peer_callback, NULL );
  578. peer_menu = m;
  579. peer_prefix = prefix;
  580. mixer->osc_endpoint->list_peer_signals( this );
  581. }
  582. void
  583. Controller_Module::add_osc_connections_to_menu ( Fl_Menu_Button *m, const char *prefix )
  584. {
  585. /* peer_menu = m; */
  586. const char *peer_prefix = prefix;
  587. // mixer->osc_endpoint->list_peer_signals( this );
  588. Port *p = control_output[0].connected_port();
  589. const char *number_path = p->osc_number_path();
  590. const char *name_path = p->osc_path();
  591. const char *paths[] = { number_path,name_path,NULL };
  592. for ( const char **cpath = paths; *cpath; cpath++ )
  593. {
  594. const char ** conn = mixer->osc_endpoint->get_connections( *cpath );
  595. if ( conn )
  596. {
  597. for ( const char **s = conn; *s; s++ )
  598. {
  599. /* building menu */
  600. char *path = strdup( *s );
  601. unescape_url( path );
  602. char *ns;
  603. asprintf( &ns, "%s/%s", peer_prefix, path );
  604. peer_menu->add( ns, 0, NULL, const_cast<char*>(*s), 0 );
  605. free( path );
  606. // free(*s);
  607. }
  608. free( conn );
  609. }
  610. }
  611. }
  612. /** build the context menu for this control */
  613. Fl_Menu_Button &
  614. Controller_Module::menu ( void )
  615. {
  616. static Fl_Menu_Button m( 0, 0, 0, 0, "Controller" );
  617. m.clear();
  618. if ( mode() == GUI )
  619. {
  620. add_osc_peers_to_menu( &m, "Connect To" );
  621. add_osc_connections_to_menu( &m, "Disconnect From" );
  622. }
  623. m.add( "Mode/GUI + OSC", 0, 0, 0, FL_MENU_RADIO | ( mode() == GUI ? FL_MENU_VALUE : 0 ));
  624. m.add( "Mode/Control Voltage (JACK)", 0, 0, 0, FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ));
  625. m.add( "Remove", 0, 0, 0, is_default() ? FL_MENU_INACTIVE : 0 );
  626. // menu_set_callback( m.items(), &Controller_Module::menu_cb, (void*)this );
  627. m.callback( &Controller_Module::menu_cb, (void*)this );
  628. // m.copy( items, (void*)this );
  629. return m;
  630. }
  631. void
  632. Controller_Module::draw ( void )
  633. {
  634. draw_box(x(),y(),w(),h());
  635. Fl_Group::draw();
  636. if ( learn_mode() )
  637. {
  638. fl_rectf( x(),y(),w(),h(), fl_color_add_alpha( FL_MAGENTA, 50 ) );
  639. }
  640. }
  641. int
  642. Controller_Module::handle ( int m )
  643. {
  644. switch ( m )
  645. {
  646. case FL_PUSH:
  647. {
  648. if ( learn_mode() )
  649. {
  650. tooltip( "Now learning control. Move the desired control on your controller" );
  651. //connect_to( &module->control_input[port] );
  652. Port *p = control_output[0].connected_port();
  653. if ( p )
  654. {
  655. const char * path = learn_by_number ? p->osc_number_path() : p->osc_path();
  656. DMESSAGE( "Will learn %s", path );
  657. mixer->osc_endpoint->learn( path );
  658. }
  659. return 1;
  660. }
  661. if ( Fl::event_button3() )
  662. {
  663. /* context menu */
  664. /* if ( type() != SPATIALIZATION ) */
  665. menu_popup( &menu() );
  666. return 1;
  667. }
  668. else
  669. return Fl_Group::handle( m );
  670. }
  671. }
  672. return Fl_Group::handle( m );
  673. }
  674. void
  675. Controller_Module::handle_control_changed ( Port *p )
  676. {
  677. /* ignore changes initiated while mouse is over widget */
  678. if ( type() == SPATIALIZATION )
  679. {
  680. if ( Mixer::spatialization_console )
  681. Mixer::spatialization_console->handle_control_changed( this );
  682. }
  683. if ( contains( Fl::pushed() ) )
  684. return;
  685. if ( p )
  686. control_value = p->control_value();
  687. if ( control->value() == control_value )
  688. return;
  689. /* if ( control->value() != control_value ) */
  690. /* { */
  691. /* redraw(); */
  692. /* } */
  693. if ( type() == SPATIALIZATION )
  694. {
  695. Panner *pan = (Panner*)control;
  696. pan->point( 0 )->azimuth( control_output[0].control_value() );
  697. pan->point( 0 )->elevation( control_output[1].control_value() );
  698. if ( control_output[2].connected() )
  699. {
  700. // Port *pp = control_output[2].connected_port();
  701. float v = control_output[2].control_value();
  702. // float s = pp->hints.maximum - pp->hints.minimum;
  703. pan->point( 0 )->radius( v );
  704. }
  705. if ( visible_r() )
  706. pan->redraw();
  707. }
  708. else
  709. {
  710. if ( type() == TOGGLE )
  711. ((Fl_Button*)control)->value(control_value);
  712. else
  713. control->value(control_value);
  714. }
  715. }
  716. void
  717. Controller_Module::command_remove ( void )
  718. {
  719. if ( is_default() )
  720. fl_alert( "Default modules may not be deleted." );
  721. else
  722. {
  723. chain()->remove( this );
  724. Fl::delete_widget( this );
  725. }
  726. }
  727. /**********/
  728. /* Client */
  729. /**********/
  730. void
  731. Controller_Module::process ( nframes_t nframes )
  732. {
  733. THREAD_ASSERT( RT );
  734. if ( type() == SPATIALIZATION )
  735. {
  736. return;
  737. }
  738. if ( control_output[0].connected() )
  739. {
  740. float f = control_value;
  741. if ( mode() == CV )
  742. {
  743. f = *((float*)aux_audio_input[0].jack_port()->buffer( nframes ));
  744. const Port *p = control_output[0].connected_port();
  745. if (p->hints.ranged )
  746. {
  747. // scale value to range.
  748. // we assume that CV values are between 0 and 1
  749. float scale = p->hints.maximum - p->hints.minimum;
  750. float offset = p->hints.minimum;
  751. f = ( f * scale ) + offset;
  752. }
  753. }
  754. // else
  755. // f = *((float*)control_output[0].buffer());
  756. *((float*)control_output[0].buffer()) = f;
  757. control_value = f;
  758. }
  759. }