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.

432 lines
9.9KB

  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 "Controller_Module.H"
  19. #include <FL/Fl.H>
  20. #include "FL/Fl_Value_SliderX.H"
  21. #include <FL/Fl_Box.H>
  22. #include <FL/Fl_Counter.H>
  23. #include "FL/Fl_Arc_Dial.H"
  24. #include <FL/Fl_Menu_Item.H>
  25. #include <FL/Fl_Menu_Button.H>
  26. #include <FL/Fl_Menu_.H>
  27. #include "FL/Fl_Light_Button.H"
  28. #include "FL/Boxtypes.H"
  29. #include <FL/fl_draw.H>
  30. #include "FL/Fl_Labelpad_Group.H"
  31. #include <stdio.h>
  32. #include "Engine/Engine.H"
  33. #include "Chain.H"
  34. const float CONTROL_UPDATE_FREQ = 0.1f;
  35. void
  36. Controller_Module::get ( Log_Entry &e ) const
  37. {
  38. Module::get( e );
  39. Port *p = control_output[0].connected_port();
  40. Module *m = p->module();
  41. e.add( ":module", m );
  42. e.add( ":port", m->control_input_port_index( p ) );
  43. e.add( ":mode", mode() );
  44. }
  45. void
  46. Controller_Module::set ( Log_Entry &e )
  47. {
  48. Module::set( e );
  49. int port = -1;
  50. Module *module = NULL;
  51. for ( int i = 0; i < e.size(); ++i )
  52. {
  53. const char *s, *v;
  54. e.get( i, &s, &v );
  55. if ( ! strcmp( s, ":port" ) )
  56. {
  57. port = atoi( v );
  58. }
  59. else if ( ! strcmp( s, ":module" ) )
  60. {
  61. int i;
  62. sscanf( v, "%X", &i );
  63. Module *t = (Module*)Loggable::find( i );
  64. assert( t );
  65. module = t;
  66. }
  67. }
  68. if ( port >= 0 && module )
  69. {
  70. connect_to( &module->control_input[port] );
  71. module->chain()->add_control( this );
  72. label( module->control_input[port].name() );
  73. }
  74. for ( int i = 0; i < e.size(); ++i )
  75. {
  76. const char *s, *v;
  77. e.get( i, &s, &v );
  78. if ( ! strcmp( s, ":mode" ) )
  79. {
  80. mode( (Mode)atoi( v ) );
  81. }
  82. }
  83. }
  84. Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
  85. {
  86. // label( "" );
  87. box( FL_NO_BOX );
  88. _pad = true;
  89. control = 0;
  90. control_value =0.0f;
  91. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  92. _mode = GUI;
  93. // mode( GUI );
  94. // mode( CV );
  95. // configure_inputs( 1 );
  96. end();
  97. Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  98. log_create();
  99. }
  100. Controller_Module::~Controller_Module ( )
  101. {
  102. Fl::remove_timeout( update_cb, this );
  103. log_destroy();
  104. }
  105. void
  106. Controller_Module::update_cb ( void *v )
  107. {
  108. ((Controller_Module*)v)->update_cb();
  109. }
  110. void
  111. Controller_Module::update_cb ( void )
  112. {
  113. Fl::repeat_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  114. if ( control && control_output[0].connected() )
  115. control->value(control_value);
  116. }
  117. void
  118. Controller_Module::cb_handle ( Fl_Widget *w, void *v )
  119. {
  120. ((Controller_Module*)v)->cb_handle( w );
  121. }
  122. void
  123. Controller_Module::cb_handle ( Fl_Widget *w )
  124. {
  125. control_value = ((Fl_Valuator*)w)->value();
  126. if ( control_output[0].connected() )
  127. {
  128. control_output[0].control_value( control_value );
  129. Port *p = control_output[0].connected_port();
  130. Module *m = p->module();
  131. m->handle_control_changed( p );
  132. }
  133. }
  134. void
  135. Controller_Module::connect_to ( Port *p )
  136. {
  137. control_output[0].connect_to( p );
  138. Fl_Widget *w;
  139. if ( p->hints.type == Module::Port::Hints::BOOLEAN )
  140. {
  141. Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
  142. w = o;
  143. o->value( p->control_value() );
  144. }
  145. else if ( p->hints.type == Module::Port::Hints::INTEGER )
  146. {
  147. Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
  148. control = o;
  149. w = o;
  150. o->type(1);
  151. o->step(1);
  152. if ( p->hints.ranged )
  153. {
  154. o->minimum( p->hints.minimum );
  155. o->maximum( p->hints.maximum );
  156. }
  157. o->value( p->control_value() );
  158. }
  159. else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
  160. {
  161. Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
  162. control = o;
  163. w = o;
  164. o->type(4);
  165. o->color(FL_GRAY0);
  166. o->selection_color((Fl_Color)1);
  167. o->minimum(1.5);
  168. o->maximum(0);
  169. o->value(1);
  170. o->textsize(14);
  171. // o->type( FL_VERTICAL );
  172. // o->type(1);
  173. if ( p->hints.ranged )
  174. {
  175. o->minimum( p->hints.maximum );
  176. o->maximum( p->hints.minimum );
  177. }
  178. o->value( p->control_value() );
  179. }
  180. else
  181. {
  182. { Fl_Arc_Dial *o = new Fl_Arc_Dial( 0, 0, 40, 40, p->name() );
  183. w = o;
  184. control = o;
  185. if ( p->hints.ranged )
  186. {
  187. o->minimum( p->hints.minimum );
  188. o->maximum( p->hints.maximum );
  189. }
  190. o->box( FL_BURNISHED_OVAL_BOX );
  191. // o->box( FL_OVAL_BOX );
  192. // o->type( FL_FILL_DIAL );
  193. o->color( fl_darker( fl_darker( FL_GRAY ) ) );
  194. o->selection_color( FL_WHITE );
  195. o->value( p->control_value() );
  196. }
  197. }
  198. control_value = p->control_value();
  199. w->align(FL_ALIGN_TOP);
  200. w->labelsize( 10 );
  201. w->callback( cb_handle, this );
  202. if ( _pad )
  203. {
  204. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
  205. size( flg->w(), flg->h() );
  206. add( flg );
  207. }
  208. else
  209. {
  210. w->resize( x(), y(), this->w(), h() );
  211. add( w );
  212. resizable( w );
  213. }
  214. }
  215. void
  216. Controller_Module::resize ( int X, int Y, int W, int H )
  217. {
  218. Module::resize( X, Y, W, H );
  219. if ( ! _pad && children() )
  220. {
  221. child( 0 )->resize( X, Y, W, H );
  222. }
  223. }
  224. void
  225. Controller_Module::menu_cb ( Fl_Widget *w, void *v )
  226. {
  227. ((Controller_Module*)v)->menu_cb( (Fl_Menu_*) w );
  228. }
  229. void
  230. Controller_Module::menu_cb ( const Fl_Menu_ *m )
  231. {
  232. char picked[256];
  233. m->item_pathname( picked, sizeof( picked ) );
  234. Logger log( this );
  235. if ( ! strcmp( picked, "Mode/Manual" ) )
  236. mode( GUI );
  237. else if ( ! strcmp( picked, "Mode/Control Voltage" ) )
  238. mode( CV );
  239. }
  240. #include "FL/test_press.H"
  241. #include "FL/menu_popup.H"
  242. /** build the context menu for this control */
  243. Fl_Menu_Button &
  244. Controller_Module::menu ( void )
  245. {
  246. static Fl_Menu_Button m( 0, 0, 0, 0, "Controller" );
  247. Fl_Menu_Item items[] =
  248. {
  249. { "Mode", 0, 0, 0, FL_SUBMENU },
  250. { "Manual", 0, 0, 0, FL_MENU_RADIO | ( mode() == GUI ? FL_MENU_VALUE : 0 ) },
  251. { "Control Voltage", 0, 0, 0, FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ) },
  252. // { "Open Sound Control (OSC)", 0, 0, 0, FL_MENU_RADIO | ( mode() == OSC ? FL_MENU_VALUE : 0 ) },
  253. { 0 },
  254. { 0 },
  255. };
  256. menu_set_callback( items, &Controller_Module::menu_cb, (void*)this );
  257. m.copy( items, (void*)this );
  258. return m;
  259. }
  260. int
  261. Controller_Module::handle ( int m )
  262. {
  263. switch ( m )
  264. {
  265. case FL_PUSH:
  266. {
  267. if ( test_press( FL_BUTTON3 ) )
  268. {
  269. /* context menu */
  270. menu_popup( &menu() );
  271. return 1;
  272. }
  273. else
  274. return Fl_Group::handle( m );
  275. }
  276. }
  277. return Fl_Group::handle( m );
  278. }
  279. void
  280. Controller_Module::mode ( Mode m )
  281. {
  282. if( mode() != CV && m == CV )
  283. {
  284. if ( control_output[0].connected() )
  285. {
  286. chain()->engine()->lock();
  287. // char name[256];
  288. // snprintf( name, sizeof( name ), "%s-CV", p->name() );
  289. Port *p = control_output[0].connected_port();
  290. JACK::Port po( chain()->engine(), JACK::Port::Input, p->name(), 0, "CV" );
  291. if ( po.valid() )
  292. {
  293. jack_input.push_back( po );
  294. }
  295. chain()->engine()->unlock();
  296. }
  297. }
  298. else if ( mode() == CV && m == GUI )
  299. {
  300. chain()->engine()->lock();
  301. jack_input.back().shutdown();
  302. jack_input.pop_back();
  303. chain()->engine()->unlock();
  304. }
  305. _mode = m ;
  306. }
  307. void
  308. Controller_Module::process ( void )
  309. {
  310. THREAD_ASSERT( RT );
  311. if ( control_output[0].connected() )
  312. {
  313. float f = control_value;
  314. if ( mode() == CV )
  315. {
  316. f = *((float*)jack_input[0].buffer( chain()->engine()->nframes() ));
  317. const Port *p = control_output[0].connected_port();
  318. if (p->hints.ranged )
  319. {
  320. // scale value to range.
  321. // we assume that CV values are between 0 and 1
  322. float scale = p->hints.maximum - p->hints.minimum;
  323. float offset = p->hints.minimum;
  324. f = ( f * scale ) + offset;
  325. }
  326. }
  327. // else
  328. // f = *((float*)control_output[0].buffer());
  329. *((float*)control_output[0].buffer()) = f;
  330. control_value = f;
  331. }
  332. }