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.

784 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. Sequence *s = sequence();
  293. sequence( (Audio_Sequence*)takes->child( 0 ) );
  294. delete s;
  295. Loggable::block_end();
  296. }
  297. break;
  298. default:
  299. sequence( (Audio_Sequence*)take_menu->menu()[ v ].user_data() );
  300. }
  301. }
  302. }
  303. static int pack_visible( Fl_Pack *p )
  304. {
  305. int v = 0;
  306. for ( int i = p->children(); i--; )
  307. if ( p->child( i )->visible() )
  308. v++;
  309. return v;
  310. }
  311. /* adjust size of widget and children */
  312. void
  313. Track::resize ( void )
  314. {
  315. for ( int i = takes->children(); i--; )
  316. takes->child( i )->size( w(), height() );
  317. for ( int i = annotation->children(); i--; )
  318. annotation->child( i )->size( w(), 24 );
  319. for ( int i = control->children(); i--; )
  320. control->child( i )->size( w(), height() );
  321. /* FIXME: hack! */
  322. if ( annotation->children() )
  323. annotation->show();
  324. else
  325. annotation->hide();
  326. if ( _show_all_takes )
  327. {
  328. takes->show();
  329. Fl_Group::size( w(), height() * ( 1 + takes->children() + pack_visible( control ) ) );
  330. }
  331. else
  332. {
  333. takes->hide();
  334. Fl_Group::size( w(), height() * ( 1 + pack_visible( control ) ) );
  335. }
  336. Fl_Group::size( w(), h() + ( ( 24 ) * pack_visible( annotation ) ) );
  337. if ( sequence() )
  338. sequence()->size( w(), height() );
  339. if ( controls->y() + controls->h() > y() + h() )
  340. controls->hide();
  341. else
  342. controls->show();
  343. /* FIXME: why is this necessary? */
  344. if ( parent() )
  345. parent()->parent()->redraw();
  346. }
  347. void
  348. Track::size ( int v )
  349. {
  350. if ( v < 0 || v > 3 )
  351. return;
  352. _size = v;
  353. resize();
  354. }
  355. void
  356. Track::update_take_menu ( void )
  357. {
  358. take_menu->clear();
  359. take_menu->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  360. take_menu->add( "New", 0, 0, 0 );
  361. take_menu->add( "Remove", 0, 0, 0, FL_MENU_DIVIDER );
  362. for ( int i = 0; i < takes->children(); ++i )
  363. {
  364. Sequence *s = (Sequence *)takes->child( i );
  365. take_menu->add( s->name(), 0, 0, s );
  366. }
  367. }
  368. void
  369. Track::add ( Audio_Sequence * t )
  370. {
  371. takes->insert( *t, 0 );
  372. if ( ! t->name() )
  373. {
  374. char pat[20];
  375. snprintf( pat, sizeof( pat ), "%d", 1 + takes->children() );
  376. t->name( strdup( pat ) );
  377. }
  378. t->labeltype( FL_ENGRAVED_LABEL );
  379. update_take_menu();
  380. }
  381. void
  382. Track::remove ( Audio_Sequence *t )
  383. {
  384. if ( ! takes )
  385. return;
  386. timeline->wrlock();
  387. takes->remove( t );
  388. /* delete t; */
  389. timeline->unlock();
  390. resize();
  391. update_take_menu();
  392. }
  393. void
  394. Track::remove ( Annotation_Sequence *t )
  395. {
  396. if ( ! annotation )
  397. return;
  398. annotation->remove( t );
  399. resize();
  400. }
  401. void
  402. Track::remove ( Control_Sequence *t )
  403. {
  404. if ( ! control )
  405. return;
  406. engine->lock();
  407. control->remove( t );
  408. engine->unlock();
  409. resize();
  410. }
  411. void
  412. Track::sequence ( Audio_Sequence * t )
  413. {
  414. t->track( this );
  415. if ( sequence() )
  416. add( sequence() );
  417. _sequence = t;
  418. pack->insert( *t, 1 );
  419. t->labeltype( FL_NO_LABEL );
  420. resize();
  421. }
  422. void
  423. Track::add ( Control_Sequence *t )
  424. {
  425. DMESSAGE( "adding control sequence" );
  426. engine->lock();
  427. t->track( this );
  428. control->add( t );
  429. engine->unlock();
  430. resize();
  431. }
  432. void
  433. Track::add ( Annotation_Sequence *t )
  434. {
  435. DMESSAGE( "adding annotation sequence" );
  436. t->track( this );
  437. annotation->add( t );
  438. resize();
  439. }
  440. /** add all widget on this track falling within the given rectangle to
  441. the selection. */
  442. void
  443. Track::select ( int X, int Y, int W, int H,
  444. bool include_control, bool merge_control )
  445. {
  446. Sequence *t = sequence();
  447. if ( ! ( t->y() > Y + H || t->y() + t->h() < Y ) )
  448. t->select_range( X, W );
  449. else
  450. include_control = true;
  451. if ( include_control )
  452. for ( int i = control->children(); i--; )
  453. {
  454. Control_Sequence *c = (Control_Sequence*)control->child( i );
  455. if ( merge_control ||
  456. ( c->y() >= Y && c->y() + c->h() <= Y + H ) )
  457. c->select_range( X, W );
  458. }
  459. }
  460. #include <FL/Fl_Menu_Button.H>
  461. void
  462. Track::menu_cb ( Fl_Widget *w, void *v )
  463. {
  464. ((Track*)v)->menu_cb( (Fl_Menu_*) w );
  465. }
  466. void
  467. Track::menu_cb ( const Fl_Menu_ *m )
  468. {
  469. char picked[256];
  470. m->item_pathname( picked, sizeof( picked ) );
  471. Logger log( this );
  472. if ( ! strcmp( picked, "Type/Mono" ) )
  473. {
  474. configure_inputs( 1 );
  475. configure_outputs( 1 );
  476. }
  477. else if ( ! strcmp( picked, "Type/Stereo" ) )
  478. {
  479. configure_inputs( 2 );
  480. configure_outputs( 2 );
  481. }
  482. else if ( ! strcmp( picked, "Type/Quad" ) )
  483. {
  484. configure_inputs( 4 );
  485. configure_outputs( 4 );
  486. }
  487. else if ( ! strcmp( picked, "Type/..." ) )
  488. {
  489. const char *s = fl_input( "How many channels?", "3" );
  490. int c = atoi( s );
  491. if ( c <= 0 || c > 10 )
  492. fl_alert( "Invalid number of channels." );
  493. else
  494. {
  495. configure_inputs( c );
  496. configure_outputs( c );
  497. }
  498. }
  499. else if ( ! strcmp( picked, "/Add Control" ) )
  500. {
  501. new Control_Sequence( this );
  502. }
  503. else if ( ! strcmp( picked, "/Add Annotation" ) )
  504. {
  505. add( new Annotation_Sequence( this ) );
  506. }
  507. else if ( ! strcmp( picked, "/Color" ) )
  508. {
  509. unsigned char r, g, b;
  510. Fl::get_color( color(), r, g, b );
  511. if ( fl_color_chooser( "Track Color", r, g, b ) )
  512. {
  513. color( fl_rgb_color( r, g, b ) );
  514. }
  515. redraw();
  516. }
  517. else if ( ! strcmp( picked, "/Remove" ) )
  518. {
  519. int r = fl_choice( "Are you certain you want to remove track \"%s\"?", "Cancel", NULL, "Remove", name() );
  520. if ( r == 2 )
  521. {
  522. timeline->remove_track( this );
  523. Fl::delete_widget( this );
  524. }
  525. }
  526. else if ( ! strcmp( picked, "/Rename" ) )
  527. {
  528. ((Fl_Sometimes_Input*)name_field)->take_focus();
  529. }
  530. }
  531. #include "FL/menu_popup.H"
  532. /** build the context menu */
  533. Fl_Menu_Button &
  534. Track::menu ( void ) const
  535. {
  536. static Fl_Menu_Button m( 0, 0, 0, 0, "Track" );
  537. int c = output.size();
  538. Fl_Menu_Item menu[] =
  539. {
  540. { "Type", 0, 0, 0, FL_SUBMENU },
  541. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  542. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  543. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  544. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  545. { 0 },
  546. { "Add Control", 0, 0, 0 },
  547. { "Add Annotation", 0, 0, 0 },
  548. { "Color", 0, 0, 0 },
  549. { "Rename", FL_CTRL + 'n', 0, 0 },
  550. { "Remove", 0, 0, 0 }, // transport->rolling ? FL_MENU_INACTIVE : 0 },
  551. { 0 },
  552. };
  553. menu_set_callback( menu, &Track::menu_cb, (void*)this );
  554. m.copy( menu, (void*)this );
  555. return m;
  556. }
  557. #include "FL/event_name.H"
  558. #include "FL/test_press.H"
  559. void
  560. Track::draw ( void )
  561. {
  562. if ( _selected )
  563. {
  564. Fl_Color c = color();
  565. color( FL_RED );
  566. Fl_Group::draw();
  567. color( c );
  568. }
  569. else
  570. Fl_Group::draw();
  571. }
  572. int
  573. Track::handle ( int m )
  574. {
  575. /* if ( m != FL_NO_EVENT ) */
  576. /* DMESSAGE( "%s", event_name( m ) ); */
  577. switch ( m )
  578. {
  579. case FL_KEYBOARD:
  580. return menu().test_shortcut() || Fl_Group::handle( m );
  581. case FL_MOUSEWHEEL:
  582. {
  583. Logger log( this );
  584. if ( ! Fl::event_shift() )
  585. return 0;
  586. int d = Fl::event_dy();
  587. if ( d < 0 )
  588. size( size() - 1 );
  589. else
  590. size( size() + 1 );
  591. return 1;
  592. }
  593. case FL_PUSH:
  594. {
  595. Logger log( this );
  596. if ( Fl_Group::handle( m ) )
  597. return 1;
  598. if ( test_press( FL_BUTTON3 ) && Fl::event_x() < Track::width() )
  599. {
  600. menu_popup( &menu() );
  601. return 1;
  602. }
  603. return 0;
  604. }
  605. default:
  606. return Fl_Group::handle( m );
  607. }
  608. return 0;
  609. #include "const.h"
  610. }