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.

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