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.

417 lines
9.6KB

  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 "Module.H"
  19. #include <FL/fl_draw.H>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "Module_Parameter_Editor.H"
  24. #include "Chain.H"
  25. #include "JACK_Module.H"
  26. #include "Gain_Module.H"
  27. #include "Mono_Pan_Module.H"
  28. #include "Meter_Module.H"
  29. #include "Plugin_Module.H"
  30. Module::Module ( int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L )
  31. {
  32. init();
  33. log_create();
  34. }
  35. Module::Module ( bool is_default, int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L ), Loggable( !is_default )
  36. {
  37. this->is_default( is_default );
  38. init();
  39. log_create();
  40. }
  41. Module::Module ( ) : Fl_Group( 0, 0, 0, 50, "Unnamed" )
  42. {
  43. init();
  44. log_create();
  45. }
  46. Module::~Module ( )
  47. {
  48. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  49. audio_input[i].disconnect();
  50. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  51. audio_output[i].disconnect();
  52. for ( unsigned int i = 0; i < control_input.size(); ++i )
  53. control_input[i].disconnect();
  54. for ( unsigned int i = 0; i < control_output.size(); ++i )
  55. control_output[i].disconnect();
  56. audio_input.clear();
  57. audio_output.clear();
  58. control_input.clear();
  59. control_output.clear();
  60. }
  61. void
  62. Module::init ( void )
  63. {
  64. _is_default = false;
  65. _editor = 0;
  66. _chain = 0;
  67. _instances = 1;
  68. box( FL_UP_BOX );
  69. labeltype( FL_NO_LABEL );
  70. clip_children( 1 );
  71. }
  72. void
  73. Module::get ( Log_Entry &e ) const
  74. {
  75. // e.add( ":name", label() );
  76. // e.add( ":color", (unsigned long)color());
  77. {
  78. char *s = get_parameters();
  79. if ( strlen( s ) )
  80. e.add( ":parameter_values", s );
  81. delete[] s;
  82. }
  83. e.add( ":is_default", is_default() );
  84. e.add( ":chain", chain() );
  85. e.add( ":active", active() );
  86. }
  87. void
  88. Module::set ( Log_Entry &e )
  89. {
  90. for ( int i = 0; i < e.size(); ++i )
  91. {
  92. const char *s, *v;
  93. e.get( i, &s, &v );
  94. if ( ! strcmp( s, ":chain" ) )
  95. {
  96. /* This trickiness is because we may need to know the name of
  97. our chain before we actually get added to it. */
  98. int i;
  99. sscanf( v, "%X", &i );
  100. Chain *t = (Chain*)Loggable::find( i );
  101. assert( t );
  102. chain( t );
  103. }
  104. }
  105. for ( int i = 0; i < e.size(); ++i )
  106. {
  107. const char *s, *v;
  108. e.get( i, &s, &v );
  109. /* if ( ! strcmp( s, ":name" ) ) */
  110. /* label( v ); */
  111. if ( ! strcmp( s, ":parameter_values" ) )
  112. {
  113. set_parameters( v );
  114. }
  115. else if ( ! ( strcmp( s, ":is_default" ) ) )
  116. {
  117. is_default( atoi( v ) );
  118. }
  119. else if ( ! ( strcmp( s, ":active" ) ) )
  120. {
  121. if ( atoi( v ) )
  122. activate();
  123. else
  124. deactivate();
  125. }
  126. else if ( ! strcmp( s, ":chain" ) )
  127. {
  128. int i;
  129. sscanf( v, "%X", &i );
  130. Chain *t = (Chain*)Loggable::find( i );
  131. assert( t );
  132. t->add( this );
  133. }
  134. }
  135. }
  136. /* return a string serializing this module's parameter settings. The
  137. format is 1.0:2.0:... Where 1.0 is the value of the first control
  138. input, 2.0 is the value of the second control input etc.
  139. */
  140. char *
  141. Module::get_parameters ( void ) const
  142. {
  143. char *s = new char[1024];
  144. s[0] = 0;
  145. char *sp = s;
  146. if ( control_input.size() )
  147. {
  148. for ( unsigned int i = 0; i < control_input.size(); ++i )
  149. sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
  150. *(sp - 1) = '\0';
  151. }
  152. return s;
  153. }
  154. void
  155. Module::set_parameters ( const char *parameters )
  156. {
  157. char *s = strdup( parameters );
  158. char *sp = s;
  159. char *start = s;
  160. int i = 0;
  161. for ( char *sp = s; ; ++sp )
  162. {
  163. if ( ':' == *sp || '\0' == *sp )
  164. {
  165. char was = *sp;
  166. *sp = '\0';
  167. DMESSAGE( start );
  168. if ( i < control_input.size() )
  169. control_input[i].control_value( atof( start ) );
  170. else
  171. {
  172. WARNING( "Module has no parameter at index %i", i );
  173. break;
  174. }
  175. i++;
  176. if ( '\0' == was )
  177. break;
  178. start = sp + 1;
  179. }
  180. }
  181. free( s );
  182. }
  183. void
  184. Module::draw_box ( void )
  185. {
  186. fl_color( FL_WHITE );
  187. int tw, th, tx, ty;
  188. tw = w();
  189. th = h();
  190. ty = y();
  191. tx = x();
  192. // bbox( tx, ty, tw, th );
  193. fl_push_clip( tx, ty, tw, th );
  194. Fl_Color c = is_default() ? FL_BLACK : color();
  195. c = active() ? c : fl_inactive( c );
  196. int spacing = w() / instances();
  197. for ( int i = instances(); i--; )
  198. {
  199. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
  200. }
  201. if ( audio_input.size() && audio_output.size() )
  202. {
  203. /* maybe draw control indicators */
  204. if ( control_input.size() )
  205. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  206. if ( control_output.size() )
  207. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  208. }
  209. fl_pop_clip();
  210. // box( FL_NO_BOX );
  211. Fl_Group::draw_children();
  212. }
  213. void
  214. Module::draw_label ( void )
  215. {
  216. int tw, th, tx, ty;
  217. bbox( tx, ty, tw, th );
  218. const char *lp = label();
  219. int l = strlen( label() );
  220. Fl_Color c = FL_FOREGROUND_COLOR;
  221. if ( ! active() )
  222. c = FL_BLACK;
  223. fl_color( c );
  224. char *s = NULL;
  225. if ( l > 10 )
  226. {
  227. s = new char[l];
  228. char *sp = s;
  229. for ( ; *lp; ++lp )
  230. switch ( *lp )
  231. {
  232. case 'i': case 'e': case 'o': case 'u': case 'a':
  233. break;
  234. default:
  235. *(sp++) = *lp;
  236. }
  237. *sp = '\0';
  238. }
  239. if ( l > 20 )
  240. fl_font( FL_HELVETICA, 10 );
  241. else
  242. fl_font( FL_HELVETICA, 14 );
  243. fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
  244. if ( s )
  245. delete[] s;
  246. }
  247. #include <FL/Fl_Menu_Button.H>
  248. Module *
  249. Module::pick_module ( void )
  250. {
  251. Fl_Menu_Button *menu = new Fl_Menu_Button( 0, 0, 400, 400 );
  252. menu->type( Fl_Menu_Button::POPUP3 );
  253. // menu->add( "JACK", 0, 0, (void*)1 );
  254. menu->add( "Gain", 0, 0, new unsigned long(-2) );
  255. menu->add( "Meter", 0, 0, new unsigned long(-3) );
  256. menu->add( "Mono Pan", 0, 0, new unsigned long(-4) );
  257. Plugin_Module::add_plugins_to_menu( menu );
  258. menu->popup();
  259. if ( menu->value() < 0 )
  260. return NULL;
  261. void * v = menu->menu()[ menu->value() ].user_data();
  262. if ( ! v )
  263. return NULL;
  264. unsigned long id = *((unsigned long *)v);
  265. switch ( id )
  266. {
  267. case -1:
  268. return new JACK_Module();
  269. case -2:
  270. return new Gain_Module();
  271. case -3:
  272. return new Meter_Module();
  273. case -4:
  274. return new Mono_Pan_Module();
  275. }
  276. /* Plugin_Module::Plugin_Info *pi = (Plugin_Module::Plugin_Info*)v; */
  277. Plugin_Module *m = new Plugin_Module();
  278. m->load( id );
  279. for ( const Fl_Menu_Item *mi = menu->menu(); mi->label(); ++mi )
  280. {
  281. if ( mi->user_data() )
  282. delete mi->user_data();
  283. }
  284. return m;
  285. }
  286. #include "FL/test_press.H"
  287. int
  288. Module::handle ( int m )
  289. {
  290. switch ( m )
  291. {
  292. case FL_PUSH:
  293. {
  294. if ( test_press( FL_BUTTON1 ) )
  295. {
  296. if ( _editor )
  297. {
  298. _editor->show();
  299. }
  300. else if ( ncontrol_inputs() )
  301. {
  302. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  303. _editor = new Module_Parameter_Editor( this );
  304. _editor->show();
  305. do { Fl::wait(); }
  306. while ( _editor->shown() );
  307. DMESSAGE( "Module parameters for \"%s\" closed",label() );
  308. delete _editor;
  309. _editor = NULL;
  310. }
  311. return 1;
  312. }
  313. break;
  314. }
  315. }
  316. return Fl_Group::handle( m );
  317. }