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.

733 lines
16KB

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