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.

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