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.

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