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.

402 lines
9.2KB

  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. int spacing = w() / instances();
  196. for ( int i = instances(); i--; )
  197. {
  198. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
  199. }
  200. if ( audio_input.size() && audio_output.size() )
  201. {
  202. /* maybe draw control indicators */
  203. if ( control_input.size() )
  204. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  205. if ( control_output.size() )
  206. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  207. }
  208. fl_pop_clip();
  209. // box( FL_NO_BOX );
  210. Fl_Group::draw_children();
  211. }
  212. void
  213. Module::draw_label ( void )
  214. {
  215. int tw, th, tx, ty;
  216. bbox( tx, ty, tw, th );
  217. const char *lp = label();
  218. int l = strlen( label() );
  219. fl_color( FL_FOREGROUND_COLOR );
  220. char *s = NULL;
  221. if ( l > 10 )
  222. {
  223. s = new char[l];
  224. char *sp = s;
  225. for ( ; *lp; ++lp )
  226. switch ( *lp )
  227. {
  228. case 'i': case 'e': case 'o': case 'u': case 'a':
  229. break;
  230. default:
  231. *(sp++) = *lp;
  232. }
  233. *sp = '\0';
  234. }
  235. if ( l > 20 )
  236. fl_font( FL_HELVETICA, 10 );
  237. else
  238. fl_font( FL_HELVETICA, 14 );
  239. fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
  240. if ( s )
  241. delete[] s;
  242. }
  243. #include <FL/Fl_Menu_Button.H>
  244. Module *
  245. Module::pick_module ( void )
  246. {
  247. Fl_Menu_Button *menu = new Fl_Menu_Button( 0, 0, 400, 400 );
  248. menu->type( Fl_Menu_Button::POPUP3 );
  249. // menu->add( "JACK", 0, 0, (void*)1 );
  250. menu->add( "Gain", 0, 0, (void*)2 );
  251. menu->add( "Meter", 0, 0, (void*)3 );
  252. menu->add( "Mono Pan", 0, 0, (void*)4 );
  253. Plugin_Module::add_plugins_to_menu( menu );
  254. menu->popup();
  255. if ( menu->value() < 0 )
  256. return NULL;
  257. void * v = menu->menu()[ menu->value() ].user_data();
  258. if ( ! v )
  259. return NULL;
  260. switch ( (int)v )
  261. {
  262. case 1:
  263. return new JACK_Module();
  264. case 2:
  265. return new Gain_Module();
  266. case 3:
  267. return new Meter_Module();
  268. case 4:
  269. return new Mono_Pan_Module();
  270. }
  271. Plugin_Module::Plugin_Info *pi = (Plugin_Module::Plugin_Info*)v;
  272. Plugin_Module *m = new Plugin_Module();
  273. m->load( pi->id );
  274. return m;
  275. }
  276. #include "FL/test_press.H"
  277. int
  278. Module::handle ( int m )
  279. {
  280. switch ( m )
  281. {
  282. case FL_PUSH:
  283. {
  284. if ( test_press( FL_BUTTON1 ) )
  285. {
  286. if ( _editor )
  287. {
  288. _editor->show();
  289. }
  290. else if ( ncontrol_inputs() )
  291. {
  292. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  293. _editor = new Module_Parameter_Editor( this );
  294. _editor->show();
  295. do { Fl::wait(); }
  296. while ( _editor->shown() );
  297. DMESSAGE( "Module parameters for \"%s\" closed",label() );
  298. delete _editor;
  299. _editor = NULL;
  300. }
  301. return 1;
  302. }
  303. break;
  304. }
  305. }
  306. return Fl_Group::handle( m );
  307. }