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.

649 lines
14KB

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