Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

615 lines
15KB

  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_Arc_Dial.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 "Engine/Engine.H"
  37. #include "Chain.H"
  38. #include "OSC/Endpoint.H"
  39. // needed for mixer->endpoint
  40. #include "Mixer.H"
  41. const float CONTROL_UPDATE_FREQ = 0.2f;
  42. Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
  43. {
  44. // label( "" );
  45. box( FL_NO_BOX );
  46. _pad = true;
  47. control = 0;
  48. control_value =0.0f;
  49. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  50. _mode = GUI;
  51. // mode( GUI );
  52. // mode( CV );
  53. // configure_inputs( 1 );
  54. end();
  55. Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  56. log_create();
  57. }
  58. Controller_Module::~Controller_Module ( )
  59. {
  60. Fl::remove_timeout( update_cb, this );
  61. log_destroy();
  62. /* shutdown JACK port, if we have one */
  63. mode( GUI );
  64. }
  65. void
  66. Controller_Module::handle_chain_name_changed()
  67. {
  68. // change_osc_path( generate_osc_path() );
  69. }
  70. void
  71. Controller_Module::get ( Log_Entry &e ) const
  72. {
  73. Module::get( e );
  74. Port *p = control_output[0].connected_port();
  75. Module *m = p->module();
  76. e.add( ":module", m );
  77. e.add( ":port", m->control_input_port_index( p ) );
  78. e.add( ":mode", mode() );
  79. }
  80. void
  81. Controller_Module::set ( Log_Entry &e )
  82. {
  83. Module::set( e );
  84. int port = -1;
  85. Module *module = NULL;
  86. for ( int i = 0; i < e.size(); ++i )
  87. {
  88. const char *s, *v;
  89. e.get( i, &s, &v );
  90. if ( ! strcmp( s, ":port" ) )
  91. {
  92. port = atoi( v );
  93. }
  94. else if ( ! strcmp( s, ":module" ) )
  95. {
  96. int i;
  97. sscanf( v, "%X", &i );
  98. Module *t = (Module*)Loggable::find( i );
  99. assert( t );
  100. module = t;
  101. }
  102. }
  103. if ( port >= 0 && module )
  104. {
  105. connect_to( &module->control_input[port] );
  106. module->chain()->add_control( this );
  107. label( module->control_input[port].name() );
  108. }
  109. for ( int i = 0; i < e.size(); ++i )
  110. {
  111. const char *s, *v;
  112. e.get( i, &s, &v );
  113. if ( ! strcmp( s, ":mode" ) )
  114. {
  115. mode( (Mode)atoi( v ) );
  116. }
  117. }
  118. }
  119. void
  120. Controller_Module::mode ( Mode m )
  121. {
  122. if( mode() != CV && m == CV )
  123. {
  124. if ( control_output[0].connected() )
  125. {
  126. chain()->engine()->lock();
  127. Port *p = control_output[0].connected_port();
  128. JACK::Port po( chain()->engine(), JACK::Port::Input, p->name(), 0, "CV" );
  129. if ( ! po.activate() )
  130. {
  131. fl_alert( "Could not activate JACK port \"%s\"", po.name() );
  132. chain()->engine()->unlock();
  133. return;
  134. }
  135. if ( po.valid() )
  136. {
  137. jack_input.push_back( po );
  138. }
  139. chain()->engine()->unlock();
  140. }
  141. }
  142. else if ( mode() == CV && m != CV )
  143. {
  144. chain()->engine()->lock();
  145. jack_input.back().shutdown();
  146. jack_input.pop_back();
  147. chain()->engine()->unlock();
  148. }
  149. _mode = m ;
  150. }
  151. /** attempt to transform this controller into a spatialization
  152. controller and connect to the given module's spatialization
  153. control inputs. Returns true on success, false if given module
  154. does not accept spatialization inputs. */
  155. bool
  156. Controller_Module::connect_spatializer_to ( Module *m )
  157. {
  158. /* these are for detecting related parameter groups which can be
  159. better represented by a single control */
  160. Port *azimuth_port = NULL;
  161. float azimuth_value = 0.0f;
  162. Port *elevation_port = NULL;
  163. float elevation_value = 0.0f;
  164. for ( unsigned int i = 0; i < m->control_input.size(); ++i )
  165. {
  166. Port *p = &m->control_input[i];
  167. if ( !strcasecmp( "Azimuth", p->name() ) &&
  168. 180.0f == p->hints.maximum &&
  169. -180.0f == p->hints.minimum )
  170. {
  171. azimuth_port = p;
  172. azimuth_value = p->control_value();
  173. continue;
  174. }
  175. else if ( !strcasecmp( "Elevation", p->name() ) &&
  176. 90.0f == p->hints.maximum &&
  177. -90.0f == p->hints.minimum )
  178. {
  179. elevation_port = p;
  180. elevation_value = p->control_value();
  181. continue;
  182. }
  183. }
  184. if ( ! ( azimuth_port && elevation_port ) )
  185. return false;
  186. control_output.clear();
  187. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  188. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  189. control_output[0].connect_to( azimuth_port );
  190. control_output[1].connect_to( elevation_port );
  191. {
  192. clear();
  193. Panner *o = new Panner( 0,0, 100, 100 );
  194. o->box(FL_THIN_UP_BOX);
  195. o->color(FL_GRAY0);
  196. o->selection_color(FL_BACKGROUND_COLOR);
  197. o->labeltype(FL_NORMAL_LABEL);
  198. o->labelfont(0);
  199. o->labelcolor(FL_FOREGROUND_COLOR);
  200. o->align(FL_ALIGN_TOP);
  201. o->when(FL_WHEN_CHANGED);
  202. o->label( "Spatialization" );
  203. o->align(FL_ALIGN_TOP);
  204. o->labelsize( 10 );
  205. // o->callback( cb_panner_value_handle, new callback_data( this, azimuth_port_number, elevation_port_number ) );
  206. o->point( 0 )->azimuth( azimuth_value );
  207. o->point( 0 )->elevation( elevation_value );
  208. o->callback( cb_spatializer_handle, this );
  209. control = (Fl_Valuator*)o;
  210. if ( _pad )
  211. {
  212. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( o );
  213. flg->position( x(), y() );
  214. flg->set_visible_focus();
  215. size( flg->w(), flg->h() );
  216. add( flg );
  217. }
  218. else
  219. {
  220. o->resize( x(), y(), w(), h() );
  221. add( o );
  222. resizable( o );
  223. init_sizes();
  224. }
  225. _type = SPATIALIZATION;
  226. return true;
  227. }
  228. }
  229. void
  230. Controller_Module::connect_to ( Port *p )
  231. {
  232. control_output[0].connect_to( p );
  233. clear();
  234. Fl_Widget *w;
  235. if ( p->hints.type == Module::Port::Hints::BOOLEAN )
  236. {
  237. Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
  238. w = o;
  239. o->value( p->control_value() );
  240. _type = TOGGLE;
  241. /* FIXME: hack */
  242. control = (Fl_Valuator*)o;
  243. }
  244. else if ( p->hints.type == Module::Port::Hints::INTEGER )
  245. {
  246. Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
  247. control = o;
  248. w = o;
  249. o->type(1);
  250. o->step(1);
  251. if ( p->hints.ranged )
  252. {
  253. o->minimum( p->hints.minimum );
  254. o->maximum( p->hints.maximum );
  255. }
  256. _type = SPINNER;
  257. o->value( p->control_value() );
  258. }
  259. else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
  260. {
  261. Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
  262. control = o;
  263. w = o;
  264. o->type(4);
  265. o->color(FL_BACKGROUND_COLOR);
  266. o->selection_color((Fl_Color)1);
  267. o->minimum(1.5);
  268. o->maximum(0);
  269. o->value(1);
  270. o->textsize(6);
  271. if ( p->hints.ranged )
  272. {
  273. o->minimum( p->hints.maximum );
  274. o->maximum( p->hints.minimum );
  275. }
  276. o->value( p->control_value() );
  277. _type = SLIDER;
  278. }
  279. else
  280. {
  281. { Fl_Arc_Dial *o = new Fl_Arc_Dial( 0, 0, 50, 50, p->name() );
  282. w = o;
  283. control = o;
  284. if ( p->hints.ranged )
  285. {
  286. DMESSAGE( "Min: %f, max: %f", p->hints.minimum, p->hints.maximum );
  287. o->minimum( p->hints.minimum );
  288. o->maximum( p->hints.maximum );
  289. }
  290. o->color( fl_darker( FL_GRAY ) );
  291. o->selection_color( FL_WHITE );
  292. o->value( p->control_value() );
  293. }
  294. _type = KNOB;
  295. }
  296. control_value = p->control_value();
  297. w->set_visible_focus();
  298. w->align(FL_ALIGN_TOP);
  299. w->labelsize( 10 );
  300. w->callback( cb_handle, this );
  301. if ( _pad )
  302. {
  303. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
  304. flg->set_visible_focus();
  305. size( flg->w(), flg->h() );
  306. flg->position( x(), y() );
  307. add( flg );
  308. }
  309. else
  310. {
  311. /* HACK: hide label */
  312. w->labeltype( FL_NO_LABEL );
  313. w->resize( x(), y(), this->w(), h() );
  314. add( w );
  315. resizable( w );
  316. // init_sizes();
  317. }
  318. }
  319. void
  320. Controller_Module::update_cb ( void *v )
  321. {
  322. ((Controller_Module*)v)->update_cb();
  323. }
  324. void
  325. Controller_Module::update_cb ( void )
  326. {
  327. Fl::repeat_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  328. /* we only need this in CV (JACK) mode, because with other forms
  329. * of control the change happens in the GUI thread and we know it */
  330. if ( mode() != CV )
  331. return;
  332. /* ensures that port value change callbacks are run */
  333. if ( control && control_output.size() > 0 && control_output[0].connected() )
  334. control_output[0].connected_port()->control_value( control_value );
  335. }
  336. void
  337. Controller_Module::cb_handle ( Fl_Widget *w, void *v )
  338. {
  339. ((Controller_Module*)v)->cb_handle( w );
  340. }
  341. void
  342. Controller_Module::cb_handle ( Fl_Widget *w )
  343. {
  344. if ( type() == TOGGLE )
  345. {
  346. control_value = ((Fl_Button*)w)->value();
  347. }
  348. else
  349. control_value = ((Fl_Valuator*)w)->value();
  350. if ( control_output[0].connected() )
  351. control_output[0].connected_port()->control_value( control_value );
  352. }
  353. void
  354. Controller_Module::cb_spatializer_handle ( Fl_Widget *w, void *v )
  355. {
  356. ((Controller_Module*)v)->cb_spatializer_handle( w );
  357. }
  358. void
  359. Controller_Module::cb_spatializer_handle ( Fl_Widget *w )
  360. {
  361. Panner *pan = (Panner*)w;
  362. if ( control_output[0].connected() &&
  363. control_output[1].connected() )
  364. {
  365. control_output[0].connected_port()->control_value( pan->point( 0 )->azimuth() );
  366. control_output[1].connected_port()->control_value( pan->point( 0 )->elevation() );
  367. }
  368. }
  369. void
  370. Controller_Module::menu_cb ( Fl_Widget *w, void *v )
  371. {
  372. ((Controller_Module*)v)->menu_cb( (Fl_Menu_*) w );
  373. }
  374. void
  375. Controller_Module::menu_cb ( const Fl_Menu_ *m )
  376. {
  377. char picked[256];
  378. m->item_pathname( picked, sizeof( picked ) );
  379. Logger log( this );
  380. if ( ! strcmp( picked, "Mode/GUI + OSC" ) )
  381. mode( GUI );
  382. else if ( ! strcmp( picked, "Mode/Control Voltage (JACK)" ) )
  383. mode( CV );
  384. }
  385. /** build the context menu for this control */
  386. Fl_Menu_Button &
  387. Controller_Module::menu ( void )
  388. {
  389. static Fl_Menu_Button m( 0, 0, 0, 0, "Controller" );
  390. Fl_Menu_Item items[] =
  391. {
  392. { "Mode", 0, 0, 0, FL_SUBMENU },
  393. { "GUI + OSC", 0, 0, 0, FL_MENU_RADIO | ( mode() == GUI ? FL_MENU_VALUE : 0 ) },
  394. { "Control Voltage (JACK)", 0, 0, 0, FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ) },
  395. { 0 },
  396. { 0 },
  397. };
  398. menu_set_callback( items, &Controller_Module::menu_cb, (void*)this );
  399. m.copy( items, (void*)this );
  400. return m;
  401. }
  402. int
  403. Controller_Module::handle ( int m )
  404. {
  405. switch ( m )
  406. {
  407. case FL_PUSH:
  408. {
  409. if ( test_press( FL_BUTTON3 ) )
  410. {
  411. /* context menu */
  412. if ( type() != SPATIALIZATION )
  413. menu_popup( &menu() );
  414. return 1;
  415. }
  416. else
  417. return Fl_Group::handle( m );
  418. }
  419. }
  420. return Fl_Group::handle( m );
  421. }
  422. void
  423. Controller_Module::handle_control_changed ( Port *p )
  424. {
  425. /* ignore changes initiated while mouse is over widget */
  426. if ( contains( Fl::pushed() ) )
  427. return;
  428. if ( p )
  429. control_value = p->control_value();
  430. if ( control->value() == control_value )
  431. return;
  432. /* if ( control->value() != control_value ) */
  433. /* { */
  434. /* redraw(); */
  435. /* } */
  436. if ( type() == SPATIALIZATION )
  437. {
  438. Panner *pan = (Panner*)control;
  439. pan->point( 0 )->azimuth( control_output[0].control_value() );
  440. pan->point( 0 )->elevation( control_output[1].control_value() );
  441. pan->redraw();
  442. }
  443. else
  444. {
  445. if ( type() == TOGGLE )
  446. ((Fl_Button*)control)->value(control_value);
  447. else
  448. control->value(control_value);
  449. }
  450. }
  451. /**********/
  452. /* Engine */
  453. /**********/
  454. void
  455. Controller_Module::process ( nframes_t nframes )
  456. {
  457. THREAD_ASSERT( RT );
  458. if ( type() == SPATIALIZATION )
  459. {
  460. return;
  461. }
  462. if ( control_output[0].connected() )
  463. {
  464. float f = control_value;
  465. if ( mode() == CV )
  466. {
  467. f = *((float*)jack_input[0].buffer( nframes ));
  468. const Port *p = control_output[0].connected_port();
  469. if (p->hints.ranged )
  470. {
  471. // scale value to range.
  472. // we assume that CV values are between 0 and 1
  473. float scale = p->hints.maximum - p->hints.minimum;
  474. float offset = p->hints.minimum;
  475. f = ( f * scale ) + offset;
  476. }
  477. }
  478. // else
  479. // f = *((float*)control_output[0].buffer());
  480. *((float*)control_output[0].buffer()) = f;
  481. control_value = f;
  482. }
  483. }