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.

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