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.

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