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.

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