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.

852 lines
20KB

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