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.

810 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. track( (Audio_Sequence*)track()->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. track( t );
  86. redraw();
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. void
  93. Track::init ( void )
  94. {
  95. _capture = NULL;
  96. _track = 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. control_out.pop_back();
  214. }
  215. log_destroy();
  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 ( track() )
  252. track()->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. takes->remove( t );
  286. resize();
  287. // take_menu->remove( t->name() );
  288. }
  289. void
  290. Track::remove ( Control_Sequence *t )
  291. {
  292. control->remove( t );
  293. resize();
  294. }
  295. void
  296. Track::track ( Audio_Sequence * t )
  297. {
  298. t->track( this );
  299. if ( track() )
  300. add( track() );
  301. _track = t;
  302. pack->insert( *t, 1 );
  303. t->labeltype( FL_NO_LABEL );
  304. resize();
  305. }
  306. void
  307. Track::add ( Control_Sequence *t )
  308. {
  309. DMESSAGE( "adding control sequence" );
  310. t->track( this );
  311. control->add( t );
  312. control_out.push_back( new Port( Port::Output, name(), control_out.size(), "cv" ) );
  313. t->output( control_out.back() );
  314. resize();
  315. }
  316. void
  317. Track::add ( Annotation_Sequence *t )
  318. {
  319. DMESSAGE( "adding annotation sequence" );
  320. t->track( this );
  321. annotation->add( t );
  322. resize();
  323. }
  324. /** add all widget on this track falling within the given rectangle to
  325. the selection. */
  326. void
  327. Track::select ( int X, int Y, int W, int H,
  328. bool include_control, bool merge_control )
  329. {
  330. Sequence *t = track();
  331. if ( ! ( t->y() > Y + H || t->y() + t->h() < Y ) )
  332. t->select_range( X, W );
  333. else
  334. include_control = true;
  335. if ( include_control )
  336. for ( int i = control->children(); i--; )
  337. {
  338. Control_Sequence *c = (Control_Sequence*)control->child( i );
  339. if ( merge_control ||
  340. ( c->y() >= Y && c->y() + c->h() <= Y + H ) )
  341. c->select_range( X, W );
  342. }
  343. }
  344. void
  345. Track::draw ( void )
  346. {
  347. if ( _selected )
  348. {
  349. Fl_Color c = color();
  350. color( FL_RED );
  351. Fl_Group::draw();
  352. color( c );
  353. }
  354. else
  355. Fl_Group::draw();
  356. }
  357. int
  358. Track::handle ( int m )
  359. {
  360. switch ( m )
  361. {
  362. case FL_MOUSEWHEEL:
  363. {
  364. Logger log( this );
  365. if ( ! Fl::event_shift() )
  366. return 0;
  367. int d = Fl::event_dy();
  368. if ( d < 0 )
  369. size( size() - 1 );
  370. else
  371. size( size() + 1 );
  372. return 1;
  373. }
  374. case FL_PUSH:
  375. {
  376. Logger log( this );
  377. int X = Fl::event_x();
  378. int Y = Fl::event_y();
  379. if ( Fl::event_button3() && X < Track::width() )
  380. {
  381. int c = output.size();
  382. /* context menu */
  383. Fl_Menu_Item menu[] =
  384. {
  385. { "Type", 0, 0, 0, FL_SUBMENU },
  386. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  387. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  388. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  389. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  390. { 0 },
  391. { "Add Control" },
  392. { "Add Annotation" },
  393. { "Color" },
  394. { "Remove", 0, 0, 0, transport->rolling ? FL_MENU_INACTIVE : 0 },
  395. { 0 },
  396. };
  397. const Fl_Menu_Item *r = menu->popup( X, Y, "Track" );
  398. if ( r && r > &menu[ 0 ] )
  399. {
  400. if ( r < &menu[ 4 ] )
  401. {
  402. int c = r - &menu[1];
  403. int ca[] = { 1, 2, 4 };
  404. configure_inputs( ca[ c ] );
  405. configure_outputs( ca[ c ] );
  406. }
  407. else
  408. {
  409. if ( r == &menu[ 4 ] )
  410. {
  411. const char *s = fl_input( "How many channels?", "3" );
  412. int c = atoi( s );
  413. if ( c <= 0 || c > 10 )
  414. fl_alert( "Invalid number of channels." );
  415. else
  416. {
  417. configure_inputs( c );
  418. configure_outputs( c );
  419. }
  420. }
  421. else if ( r == &menu[ 6 ] )
  422. {
  423. new Control_Sequence( this );
  424. }
  425. else if ( r == &menu[ 7 ] )
  426. {
  427. add( new Annotation_Sequence( this ) );
  428. }
  429. else if ( r == &menu[ 8 ] )
  430. {
  431. unsigned char r, g, b;
  432. Fl::get_color( color(), r, g, b );
  433. if ( fl_color_chooser( "Track Color", r, g, b ) )
  434. {
  435. color( fl_rgb_color( r, g, b ) );
  436. }
  437. // color( fl_show_colormap( color() ) );
  438. redraw();
  439. }
  440. else if ( r == &menu[ 9 ] )
  441. {
  442. int r = fl_choice( "Are you certain you want to remove track \"%s\"?", "Cancel", NULL, "Remove", name() );
  443. if ( r == 2 )
  444. {
  445. timeline->remove_track( this );
  446. /* FIXME: need to queue deletion. in a way that won't crash the playback. */
  447. // delete this;
  448. Fl::delete_widget( this );
  449. }
  450. }
  451. }
  452. }
  453. return 1;
  454. }
  455. }
  456. default:
  457. return Fl_Group::handle( m );
  458. }
  459. return 0;
  460. }
  461. /**********/
  462. /* Engine */
  463. /**********/
  464. void
  465. Track::update_port_names ( void )
  466. {
  467. for ( int i = 0; i < output.size(); ++i )
  468. output[ i ].name( name(), i );
  469. for ( int i = 0; i < input.size(); ++i )
  470. input[ i ].name( name(), i );
  471. for ( int i = 0; i < control_out.size(); ++i )
  472. control_out[ i ]->name( name(), i, "cv" );
  473. /* /\* tell any attached control sequences to do the same *\/ */
  474. /* for ( int i = control->children(); i-- ) */
  475. /* ((Control_Sequence*)control->child( i ))->update_port_names(); */
  476. }
  477. bool
  478. Track::configure_outputs ( int n )
  479. {
  480. int on = output.size();
  481. if ( n == on )
  482. return true;
  483. // engine->lock();
  484. if ( playback_ds )
  485. {
  486. Playback_DS *ds = playback_ds;
  487. playback_ds = NULL;
  488. ds->shutdown();
  489. delete ds;
  490. }
  491. if ( n > on )
  492. {
  493. for ( int i = on; i < n; ++i )
  494. {
  495. Port p( Port::Output, name(), i );
  496. if ( p.valid() )
  497. output.push_back( p );
  498. else
  499. WARNING( "could not create output port!" );
  500. }
  501. }
  502. else
  503. {
  504. for ( int i = on; i > n; --i )
  505. {
  506. output.back().shutdown();
  507. output.pop_back();
  508. }
  509. }
  510. if ( output.size() )
  511. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  512. // engine->unlock();
  513. /* FIXME: bogus */
  514. return true;
  515. }
  516. bool
  517. Track::configure_inputs ( int n )
  518. {
  519. int on = input.size();
  520. if ( n == on )
  521. return true;
  522. // engine->lock();
  523. if ( record_ds )
  524. {
  525. Record_DS *ds = record_ds;
  526. record_ds = NULL;
  527. ds->shutdown();
  528. delete ds;
  529. }
  530. if ( n > on )
  531. {
  532. for ( int i = on; i < n; ++i )
  533. {
  534. Port p( Port::Input, name(), i );
  535. if ( p.valid() )
  536. input.push_back( p );
  537. else
  538. WARNING( "could not create input port!" );
  539. }
  540. }
  541. else
  542. {
  543. for ( int i = on; i > n; --i )
  544. {
  545. input.back().shutdown();
  546. input.pop_back();
  547. }
  548. }
  549. if ( input.size() )
  550. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  551. // engine->unlock();
  552. /* FIXME: bogus */
  553. return true;
  554. }
  555. /* THREAD: RT */
  556. nframes_t
  557. Track::process ( nframes_t nframes )
  558. {
  559. if ( ! transport->rolling )
  560. {
  561. for ( int i = output.size(); i--; )
  562. output[ i ].silence( nframes );
  563. for ( int i = input.size(); i--; )
  564. input[ i ].silence( nframes );
  565. /* FIXME: is this really the right thing to do for control ports? */
  566. for ( int i = control_out.size(); i--; )
  567. control_out[ i ]->silence( nframes );
  568. return 0;
  569. }
  570. for ( int i = control->children(); i--; )
  571. ((Control_Sequence*)control->child( i ))->process( nframes );
  572. if ( playback_ds )
  573. {
  574. record_ds->process( nframes );
  575. return playback_ds->process( nframes );
  576. }
  577. else
  578. return 0;
  579. }
  580. /* THREAD: RT */
  581. void
  582. Track::seek ( nframes_t frame )
  583. {
  584. if ( playback_ds )
  585. return playback_ds->seek( frame );
  586. }
  587. /* FIXME: what about theading issues with this region/audiofile being
  588. accessible from the UI thread? Need locking? */
  589. #include "Audio_Region.H"
  590. #include <time.h>
  591. /** very cheap UUID generator... */
  592. unsigned long long
  593. uuid ( void )
  594. {
  595. time_t t = time( NULL );
  596. return (unsigned long long) t;
  597. }
  598. /* THREAD: IO */
  599. /** create capture region and prepare to record */
  600. void
  601. Track::record ( nframes_t frame )
  602. {
  603. assert( _capture == NULL );
  604. char pat[256];
  605. snprintf( pat, sizeof( pat ), "%s-%llu", name(), uuid() );
  606. _capture_af = Audio_File_SF::create( pat, engine->sample_rate(), input.size(), Track::capture_format );
  607. if ( ! _capture_af )
  608. {
  609. /* ERROR */
  610. }
  611. /* open it again for reading in the GUI thread */
  612. Audio_File *af = Audio_File::from_file( _capture_af->name() );
  613. _capture = new Audio_Region( af, track(), frame );
  614. _capture->prepare();
  615. }
  616. /* THREAD: IO */
  617. /** write a block to the (already opened) capture file */
  618. void
  619. Track::write ( sample_t *buf, nframes_t nframes )
  620. {
  621. nframes_t l = _capture_af->write( buf, nframes );
  622. _capture->write( l );
  623. }
  624. #include <stdio.h>
  625. /* THREAD: IO */
  626. void
  627. Track::stop ( nframes_t frame )
  628. {
  629. _capture->finalize( frame );
  630. _capture = NULL;
  631. }