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.

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