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.

826 lines
19KB

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