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.

780 lines
18KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 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. /* A Track is a container for various sequences; the sequence, the
  19. * takes (inactive sequences), annotation sequences, control
  20. * sequences */
  21. /* TODO: split into Track and Audio_Track (and maybe later Video_Track
  22. * and MIDI_Track */
  23. #include "Track.H"
  24. #include "Transport.H"
  25. #include "../FL/Fl_Sometimes_Input.H"
  26. #include <FL/fl_ask.H>
  27. #include <FL/Fl_Color_Chooser.H>
  28. #include <FL/Fl.H>
  29. #include "Engine/Engine.H" // for lock()
  30. #include "Control_Sequence.H"
  31. #include "Annotation_Sequence.H"
  32. int Track::_soloing = 0;
  33. const char *Track::capture_format = "Wav 24";
  34. Track::Track ( const char *L, int channels ) :
  35. Fl_Group ( 0, 0, 0, 0, 0 )
  36. {
  37. init();
  38. if ( L )
  39. name( L );
  40. color( (Fl_Color)rand() );
  41. configure_inputs( channels );
  42. configure_outputs( channels );
  43. log_create();
  44. }
  45. Track::Track ( ) : Fl_Group( 0, 0, 1, 1 )
  46. {
  47. init();
  48. timeline->add_track( this );
  49. }
  50. void
  51. Track::solo ( bool b )
  52. {
  53. if ( b && ! solo_button->value() )
  54. ++_soloing;
  55. else if ( ! b && solo_button->value() )
  56. --_soloing;
  57. solo_button->value( b );
  58. }
  59. Track::~Track ( )
  60. {
  61. Loggable::block_start();
  62. takes = NULL;
  63. control = NULL;
  64. annotation = NULL;
  65. Fl_Group::clear();
  66. log_destroy();
  67. timeline->remove_track( this );
  68. /* give up our ports */
  69. configure_inputs( 0 );
  70. configure_outputs( 0 );
  71. _sequence = NULL;
  72. solo( false );
  73. if ( _name )
  74. free( _name );
  75. Loggable::block_end();
  76. }
  77. #include "FL/Boxtypes.H"
  78. void
  79. Track::init ( void )
  80. {
  81. _sequence = NULL;
  82. _name = NULL;
  83. _selected = false;
  84. _show_all_takes = false;
  85. _size = 1;
  86. record_ds = NULL;
  87. playback_ds = NULL;
  88. labeltype( FL_NO_LABEL );
  89. // clear_visible_focus();
  90. Fl_Group::size( timeline->w(), height() );
  91. Track *o = this;
  92. o->box( FL_THIN_UP_BOX );
  93. {
  94. Fl_Group *o = new Fl_Group( 0, 0, 149, 70 );
  95. o->color( ( Fl_Color ) 53 );
  96. {
  97. Fl_Input *o = name_field = new Fl_Sometimes_Input( 2, 2, 144, 24 );
  98. o->color( FL_BACKGROUND_COLOR );
  99. o->labeltype( FL_NO_LABEL );
  100. o->labelcolor( FL_GRAY0 );
  101. o->textcolor( FL_FOREGROUND_COLOR );
  102. o->callback( cb_input_field, (void*)this );
  103. }
  104. {
  105. Fl_Group *o = controls = new Fl_Group( 2, 28, 149, 24 );
  106. {
  107. Fl_Button *o = record_button =
  108. new Fl_Button( 6, 28, 26, 24, "@circle" );
  109. o->type( 1 );
  110. o->box( FL_ROUNDED_BOX );
  111. o->down_box( FL_ROUNDED_BOX );
  112. o->color( FL_LIGHT1 );
  113. o->selection_color( FL_RED );
  114. o->labelsize( 9 );
  115. o->callback( cb_button, this );
  116. }
  117. {
  118. Fl_Button *o = mute_button =
  119. new Fl_Button( 35, 28, 26, 24, "m" );
  120. o->selection_color( fl_color_average( FL_YELLOW, FL_GREEN, 0.50 ) );
  121. o->type( 1 );
  122. o->box( FL_ROUNDED_BOX );
  123. o->down_box( FL_ROUNDED_BOX );
  124. o->color( FL_LIGHT1 );
  125. o->labelsize( 15 );
  126. o->callback( cb_button, this );
  127. }
  128. {
  129. Fl_Button *o = solo_button =
  130. new Fl_Button( 66, 28, 26, 24, "s" );
  131. o->selection_color( fl_color_average( FL_YELLOW, FL_RED, 0.50 ) );
  132. o->type( 1 );
  133. o->box( FL_ROUNDED_BOX );
  134. o->down_box( FL_ROUNDED_BOX );
  135. o->color( FL_LIGHT1 );
  136. o->labelsize( 15 );
  137. o->callback( cb_button, this );
  138. }
  139. {
  140. Fl_Menu_Button *o = take_menu =
  141. new Fl_Menu_Button( 97, 28, 47, 24, "T" );
  142. o->box( FL_THIN_UP_BOX );
  143. o->color( FL_LIGHT1 );
  144. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  145. o->callback( cb_button, this );
  146. o->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  147. o->add( "New", 0, 0, 0, FL_MENU_DIVIDER );
  148. }
  149. o->end();
  150. }
  151. {
  152. Fl_Box *o = new Fl_Box( 0, 72, 149, 38 );
  153. o->box( FL_NO_BOX );
  154. Fl_Group::current()->resizable( o );
  155. }
  156. o->size( Track::width(), h() );
  157. o->end();
  158. }
  159. {
  160. Fl_Pack *o = pack = new Fl_Pack( width(), 0, 1006, 115 );
  161. o->labeltype( FL_NO_LABEL );
  162. o->resize( x() + width(), y(), w() - width(), h() );
  163. resizable( o );
  164. {
  165. // Fl_Pack *o = annotation = new Fl_Pack( width(), 0, pack->w(), 0 );
  166. Fl_Pack *o = annotation = new Fl_Pack( width(), 0, pack->w(), 1 );
  167. o->end();
  168. }
  169. {
  170. Fl_Pack *o = control = new Fl_Pack( width(), 0, pack->w(), 0 );
  171. o->end();
  172. }
  173. {
  174. Fl_Pack *o = takes = new Fl_Pack( width(), 0, pack->w(), 0 );
  175. o->end();
  176. o->hide();
  177. }
  178. o->end();
  179. }
  180. end();
  181. }
  182. void
  183. Track::set ( Log_Entry &e )
  184. {
  185. for ( int i = 0; i < e.size(); ++i )
  186. {
  187. const char *s, *v;
  188. e.get( i, &s, &v );
  189. if ( ! strcmp( s, ":height" ) )
  190. {
  191. size( atoi( v ) );
  192. resize();
  193. }
  194. else if ( ! strcmp( s, ":selected" ) )
  195. _selected = atoi( v );
  196. // else if ( ! strcmp( s, ":armed"
  197. else if ( ! strcmp( s, ":name" ) )
  198. name( v );
  199. else if ( ! strcmp( s, ":inputs" ) )
  200. configure_inputs( atoi( v ) );
  201. else if ( ! strcmp( s, ":outputs" ) )
  202. configure_outputs( atoi( v ) );
  203. else if ( ! strcmp( s, ":color" ) )
  204. {
  205. color( (Fl_Color)atoll( v ) );
  206. redraw();
  207. }
  208. else if ( ! strcmp( s, ":show-all-takes" ) )
  209. show_all_takes( atoi( v ) );
  210. else if ( ! strcmp( s, ":sequence" ) )
  211. {
  212. int i;
  213. sscanf( v, "%X", &i );
  214. if ( i )
  215. {
  216. Audio_Sequence *t = (Audio_Sequence*)Loggable::find( i );
  217. /* FIXME: our track might not have been
  218. * defined yet... what should we do about this
  219. * chicken/egg problem? */
  220. if ( t )
  221. {
  222. // assert( t );
  223. sequence( t );
  224. }
  225. }
  226. }
  227. }
  228. }
  229. void
  230. Track::get ( Log_Entry &e ) const
  231. {
  232. e.add( ":name", _name );
  233. e.add( ":sequence", sequence() );
  234. e.add( ":selected", _selected );
  235. e.add( ":height", size() );
  236. e.add( ":inputs", input.size() );
  237. e.add( ":outputs", output.size() );
  238. e.add( ":color", (unsigned long)color());
  239. e.add( ":show-all-takes", _show_all_takes );
  240. }
  241. void
  242. Track::cb_input_field ( Fl_Widget *, void *v )
  243. {
  244. ((Track*)v)->cb_input_field();
  245. }
  246. void
  247. Track::cb_button ( Fl_Widget *w, void *v )
  248. {
  249. ((Track*)v)->cb_button( w );
  250. }
  251. void
  252. Track::cb_input_field ( void )
  253. {
  254. log_start();
  255. name( name_field->value() );
  256. log_end();
  257. }
  258. void
  259. Track::cb_button ( Fl_Widget *w )
  260. {
  261. if ( w == record_button )
  262. {
  263. }
  264. if ( w == mute_button )
  265. {
  266. }
  267. if ( w == solo_button )
  268. {
  269. if ( solo_button->value() )
  270. ++_soloing;
  271. else
  272. --_soloing;
  273. }
  274. else
  275. if ( w == take_menu )
  276. {
  277. int v = take_menu->value();
  278. switch ( v )
  279. {
  280. case 0: /* show all takes */
  281. show_all_takes( take_menu->menu()[ v ].value() );
  282. break;
  283. case 1: /* new */
  284. sequence( (Audio_Sequence*)sequence()->clone_empty() );
  285. break;
  286. case 2: /* remove */
  287. if ( takes->children() )
  288. {
  289. Loggable::block_start();
  290. Sequence *s = sequence();
  291. sequence( (Audio_Sequence*)takes->child( 0 ) );
  292. delete s;
  293. Loggable::block_end();
  294. }
  295. break;
  296. default:
  297. sequence( (Audio_Sequence*)take_menu->menu()[ v ].user_data() );
  298. }
  299. }
  300. }
  301. static int pack_visible( Fl_Pack *p )
  302. {
  303. int v = 0;
  304. for ( int i = p->children(); i--; )
  305. if ( p->child( i )->visible() )
  306. v++;
  307. return v;
  308. }
  309. /* adjust size of widget and children */
  310. void
  311. Track::resize ( void )
  312. {
  313. for ( int i = takes->children(); i--; )
  314. takes->child( i )->size( w(), height() );
  315. for ( int i = annotation->children(); i--; )
  316. annotation->child( i )->size( w(), 24 );
  317. for ( int i = control->children(); i--; )
  318. control->child( i )->size( w(), height() );
  319. /* FIXME: hack! */
  320. if ( annotation->children() )
  321. annotation->show();
  322. else
  323. annotation->hide();
  324. if ( _show_all_takes )
  325. {
  326. takes->show();
  327. Fl_Group::size( w(), height() * ( 1 + takes->children() + pack_visible( control ) ) );
  328. }
  329. else
  330. {
  331. takes->hide();
  332. Fl_Group::size( w(), height() * ( 1 + pack_visible( control ) ) );
  333. }
  334. Fl_Group::size( w(), h() + ( ( 24 ) * pack_visible( annotation ) ) );
  335. if ( sequence() )
  336. sequence()->size( w(), height() );
  337. if ( controls->y() + controls->h() > y() + h() )
  338. controls->hide();
  339. else
  340. controls->show();
  341. /* FIXME: why is this necessary? */
  342. if ( parent() )
  343. parent()->parent()->redraw();
  344. }
  345. void
  346. Track::size ( int v )
  347. {
  348. if ( v < 0 || v > 3 )
  349. return;
  350. _size = v;
  351. resize();
  352. }
  353. void
  354. Track::update_take_menu ( void )
  355. {
  356. take_menu->clear();
  357. take_menu->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  358. take_menu->add( "New", 0, 0, 0 );
  359. take_menu->add( "Remove", 0, 0, 0, FL_MENU_DIVIDER );
  360. for ( int i = 0; i < takes->children(); ++i )
  361. {
  362. Sequence *s = (Sequence *)takes->child( i );
  363. take_menu->add( s->name(), 0, 0, s );
  364. }
  365. }
  366. void
  367. Track::add ( Audio_Sequence * t )
  368. {
  369. takes->insert( *t, 0 );
  370. if ( ! t->name() )
  371. {
  372. char pat[20];
  373. snprintf( pat, sizeof( pat ), "%d", 1 + takes->children() );
  374. t->name( strdup( pat ) );
  375. }
  376. t->labeltype( FL_ENGRAVED_LABEL );
  377. update_take_menu();
  378. }
  379. void
  380. Track::remove ( Audio_Sequence *t )
  381. {
  382. if ( ! takes )
  383. return;
  384. timeline->wrlock();
  385. takes->remove( t );
  386. /* delete t; */
  387. timeline->unlock();
  388. resize();
  389. update_take_menu();
  390. }
  391. void
  392. Track::remove ( Annotation_Sequence *t )
  393. {
  394. if ( ! annotation )
  395. return;
  396. annotation->remove( t );
  397. resize();
  398. }
  399. void
  400. Track::remove ( Control_Sequence *t )
  401. {
  402. if ( ! control )
  403. return;
  404. engine->lock();
  405. control->remove( t );
  406. engine->unlock();
  407. resize();
  408. }
  409. void
  410. Track::sequence ( Audio_Sequence * t )
  411. {
  412. t->track( this );
  413. if ( sequence() )
  414. add( sequence() );
  415. _sequence = t;
  416. pack->insert( *t, 1 );
  417. t->labeltype( FL_NO_LABEL );
  418. resize();
  419. }
  420. void
  421. Track::add ( Control_Sequence *t )
  422. {
  423. DMESSAGE( "adding control sequence" );
  424. engine->lock();
  425. t->track( this );
  426. control->add( t );
  427. engine->unlock();
  428. resize();
  429. }
  430. void
  431. Track::add ( Annotation_Sequence *t )
  432. {
  433. DMESSAGE( "adding annotation sequence" );
  434. t->track( this );
  435. annotation->add( t );
  436. resize();
  437. }
  438. /** add all widget on this track falling within the given rectangle to
  439. the selection. */
  440. void
  441. Track::select ( int X, int Y, int W, int H,
  442. bool include_control, bool merge_control )
  443. {
  444. Sequence *t = sequence();
  445. if ( ! ( t->y() > Y + H || t->y() + t->h() < Y ) )
  446. t->select_range( X, W );
  447. else
  448. include_control = true;
  449. if ( include_control )
  450. for ( int i = control->children(); i--; )
  451. {
  452. Control_Sequence *c = (Control_Sequence*)control->child( i );
  453. if ( merge_control ||
  454. ( c->y() >= Y && c->y() + c->h() <= Y + H ) )
  455. c->select_range( X, W );
  456. }
  457. }
  458. #include <FL/Fl_Menu_Button.H>
  459. void
  460. Track::menu_cb ( Fl_Widget *w, void *v )
  461. {
  462. ((Track*)v)->menu_cb( (Fl_Menu_*) w );
  463. }
  464. void
  465. Track::menu_cb ( const Fl_Menu_ *m )
  466. {
  467. char picked[256];
  468. m->item_pathname( picked, sizeof( picked ) );
  469. Logger log( this );
  470. if ( ! strcmp( picked, "Type/Mono" ) )
  471. {
  472. configure_inputs( 1 );
  473. configure_outputs( 1 );
  474. }
  475. else if ( ! strcmp( picked, "Type/Stereo" ) )
  476. {
  477. configure_inputs( 2 );
  478. configure_outputs( 2 );
  479. }
  480. else if ( ! strcmp( picked, "Type/Quad" ) )
  481. {
  482. configure_inputs( 4 );
  483. configure_outputs( 4 );
  484. }
  485. else if ( ! strcmp( picked, "Type/..." ) )
  486. {
  487. const char *s = fl_input( "How many channels?", "3" );
  488. int c = atoi( s );
  489. if ( c <= 0 || c > 10 )
  490. fl_alert( "Invalid number of channels." );
  491. else
  492. {
  493. configure_inputs( c );
  494. configure_outputs( c );
  495. }
  496. }
  497. else if ( ! strcmp( picked, "/Add Control" ) )
  498. {
  499. new Control_Sequence( this );
  500. }
  501. else if ( ! strcmp( picked, "/Add Annotation" ) )
  502. {
  503. add( new Annotation_Sequence( this ) );
  504. }
  505. else if ( ! strcmp( picked, "/Color" ) )
  506. {
  507. unsigned char r, g, b;
  508. Fl::get_color( color(), r, g, b );
  509. if ( fl_color_chooser( "Track Color", r, g, b ) )
  510. {
  511. color( fl_rgb_color( r, g, b ) );
  512. }
  513. redraw();
  514. }
  515. else if ( ! strcmp( picked, "/Remove" ) )
  516. {
  517. int r = fl_choice( "Are you certain you want to remove track \"%s\"?", "Cancel", NULL, "Remove", name() );
  518. if ( r == 2 )
  519. {
  520. timeline->remove_track( this );
  521. Fl::delete_widget( this );
  522. }
  523. }
  524. else if ( ! strcmp( picked, "/Rename" ) )
  525. {
  526. ((Fl_Sometimes_Input*)name_field)->take_focus();
  527. }
  528. }
  529. #include "FL/menu_popup.H"
  530. /** build the context menu */
  531. Fl_Menu_Button &
  532. Track::menu ( void ) const
  533. {
  534. static Fl_Menu_Button m( 0, 0, 0, 0, "Track" );
  535. int c = output.size();
  536. Fl_Menu_Item menu[] =
  537. {
  538. { "Type", 0, 0, 0, FL_SUBMENU },
  539. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  540. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  541. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  542. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  543. { 0 },
  544. { "Add Control", 0, 0, 0 },
  545. { "Add Annotation", 0, 0, 0 },
  546. { "Color", 0, 0, 0 },
  547. { "Rename", FL_CTRL + 'n', 0, 0 },
  548. { "Remove", 0, 0, 0 }, // transport->rolling ? FL_MENU_INACTIVE : 0 },
  549. { 0 },
  550. };
  551. menu_set_callback( menu, &Track::menu_cb, (void*)this );
  552. m.copy( menu, (void*)this );
  553. return m;
  554. }
  555. #include "FL/event_name.H"
  556. #include "FL/test_press.H"
  557. void
  558. Track::draw ( void )
  559. {
  560. if ( _selected )
  561. {
  562. Fl_Color c = color();
  563. color( FL_RED );
  564. Fl_Group::draw();
  565. color( c );
  566. }
  567. else
  568. Fl_Group::draw();
  569. }
  570. int
  571. Track::handle ( int m )
  572. {
  573. /* if ( m != FL_NO_EVENT ) */
  574. /* DMESSAGE( "%s", event_name( m ) ); */
  575. switch ( m )
  576. {
  577. case FL_KEYBOARD:
  578. return menu().test_shortcut() || Fl_Group::handle( m );
  579. case FL_MOUSEWHEEL:
  580. {
  581. Logger log( this );
  582. if ( ! Fl::event_shift() )
  583. return 0;
  584. int d = Fl::event_dy();
  585. if ( d < 0 )
  586. size( size() - 1 );
  587. else
  588. size( size() + 1 );
  589. return 1;
  590. }
  591. case FL_PUSH:
  592. {
  593. Logger log( this );
  594. if ( Fl_Group::handle( m ) )
  595. return 1;
  596. if ( test_press( FL_BUTTON3 ) && Fl::event_x() < Track::width() )
  597. {
  598. menu_popup( &menu() );
  599. return 1;
  600. }
  601. return 0;
  602. }
  603. default:
  604. return Fl_Group::handle( m );
  605. }
  606. return 0;
  607. }