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.

763 lines
17KB

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