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.

521 lines
11KB

  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. if ( _name )
  40. free( _name );
  41. _name = strdup( name_field->value() );
  42. log_end();
  43. }
  44. void
  45. Track::cb_button ( Fl_Widget *w )
  46. {
  47. printf( "FIXME: inform mixer here\n" );
  48. if ( w == record_button )
  49. {
  50. /* FIXME: wrong place for this! */
  51. if ( record_button->value() )
  52. record_ds->start( transport.frame );
  53. else
  54. record_ds->stop( transport.frame );
  55. }
  56. else
  57. if ( w == take_menu )
  58. {
  59. int v = take_menu->value();
  60. switch ( v )
  61. {
  62. case 0: /* show all takes */
  63. show_all_takes( take_menu->menu()[ v ].value() );
  64. return;
  65. case 1: /* new */
  66. track( track()->clone_empty() );
  67. return;
  68. }
  69. const char *s = take_menu->menu()[ v ].text;
  70. for ( int i = takes->children(); i--; )
  71. {
  72. Sequence *t = (Sequence*)takes->child( i );
  73. if ( ! strcmp( s, t->name() ) )
  74. {
  75. track( t );
  76. redraw();
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. void
  83. Track::init ( void )
  84. {
  85. _capture = NULL;
  86. _track = NULL;
  87. _name = NULL;
  88. _selected = false;
  89. _show_all_takes = false;
  90. _size = 1;
  91. labeltype( FL_NO_LABEL );
  92. Fl_Group::size( timeline->w(), height() );
  93. Track *o = this;
  94. o->box( FL_THIN_UP_BOX );
  95. {
  96. Fl_Group *o = new Fl_Group( 2, 2, 149, 70 );
  97. o->color( ( Fl_Color ) 53 );
  98. {
  99. Fl_Input *o = name_field = new Fl_Sometimes_Input( 2, 2, 144, 24 );
  100. o->color( ( Fl_Color ) 33 );
  101. o->labeltype( FL_NO_LABEL );
  102. o->labelcolor( FL_GRAY0 );
  103. o->textcolor( 32 );
  104. o->callback( cb_input_field, (void*)this );
  105. // o->hide();
  106. }
  107. {
  108. Fl_Group *o = controls = new Fl_Group( 2, 28, 149, 24 );
  109. {
  110. Fl_Button *o = record_button =
  111. new Fl_Button( 6, 28, 26, 24, "@circle" );
  112. o->type( 1 );
  113. o->box( FL_THIN_UP_BOX );
  114. o->color( FL_LIGHT1 );
  115. o->selection_color( FL_RED );
  116. o->labelsize( 8 );
  117. o->callback( cb_button, this );
  118. }
  119. {
  120. Fl_Button *o = mute_button =
  121. new Fl_Button( 35, 28, 26, 24, "m" );
  122. o->type( 1 );
  123. o->box( FL_THIN_UP_BOX );
  124. o->color( FL_LIGHT1 );
  125. o->labelsize( 11 );
  126. o->callback( cb_button, this );
  127. }
  128. {
  129. Fl_Button *o = solo_button =
  130. new Fl_Button( 66, 28, 26, 24, "s" );
  131. o->type( 1 );
  132. o->box( FL_THIN_UP_BOX );
  133. o->color( FL_LIGHT1 );
  134. o->labelsize( 11 );
  135. o->callback( cb_button, this );
  136. }
  137. {
  138. Fl_Menu_Button *o = take_menu =
  139. new Fl_Menu_Button( 97, 28, 47, 24, "T" );
  140. o->box( FL_THIN_UP_BOX );
  141. o->color( FL_LIGHT1 );
  142. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  143. o->callback( cb_button, this );
  144. o->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  145. o->add( "New", 0, 0, 0, FL_MENU_DIVIDER );
  146. }
  147. o->end();
  148. }
  149. {
  150. Fl_Box *o = new Fl_Box( 0, 76, 149, 38 );
  151. o->box( FL_FLAT_BOX );
  152. Fl_Group::current()->resizable( o );
  153. }
  154. o->size( Track::width(), h() );
  155. o->end();
  156. }
  157. {
  158. Fl_Pack *o = pack = new Fl_Pack( width(), 0, 1006, 115 );
  159. o->labeltype( FL_NO_LABEL );
  160. o->resize( x() + width(), y(), w() - width(), h() );
  161. Fl_Group::current()->resizable( o );
  162. {
  163. Fl_Pack *o = control = new Fl_Pack( width(), 0, pack->w(), 115 );
  164. o->end();
  165. }
  166. {
  167. Fl_Pack *o = takes = new Fl_Pack( width(), 0, pack->w(), 115 );
  168. o->end();
  169. o->hide();
  170. }
  171. o->end();
  172. }
  173. end();
  174. /* FIXME: should be configurable, but where? */
  175. create_outputs( 2 );
  176. create_inputs( 2 );
  177. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  178. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  179. }
  180. Track::Track ( const char *L ) :
  181. Fl_Group ( 0, 0, 0, 0, L )
  182. {
  183. init();
  184. if ( L )
  185. name( L );
  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. /* if ( ! name_field->visible() ) */
  290. /* { */
  291. /* fl_color( FL_WHITE ); */
  292. /* fl_font( FL_HELVETICA, 14 ); */
  293. /* fl_draw( name_field->value(), name_field->x(), name_field->y(), name_field->w(), name_field->h(), FL_ALIGN_CENTER ); */
  294. /* } */
  295. }
  296. int
  297. Track::handle ( int m )
  298. {
  299. Logger log( this );
  300. switch ( m )
  301. {
  302. case FL_MOUSEWHEEL:
  303. {
  304. if ( ! Fl::event_shift() )
  305. return 0;
  306. int d = Fl::event_dy();
  307. printf( "%d\n", d );
  308. if ( d < 0 )
  309. size( size() - 1 );
  310. else
  311. size( size() + 1 );
  312. return 1;
  313. }
  314. default:
  315. return Fl_Group::handle( m );
  316. }
  317. }
  318. /**********/
  319. /* Engine */
  320. /**********/
  321. bool
  322. Track::create_outputs ( int n )
  323. {
  324. char pname[256];
  325. for ( int i = 0; i < n; ++i )
  326. {
  327. snprintf( pname, sizeof( pname ), "%s/out-%d", name(), i + 1 );
  328. output.push_back( Port( strdup( pname ), Port::Output ) );
  329. }
  330. /* FIXME: bogus */
  331. return true;
  332. }
  333. bool
  334. Track::create_inputs ( int n )
  335. {
  336. char pname[256];
  337. for ( int i = 0; i < n; ++i )
  338. {
  339. snprintf( pname, sizeof( pname ), "%s/in-%d", name(), i + 1 );
  340. input.push_back( Port( strdup( pname ), Port::Input ) );
  341. }
  342. /* FIXME: bogus */
  343. return true;
  344. }
  345. /* THREAD: RT */
  346. nframes_t
  347. Track::process ( nframes_t nframes )
  348. {
  349. if ( playback_ds )
  350. {
  351. record_ds->process( nframes );
  352. return playback_ds->process( nframes );
  353. }
  354. else
  355. return 0;
  356. }
  357. /* THREAD: RT */
  358. void
  359. Track::seek ( nframes_t frame )
  360. {
  361. if ( playback_ds )
  362. return playback_ds->seek( frame );
  363. }
  364. /* FIXME: what about theading issues with this region/audiofile being
  365. accessible from the UI thread? Need locking? */
  366. #include "Region.H"
  367. #include <time.h>
  368. /** very cheap UUID generator... */
  369. unsigned long long
  370. uuid ( void )
  371. {
  372. time_t t = time( NULL );
  373. return (unsigned long long) t;
  374. }
  375. /* THREAD: IO */
  376. /** create capture region and prepare to record */
  377. void
  378. Track::record ( nframes_t frame )
  379. {
  380. assert( _capture == NULL );
  381. char pat[256];
  382. snprintf( pat, sizeof( pat ), "%s-%llu.wav", name(), uuid() );
  383. /* FIXME: hack */
  384. Audio_File *af = Audio_File_SF::create( pat, 48000, input.size(), "Wav/24" );
  385. _capture = new Region( af, track(), frame );
  386. /* FIXME: wrong place for this */
  387. _capture->_r->end = 0;
  388. }
  389. /* THREAD: IO */
  390. /** write a block to the (already opened) capture file */
  391. void
  392. Track::write ( sample_t *buf, nframes_t nframes )
  393. {
  394. _capture->write( buf, nframes );
  395. }
  396. #include <stdio.h>
  397. /* THREAD: IO */
  398. void
  399. Track::stop ( nframes_t nframes )
  400. {
  401. _capture->finalize();
  402. _capture = NULL;
  403. }