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.

704 lines
16KB

  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. labeltype( FL_NO_LABEL );
  100. Fl_Group::size( timeline->w(), height() );
  101. Track *o = this;
  102. o->box( FL_THIN_UP_BOX );
  103. {
  104. Fl_Group *o = new Fl_Group( 2, 2, 149, 70 );
  105. o->color( ( Fl_Color ) 53 );
  106. {
  107. Fl_Input *o = name_field = new Fl_Sometimes_Input( 2, 2, 144, 24 );
  108. o->color( ( Fl_Color ) 33 );
  109. o->labeltype( FL_NO_LABEL );
  110. o->labelcolor( FL_GRAY0 );
  111. o->textcolor( 32 );
  112. o->callback( cb_input_field, (void*)this );
  113. }
  114. {
  115. Fl_Group *o = controls = new Fl_Group( 2, 28, 149, 24 );
  116. {
  117. Fl_Button *o = record_button =
  118. new Fl_Button( 6, 28, 26, 24, "@circle" );
  119. o->type( 1 );
  120. o->box( FL_THIN_UP_BOX );
  121. o->color( FL_LIGHT1 );
  122. o->selection_color( FL_RED );
  123. o->labelsize( 8 );
  124. o->callback( cb_button, this );
  125. }
  126. {
  127. Fl_Button *o = mute_button =
  128. new Fl_Button( 35, 28, 26, 24, "m" );
  129. o->type( 1 );
  130. o->box( FL_THIN_UP_BOX );
  131. o->color( FL_LIGHT1 );
  132. o->labelsize( 11 );
  133. o->callback( cb_button, this );
  134. }
  135. {
  136. Fl_Button *o = solo_button =
  137. new Fl_Button( 66, 28, 26, 24, "s" );
  138. o->type( 1 );
  139. o->box( FL_THIN_UP_BOX );
  140. o->color( FL_LIGHT1 );
  141. o->labelsize( 11 );
  142. o->callback( cb_button, this );
  143. }
  144. {
  145. Fl_Menu_Button *o = take_menu =
  146. new Fl_Menu_Button( 97, 28, 47, 24, "T" );
  147. o->box( FL_THIN_UP_BOX );
  148. o->color( FL_LIGHT1 );
  149. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  150. o->callback( cb_button, this );
  151. o->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  152. o->add( "New", 0, 0, 0, FL_MENU_DIVIDER );
  153. }
  154. o->end();
  155. }
  156. {
  157. Fl_Box *o = new Fl_Box( 0, 76, 149, 38 );
  158. o->box( FL_FLAT_BOX );
  159. Fl_Group::current()->resizable( o );
  160. }
  161. o->size( Track::width(), h() );
  162. o->end();
  163. }
  164. {
  165. Fl_Pack *o = pack = new Fl_Pack( width(), 0, 1006, 115 );
  166. o->labeltype( FL_NO_LABEL );
  167. o->resize( x() + width(), y(), w() - width(), h() );
  168. resizable( o );
  169. {
  170. Fl_Pack *o = control = new Fl_Pack( width(), 0, pack->w(), 115 );
  171. o->end();
  172. }
  173. {
  174. Fl_Pack *o = takes = new Fl_Pack( width(), 0, pack->w(), 115 );
  175. o->end();
  176. o->hide();
  177. }
  178. o->end();
  179. }
  180. end();
  181. /* /\* FIXME: should be configurable, but where? *\/ */
  182. /* create_outputs( 2 ); */
  183. /* create_inputs( 2 ); */
  184. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  185. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  186. }
  187. Track::Track ( const char *L, int channels ) :
  188. Fl_Group ( 0, 0, 0, 0, 0 )
  189. {
  190. init();
  191. if ( L )
  192. name( L );
  193. configure_inputs( channels );
  194. configure_outputs( channels );
  195. log_create();
  196. }
  197. Track::~Track ( )
  198. {
  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. if ( parent() )
  234. parent()->redraw();
  235. }
  236. void
  237. Track::size ( int v )
  238. {
  239. if ( v < 0 || v > 3 )
  240. return;
  241. _size = v;
  242. resize();
  243. }
  244. void
  245. Track::add ( Sequence * t )
  246. {
  247. takes->insert( *t, 0 );
  248. if ( ! t->name() )
  249. {
  250. char pat[20];
  251. snprintf( pat, sizeof( pat ), "%d", takes->children() );
  252. t->name( strdup( pat ) );
  253. }
  254. take_menu->add( t->name() );
  255. t->labeltype( FL_ENGRAVED_LABEL );
  256. }
  257. void
  258. Track::remove ( Sequence *t )
  259. {
  260. takes->remove( t );
  261. resize();
  262. // take_menu->remove( t->name() );
  263. }
  264. void
  265. Track::remove ( Control_Sequence *t )
  266. {
  267. control->remove( t );
  268. resize();
  269. }
  270. void
  271. Track::track ( Sequence * t )
  272. {
  273. t->track( this );
  274. if ( track() )
  275. add( track() );
  276. _track = t;
  277. pack->insert( *t, 0 );
  278. t->labeltype( FL_NO_LABEL );
  279. resize();
  280. }
  281. void
  282. Track::add ( Control_Sequence *t )
  283. {
  284. printf( "adding control sequence\n" );
  285. t->track( this );
  286. control->add( t );
  287. resize();
  288. }
  289. void
  290. Track::draw ( void )
  291. {
  292. if ( _selected )
  293. {
  294. Fl_Color c = color();
  295. color( FL_RED );
  296. Fl_Group::draw();
  297. color( c );
  298. }
  299. else
  300. Fl_Group::draw();
  301. }
  302. int
  303. Track::handle ( int m )
  304. {
  305. Logger log( this );
  306. switch ( m )
  307. {
  308. case FL_MOUSEWHEEL:
  309. {
  310. if ( ! Fl::event_shift() )
  311. return 0;
  312. int d = Fl::event_dy();
  313. printf( "%d\n", d );
  314. if ( d < 0 )
  315. size( size() - 1 );
  316. else
  317. size( size() + 1 );
  318. return 1;
  319. }
  320. case FL_PUSH:
  321. {
  322. int X = Fl::event_x();
  323. int Y = Fl::event_y();
  324. if ( Fl::event_button3() && X < Track::width() )
  325. {
  326. int c = output.size();
  327. /* context menu */
  328. Fl_Menu_Item menu[] =
  329. {
  330. { "Type", 0, 0, 0, FL_SUBMENU },
  331. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  332. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  333. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  334. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  335. { 0 },
  336. { "Color" },
  337. { "Remove", 0, 0, 0, transport->rolling ? FL_MENU_INACTIVE : 0 },
  338. { 0 },
  339. };
  340. const Fl_Menu_Item *r = menu->popup( X, Y, "Track" );
  341. if ( r && r > &menu[ 0 ] )
  342. {
  343. if ( r < &menu[ 4 ] )
  344. {
  345. int c = r - &menu[1];
  346. int ca[] = { 1, 2, 4 };
  347. configure_inputs( ca[ c ] );
  348. configure_outputs( ca[ c ] );
  349. }
  350. else
  351. {
  352. if ( r == &menu[ 4 ] )
  353. {
  354. const char *s = fl_input( "How many channels?", "3" );
  355. int c = atoi( s );
  356. if ( c <= 0 || c > 10 )
  357. fl_alert( "Invalid number of channels." );
  358. else
  359. {
  360. configure_inputs( c );
  361. configure_outputs( c );
  362. }
  363. }
  364. else if ( r == &menu[ 6 ] )
  365. {
  366. unsigned char r, g, b;
  367. Fl::get_color( color(), r, g, b );
  368. if ( fl_color_chooser( "Track Color", r, g, b ) )
  369. {
  370. color( fl_rgb_color( r, g, b ) );
  371. }
  372. // color( fl_show_colormap( color() ) );
  373. redraw();
  374. }
  375. else if ( r == &menu[ 7 ] )
  376. {
  377. int r = fl_choice( "Are you certain you want to remove track \"%s\"?", "Cancel", NULL, "Remove", name() );
  378. if ( r == 2 )
  379. {
  380. timeline->remove_track( this );
  381. /* FIXME: need to queue deletion. in a way that won't crash the playback. */
  382. // delete this;
  383. Fl::delete_widget( this );
  384. }
  385. }
  386. }
  387. }
  388. return 1;
  389. }
  390. }
  391. default:
  392. return Fl_Group::handle( m );
  393. }
  394. return 0;
  395. }
  396. /**********/
  397. /* Engine */
  398. /**********/
  399. const char *
  400. Track::name_for_port ( Port::type_e type, int n )
  401. {
  402. static char pname[256];
  403. snprintf( pname, sizeof( pname ), "%s/%s-%d",
  404. name(),
  405. type == Port::Output ? "out" : "in",
  406. n + 1 );
  407. return pname;
  408. }
  409. void
  410. Track::update_port_names ( void )
  411. {
  412. for ( int i = 0; i < output.size(); ++i )
  413. output[ i ].name( name_for_port( output[ i ].type(), i ) );
  414. for ( int i = 0; i < input.size(); ++i )
  415. input[ i ].name( name_for_port( input[ i ].type(), i ) );
  416. }
  417. bool
  418. Track::configure_outputs ( int n )
  419. {
  420. int on = output.size();
  421. if ( n == on )
  422. return true;
  423. // engine->lock();
  424. Playback_DS *ds = playback_ds;
  425. playback_ds = NULL;
  426. ds->shutdown();
  427. delete ds;
  428. if ( n > on )
  429. {
  430. for ( int i = on; i < n; ++i )
  431. {
  432. Port p( strdup( name_for_port( Port::Output, i ) ), Port::Output );
  433. if ( p.valid() )
  434. output.push_back( p );
  435. else
  436. printf( "error: could not create output port!\n" );
  437. }
  438. }
  439. else
  440. {
  441. for ( int i = on; i > n; --i )
  442. {
  443. output.back().shutdown();
  444. output.pop_back();
  445. }
  446. }
  447. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  448. // engine->unlock();
  449. /* FIXME: bogus */
  450. return true;
  451. }
  452. bool
  453. Track::configure_inputs ( int n )
  454. {
  455. int on = input.size();
  456. if ( n == on )
  457. return true;
  458. // engine->lock();
  459. Record_DS *ds = record_ds;
  460. record_ds = NULL;
  461. ds->shutdown();
  462. delete ds;
  463. if ( n > on )
  464. {
  465. for ( int i = on; i < n; ++i )
  466. {
  467. Port p( strdup( name_for_port( Port::Input, i ) ), Port::Input );
  468. if ( p.valid() )
  469. input.push_back( p );
  470. else
  471. printf( "error: could not create input port!\n" );
  472. }
  473. }
  474. else
  475. {
  476. for ( int i = on; i > n; --i )
  477. {
  478. input.back().shutdown();
  479. input.pop_back();
  480. }
  481. }
  482. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  483. // engine->unlock();
  484. /* FIXME: bogus */
  485. return true;
  486. }
  487. /* THREAD: RT */
  488. nframes_t
  489. Track::process ( nframes_t nframes )
  490. {
  491. if ( playback_ds )
  492. {
  493. record_ds->process( nframes );
  494. return playback_ds->process( nframes );
  495. }
  496. else
  497. return 0;
  498. }
  499. /* THREAD: RT */
  500. void
  501. Track::seek ( nframes_t frame )
  502. {
  503. if ( playback_ds )
  504. return playback_ds->seek( frame );
  505. }
  506. /* FIXME: what about theading issues with this region/audiofile being
  507. accessible from the UI thread? Need locking? */
  508. #include "Region.H"
  509. #include <time.h>
  510. /** very cheap UUID generator... */
  511. unsigned long long
  512. uuid ( void )
  513. {
  514. time_t t = time( NULL );
  515. return (unsigned long long) t;
  516. }
  517. /* THREAD: IO */
  518. /** create capture region and prepare to record */
  519. void
  520. Track::record ( nframes_t frame )
  521. {
  522. assert( _capture == NULL );
  523. char pat[256];
  524. snprintf( pat, sizeof( pat ), "%s-%llu", name(), uuid() );
  525. _capture_af = Audio_File_SF::create( pat, 48000, input.size(), Track::capture_format );
  526. if ( ! _capture_af )
  527. {
  528. /* ERROR */
  529. }
  530. /* open it again for reading in the GUI thread */
  531. Audio_File *af = Audio_File::from_file( _capture_af->name() );
  532. _capture = new Region( af, track(), frame );
  533. _capture->prepare();
  534. }
  535. /* THREAD: IO */
  536. /** write a block to the (already opened) capture file */
  537. void
  538. Track::write ( sample_t *buf, nframes_t nframes )
  539. {
  540. nframes_t l = _capture_af->write( buf, nframes );
  541. _capture->write( l );
  542. }
  543. #include <stdio.h>
  544. /* THREAD: IO */
  545. void
  546. Track::stop ( nframes_t nframes )
  547. {
  548. _capture->finalize();
  549. _capture = NULL;
  550. }