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.

431 lines
9.8KB

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