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.

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