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.

793 lines
18KB

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