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.

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