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.

791 lines
18KB

  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 <FL/fl_ask.H>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "Module_Parameter_Editor.H"
  25. #include "Chain.H"
  26. #include "JACK_Module.H"
  27. #include "Gain_Module.H"
  28. #include "Mono_Pan_Module.H"
  29. #include "Meter_Module.H"
  30. #include "Plugin_Module.H"
  31. #include <FL/Fl_Menu_Button.H>
  32. #include "FL/test_press.H"
  33. #include "FL/menu_popup.H"
  34. #include "Mixer.H"
  35. Module *Module::_copied_module_empty = 0;
  36. char *Module::_copied_module_settings = 0;
  37. Module::Module ( int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L )
  38. {
  39. init();
  40. log_create();
  41. }
  42. Module::Module ( bool is_default, int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L ), Loggable( !is_default )
  43. {
  44. this->is_default( is_default );
  45. init();
  46. log_create();
  47. }
  48. Module::Module ( ) : Fl_Group( 0, 0, 50, 50, "Unnamed" )
  49. {
  50. init();
  51. log_create();
  52. }
  53. Module::~Module ( )
  54. {
  55. if ( _editor )
  56. {
  57. delete _editor;
  58. _editor = NULL;
  59. }
  60. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  61. audio_input[i].disconnect();
  62. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  63. audio_output[i].disconnect();
  64. for ( unsigned int i = 0; i < control_input.size(); ++i )
  65. {
  66. control_input[i].disconnect();
  67. control_input[i].destroy_osc_port();
  68. }
  69. for ( unsigned int i = 0; i < control_output.size(); ++i )
  70. control_output[i].disconnect();
  71. audio_input.clear();
  72. audio_output.clear();
  73. control_input.clear();
  74. control_output.clear();
  75. }
  76. void
  77. Module::init ( void )
  78. {
  79. _is_default = false;
  80. _editor = 0;
  81. _chain = 0;
  82. _instances = 1;
  83. _bypass = 0;
  84. box( FL_UP_BOX );
  85. labeltype( FL_NO_LABEL );
  86. set_visible_focus();
  87. selection_color( FL_RED );
  88. }
  89. void
  90. Module::get ( Log_Entry &e ) const
  91. {
  92. // e.add( ":name", label() );
  93. // e.add( ":color", (unsigned long)color());
  94. {
  95. char *s = get_parameters();
  96. if ( strlen( s ) )
  97. e.add( ":parameter_values", s );
  98. delete[] s;
  99. }
  100. e.add( ":is_default", is_default() );
  101. e.add( ":chain", chain() );
  102. e.add( ":active", ! bypass() );
  103. }
  104. void
  105. Module::copy ( void ) const
  106. {
  107. Module *m = clone_empty();
  108. if ( ! m )
  109. {
  110. DMESSAGE( "Module \"%s\" doesn't support cloning", name() );
  111. }
  112. Log_Entry *ne = new Log_Entry();
  113. _copied_module_empty = m;
  114. {
  115. Log_Entry e;
  116. get( e );
  117. for ( int i = 0; i < e.size(); ++i )
  118. {
  119. const char *s, *v;
  120. e.get( i, &s, &v );
  121. /* we don't want this module to get added to the current
  122. chain... */
  123. if ( !( !strcmp( s, ":chain" ) ||
  124. !strcmp( s, ":is_default" ) ) )
  125. {
  126. DMESSAGE( "%s = %s", s, v );
  127. ne->add_raw( s, v );
  128. }
  129. }
  130. }
  131. _copied_module_settings = ne->print();
  132. }
  133. void
  134. Module::paste_before ( void )
  135. {
  136. Module *m = _copied_module_empty;
  137. m->chain( chain() );
  138. Log_Entry le( _copied_module_settings );
  139. m->set( le );
  140. if ( ! chain()->insert( this, m ) )
  141. {
  142. fl_alert( "Copied module cannot be inserted at this point in the chain" );
  143. }
  144. free( _copied_module_settings );
  145. _copied_module_settings = NULL;
  146. _copied_module_empty = NULL;
  147. /* set up for another copy */
  148. m->copy();
  149. }
  150. void
  151. Module::handle_control_changed ( Port *p )
  152. {
  153. if ( _editor )
  154. _editor->handle_control_changed ( p );
  155. }
  156. char *
  157. Module::Port::generate_osc_path ()
  158. {
  159. const Port *p = this;
  160. char *path = NULL;
  161. // /mixer/strip/STRIPNAME/control/MODULENAME/CONTROLNAME
  162. int n = module()->chain()->get_module_instance_number( module() );
  163. if ( n > 0 )
  164. asprintf( &path, "/mixer/strip/%s/control/%s.%i/%s", module()->chain()->name(), p->module()->label(), n, p->name() );
  165. else
  166. asprintf( &path, "/mixer/strip/%s/control/%s/%s", module()->chain()->name(), p->module()->label(), p->name() );
  167. // asprintf( &path, "/mixer/strip/control/%s/%s", p->module()->label(), p->name() );
  168. // Hack to keep spaces out of OSC URL... Probably need to handle other special characters similarly.
  169. for ( int i = strlen( path ); i--; )
  170. {
  171. if ( path[i] == ' ' )
  172. path[i] = '_';
  173. }
  174. return path;
  175. }
  176. void
  177. Module::Port::change_osc_path ( char *path )
  178. {
  179. if ( _osc_path )
  180. {
  181. mixer->osc_endpoint->del_method( _osc_path, "f" );
  182. mixer->osc_endpoint->del_method( _osc_path_cv, "f" );
  183. free( _osc_path );
  184. free( _osc_path_cv );
  185. _osc_path = NULL;
  186. _osc_path_cv = NULL;
  187. }
  188. if ( path )
  189. {
  190. _osc_path_cv = NULL;
  191. asprintf( &_osc_path_cv, "%s/unscaled", path );
  192. mixer->osc_endpoint->add_method( path, "f", &Module::Port::osc_control_change_cv, this, "value" );
  193. mixer->osc_endpoint->add_method( _osc_path_cv, "f", &Module::Port::osc_control_change_exact, this, "value" );
  194. _osc_path = path;
  195. // tooltip( _osc_path );
  196. }
  197. }
  198. int
  199. Module::Port::osc_control_change_exact ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  200. {
  201. Module::Port *p = (Module::Port*)user_data;
  202. OSC_DMSG();
  203. float f = argv[0]->f;
  204. if ( p->hints.ranged )
  205. {
  206. if ( f > p->hints.maximum )
  207. f = p->hints.maximum;
  208. else if ( f < p->hints.minimum )
  209. f = p->hints.minimum;
  210. }
  211. p->control_value( f );
  212. // mixer->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, "ok" );
  213. mixer->osc_endpoint->send( lo_message_get_source( msg ), path, argv[0]->f );
  214. return 0;
  215. }
  216. int
  217. Module::Port::osc_control_change_cv ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  218. {
  219. Module::Port *p = (Module::Port*)user_data;
  220. float f = argv[0]->f;
  221. if (p->hints.ranged )
  222. {
  223. // scale value to range.
  224. // we assume that CV values are between 0 and 1
  225. float scale = p->hints.maximum - p->hints.minimum;
  226. float offset = p->hints.minimum;
  227. f = ( f * scale ) + offset;
  228. }
  229. p->control_value( f );
  230. // mixer->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, "ok" );
  231. mixer->osc_endpoint->send( lo_message_get_source( msg ), path, argv[0]->f );
  232. return 0;
  233. }
  234. void
  235. Module::set ( Log_Entry &e )
  236. {
  237. for ( int i = 0; i < e.size(); ++i )
  238. {
  239. const char *s, *v;
  240. e.get( i, &s, &v );
  241. if ( ! strcmp( s, ":chain" ) )
  242. {
  243. /* This trickiness is because we may need to know the name of
  244. our chain before we actually get added to it. */
  245. int i;
  246. sscanf( v, "%X", &i );
  247. Chain *t = (Chain*)Loggable::find( i );
  248. assert( t );
  249. chain( t );
  250. }
  251. }
  252. for ( int i = 0; i < e.size(); ++i )
  253. {
  254. const char *s, *v;
  255. e.get( i, &s, &v );
  256. /* if ( ! strcmp( s, ":name" ) ) */
  257. /* label( v ); */
  258. if ( ! strcmp( s, ":parameter_values" ) )
  259. {
  260. set_parameters( v );
  261. }
  262. else if ( ! ( strcmp( s, ":is_default" ) ) )
  263. {
  264. is_default( atoi( v ) );
  265. }
  266. else if ( ! ( strcmp( s, ":active" ) ) )
  267. {
  268. bypass( ! atoi( v ) );
  269. }
  270. else if ( ! strcmp( s, ":chain" ) )
  271. {
  272. int i;
  273. sscanf( v, "%X", &i );
  274. Chain *t = (Chain*)Loggable::find( i );
  275. assert( t );
  276. t->add( this );
  277. }
  278. }
  279. }
  280. /* return a string serializing this module's parameter settings. The
  281. format is 1.0:2.0:... Where 1.0 is the value of the first control
  282. input, 2.0 is the value of the second control input etc.
  283. */
  284. char *
  285. Module::get_parameters ( void ) const
  286. {
  287. char *s = new char[1024];
  288. s[0] = 0;
  289. char *sp = s;
  290. if ( control_input.size() )
  291. {
  292. for ( unsigned int i = 0; i < control_input.size(); ++i )
  293. sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
  294. *(sp - 1) = '\0';
  295. }
  296. return s;
  297. }
  298. void
  299. Module::set_parameters ( const char *parameters )
  300. {
  301. char *s = strdup( parameters );
  302. char *start = s;
  303. unsigned int i = 0;
  304. for ( char *sp = s; ; ++sp )
  305. {
  306. if ( ':' == *sp || '\0' == *sp )
  307. {
  308. char was = *sp;
  309. *sp = '\0';
  310. DMESSAGE( start );
  311. if ( i < control_input.size() )
  312. control_input[i].control_value( atof( start ) );
  313. else
  314. {
  315. WARNING( "Module has no parameter at index %i", i );
  316. break;
  317. }
  318. i++;
  319. if ( '\0' == was )
  320. break;
  321. start = sp + 1;
  322. }
  323. }
  324. free( s );
  325. }
  326. void
  327. Module::draw_box ( void )
  328. {
  329. fl_color( fl_contrast( FL_FOREGROUND_COLOR, color() ) );
  330. int tw, th, tx, ty;
  331. tw = w();
  332. th = h();
  333. ty = y();
  334. tx = x();
  335. fl_push_clip( tx, ty, tw, th );
  336. Fl_Color c = is_default() ? FL_BLACK : color();
  337. c = active() && ! bypass() ? c : fl_inactive( c );
  338. int spacing = w() / instances();
  339. for ( int i = instances(); i--; )
  340. {
  341. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, Fl::belowmouse() == this ? fl_lighter( c ) : c );
  342. }
  343. if ( this == Fl::focus() )
  344. {
  345. fl_draw_box( FL_UP_FRAME, x(), y(), w(), h(), selection_color() );
  346. }
  347. if ( audio_input.size() && audio_output.size() )
  348. {
  349. /* maybe draw control indicators */
  350. if ( control_input.size() )
  351. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  352. if ( control_output.size() )
  353. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  354. }
  355. Fl_Group::draw_children();
  356. fl_pop_clip();
  357. }
  358. void
  359. Module::draw_label ( void )
  360. {
  361. int tw, th, tx, ty;
  362. bbox( tx, ty, tw, th );
  363. const char *lp = label();
  364. int l = strlen( label() );
  365. Fl_Color c = FL_FOREGROUND_COLOR;
  366. if ( bypass() || ! active() )
  367. c = FL_BLACK;
  368. fl_color( fl_contrast( c, is_default() ? FL_BLACK : color() ) );
  369. char *s = NULL;
  370. if ( l > 10 )
  371. {
  372. s = new char[l];
  373. char *sp = s;
  374. for ( ; *lp; ++lp )
  375. switch ( *lp )
  376. {
  377. case 'i': case 'e': case 'o': case 'u': case 'a':
  378. break;
  379. default:
  380. *(sp++) = *lp;
  381. }
  382. *sp = '\0';
  383. }
  384. if ( l > 20 )
  385. fl_font( FL_HELVETICA, 10 );
  386. else
  387. fl_font( FL_HELVETICA, 14 );
  388. fl_draw( s ? s : lp, tx, ty, tw, th, (Fl_Align)(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP ) );
  389. if ( s )
  390. delete[] s;
  391. }
  392. void
  393. Module::insert_menu_cb ( const Fl_Menu_ *m )
  394. {
  395. if ( ! m->mvalue() || m->mvalue()->flags & FL_SUBMENU_POINTER || m->mvalue()->flags & FL_SUBMENU )
  396. return;
  397. void * v = m->mvalue()->user_data();
  398. if ( v )
  399. {
  400. unsigned long id = *((unsigned long *)v);
  401. Module *mod = NULL;
  402. switch ( id )
  403. {
  404. case 1:
  405. mod = new JACK_Module();
  406. break;
  407. case 2:
  408. mod = new Gain_Module();
  409. break;
  410. case 3:
  411. mod = new Meter_Module();
  412. break;
  413. case 4:
  414. mod = new Mono_Pan_Module();
  415. break;
  416. default:
  417. {
  418. Plugin_Module *m = new Plugin_Module();
  419. m->load( id );
  420. mod = m;
  421. }
  422. }
  423. if ( mod )
  424. {
  425. if ( !strcmp( mod->name(), "JACK" ) )
  426. {
  427. DMESSAGE( "Special casing JACK module" );
  428. JACK_Module *jm = (JACK_Module*)mod;
  429. jm->chain( chain() );
  430. jm->configure_inputs( ninputs() );
  431. jm->configure_outputs( ninputs() );
  432. }
  433. if ( ! chain()->insert( this, mod ) )
  434. {
  435. fl_alert( "Cannot insert this module at this point in the chain" );
  436. delete mod;
  437. return;
  438. }
  439. redraw();
  440. }
  441. }
  442. }
  443. void
  444. Module::insert_menu_cb ( Fl_Widget *w, void *v )
  445. {
  446. ((Module*)v)->insert_menu_cb( (Fl_Menu_*) w );
  447. }
  448. void
  449. Module::menu_cb ( const Fl_Menu_ *m )
  450. {
  451. char picked[256];
  452. if ( ! m->mvalue() || m->mvalue()->flags & FL_SUBMENU_POINTER || m->mvalue()->flags & FL_SUBMENU )
  453. return;
  454. strncpy( picked, m->mvalue()->label(), sizeof( picked ) );
  455. // m->item_pathname( picked, sizeof( picked ) );
  456. DMESSAGE( "%s", picked );
  457. Logger log( this );
  458. if ( ! strcmp( picked, "Edit Parameters" ) )
  459. command_open_parameter_editor();
  460. else if ( ! strcmp( picked, "Bypass" ) )
  461. bypass( ! ( m->mvalue()->flags & FL_MENU_VALUE ) );
  462. else if ( ! strcmp( picked, "Cut" ) )
  463. {
  464. copy();
  465. chain()->remove( this );
  466. Fl::delete_widget( this );
  467. }
  468. else if ( ! strcmp( picked, "Copy" ) )
  469. {
  470. copy();
  471. }
  472. else if ( ! strcmp( picked, "Paste" ) )
  473. {
  474. paste_before();
  475. }
  476. else if ( ! strcmp( picked, "Remove" ) )
  477. command_remove();
  478. }
  479. void
  480. Module::menu_cb ( Fl_Widget *w, void *v )
  481. {
  482. ((Module*)v)->menu_cb( (Fl_Menu_*) w );
  483. }
  484. /** build the context menu */
  485. Fl_Menu_Button &
  486. Module::menu ( void ) const
  487. {
  488. static Fl_Menu_Button m( 0, 0, 0, 0, "Module" );
  489. static Fl_Menu_Button *insert_menu = NULL;
  490. if ( ! insert_menu )
  491. {
  492. insert_menu = new Fl_Menu_Button( 0, 0, 0, 0 );
  493. insert_menu->add( "Gain", 0, 0, new unsigned long(2) );
  494. insert_menu->add( "Meter", 0, 0, new unsigned long(3) );
  495. insert_menu->add( "Mono Pan", 0, 0, new unsigned long(4) );
  496. Plugin_Module::add_plugins_to_menu( insert_menu );
  497. // menu_set_callback( insert_menu, &Module::insert_menu_cb, (void*)this );
  498. insert_menu->callback( &Module::insert_menu_cb, (void*)this );
  499. }
  500. m.clear();
  501. m.add( "Insert", 0, &Module::menu_cb, (void*)this, 0);
  502. m.add( "Insert", 0, &Module::menu_cb, const_cast< Fl_Menu_Item *>( insert_menu->menu() ), FL_SUBMENU_POINTER );
  503. m.add( "Edit Parameters", ' ', &Module::menu_cb, (void*)this, 0 );
  504. m.add( "Bypass", 'b', &Module::menu_cb, (void*)this, FL_MENU_TOGGLE | ( bypass() ? FL_MENU_VALUE : 0 ) );
  505. m.add( "Cut", FL_CTRL + 'x', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  506. m.add( "Copy", FL_CTRL + 'c', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  507. m.add( "Paste", FL_CTRL + 'v', &Module::menu_cb, (void*)this, _copied_module_empty ? 0 : FL_MENU_INACTIVE );
  508. m.add( "Remove", FL_Delete, &Module::menu_cb, (void*)this );
  509. // menu_set_callback( menu, &Module::menu_cb, (void*)this );
  510. m.callback( &Module::insert_menu_cb, (void*)this );
  511. return m;
  512. }
  513. void
  514. Module::handle_chain_name_changed ( )
  515. {
  516. // pass it along to our connected Controller_Modules, if any.
  517. for ( int i = 0; i < ncontrol_inputs(); ++i )
  518. {
  519. if ( control_input[i].connected() )
  520. control_input[i].connected_port()->module()->handle_chain_name_changed();
  521. control_input[i].update_osc_port();
  522. }
  523. }
  524. int
  525. Module::handle ( int m )
  526. {
  527. switch ( m )
  528. {
  529. case FL_KEYBOARD:
  530. {
  531. if ( Fl_Group::handle( m ) )
  532. return 1;
  533. if ( Fl::event_key() == FL_Menu )
  534. {
  535. menu_popup( &menu(), x(), y() );
  536. return 1;
  537. }
  538. else
  539. return menu().test_shortcut() != 0;
  540. }
  541. case FL_PUSH:
  542. {
  543. take_focus();
  544. if ( Fl_Group::handle( m ) )
  545. return 1;
  546. else if ( test_press( FL_BUTTON3 ) )
  547. {
  548. menu_popup( &menu() );
  549. return 1;
  550. }
  551. else if ( test_press( FL_BUTTON1 ) )
  552. {
  553. command_open_parameter_editor();
  554. return 1;
  555. }
  556. else if ( test_press( FL_BUTTON3 | FL_CTRL ) )
  557. {
  558. command_remove();
  559. return 1;
  560. }
  561. else if ( test_press( FL_BUTTON2 ) )
  562. {
  563. bypass( !bypass() );
  564. redraw();
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. case FL_FOCUS:
  570. case FL_UNFOCUS:
  571. redraw();
  572. return 1;
  573. }
  574. return Fl_Group::handle( m );
  575. }
  576. /************/
  577. /* Commands */
  578. /************/
  579. void
  580. Module::command_open_parameter_editor ( void )
  581. {
  582. if ( _editor )
  583. {
  584. _editor->show();
  585. }
  586. else if ( ncontrol_inputs() )
  587. {
  588. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  589. _editor = new Module_Parameter_Editor( this );
  590. _editor->show();
  591. }
  592. }
  593. void
  594. Module::command_activate ( void )
  595. {
  596. bypass( false );
  597. }
  598. void
  599. Module::command_deactivate ( void )
  600. {
  601. bypass( true );
  602. }
  603. void
  604. Module::command_remove ( void )
  605. {
  606. if ( is_default() )
  607. fl_alert( "Default modules may not be deleted." );
  608. else
  609. {
  610. chain()->remove( this );
  611. Fl::delete_widget( this );
  612. }
  613. }