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.

660 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. { 0 },
  333. };
  334. const Fl_Menu_Item *r = menu->popup( X, Y, "Track" );
  335. if ( r && r > &menu[ 0 ] )
  336. {
  337. if ( r < &menu[ 4 ] )
  338. {
  339. int c = r - &menu[1];
  340. int ca[] = { 1, 2, 4 };
  341. configure_inputs( ca[ c ] );
  342. configure_outputs( ca[ c ] );
  343. }
  344. else
  345. {
  346. if ( r == &menu[ 4 ] )
  347. {
  348. const char *s = fl_input( "How many channels?", "3" );
  349. int c = atoi( s );
  350. if ( c <= 0 || c > 10 )
  351. fl_alert( "Invalid number of channels." );
  352. else
  353. {
  354. configure_inputs( c );
  355. configure_outputs( c );
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. default:
  363. return Fl_Group::handle( m );
  364. }
  365. }
  366. /**********/
  367. /* Engine */
  368. /**********/
  369. const char *
  370. Track::name_for_port ( Port::type_e type, int n )
  371. {
  372. static char pname[256];
  373. snprintf( pname, sizeof( pname ), "%s/%s-%d",
  374. name(),
  375. type == Port::Output ? "out" : "in",
  376. n + 1 );
  377. return pname;
  378. }
  379. void
  380. Track::update_port_names ( void )
  381. {
  382. for ( int i = 0; i < output.size(); ++i )
  383. output[ i ].name( name_for_port( output[ i ].type(), i ) );
  384. for ( int i = 0; i < input.size(); ++i )
  385. input[ i ].name( name_for_port( input[ i ].type(), i ) );
  386. }
  387. bool
  388. Track::configure_outputs ( int n )
  389. {
  390. int on = output.size();
  391. if ( n == on )
  392. return true;
  393. // engine->lock();
  394. Playback_DS *ds = playback_ds;
  395. playback_ds = NULL;
  396. ds->shutdown();
  397. delete ds;
  398. if ( n > on )
  399. {
  400. for ( int i = on; i < n; ++i )
  401. {
  402. Port p( strdup( name_for_port( Port::Output, i ) ), Port::Output );
  403. if ( p.valid() )
  404. output.push_back( p );
  405. else
  406. printf( "error: could not create output port!\n" );
  407. }
  408. }
  409. else
  410. {
  411. for ( int i = on; i > n; --i )
  412. {
  413. output.back().shutdown();
  414. output.pop_back();
  415. }
  416. }
  417. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  418. // engine->unlock();
  419. /* FIXME: bogus */
  420. return true;
  421. }
  422. bool
  423. Track::configure_inputs ( int n )
  424. {
  425. int on = input.size();
  426. if ( n == on )
  427. return true;
  428. // engine->lock();
  429. Record_DS *ds = record_ds;
  430. record_ds = NULL;
  431. ds->shutdown();
  432. delete ds;
  433. if ( n > on )
  434. {
  435. for ( int i = on; i < n; ++i )
  436. {
  437. Port p( strdup( name_for_port( Port::Input, i ) ), Port::Input );
  438. if ( p.valid() )
  439. input.push_back( p );
  440. else
  441. printf( "error: could not create input port!\n" );
  442. }
  443. }
  444. else
  445. {
  446. for ( int i = on; i > n; --i )
  447. {
  448. input.back().shutdown();
  449. input.pop_back();
  450. }
  451. }
  452. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  453. // engine->unlock();
  454. /* FIXME: bogus */
  455. return true;
  456. }
  457. /* THREAD: RT */
  458. nframes_t
  459. Track::process ( nframes_t nframes )
  460. {
  461. if ( playback_ds )
  462. {
  463. record_ds->process( nframes );
  464. return playback_ds->process( nframes );
  465. }
  466. else
  467. return 0;
  468. }
  469. /* THREAD: RT */
  470. void
  471. Track::seek ( nframes_t frame )
  472. {
  473. if ( playback_ds )
  474. return playback_ds->seek( frame );
  475. }
  476. /* FIXME: what about theading issues with this region/audiofile being
  477. accessible from the UI thread? Need locking? */
  478. #include "Region.H"
  479. #include <time.h>
  480. /** very cheap UUID generator... */
  481. unsigned long long
  482. uuid ( void )
  483. {
  484. time_t t = time( NULL );
  485. return (unsigned long long) t;
  486. }
  487. /* THREAD: IO */
  488. /** create capture region and prepare to record */
  489. void
  490. Track::record ( nframes_t frame )
  491. {
  492. assert( _capture == NULL );
  493. char pat[256];
  494. snprintf( pat, sizeof( pat ), "%s-%llu.wav", name(), uuid() );
  495. /* FIXME: hack */
  496. Audio_File *af = Audio_File_SF::create( pat, 48000, input.size(), "Wav/24" );
  497. _capture = new Region( af, track(), frame );
  498. /* FIXME: wrong place for this */
  499. _capture->_r->end = 0;
  500. }
  501. /* THREAD: IO */
  502. /** write a block to the (already opened) capture file */
  503. void
  504. Track::write ( sample_t *buf, nframes_t nframes )
  505. {
  506. _capture->write( buf, nframes );
  507. }
  508. #include <stdio.h>
  509. /* THREAD: IO */
  510. void
  511. Track::stop ( nframes_t nframes )
  512. {
  513. _capture->finalize();
  514. _capture = NULL;
  515. }