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.

590 lines
13KB

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