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.

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