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.

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