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.

651 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. parent()->redraw();
  225. }
  226. void
  227. Track::size ( int v )
  228. {
  229. if ( v < 0 || v > 3 )
  230. return;
  231. _size = v;
  232. resize();
  233. }
  234. void
  235. Track::add ( Sequence * t )
  236. {
  237. takes->insert( *t, 0 );
  238. if ( ! t->name() )
  239. {
  240. char pat[20];
  241. snprintf( pat, sizeof( pat ), "%d", takes->children() );
  242. t->name( strdup( pat ) );
  243. }
  244. take_menu->add( t->name() );
  245. t->labeltype( FL_ENGRAVED_LABEL );
  246. }
  247. void
  248. Track::remove ( Sequence *t )
  249. {
  250. takes->remove( t );
  251. resize();
  252. // take_menu->remove( t->name() );
  253. }
  254. void
  255. Track::remove ( Control_Sequence *t )
  256. {
  257. control->remove( t );
  258. resize();
  259. }
  260. void
  261. Track::track ( Sequence * t )
  262. {
  263. t->track( this );
  264. if ( track() )
  265. add( track() );
  266. _track = t;
  267. pack->insert( *t, 0 );
  268. t->labeltype( FL_NO_LABEL );
  269. resize();
  270. }
  271. void
  272. Track::add ( Control_Sequence *t )
  273. {
  274. printf( "adding control sequence\n" );
  275. t->track( this );
  276. control->add( t );
  277. resize();
  278. }
  279. void
  280. Track::draw ( void )
  281. {
  282. if ( _selected )
  283. {
  284. Fl_Color c = color();
  285. color( FL_RED );
  286. Fl_Group::draw();
  287. color( c );
  288. }
  289. else
  290. Fl_Group::draw();
  291. }
  292. int
  293. Track::handle ( int m )
  294. {
  295. Logger log( this );
  296. switch ( m )
  297. {
  298. case FL_MOUSEWHEEL:
  299. {
  300. if ( ! Fl::event_shift() )
  301. return 0;
  302. int d = Fl::event_dy();
  303. printf( "%d\n", d );
  304. if ( d < 0 )
  305. size( size() - 1 );
  306. else
  307. size( size() + 1 );
  308. return 1;
  309. }
  310. case FL_PUSH:
  311. {
  312. int X = Fl::event_x();
  313. int Y = Fl::event_y();
  314. if ( Fl::event_button3() && X < Track::width() )
  315. {
  316. int c = output.size();
  317. /* context menu */
  318. Fl_Menu_Item menu[] =
  319. {
  320. { "Type", 0, 0, 0, FL_SUBMENU },
  321. { "Mono", 0, 0, 0, FL_MENU_RADIO | ( c == 1 ? FL_MENU_VALUE : 0 ) },
  322. { "Stereo", 0, 0, 0, FL_MENU_RADIO | ( c == 2 ? FL_MENU_VALUE : 0 ) },
  323. { "Quad", 0, 0, 0, FL_MENU_RADIO | ( c == 4 ? FL_MENU_VALUE : 0 ) },
  324. { "...", 0, 0, 0, FL_MENU_RADIO | ( c == 3 || c > 4 ? FL_MENU_VALUE : 0 ) },
  325. { 0 },
  326. { 0 },
  327. };
  328. const Fl_Menu_Item *r = menu->popup( X, Y, "Track" );
  329. if ( r && r > &menu[ 0 ] )
  330. {
  331. if ( r < &menu[ 4 ] )
  332. {
  333. int c = r - &menu[1];
  334. int ca[] = { 1, 2, 4 };
  335. configure_inputs( ca[ c ] );
  336. configure_outputs( ca[ c ] );
  337. }
  338. else
  339. {
  340. if ( r == &menu[ 4 ] )
  341. {
  342. const char *s = fl_input( "How many channels?", "3" );
  343. int c = atoi( s );
  344. if ( c <= 0 || c > 10 )
  345. fl_alert( "Invalid number of channels." );
  346. else
  347. {
  348. configure_inputs( c );
  349. configure_outputs( c );
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. default:
  357. return Fl_Group::handle( m );
  358. }
  359. }
  360. /**********/
  361. /* Engine */
  362. /**********/
  363. const char *
  364. Track::name_for_port ( Port::type_e type, int n )
  365. {
  366. static char pname[256];
  367. snprintf( pname, sizeof( pname ), "%s/%s-%d",
  368. name(),
  369. type == Port::Output ? "out" : "in",
  370. n + 1 );
  371. return pname;
  372. }
  373. void
  374. Track::update_port_names ( void )
  375. {
  376. for ( int i = 0; i < output.size(); ++i )
  377. output[ i ].name( name_for_port( output[ i ].type(), i ) );
  378. for ( int i = 0; i < input.size(); ++i )
  379. input[ i ].name( name_for_port( input[ i ].type(), i ) );
  380. }
  381. bool
  382. Track::configure_outputs ( int n )
  383. {
  384. int on = output.size();
  385. if ( n == on )
  386. return true;
  387. // engine->lock();
  388. Playback_DS *ds = playback_ds;
  389. playback_ds = NULL;
  390. ds->shutdown();
  391. delete ds;
  392. if ( n > on )
  393. {
  394. for ( int i = on; i < n; ++i )
  395. {
  396. Port p( strdup( name_for_port( Port::Output, i ) ), Port::Output );
  397. if ( p.valid() )
  398. output.push_back( p );
  399. else
  400. printf( "error: could not create output port!\n" );
  401. }
  402. }
  403. else
  404. {
  405. for ( int i = on; i > n; --i )
  406. {
  407. output.back().shutdown();
  408. output.pop_back();
  409. }
  410. }
  411. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  412. // engine->unlock();
  413. /* FIXME: bogus */
  414. return true;
  415. }
  416. bool
  417. Track::configure_inputs ( int n )
  418. {
  419. int on = input.size();
  420. if ( n == on )
  421. return true;
  422. // engine->lock();
  423. Record_DS *ds = record_ds;
  424. record_ds = NULL;
  425. ds->shutdown();
  426. delete ds;
  427. if ( n > on )
  428. {
  429. for ( int i = on; i < n; ++i )
  430. {
  431. Port p( strdup( name_for_port( Port::Input, i ) ), Port::Input );
  432. if ( p.valid() )
  433. input.push_back( p );
  434. else
  435. printf( "error: could not create input port!\n" );
  436. }
  437. }
  438. else
  439. {
  440. for ( int i = on; i > n; --i )
  441. {
  442. input.back().shutdown();
  443. input.pop_back();
  444. }
  445. }
  446. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  447. // engine->unlock();
  448. /* FIXME: bogus */
  449. return true;
  450. }
  451. /* THREAD: RT */
  452. nframes_t
  453. Track::process ( nframes_t nframes )
  454. {
  455. if ( playback_ds )
  456. {
  457. record_ds->process( nframes );
  458. return playback_ds->process( nframes );
  459. }
  460. else
  461. return 0;
  462. }
  463. /* THREAD: RT */
  464. void
  465. Track::seek ( nframes_t frame )
  466. {
  467. if ( playback_ds )
  468. return playback_ds->seek( frame );
  469. }
  470. /* FIXME: what about theading issues with this region/audiofile being
  471. accessible from the UI thread? Need locking? */
  472. #include "Region.H"
  473. #include <time.h>
  474. /** very cheap UUID generator... */
  475. unsigned long long
  476. uuid ( void )
  477. {
  478. time_t t = time( NULL );
  479. return (unsigned long long) t;
  480. }
  481. /* THREAD: IO */
  482. /** create capture region and prepare to record */
  483. void
  484. Track::record ( nframes_t frame )
  485. {
  486. assert( _capture == NULL );
  487. char pat[256];
  488. snprintf( pat, sizeof( pat ), "%s-%llu.wav", name(), uuid() );
  489. /* FIXME: hack */
  490. Audio_File *af = Audio_File_SF::create( pat, 48000, input.size(), "Wav/24" );
  491. _capture = new Region( af, track(), frame );
  492. /* FIXME: wrong place for this */
  493. _capture->_r->end = 0;
  494. }
  495. /* THREAD: IO */
  496. /** write a block to the (already opened) capture file */
  497. void
  498. Track::write ( sample_t *buf, nframes_t nframes )
  499. {
  500. _capture->write( buf, nframes );
  501. }
  502. #include <stdio.h>
  503. /* THREAD: IO */
  504. void
  505. Track::stop ( nframes_t nframes )
  506. {
  507. _capture->finalize();
  508. _capture = NULL;
  509. }