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.

578 lines
14KB

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