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.

1427 lines
34KB

  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 "AUX_Module.H"
  32. #include "Spatializer_Module.H"
  33. #include "FL/focus_frame.H"
  34. #include <FL/Fl_Menu_Button.H>
  35. #include "FL/test_press.H"
  36. #include "FL/menu_popup.H"
  37. #include "Mixer.H"
  38. #include "Plugin_Chooser.H"
  39. #include "OSC/Endpoint.H"
  40. #include "string_util.h"
  41. nframes_t Module::_sample_rate = 0;
  42. Module *Module::_copied_module_empty = 0;
  43. char *Module::_copied_module_settings = 0;
  44. Module::Module ( int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L )
  45. {
  46. init();
  47. }
  48. Module::Module ( bool is_default, int W, int H, const char *L ) : Fl_Group( 0, 0, W, H, L ), Loggable( !is_default )
  49. {
  50. init();
  51. this->is_default( is_default );
  52. }
  53. Module::Module ( ) : Fl_Group( 0, 0, 50, 50, "Unnamed" )
  54. {
  55. init();
  56. }
  57. Module::~Module ( )
  58. {
  59. /* we assume that the client for this chain is already locked */
  60. if ( _editor )
  61. {
  62. delete _editor;
  63. _editor = NULL;
  64. }
  65. for ( unsigned int i = 0; i < audio_input.size(); ++i )
  66. audio_input[i].disconnect();
  67. for ( unsigned int i = 0; i < audio_output.size(); ++i )
  68. audio_output[i].disconnect();
  69. for ( unsigned int i = 0; i < aux_audio_input.size(); ++i )
  70. {
  71. aux_audio_input[i].disconnect();
  72. aux_audio_input[i].jack_port()->shutdown();
  73. delete aux_audio_input[i].jack_port();
  74. }
  75. for ( unsigned int i = 0; i < aux_audio_output.size(); ++i )
  76. {
  77. aux_audio_output[i].disconnect();
  78. aux_audio_output[i].jack_port()->shutdown();
  79. delete aux_audio_output[i].jack_port();
  80. }
  81. for ( unsigned int i = 0; i < control_input.size(); ++i )
  82. {
  83. /* destroy connected Controller_Module */
  84. if ( control_input[i].connected() )
  85. {
  86. Module *o = (Module*)control_input[i].connected_port()->module();
  87. if ( ! o->is_default() )
  88. {
  89. control_input[i].disconnect();
  90. DMESSAGE( "Deleting connected module %s", o->label() );
  91. delete o;
  92. }
  93. else
  94. {
  95. control_input[i].disconnect();
  96. }
  97. }
  98. control_input[i].destroy_osc_port();
  99. }
  100. for ( unsigned int i = 0; i < control_output.size(); ++i )
  101. control_output[i].disconnect();
  102. audio_input.clear();
  103. audio_output.clear();
  104. control_input.clear();
  105. control_output.clear();
  106. if ( parent() )
  107. parent()->remove( this );
  108. }
  109. void
  110. Module::init ( void )
  111. {
  112. /* we use pointers to these vector elements for port auto connection stuff and need to prevent reallocation from invalidating them. */
  113. audio_input.reserve(16);
  114. audio_output.reserve(16);
  115. control_input.reserve(16);
  116. control_output.reserve(16);
  117. aux_audio_input.reserve(16);
  118. aux_audio_output.reserve(16);
  119. // _latency = 0;
  120. _is_default = false;
  121. _editor = 0;
  122. _chain = 0;
  123. _instances = 1;
  124. _bypass = 0;
  125. box( FL_UP_BOX );
  126. labeltype( FL_NO_LABEL );
  127. align( FL_ALIGN_CENTER | FL_ALIGN_INSIDE );
  128. set_visible_focus();
  129. selection_color( FL_YELLOW );
  130. labelsize(12);
  131. color( fl_rgb_color( 122,190,200 ) );
  132. tooltip();
  133. }
  134. void
  135. Module::update_tooltip ( void )
  136. {
  137. char *s;
  138. asprintf( &s, "Left click to edit parameters; Ctrl + left click to select; right click or MENU key for menu. (info: latency: %lu)", (unsigned long) get_module_latency() );
  139. copy_tooltip(s);
  140. free(s);
  141. }
  142. void
  143. Module::get ( Log_Entry &e ) const
  144. {
  145. // e.add( ":name", label() );
  146. // e.add( ":color", (unsigned long)color());
  147. {
  148. char *s = get_parameters();
  149. if ( strlen( s ) )
  150. e.add( ":parameter_values", s );
  151. delete[] s;
  152. }
  153. e.add( ":is_default", is_default() );
  154. e.add( ":chain", chain() );
  155. e.add( ":active", ! bypass() );
  156. }
  157. bool
  158. Module::copy ( void ) const
  159. {
  160. Module *m = clone_empty();
  161. if ( ! m )
  162. {
  163. DMESSAGE( "Module \"%s\" doesn't support cloning", name() );
  164. return false;
  165. }
  166. Log_Entry *ne = new Log_Entry();
  167. _copied_module_empty = m;
  168. {
  169. Log_Entry e;
  170. get( e );
  171. for ( int i = 0; i < e.size(); ++i )
  172. {
  173. const char *s, *v;
  174. e.get( i, &s, &v );
  175. /* we don't want this module to get added to the current
  176. chain... */
  177. if ( !( !strcmp( s, ":chain" ) ||
  178. !strcmp( s, ":is_default" ) ) )
  179. {
  180. DMESSAGE( "%s = %s", s, v );
  181. ne->add_raw( s, v );
  182. }
  183. }
  184. }
  185. _copied_module_settings = ne->print();
  186. return true;
  187. }
  188. void
  189. Module::paste_before ( void )
  190. {
  191. Module *m = _copied_module_empty;
  192. Log_Entry le( _copied_module_settings );
  193. le.remove( ":chain" );
  194. char *print = le.print();
  195. DMESSAGE( "Pasting settings: %s", print );
  196. free( print );
  197. m->set( le );
  198. if ( ! chain()->insert( this, m ) )
  199. {
  200. fl_alert( "Copied module cannot be inserted at this point in the chain" );
  201. }
  202. free( _copied_module_settings );
  203. _copied_module_settings = NULL;
  204. _copied_module_empty = NULL;
  205. /* set up for another paste */
  206. m->copy();
  207. }
  208. void
  209. Module::Port::disconnect_from_strip ( Mixer_Strip *o )
  210. {
  211. for ( std::list<Port*>::iterator i = _connected.begin(); i != _connected.end(); i++ )
  212. {
  213. Port *p = *i;
  214. if ( p->module()->chain()->strip() == o )
  215. {
  216. /* iterator about to be invalidated... */
  217. i = _connected.erase(i);
  218. disconnect(p);
  219. }
  220. }
  221. }
  222. const char *
  223. Module::Port::osc_number_path ( void )
  224. {
  225. if ( ! _scaled_signal )
  226. return NULL;
  227. int n = _module->chain()->strip()->number();
  228. if ( _by_number_path && n == _by_number_number )
  229. return _by_number_path;
  230. if ( _by_number_path )
  231. free( _by_number_path );
  232. char *rem;
  233. char *client_name;
  234. char *strip_name;
  235. if ( 3 != sscanf( _scaled_signal->path(), "%m[^/]/strip/%m[^/]/%m[^\n]", &client_name, &strip_name, &rem ) )
  236. return NULL;
  237. free( strip_name );
  238. char *path;
  239. asprintf( &path, "%s/strip#/%i/%s", client_name, n, rem );
  240. free( client_name );
  241. free( rem );
  242. _by_number_path = path;
  243. _by_number_number = n;
  244. return path;
  245. }
  246. void
  247. Module::Port::send_feedback ( void )
  248. {
  249. float f = control_value();
  250. if ( hints.ranged )
  251. {
  252. // scale value to range.
  253. float scale = hints.maximum - hints.minimum;
  254. float offset = hints.minimum;
  255. f = ( f - offset ) / scale;
  256. }
  257. if ( f > 1.0 )
  258. f = 1.0;
  259. else if ( f < 0.0 )
  260. f = 0.0;
  261. if ( _scaled_signal )
  262. {
  263. /* send feedback for by_name signal */
  264. mixer->osc_endpoint->send_feedback( _scaled_signal->path(), f );
  265. /* send feedback for by number signal */
  266. mixer->osc_endpoint->send_feedback( osc_number_path(), f );
  267. }
  268. }
  269. void
  270. Module::send_feedback ( void )
  271. {
  272. for ( int i = 0; i < ncontrol_inputs(); i++ )
  273. control_input[i].send_feedback();
  274. }
  275. void
  276. Module::handle_control_changed ( Port *p )
  277. {
  278. if ( _editor )
  279. _editor->handle_control_changed ( p );
  280. p->send_feedback();
  281. }
  282. /* bool */
  283. /* Module::Port::connected_osc ( void ) const */
  284. /* { */
  285. /* if ( _scaled_signal ) */
  286. /* return _scaled_signal->connected(); */
  287. /* else */
  288. /* return false; */
  289. /* } */
  290. char *
  291. Module::Port::generate_osc_path ()
  292. {
  293. const Port *p = this;
  294. char *path = NULL;
  295. // /strip/STRIPNAME/MODULENAME/CONTROLNAME
  296. if ( ! p->hints.visible )
  297. {
  298. return NULL;
  299. }
  300. int n = module()->chain()->get_module_instance_number( module() );
  301. if ( n > 0 )
  302. asprintf( &path, "/strip/%s/%s.%i/%s", module()->chain()->name(), p->module()->label(), n, p->name() );
  303. else
  304. asprintf( &path, "/strip/%s/%s/%s", module()->chain()->name(), p->module()->label(), p->name() );
  305. char *s = escape_url( path );
  306. free( path );
  307. path = s;
  308. return path;
  309. }
  310. void
  311. Module::Port::handle_signal_connection_state_changed ( OSC::Signal *, void *o )
  312. {
  313. ((Module::Port*)o)->module()->redraw();
  314. }
  315. void
  316. Module::Port::change_osc_path ( char *path )
  317. {
  318. if ( path )
  319. {
  320. char *scaled_path = path;
  321. char *unscaled_path = NULL;
  322. asprintf( &unscaled_path, "%s/unscaled", path );
  323. if ( NULL == _scaled_signal )
  324. {
  325. float scaled_default = 0.5f;
  326. if ( hints.ranged )
  327. {
  328. float scale = hints.maximum - hints.minimum;
  329. float offset = hints.minimum;
  330. scaled_default = ( hints.default_value - offset ) / scale;
  331. }
  332. _scaled_signal = mixer->osc_endpoint->add_signal( scaled_path,
  333. OSC::Signal::Input,
  334. 0.0, 1.0, scaled_default,
  335. &Module::Port::osc_control_change_cv, this );
  336. _scaled_signal->connection_state_callback( handle_signal_connection_state_changed, this );
  337. _unscaled_signal = mixer->osc_endpoint->add_signal( unscaled_path,
  338. OSC::Signal::Input,
  339. hints.minimum, hints.maximum, hints.default_value,
  340. &Module::Port::osc_control_change_exact, this );
  341. }
  342. else
  343. {
  344. DMESSAGE( "Renaming OSC signals" );
  345. _scaled_signal->rename( scaled_path );
  346. _unscaled_signal->rename( unscaled_path );
  347. }
  348. free( unscaled_path );
  349. /* this was path, it's ok to free because it was malloc()'d in generate_osc_path */
  350. free( scaled_path );
  351. }
  352. }
  353. int
  354. Module::Port::osc_control_change_exact ( float v, void *user_data )
  355. {
  356. Module::Port *p = (Module::Port*)user_data;
  357. Fl::lock();
  358. float f = v;
  359. if ( p->hints.ranged )
  360. {
  361. if ( f > p->hints.maximum )
  362. f = p->hints.maximum;
  363. else if ( f < p->hints.minimum )
  364. f = p->hints.minimum;
  365. if ( Hints::BOOLEAN == p->hints.type )
  366. f = f > (p->hints.maximum - (p->hints.maximum - p->hints.minimum)) * 0.5f ?
  367. p->hints.maximum :
  368. p->hints.minimum;
  369. }
  370. p->control_value( f );
  371. Fl::unlock();
  372. // mixer->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, f );
  373. return 0;
  374. }
  375. int
  376. Module::Port::osc_control_change_cv ( float v, void *user_data )
  377. {
  378. Module::Port *p = (Module::Port*)user_data;
  379. float f = v;
  380. Fl::lock();
  381. // clamp value to control voltage range.
  382. if ( f > 1.0 )
  383. f = 1.0;
  384. else if ( f < 0.0 )
  385. f = 0.0;
  386. if ( p->hints.ranged )
  387. {
  388. if ( Hints::BOOLEAN == p->hints.type )
  389. f = f > 0.5f ? p->hints.maximum : p->hints.minimum;
  390. // scale value to range.
  391. float scale = p->hints.maximum - p->hints.minimum;
  392. float offset = p->hints.minimum;
  393. f = ( f * scale ) + offset;
  394. }
  395. p->control_value( f );
  396. Fl::unlock();
  397. // mixer->osc_endpoint->send( lo_message_get_source( msg ), "/reply", path, f );
  398. return 0;
  399. }
  400. void
  401. Module::set ( Log_Entry &e )
  402. {
  403. for ( int i = 0; i < e.size(); ++i )
  404. {
  405. const char *s, *v;
  406. e.get( i, &s, &v );
  407. if ( ! ( strcmp( s, ":is_default" ) ) )
  408. {
  409. is_default( atoi( v ) );
  410. }
  411. else if ( ! strcmp( s, ":chain" ) )
  412. {
  413. /* This trickiness is because we may need to know the name of
  414. our chain before we actually get added to it. */
  415. int i;
  416. sscanf( v, "%X", &i );
  417. Chain *t = (Chain*)Loggable::find( i );
  418. assert( t );
  419. chain( t );
  420. }
  421. }
  422. for ( int i = 0; i < e.size(); ++i )
  423. {
  424. const char *s, *v;
  425. e.get( i, &s, &v );
  426. /* if ( ! strcmp( s, ":name" ) ) */
  427. /* label( v ); */
  428. if ( ! strcmp( s, ":parameter_values" ) )
  429. {
  430. set_parameters( v );
  431. }
  432. else if ( ! ( strcmp( s, ":active" ) ) )
  433. {
  434. bypass( ! atoi( v ) );
  435. }
  436. else if ( ! strcmp( s, ":chain" ) )
  437. {
  438. int i;
  439. sscanf( v, "%X", &i );
  440. Chain *t = (Chain*)Loggable::find( i );
  441. assert( t );
  442. t->add( this );
  443. }
  444. }
  445. }
  446. void
  447. Module::chain ( Chain *v )
  448. {
  449. if ( _chain != v )
  450. {
  451. DMESSAGE( "Adding module %s in to chain %s", label(), v ? v->name() : "NULL" );
  452. _chain = v;
  453. for ( int i = 0; i < ncontrol_inputs(); ++i )
  454. {
  455. control_input[i].update_osc_port();
  456. }
  457. }
  458. else
  459. {
  460. DMESSAGE( "Module %s already belongs to chain %s", label(), v ? v->name() : "NULL" );
  461. }
  462. }
  463. /* return a string serializing this module's parameter settings. The
  464. format is 1.0:2.0:... Where 1.0 is the value of the first control
  465. input, 2.0 is the value of the second control input etc.
  466. */
  467. char *
  468. Module::get_parameters ( void ) const
  469. {
  470. char *s = new char[1024];
  471. s[0] = 0;
  472. char *sp = s;
  473. if ( control_input.size() )
  474. {
  475. for ( unsigned int i = 0; i < control_input.size(); ++i )
  476. sp += snprintf( sp, 1024 - (sp - s),"%f:", control_input[i].control_value() );
  477. *(sp - 1) = '\0';
  478. }
  479. return s;
  480. }
  481. void
  482. Module::set_parameters ( const char *parameters )
  483. {
  484. char *s = strdup( parameters );
  485. char *start = s;
  486. unsigned int i = 0;
  487. for ( char *sp = s; ; ++sp )
  488. {
  489. if ( ':' == *sp || '\0' == *sp )
  490. {
  491. char was = *sp;
  492. *sp = '\0';
  493. DMESSAGE( start );
  494. if ( i < control_input.size() )
  495. control_input[i].control_value( atof( start ) );
  496. else
  497. {
  498. WARNING( "Module has no parameter at index %i", i );
  499. break;
  500. }
  501. i++;
  502. if ( '\0' == was )
  503. break;
  504. start = sp + 1;
  505. }
  506. }
  507. free( s );
  508. }
  509. void
  510. Module::draw_box ( int tx, int ty, int tw, int th )
  511. {
  512. fl_color( fl_contrast( FL_FOREGROUND_COLOR, color() ) );
  513. fl_push_clip( tx, ty, tw, th );
  514. Fl_Color c = color();
  515. if ( ! active_r() )
  516. c = fl_inactive( c );
  517. int spacing = w() / instances();
  518. for ( int i = instances(); i--; )
  519. {
  520. fl_draw_box( box(), tx + (spacing * i), ty, tw / instances(), th, c );
  521. }
  522. if ( audio_input.size() && audio_output.size() )
  523. {
  524. /* maybe draw control indicators */
  525. if ( control_input.size() )
  526. {
  527. fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + 4, 5, 5, is_being_controlled() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  528. /* fl_draw_box( FL_ROUNDED_BOX, tx + 4, ty + th - 8, 5, 5, is_being_controlled_osc() ? FL_YELLOW : fl_inactive( FL_YELLOW ) ); */
  529. }
  530. if ( control_output.size() )
  531. fl_draw_box( FL_ROUNDED_BOX, tx + tw - 8, ty + 4, 5, 5, is_controlling() ? FL_YELLOW : fl_inactive( FL_YELLOW ) );
  532. }
  533. fl_push_clip( tx + Fl::box_dx(box()), ty + Fl::box_dy(box()), tw - Fl::box_dw(box()), th - Fl::box_dh(box()) );
  534. Fl_Group::draw_children();
  535. fl_pop_clip();
  536. if ( focused_r( this ) )
  537. draw_focus_frame( tx,ty,tw,th, selection_color() );
  538. fl_pop_clip();
  539. }
  540. #include "SpectrumView.H"
  541. #include <FL/Fl_Double_Window.H>
  542. bool
  543. Module::show_analysis_window ( void )
  544. {
  545. /* use a large window for more accuracy at low frequencies */
  546. nframes_t nframes = sample_rate() / 2;
  547. float *buf = new float[nframes];
  548. memset( buf, 0, sizeof(float) * nframes );
  549. buf[0] = 1;
  550. if ( ! get_impulse_response( buf, nframes ) )
  551. {
  552. // return false;
  553. }
  554. Fl_Double_Window *w = new Fl_Double_Window( 1000, 500 );
  555. {
  556. SpectrumView * o = new SpectrumView( 25,25, 1000 - 50, 500 - 50, label() );
  557. o->labelsize(10);
  558. o->align(FL_ALIGN_RIGHT|FL_ALIGN_TOP);
  559. o->sample_rate( sample_rate() );
  560. o->data( buf, nframes );
  561. }
  562. w->end();
  563. w->show();
  564. while ( w->shown() )
  565. Fl::wait();
  566. delete w;
  567. return true;
  568. }
  569. void
  570. Module::draw_label ( int tx, int ty, int tw, int th )
  571. {
  572. bbox( tx, ty, tw, th );
  573. if ( ! label() )
  574. return;
  575. char *lab = strdup( label() );
  576. Fl_Color c = fl_contrast( FL_FOREGROUND_COLOR, color() );
  577. fl_color( active_r() && ! bypass() ? c : fl_inactive(c) );
  578. fl_font( FL_HELVETICA, labelsize() );
  579. char *di = strstr( lab, " -" );
  580. if ( ! di )
  581. di = strstr( lab, " " );
  582. if ( di )
  583. *di = '\0';
  584. int LW = fl_width( lab );
  585. char *s = NULL;
  586. bool initial = true;
  587. if ( LW > tw )
  588. {
  589. s = new char[strlen(lab) + 1];
  590. char *sp = s;
  591. const char *lp = lab;
  592. for ( ; *lp; ++lp )
  593. {
  594. bool skip = false;
  595. switch ( *lp )
  596. {
  597. case ' ':
  598. initial = true;
  599. skip = false;
  600. break;
  601. case 'i': case 'e': case 'o': case 'u': case 'a':
  602. skip = ! initial;
  603. initial = false;
  604. break;
  605. default:
  606. skip = false;
  607. initial = false;
  608. break;
  609. }
  610. if ( ! skip )
  611. *(sp++) = *lp;
  612. }
  613. *sp = '\0';
  614. }
  615. fl_draw( s ? s : lab, tx, ty, tw, th, align() | FL_ALIGN_CLIP );
  616. if ( bypass() )
  617. {
  618. fl_color( fl_color_add_alpha( fl_color(), 127 ) );
  619. fl_line_style( FL_SOLID, 2 );
  620. fl_line( tx, ty + th * 0.5, tx + tw, ty + th * 0.5 );
  621. fl_line_style( FL_SOLID, 0 );
  622. }
  623. free(lab);
  624. if ( s )
  625. delete[] s;
  626. }
  627. void
  628. Module::insert_menu_cb ( const Fl_Menu_ *m )
  629. {
  630. const char * picked = m->mvalue()->label();
  631. DMESSAGE("picked = %s", picked );
  632. Module *mod = NULL;
  633. if ( !strcmp( picked, "Aux" ) )
  634. {
  635. int n = 0;
  636. for ( int i = 0; i < chain()->modules(); i++ )
  637. {
  638. if ( !strcmp( chain()->module(i)->name(), "AUX" ) )
  639. n++;
  640. }
  641. AUX_Module *jm = new AUX_Module();
  642. jm->chain( chain() );
  643. jm->number( n );
  644. jm->configure_inputs( ninputs() );
  645. jm->configure_outputs( ninputs() );
  646. jm->initialize();
  647. mod = jm;
  648. }
  649. if ( !strcmp( picked, "Spatializer" ) )
  650. {
  651. int n = 0;
  652. for ( int i = 0; i < chain()->modules(); i++ )
  653. {
  654. if ( !strcmp( chain()->module(i)->name(), "Spatializer" ) )
  655. n++;
  656. }
  657. if ( n == 0 )
  658. {
  659. Spatializer_Module *jm = new Spatializer_Module();
  660. jm->chain( chain() );
  661. // jm->number( n );
  662. // jm->configure_inputs( ninputs() );
  663. // jm->configure_outputs( ninputs() );
  664. jm->initialize();
  665. mod = jm;
  666. }
  667. }
  668. else if ( !strcmp( picked, "Gain" ) )
  669. mod = new Gain_Module();
  670. /* else if ( !strcmp( picked, "Spatializer" ) ) */
  671. /* mod = new Spatializer_Module(); */
  672. else if ( !strcmp( picked, "Meter" ) )
  673. mod = new Meter_Module();
  674. else if ( !strcmp( picked, "Mono Pan" ))
  675. mod = new Mono_Pan_Module();
  676. else if ( !strcmp(picked, "Plugin" ))
  677. {
  678. unsigned long id = Plugin_Chooser::plugin_chooser( this->ninputs() );
  679. if ( id == 0 )
  680. return;
  681. Plugin_Module *m = new Plugin_Module();
  682. m->load( id );
  683. mod = m;
  684. }
  685. if ( mod )
  686. {
  687. if ( ! chain()->insert( this, mod ) )
  688. {
  689. fl_alert( "Cannot insert this module at this point in the chain" );
  690. delete mod;
  691. return;
  692. }
  693. redraw();
  694. }
  695. }
  696. void
  697. Module::insert_menu_cb ( Fl_Widget *w, void *v )
  698. {
  699. ((Module*)v)->insert_menu_cb( (Fl_Menu_*) w );
  700. }
  701. void
  702. Module::menu_cb ( const Fl_Menu_ *m )
  703. {
  704. char picked[256];
  705. if ( ! m->mvalue() || m->mvalue()->flags & FL_SUBMENU_POINTER || m->mvalue()->flags & FL_SUBMENU )
  706. return;
  707. strncpy( picked, m->mvalue()->label(), sizeof( picked ) );
  708. // m->item_pathname( picked, sizeof( picked ) );
  709. DMESSAGE( "%s", picked );
  710. Logger log( this );
  711. if ( ! strcmp( picked, "Edit Parameters" ) )
  712. command_open_parameter_editor();
  713. else if ( ! strcmp( picked, "Bypass" ) )
  714. {
  715. if ( ! bypassable() )
  716. {
  717. fl_alert( "Due to its channel configuration, this module cannot be bypassed." );
  718. }
  719. else
  720. {
  721. bypass( !bypass() );
  722. redraw();
  723. }
  724. }
  725. else if ( ! strcmp( picked, "Cut" ) )
  726. {
  727. if ( copy() )
  728. {
  729. chain()->remove( this );
  730. Fl::delete_widget( this );
  731. }
  732. }
  733. else if ( ! strcmp( picked, "Copy" ) )
  734. {
  735. copy();
  736. }
  737. else if ( ! strcmp( picked, "Paste" ) )
  738. {
  739. paste_before();
  740. }
  741. else if ( ! strcmp( picked, "Show Analysis" ) )
  742. {
  743. show_analysis_window();
  744. }
  745. else if ( ! strcmp( picked, "Remove" ) )
  746. command_remove();
  747. }
  748. void
  749. Module::menu_cb ( Fl_Widget *w, void *v )
  750. {
  751. ((Module*)v)->menu_cb( (Fl_Menu_*) w );
  752. }
  753. /** build the context menu */
  754. Fl_Menu_Button &
  755. Module::menu ( void ) const
  756. {
  757. static Fl_Menu_Button m( 0, 0, 0, 0, "Module" );
  758. static Fl_Menu_Button *insert_menu = NULL;
  759. if ( ! insert_menu )
  760. {
  761. insert_menu = new Fl_Menu_Button( 0, 0, 0, 0 );
  762. insert_menu->add( "Gain", 0, 0 );
  763. insert_menu->add( "Meter", 0, 0 );
  764. insert_menu->add( "Mono Pan", 0, 0 );
  765. insert_menu->add( "Aux", 0, 0 );
  766. insert_menu->add( "Spatializer", 0, 0 );
  767. insert_menu->add( "Plugin", 0, 0 );
  768. insert_menu->callback( &Module::insert_menu_cb, (void*)this );
  769. }
  770. m.clear();
  771. m.add( "Insert", 0, &Module::menu_cb, (void*)this, 0);
  772. m.add( "Insert", 0, &Module::menu_cb, const_cast< Fl_Menu_Item *>( insert_menu->menu() ), FL_SUBMENU_POINTER );
  773. m.add( "Edit Parameters", ' ', &Module::menu_cb, (void*)this, 0 );
  774. m.add( "Show Analysis", 's', &Module::menu_cb, (void*)this, 0);
  775. m.add( "Bypass", 'b', &Module::menu_cb, (void*)this, FL_MENU_TOGGLE | ( bypass() ? FL_MENU_VALUE : 0 ) );
  776. m.add( "Cut", FL_CTRL + 'x', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  777. m.add( "Copy", FL_CTRL + 'c', &Module::menu_cb, (void*)this, is_default() ? FL_MENU_INACTIVE : 0 );
  778. m.add( "Paste", FL_CTRL + 'v', &Module::menu_cb, (void*)this, _copied_module_empty ? 0 : FL_MENU_INACTIVE );
  779. m.add( "Remove", FL_Delete, &Module::menu_cb, (void*)this );
  780. // menu_set_callback( menu, &Module::menu_cb, (void*)this );
  781. m.callback( &Module::insert_menu_cb, (void*)this );
  782. return m;
  783. }
  784. void
  785. Module::handle_chain_name_changed ( )
  786. {
  787. // pass it along to our connected Controller_Modules, if any.
  788. for ( int i = 0; i < ncontrol_inputs(); ++i )
  789. {
  790. if ( control_input[i].connected() )
  791. control_input[i].connected_port()->module()->handle_chain_name_changed();
  792. control_input[i].update_osc_port();
  793. }
  794. if ( ! chain()->strip()->group()->single() )
  795. {
  796. /* we have to rename our JACK ports... */
  797. for ( unsigned int i = 0; i < aux_audio_input.size(); i++ )
  798. {
  799. aux_audio_input[i].jack_port()->trackname( chain()->name() );
  800. aux_audio_input[i].jack_port()->rename();
  801. }
  802. for ( unsigned int i = 0; i < aux_audio_output.size(); i++ )
  803. {
  804. aux_audio_output[i].jack_port()->trackname( chain()->name() );
  805. aux_audio_output[i].jack_port()->rename();
  806. }
  807. }
  808. }
  809. int
  810. Module::handle ( int m )
  811. {
  812. static unsigned long _event_state = 0;
  813. unsigned long evstate = Fl::event_state();
  814. switch ( m )
  815. {
  816. case FL_ENTER:
  817. // Fl::focus(this);
  818. case FL_LEAVE:
  819. return 1;
  820. }
  821. if ( Fl_Group::handle( m ) )
  822. return 1;
  823. switch ( m )
  824. {
  825. case FL_KEYBOARD:
  826. {
  827. if ( Fl::event_key() == FL_Menu )
  828. {
  829. menu_popup( &menu(), x(), y() );
  830. return 1;
  831. }
  832. else
  833. return menu().test_shortcut() != 0;
  834. }
  835. case FL_PUSH:
  836. take_focus();
  837. _event_state = evstate;
  838. return 1;
  839. // if ( Fl::visible_focus() && handle( FL_FOCUS )) Fl::focus(this);
  840. case FL_DRAG:
  841. _event_state = evstate;
  842. return 1;
  843. case FL_RELEASE:
  844. {
  845. unsigned long e = _event_state;
  846. _event_state = 0;
  847. if ( ! Fl::event_inside( this ) )
  848. return 1;
  849. if ( ( e & FL_BUTTON1 ) && ( e & FL_CTRL ) )
  850. {
  851. Fl::focus(this);
  852. return 1;
  853. }
  854. else if ( e & FL_BUTTON1 )
  855. {
  856. command_open_parameter_editor();
  857. return 1;
  858. }
  859. else if ( e & FL_BUTTON3 && e & FL_CTRL )
  860. {
  861. command_remove();
  862. return 1;
  863. }
  864. else if ( e & FL_BUTTON3 )
  865. {
  866. menu_popup( &menu() );
  867. return 1;
  868. }
  869. else if ( e & FL_BUTTON2 )
  870. {
  871. if ( !bypassable() )
  872. {
  873. fl_alert( "Due to its channel configuration, this module cannot be bypassed." );
  874. }
  875. else
  876. {
  877. bypass( !bypass() );
  878. redraw();
  879. }
  880. return 1;
  881. }
  882. /* else */
  883. /* { */
  884. /* take_focus(); */
  885. /* } */
  886. return 0;
  887. }
  888. case FL_FOCUS:
  889. case FL_UNFOCUS:
  890. redraw();
  891. return 1;
  892. }
  893. return 0;
  894. }
  895. /*************/
  896. /* AUX Ports */
  897. /*************/
  898. static char *
  899. generate_port_name ( const char *aux, int direction, int n )
  900. {
  901. char *s;
  902. asprintf( &s, "%s%s%s-%i",
  903. aux ? aux : "",
  904. aux ? "/" : "",
  905. direction == JACK::Port::Input ? "in" : "out",
  906. n + 1 );
  907. return s;
  908. }
  909. static void
  910. jack_port_activation_error ( JACK::Port *p )
  911. {
  912. fl_alert( "Could not activate JACK port \"%s\"", p->name() );
  913. }
  914. /* freeze/disconnect all jack ports--used when changing groups */
  915. void
  916. Module::freeze_ports ( void )
  917. {
  918. // pass it along to our connected Controller_Modules, if any.
  919. for ( int i = 0; i < ncontrol_inputs(); ++i )
  920. {
  921. if ( control_input[i].connected() )
  922. control_input[i].connected_port()->module()->freeze_ports();
  923. }
  924. for ( unsigned int i = 0; i < aux_audio_input.size(); ++i )
  925. {
  926. aux_audio_input[i].jack_port()->freeze();
  927. aux_audio_input[i].jack_port()->shutdown();
  928. }
  929. for ( unsigned int i = 0; i < aux_audio_output.size(); ++i )
  930. {
  931. aux_audio_output[i].jack_port()->freeze();
  932. aux_audio_output[i].jack_port()->shutdown();
  933. }
  934. }
  935. /* rename and thaw all jack ports--used when changing groups */
  936. void
  937. Module::thaw_ports ( void )
  938. {
  939. // pass it along to our connected Controller_Modules, if any.
  940. for ( int i = 0; i < ncontrol_inputs(); ++i )
  941. {
  942. if ( control_input[i].connected() )
  943. control_input[i].connected_port()->module()->thaw_ports();
  944. }
  945. const char *trackname = chain()->strip()->group()->single() ? NULL : chain()->name();
  946. for ( unsigned int i = 0; i < aux_audio_input.size(); ++i )
  947. {
  948. /* if we're entering a group we need to add the chain name
  949. * prefix and if we're leaving one, we need to remove it */
  950. aux_audio_input[i].jack_port()->client( chain()->client() );
  951. aux_audio_input[i].jack_port()->trackname( trackname );
  952. aux_audio_input[i].jack_port()->thaw();
  953. }
  954. for ( unsigned int i = 0; i < aux_audio_output.size(); ++i )
  955. {
  956. /* if we're entering a group we won't actually be using our
  957. * JACK output ports anymore, just mixing into the group outputs */
  958. aux_audio_output[i].jack_port()->client( chain()->client() );
  959. aux_audio_output[i].jack_port()->trackname( trackname );
  960. aux_audio_output[i].jack_port()->thaw();
  961. mixer->maybe_auto_connect_output( &aux_audio_output[i] );
  962. }
  963. }
  964. void
  965. Module::auto_connect_outputs ( void )
  966. {
  967. for ( unsigned int i = 0; i < aux_audio_output.size(); ++i )
  968. {
  969. mixer->maybe_auto_connect_output( &aux_audio_output[i] );
  970. }
  971. }
  972. void
  973. Module::auto_disconnect_outputs ( void )
  974. {
  975. for ( unsigned int i = 0; i < aux_audio_output.size(); ++i )
  976. {
  977. Module::Port *p = &aux_audio_output[i];
  978. if ( p->connected_port() )
  979. {
  980. p->connected_port()->jack_port()->disconnect( p->jack_port()->jack_name() );
  981. p->disconnect();
  982. }
  983. }
  984. }
  985. void
  986. Module::get_latency ( JACK::Port::direction_e dir, nframes_t *min, nframes_t *max ) const
  987. {
  988. nframes_t tmin = JACK_MAX_FRAMES >> 1;
  989. nframes_t tmax = 0;
  990. const std::vector<Port> *ports;
  991. if ( dir == JACK::Port::Input )
  992. ports = &aux_audio_input;
  993. else
  994. ports = &aux_audio_output;
  995. if ( ports->size() )
  996. {
  997. for ( unsigned int i = 0; i < ports->size(); i++ )
  998. {
  999. /* if ( ! ports->[i].jack_port()->connected() ) */
  1000. /* continue; */
  1001. nframes_t min,max;
  1002. (*ports)[i].jack_port()->get_latency( dir, &min, &max );
  1003. if ( min < tmin )
  1004. tmin = min;
  1005. if ( max > tmax )
  1006. tmax = max;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. tmin = 0;
  1012. }
  1013. *min = tmin;
  1014. *max = tmax;
  1015. }
  1016. void
  1017. Module::set_latency ( JACK::Port::direction_e dir, nframes_t min, nframes_t max )
  1018. {
  1019. if ( dir == JACK::Port::Output )
  1020. {
  1021. for ( unsigned int i = 0; i < aux_audio_input.size(); i++ )
  1022. aux_audio_input[i].jack_port()->set_latency( dir, min, max );
  1023. }
  1024. else
  1025. {
  1026. for ( unsigned int i = 0; i < aux_audio_output.size(); i++ )
  1027. aux_audio_output[i].jack_port()->set_latency( dir, min, max );
  1028. }
  1029. }
  1030. bool
  1031. Module::add_aux_port ( bool input, const char *prefix, int i, JACK::Port::type_e type )
  1032. {
  1033. const char *trackname = chain()->strip()->group()->single() ? NULL : chain()->name();
  1034. JACK::Port::direction_e direction = input ? JACK::Port::Input : JACK::Port::Output;
  1035. char *portname = generate_port_name( prefix, direction, i );
  1036. JACK::Port *po = new JACK::Port( chain()->client(), trackname, portname, direction, type );
  1037. free(portname);
  1038. if ( ! po->activate() )
  1039. {
  1040. jack_port_activation_error( po );
  1041. return false;
  1042. }
  1043. if ( po->valid() )
  1044. {
  1045. if ( input )
  1046. {
  1047. Module::Port mp( (Module*)this, Module::Port::INPUT, Module::Port::AUX_AUDIO );
  1048. mp.jack_port( po );
  1049. aux_audio_input.push_back( mp );
  1050. }
  1051. else
  1052. {
  1053. Module::Port mp( (Module*)this, Module::Port::OUTPUT, Module::Port::AUX_AUDIO );
  1054. mp.jack_port( po );
  1055. aux_audio_output.push_back( mp );
  1056. }
  1057. }
  1058. else
  1059. {
  1060. delete po;
  1061. return false;
  1062. }
  1063. return true;
  1064. }
  1065. bool
  1066. Module::add_aux_audio_output( const char *prefix, int i )
  1067. {
  1068. bool r = add_aux_port ( false, prefix, i , JACK::Port::Audio);
  1069. if ( r )
  1070. mixer->maybe_auto_connect_output( &aux_audio_output.back() );
  1071. return r;
  1072. }
  1073. bool
  1074. Module::add_aux_audio_input( const char *prefix, int i )
  1075. {
  1076. return add_aux_port ( true, prefix, i , JACK::Port::Audio);
  1077. }
  1078. bool
  1079. Module::add_aux_cv_input( const char *prefix, int i )
  1080. {
  1081. return add_aux_port ( true, prefix, i , JACK::Port::CV);
  1082. }
  1083. /************/
  1084. /* Commands */
  1085. /************/
  1086. void
  1087. Module::command_open_parameter_editor ( void )
  1088. {
  1089. if ( _editor )
  1090. {
  1091. _editor->show();
  1092. }
  1093. else if ( ncontrol_inputs() && nvisible_control_inputs() )
  1094. {
  1095. DMESSAGE( "Opening module parameters for \"%s\"", label() );
  1096. _editor = new Module_Parameter_Editor( this );
  1097. _editor->show();
  1098. }
  1099. }
  1100. void
  1101. Module::command_activate ( void )
  1102. {
  1103. bypass( false );
  1104. }
  1105. void
  1106. Module::command_deactivate ( void )
  1107. {
  1108. bypass( true );
  1109. }
  1110. void
  1111. Module::command_remove ( void )
  1112. {
  1113. if ( is_default() )
  1114. fl_alert( "Default modules may not be deleted." );
  1115. else
  1116. {
  1117. chain()->remove( this );
  1118. Fl::delete_widget( this );
  1119. }
  1120. }