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.

814 lines
19KB

  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 "Playback_DS.H"
  21. #include "Record_DS.H"
  22. #include "Engine.H"
  23. #include "Port.H"
  24. #include "../FL/Fl_Sometimes_Input.H"
  25. #include <FL/fl_ask.H>
  26. #include <FL/Fl_Color_Chooser.H>
  27. // #include <FL/fl_draw.H>
  28. #include <FL/Fl.H>
  29. #include "Control_Sequence.H"
  30. #include "Annotation_Sequence.H"
  31. int Track::_soloing = 0;
  32. const char *Track::capture_format = "Wav 24";
  33. void
  34. Track::cb_input_field ( Fl_Widget *w, void *v )
  35. {
  36. ((Track*)v)->cb_input_field();
  37. }
  38. void
  39. Track::cb_button ( Fl_Widget *w, void *v )
  40. {
  41. ((Track*)v)->cb_button( w );
  42. }
  43. void
  44. Track::cb_input_field ( void )
  45. {
  46. log_start();
  47. name( name_field->value() );
  48. log_end();
  49. }
  50. void
  51. Track::cb_button ( Fl_Widget *w )
  52. {
  53. if ( w == record_button )
  54. {
  55. }
  56. if ( w == mute_button )
  57. {
  58. }
  59. if ( w == solo_button )
  60. {
  61. if ( solo_button->value() )
  62. ++_soloing;
  63. else
  64. --_soloing;
  65. }
  66. else
  67. if ( w == take_menu )
  68. {
  69. int v = take_menu->value();
  70. switch ( v )
  71. {
  72. case 0: /* show all takes */
  73. show_all_takes( take_menu->menu()[ v ].value() );
  74. return;
  75. case 1: /* new */
  76. sequence( (Audio_Sequence*)sequence()->clone_empty() );
  77. return;
  78. }
  79. const char *s = take_menu->menu()[ v ].text;
  80. for ( int i = takes->children(); i--; )
  81. {
  82. Audio_Sequence *t = (Audio_Sequence*)takes->child( i );
  83. if ( ! strcmp( s, t->name() ) )
  84. {
  85. sequence( t );
  86. redraw();
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. void
  93. Track::init ( void )
  94. {
  95. _capture = NULL;
  96. _sequence = NULL;
  97. _name = NULL;
  98. _selected = false;
  99. _show_all_takes = false;
  100. _size = 1;
  101. record_ds = NULL;
  102. playback_ds = NULL;
  103. labeltype( FL_NO_LABEL );
  104. Fl_Group::size( timeline->w(), height() );
  105. Track *o = this;
  106. o->box( FL_THIN_UP_BOX );
  107. {
  108. Fl_Group *o = new Fl_Group( 0, 0, 149, 70 );
  109. o->color( ( Fl_Color ) 53 );
  110. {
  111. Fl_Input *o = name_field = new Fl_Sometimes_Input( 2, 2, 144, 24 );
  112. o->color( FL_BACKGROUND_COLOR );
  113. o->labeltype( FL_NO_LABEL );
  114. o->labelcolor( FL_GRAY0 );
  115. o->textcolor( FL_FOREGROUND_COLOR );
  116. o->callback( cb_input_field, (void*)this );
  117. }
  118. {
  119. Fl_Group *o = controls = new Fl_Group( 2, 28, 149, 24 );
  120. {
  121. Fl_Button *o = record_button =
  122. new Fl_Button( 6, 28, 26, 24, "@circle" );
  123. o->type( 1 );
  124. o->box( FL_THIN_UP_BOX );
  125. o->color( FL_LIGHT1 );
  126. o->selection_color( FL_RED );
  127. o->labelsize( 8 );
  128. o->callback( cb_button, this );
  129. }
  130. {
  131. Fl_Button *o = mute_button =
  132. new Fl_Button( 35, 28, 26, 24, "m" );
  133. o->type( 1 );
  134. o->box( FL_THIN_UP_BOX );
  135. o->color( FL_LIGHT1 );
  136. o->labelsize( 11 );
  137. o->callback( cb_button, this );
  138. }
  139. {
  140. Fl_Button *o = solo_button =
  141. new Fl_Button( 66, 28, 26, 24, "s" );
  142. o->type( 1 );
  143. o->box( FL_THIN_UP_BOX );
  144. o->color( FL_LIGHT1 );
  145. o->labelsize( 11 );
  146. o->callback( cb_button, this );
  147. }
  148. {
  149. Fl_Menu_Button *o = take_menu =
  150. new Fl_Menu_Button( 97, 28, 47, 24, "T" );
  151. o->box( FL_THIN_UP_BOX );
  152. o->color( FL_LIGHT1 );
  153. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  154. o->callback( cb_button, this );
  155. o->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  156. o->add( "New", 0, 0, 0, FL_MENU_DIVIDER );
  157. }
  158. o->end();
  159. }
  160. {
  161. Fl_Box *o = new Fl_Box( 0, 72, 149, 38 );
  162. o->box( FL_NO_BOX );
  163. Fl_Group::current()->resizable( o );
  164. }
  165. o->size( Track::width(), h() );
  166. o->end();
  167. }
  168. {
  169. Fl_Pack *o = pack = new Fl_Pack( width(), 0, 1006, 115 );
  170. o->labeltype( FL_NO_LABEL );
  171. o->resize( x() + width(), y(), w() - width(), h() );
  172. resizable( o );
  173. {
  174. // Fl_Pack *o = annotation = new Fl_Pack( width(), 0, pack->w(), 0 );
  175. Fl_Pack *o = annotation = new Fl_Pack( width(), 0, pack->w(), 1 );
  176. o->end();
  177. }
  178. {
  179. Fl_Pack *o = control = new Fl_Pack( width(), 0, pack->w(), 0 );
  180. o->end();
  181. }
  182. {
  183. Fl_Pack *o = takes = new Fl_Pack( width(), 0, pack->w(), 0 );
  184. o->end();
  185. o->hide();
  186. }
  187. o->end();
  188. }
  189. end();
  190. }
  191. Track::Track ( const char *L, int channels ) :
  192. Fl_Group ( 0, 0, 0, 0, 0 )
  193. {
  194. init();
  195. if ( L )
  196. name( L );
  197. color( (Fl_Color)rand() );
  198. configure_inputs( channels );
  199. configure_outputs( channels );
  200. log_create();
  201. }
  202. Track::~Track ( )
  203. {
  204. /* FIXME: why is this necessary? */
  205. timeline->remove_track( this );
  206. /* give up our ports */
  207. configure_inputs( 0 );
  208. configure_outputs( 0 );
  209. /* controls too */
  210. for ( int i = control_out.size(); i--; )
  211. {
  212. control_out.back()->shutdown();
  213. delete control_out.back();
  214. control_out.pop_back();
  215. }
  216. log_destroy();
  217. if ( _name )
  218. free( _name );
  219. }
  220. static int pack_visible( Fl_Pack *p )
  221. {
  222. int v = 0;
  223. for ( int i = p->children(); i--; )
  224. if ( p->child( i )->visible() )
  225. v++;
  226. return v;
  227. }
  228. /* adjust size of widget and children */
  229. void
  230. Track::resize ( void )
  231. {
  232. for ( int i = takes->children(); i--; )
  233. takes->child( i )->size( w(), height() );
  234. for ( int i = annotation->children(); i--; )
  235. annotation->child( i )->size( w(), 24 );
  236. for ( int i = control->children(); i--; )
  237. control->child( i )->size( w(), height() );
  238. /* FIXME: hack! */
  239. if ( annotation->children() )
  240. annotation->show();
  241. else
  242. annotation->hide();
  243. if ( _show_all_takes )
  244. {
  245. takes->show();
  246. Fl_Group::size( w(), height() * ( 1 + takes->children() + pack_visible( control ) ) );
  247. }
  248. else
  249. {
  250. takes->hide();
  251. Fl_Group::size( w(), height() * ( 1 + pack_visible( control ) ) );
  252. }
  253. Fl_Group::size( w(), h() + ( ( 24 ) * pack_visible( annotation ) ) );
  254. if ( sequence() )
  255. sequence()->size( w(), height() );
  256. if ( controls->y() + controls->h() > y() + h() )
  257. controls->hide();
  258. else
  259. controls->show();
  260. /* FIXME: why is this necessary? */
  261. if ( parent() )
  262. parent()->parent()->redraw();
  263. }
  264. void
  265. Track::size ( int v )
  266. {
  267. if ( v < 0 || v > 3 )
  268. return;
  269. _size = v;
  270. resize();
  271. }
  272. void
  273. Track::add ( Audio_Sequence * t )
  274. {
  275. takes->insert( *t, 0 );
  276. if ( ! t->name() )
  277. {
  278. char pat[20];
  279. snprintf( pat, sizeof( pat ), "%d", takes->children() );
  280. t->name( strdup( pat ) );
  281. }
  282. take_menu->add( t->name() );
  283. t->labeltype( FL_ENGRAVED_LABEL );
  284. }
  285. void
  286. Track::remove ( Audio_Sequence *t )
  287. {
  288. takes->remove( t );
  289. resize();
  290. // take_menu->remove( t->name() );
  291. }
  292. void
  293. Track::remove ( Control_Sequence *t )
  294. {
  295. control->remove( t );
  296. resize();
  297. }
  298. void
  299. Track::sequence ( Audio_Sequence * t )
  300. {
  301. t->track( this );
  302. if ( sequence() )
  303. add( sequence() );
  304. _sequence = t;
  305. pack->insert( *t, 1 );
  306. t->labeltype( FL_NO_LABEL );
  307. resize();
  308. }
  309. void
  310. Track::add ( Control_Sequence *t )
  311. {
  312. DMESSAGE( "adding control sequence" );
  313. t->track( this );
  314. control->add( t );
  315. control_out.push_back( new Port( Port::Output, name(), control_out.size(), "cv" ) );
  316. t->output( control_out.back() );
  317. resize();
  318. }
  319. void
  320. Track::add ( Annotation_Sequence *t )
  321. {
  322. DMESSAGE( "adding annotation sequence" );
  323. t->track( this );
  324. annotation->add( t );
  325. resize();
  326. }
  327. /** add all widget on this track falling within the given rectangle to
  328. the selection. */
  329. void
  330. Track::select ( int X, int Y, int W, int H,
  331. bool include_control, bool merge_control )
  332. {
  333. Sequence *t = sequence();
  334. if ( ! ( t->y() > Y + H || t->y() + t->h() < Y ) )
  335. t->select_range( X, W );
  336. else
  337. include_control = true;
  338. if ( include_control )
  339. for ( int i = control->children(); i--; )
  340. {
  341. Control_Sequence *c = (Control_Sequence*)control->child( i );
  342. if ( merge_control ||
  343. ( c->y() >= Y && c->y() + c->h() <= Y + H ) )
  344. c->select_range( X, W );
  345. }
  346. }
  347. void
  348. Track::draw ( void )
  349. {
  350. if ( _selected )
  351. {
  352. Fl_Color c = color();
  353. color( FL_RED );
  354. Fl_Group::draw();
  355. color( c );
  356. }
  357. else
  358. Fl_Group::draw();
  359. }
  360. int
  361. Track::handle ( int m )
  362. {
  363. switch ( m )
  364. {
  365. case FL_MOUSEWHEEL:
  366. {
  367. Logger log( this );
  368. if ( ! Fl::event_shift() )
  369. return 0;
  370. int d = Fl::event_dy();
  371. if ( d < 0 )
  372. size( size() - 1 );
  373. else
  374. size( size() + 1 );
  375. return 1;
  376. }
  377. case FL_PUSH:
  378. {
  379. Logger log( this );
  380. int X = Fl::event_x();
  381. int Y = Fl::event_y();
  382. if ( Fl::event_button3() && X < Track::width() )
  383. {
  384. int c = output.size();
  385. /* context menu */
  386. Fl_Menu_Item menu[] =
  387. {
  388. { "Type", 0, 0, 0, FL_SUBMENU },
  389. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  390. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  391. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  392. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  393. { 0 },
  394. { "Add Control" },
  395. { "Add Annotation" },
  396. { "Color" },
  397. { "Remove", 0, 0, 0, transport->rolling ? FL_MENU_INACTIVE : 0 },
  398. { 0 },
  399. };
  400. const Fl_Menu_Item *r = menu->popup( X, Y, "Track" );
  401. if ( r && r > &menu[ 0 ] )
  402. {
  403. if ( r < &menu[ 4 ] )
  404. {
  405. int c = r - &menu[1];
  406. int ca[] = { 1, 2, 4 };
  407. configure_inputs( ca[ c ] );
  408. configure_outputs( ca[ c ] );
  409. }
  410. else
  411. {
  412. if ( r == &menu[ 4 ] )
  413. {
  414. const char *s = fl_input( "How many channels?", "3" );
  415. int c = atoi( s );
  416. if ( c <= 0 || c > 10 )
  417. fl_alert( "Invalid number of channels." );
  418. else
  419. {
  420. configure_inputs( c );
  421. configure_outputs( c );
  422. }
  423. }
  424. else if ( r == &menu[ 6 ] )
  425. {
  426. new Control_Sequence( this );
  427. }
  428. else if ( r == &menu[ 7 ] )
  429. {
  430. add( new Annotation_Sequence( this ) );
  431. }
  432. else if ( r == &menu[ 8 ] )
  433. {
  434. unsigned char r, g, b;
  435. Fl::get_color( color(), r, g, b );
  436. if ( fl_color_chooser( "Track Color", r, g, b ) )
  437. {
  438. color( fl_rgb_color( r, g, b ) );
  439. }
  440. // color( fl_show_colormap( color() ) );
  441. redraw();
  442. }
  443. else if ( r == &menu[ 9 ] )
  444. {
  445. int r = fl_choice( "Are you certain you want to remove track \"%s\"?", "Cancel", NULL, "Remove", name() );
  446. if ( r == 2 )
  447. {
  448. timeline->remove_track( this );
  449. /* FIXME: need to queue deletion. in a way that won't crash the playback. */
  450. // delete this;
  451. Fl::delete_widget( this );
  452. }
  453. }
  454. }
  455. }
  456. return 1;
  457. }
  458. }
  459. default:
  460. return Fl_Group::handle( m );
  461. }
  462. return 0;
  463. }
  464. /**********/
  465. /* Engine */
  466. /**********/
  467. void
  468. Track::update_port_names ( void )
  469. {
  470. for ( int i = 0; i < output.size(); ++i )
  471. output[ i ].name( name(), i );
  472. for ( int i = 0; i < input.size(); ++i )
  473. input[ i ].name( name(), i );
  474. for ( int i = 0; i < control_out.size(); ++i )
  475. control_out[ i ]->name( name(), i, "cv" );
  476. /* /\* tell any attached control sequences to do the same *\/ */
  477. /* for ( int i = control->children(); i-- ) */
  478. /* ((Control_Sequence*)control->child( i ))->update_port_names(); */
  479. }
  480. bool
  481. Track::configure_outputs ( int n )
  482. {
  483. int on = output.size();
  484. if ( n == on )
  485. return true;
  486. // engine->lock();
  487. if ( playback_ds )
  488. {
  489. Playback_DS *ds = playback_ds;
  490. playback_ds = NULL;
  491. ds->shutdown();
  492. delete ds;
  493. }
  494. if ( n > on )
  495. {
  496. for ( int i = on; i < n; ++i )
  497. {
  498. Port p( Port::Output, name(), i );
  499. if ( p.valid() )
  500. output.push_back( p );
  501. else
  502. WARNING( "could not create output port!" );
  503. }
  504. }
  505. else
  506. {
  507. for ( int i = on; i > n; --i )
  508. {
  509. output.back().shutdown();
  510. output.pop_back();
  511. }
  512. }
  513. if ( output.size() )
  514. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  515. // engine->unlock();
  516. /* FIXME: bogus */
  517. return true;
  518. }
  519. bool
  520. Track::configure_inputs ( int n )
  521. {
  522. int on = input.size();
  523. if ( n == on )
  524. return true;
  525. // engine->lock();
  526. if ( record_ds )
  527. {
  528. Record_DS *ds = record_ds;
  529. record_ds = NULL;
  530. ds->shutdown();
  531. delete ds;
  532. }
  533. if ( n > on )
  534. {
  535. for ( int i = on; i < n; ++i )
  536. {
  537. Port p( Port::Input, name(), i );
  538. if ( p.valid() )
  539. input.push_back( p );
  540. else
  541. WARNING( "could not create input port!" );
  542. }
  543. }
  544. else
  545. {
  546. for ( int i = on; i > n; --i )
  547. {
  548. input.back().shutdown();
  549. input.pop_back();
  550. }
  551. }
  552. if ( input.size() )
  553. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  554. // engine->unlock();
  555. /* FIXME: bogus */
  556. return true;
  557. }
  558. /* THREAD: RT */
  559. nframes_t
  560. Track::process ( nframes_t nframes )
  561. {
  562. if ( ! transport->rolling )
  563. {
  564. for ( int i = output.size(); i--; )
  565. output[ i ].silence( nframes );
  566. for ( int i = input.size(); i--; )
  567. input[ i ].silence( nframes );
  568. /* FIXME: is this really the right thing to do for control ports? */
  569. for ( int i = control_out.size(); i--; )
  570. control_out[ i ]->silence( nframes );
  571. return 0;
  572. }
  573. for ( int i = control->children(); i--; )
  574. ((Control_Sequence*)control->child( i ))->process( nframes );
  575. if ( playback_ds )
  576. {
  577. record_ds->process( nframes );
  578. return playback_ds->process( nframes );
  579. }
  580. else
  581. return 0;
  582. }
  583. /* THREAD: RT */
  584. void
  585. Track::seek ( nframes_t frame )
  586. {
  587. if ( playback_ds )
  588. return playback_ds->seek( frame );
  589. }
  590. /* FIXME: what about theading issues with this region/audiofile being
  591. accessible from the UI thread? Need locking? */
  592. #include "Audio_Region.H"
  593. #include <time.h>
  594. /** very cheap UUID generator... */
  595. unsigned long long
  596. uuid ( void )
  597. {
  598. time_t t = time( NULL );
  599. return (unsigned long long) t;
  600. }
  601. /* THREAD: IO */
  602. /** create capture region and prepare to record */
  603. void
  604. Track::record ( nframes_t frame )
  605. {
  606. assert( _capture == NULL );
  607. char pat[256];
  608. snprintf( pat, sizeof( pat ), "%s-%llu", name(), uuid() );
  609. _capture_af = Audio_File_SF::create( pat, engine->sample_rate(), input.size(), Track::capture_format );
  610. if ( ! _capture_af )
  611. {
  612. /* ERROR */
  613. }
  614. /* open it again for reading in the GUI thread */
  615. Audio_File *af = Audio_File::from_file( _capture_af->name() );
  616. _capture = new Audio_Region( af, sequence(), frame );
  617. _capture->prepare();
  618. }
  619. /* THREAD: IO */
  620. /** write a block to the (already opened) capture file */
  621. void
  622. Track::write ( sample_t *buf, nframes_t nframes )
  623. {
  624. nframes_t l = _capture_af->write( buf, nframes );
  625. _capture->write( l );
  626. }
  627. #include <stdio.h>
  628. /* THREAD: IO */
  629. void
  630. Track::stop ( nframes_t frame )
  631. {
  632. _capture->finalize( frame );
  633. _capture = NULL;
  634. }