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.

578 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 "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. _capture = NULL;
  93. _capture_af = NULL;
  94. _sequence = NULL;
  95. _name = NULL;
  96. _selected = false;
  97. _show_all_takes = false;
  98. _size = 1;
  99. record_ds = NULL;
  100. playback_ds = NULL;
  101. labeltype( FL_NO_LABEL );
  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. /* FIXME: why is this necessary? */
  203. timeline->remove_track( this );
  204. /* give up our ports */
  205. configure_inputs( 0 );
  206. configure_outputs( 0 );
  207. /* controls too */
  208. for ( int i = control_out.size(); i--; )
  209. {
  210. control_out.back()->shutdown();
  211. delete control_out.back();
  212. control_out.pop_back();
  213. }
  214. log_destroy();
  215. if ( _name )
  216. free( _name );
  217. }
  218. static int pack_visible( Fl_Pack *p )
  219. {
  220. int v = 0;
  221. for ( int i = p->children(); i--; )
  222. if ( p->child( i )->visible() )
  223. v++;
  224. return v;
  225. }
  226. /* adjust size of widget and children */
  227. void
  228. Track::resize ( void )
  229. {
  230. for ( int i = takes->children(); i--; )
  231. takes->child( i )->size( w(), height() );
  232. for ( int i = annotation->children(); i--; )
  233. annotation->child( i )->size( w(), 24 );
  234. for ( int i = control->children(); i--; )
  235. control->child( i )->size( w(), height() );
  236. /* FIXME: hack! */
  237. if ( annotation->children() )
  238. annotation->show();
  239. else
  240. annotation->hide();
  241. if ( _show_all_takes )
  242. {
  243. takes->show();
  244. Fl_Group::size( w(), height() * ( 1 + takes->children() + pack_visible( control ) ) );
  245. }
  246. else
  247. {
  248. takes->hide();
  249. Fl_Group::size( w(), height() * ( 1 + pack_visible( control ) ) );
  250. }
  251. Fl_Group::size( w(), h() + ( ( 24 ) * pack_visible( annotation ) ) );
  252. if ( sequence() )
  253. sequence()->size( w(), height() );
  254. if ( controls->y() + controls->h() > y() + h() )
  255. controls->hide();
  256. else
  257. controls->show();
  258. /* FIXME: why is this necessary? */
  259. if ( parent() )
  260. parent()->parent()->redraw();
  261. }
  262. void
  263. Track::size ( int v )
  264. {
  265. if ( v < 0 || v > 3 )
  266. return;
  267. _size = v;
  268. resize();
  269. }
  270. void
  271. Track::add ( Audio_Sequence * t )
  272. {
  273. takes->insert( *t, 0 );
  274. if ( ! t->name() )
  275. {
  276. char pat[20];
  277. snprintf( pat, sizeof( pat ), "%d", takes->children() );
  278. t->name( strdup( pat ) );
  279. }
  280. take_menu->add( t->name() );
  281. t->labeltype( FL_ENGRAVED_LABEL );
  282. }
  283. void
  284. Track::remove ( Audio_Sequence *t )
  285. {
  286. takes->remove( t );
  287. resize();
  288. // take_menu->remove( t->name() );
  289. }
  290. void
  291. Track::remove ( Control_Sequence *t )
  292. {
  293. control->remove( t );
  294. resize();
  295. }
  296. void
  297. Track::sequence ( Audio_Sequence * t )
  298. {
  299. t->track( this );
  300. if ( sequence() )
  301. add( sequence() );
  302. _sequence = t;
  303. pack->insert( *t, 1 );
  304. t->labeltype( FL_NO_LABEL );
  305. resize();
  306. }
  307. void
  308. Track::add ( Control_Sequence *t )
  309. {
  310. DMESSAGE( "adding control sequence" );
  311. t->track( this );
  312. control->add( t );
  313. resize();
  314. }
  315. void
  316. Track::add ( Annotation_Sequence *t )
  317. {
  318. DMESSAGE( "adding annotation sequence" );
  319. t->track( this );
  320. annotation->add( t );
  321. resize();
  322. }
  323. /** add all widget on this track falling within the given rectangle to
  324. the selection. */
  325. void
  326. Track::select ( int X, int Y, int W, int H,
  327. bool include_control, bool merge_control )
  328. {
  329. Sequence *t = sequence();
  330. if ( ! ( t->y() > Y + H || t->y() + t->h() < Y ) )
  331. t->select_range( X, W );
  332. else
  333. include_control = true;
  334. if ( include_control )
  335. for ( int i = control->children(); i--; )
  336. {
  337. Control_Sequence *c = (Control_Sequence*)control->child( i );
  338. if ( merge_control ||
  339. ( c->y() >= Y && c->y() + c->h() <= Y + H ) )
  340. c->select_range( X, W );
  341. }
  342. }
  343. void
  344. Track::draw ( void )
  345. {
  346. if ( _selected )
  347. {
  348. Fl_Color c = color();
  349. color( FL_RED );
  350. Fl_Group::draw();
  351. color( c );
  352. }
  353. else
  354. Fl_Group::draw();
  355. }
  356. int
  357. Track::handle ( int m )
  358. {
  359. switch ( m )
  360. {
  361. case FL_MOUSEWHEEL:
  362. {
  363. Logger log( this );
  364. if ( ! Fl::event_shift() )
  365. return 0;
  366. int d = Fl::event_dy();
  367. if ( d < 0 )
  368. size( size() - 1 );
  369. else
  370. size( size() + 1 );
  371. return 1;
  372. }
  373. case FL_PUSH:
  374. {
  375. Logger log( this );
  376. int X = Fl::event_x();
  377. int Y = Fl::event_y();
  378. if ( Fl::event_button3() && X < Track::width() )
  379. {
  380. int c = output.size();
  381. /* context menu */
  382. Fl_Menu_Item menu[] =
  383. {
  384. { "Type", 0, 0, 0, FL_SUBMENU },
  385. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  386. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  387. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  388. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  389. { 0 },
  390. { "Add Control" },
  391. { "Add Annotation" },
  392. { "Color" },
  393. { "Remove", 0, 0, 0, transport->rolling ? FL_MENU_INACTIVE : 0 },
  394. { 0 },
  395. };
  396. const Fl_Menu_Item *r = menu->popup( X, Y, "Track" );
  397. if ( r && r > &menu[ 0 ] )
  398. {
  399. if ( r < &menu[ 4 ] )
  400. {
  401. int c = r - &menu[1];
  402. int ca[] = { 1, 2, 4 };
  403. configure_inputs( ca[ c ] );
  404. configure_outputs( ca[ c ] );
  405. }
  406. else
  407. {
  408. if ( r == &menu[ 4 ] )
  409. {
  410. const char *s = fl_input( "How many channels?", "3" );
  411. int c = atoi( s );
  412. if ( c <= 0 || c > 10 )
  413. fl_alert( "Invalid number of channels." );
  414. else
  415. {
  416. configure_inputs( c );
  417. configure_outputs( c );
  418. }
  419. }
  420. else if ( r == &menu[ 6 ] )
  421. {
  422. new Control_Sequence( this );
  423. }
  424. else if ( r == &menu[ 7 ] )
  425. {
  426. add( new Annotation_Sequence( this ) );
  427. }
  428. else if ( r == &menu[ 8 ] )
  429. {
  430. unsigned char r, g, b;
  431. Fl::get_color( color(), r, g, b );
  432. if ( fl_color_chooser( "Track Color", r, g, b ) )
  433. {
  434. color( fl_rgb_color( r, g, b ) );
  435. }
  436. // color( fl_show_colormap( color() ) );
  437. redraw();
  438. }
  439. else if ( r == &menu[ 9 ] )
  440. {
  441. int r = fl_choice( "Are you certain you want to remove track \"%s\"?", "Cancel", NULL, "Remove", name() );
  442. if ( r == 2 )
  443. {
  444. timeline->remove_track( this );
  445. /* FIXME: need to queue deletion. in a way that won't crash the playback. */
  446. // delete this;
  447. Fl::delete_widget( this );
  448. }
  449. }
  450. }
  451. }
  452. return 1;
  453. }
  454. }
  455. default:
  456. return Fl_Group::handle( m );
  457. }
  458. return 0;
  459. }