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.

670 lines
15KB

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