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.

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