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.

647 lines
14KB

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