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.

420 lines
9.6KB

  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. void
  25. Track::cb_input_field ( Fl_Widget *w, void *v )
  26. {
  27. ((Track*)v)->cb_input_field();
  28. }
  29. void
  30. Track::cb_button ( Fl_Widget *w, void *v )
  31. {
  32. ((Track*)v)->cb_button( w );
  33. }
  34. void
  35. Track::cb_input_field ( void )
  36. {
  37. log_start();
  38. if ( _name )
  39. free( _name );
  40. _name = strdup( name_field->value() );
  41. log_end();
  42. }
  43. void
  44. Track::cb_button ( Fl_Widget *w )
  45. {
  46. printf( "FIXME: inform mixer here\n" );
  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. else
  56. if ( w == take_menu )
  57. {
  58. int v = take_menu->value();
  59. switch ( v )
  60. {
  61. case 0: /* show all takes */
  62. show_all_takes( take_menu->menu()[ v ].value() );
  63. return;
  64. case 1: /* new */
  65. track( track()->clone_empty() );
  66. return;
  67. }
  68. const char *s = take_menu->menu()[ v ].text;
  69. for ( int i = takes->children(); i--; )
  70. {
  71. Sequence *t = (Sequence*)takes->child( i );
  72. if ( ! strcmp( s, t->name() ) )
  73. {
  74. track( t );
  75. redraw();
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. Track::Track ( int X, int Y, int W, int H, const char *L ) :
  82. Fl_Group ( X, Y, W, H, L )
  83. {
  84. _capture = NULL;
  85. _track = NULL;
  86. _name = NULL;
  87. _selected = false;
  88. _show_all_takes = false;
  89. _size = 1;
  90. labeltype( FL_NO_LABEL );
  91. Fl_Group::size( w(), height() );
  92. Track *o = this;
  93. o->box( FL_THIN_UP_BOX );
  94. {
  95. Fl_Group *o = new Fl_Group( 2, 2, 149, 70 );
  96. o->color( ( Fl_Color ) 53 );
  97. {
  98. Fl_Input *o = name_field = new Fl_Input( 2, 2, 144, 24 );
  99. o->color( ( Fl_Color ) 33 );
  100. o->labeltype( FL_NO_LABEL );
  101. o->labelcolor( FL_GRAY0 );
  102. o->textcolor( 32 );
  103. o->callback( cb_input_field, (void*)this );
  104. o->hide();
  105. }
  106. {
  107. Fl_Group *o = controls = new Fl_Group( 2, 28, 149, 24 );
  108. {
  109. Fl_Button *o = record_button =
  110. new Fl_Button( 6, 28, 26, 24, "@circle" );
  111. o->type( 1 );
  112. o->box( FL_THIN_UP_BOX );
  113. o->color( FL_LIGHT1 );
  114. o->selection_color( FL_RED );
  115. o->labelsize( 8 );
  116. o->callback( cb_button, this );
  117. }
  118. {
  119. Fl_Button *o = mute_button =
  120. new Fl_Button( 35, 28, 26, 24, "m" );
  121. o->type( 1 );
  122. o->box( FL_THIN_UP_BOX );
  123. o->color( FL_LIGHT1 );
  124. o->labelsize( 11 );
  125. o->callback( cb_button, this );
  126. }
  127. {
  128. Fl_Button *o = solo_button =
  129. new Fl_Button( 66, 28, 26, 24, "s" );
  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_Menu_Button *o = take_menu =
  138. new Fl_Menu_Button( 97, 28, 47, 24, "T" );
  139. o->box( FL_THIN_UP_BOX );
  140. o->color( FL_LIGHT1 );
  141. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  142. o->callback( cb_button, this );
  143. o->add( "Show all takes", 0, 0, 0, FL_MENU_TOGGLE );
  144. o->add( "New", 0, 0, 0, FL_MENU_DIVIDER );
  145. }
  146. o->end();
  147. }
  148. {
  149. Fl_Box *o = new Fl_Box( 0, 76, 149, 38 );
  150. o->box( FL_FLAT_BOX );
  151. Fl_Group::current()->resizable( o );
  152. }
  153. o->size( Track::width(), h() );
  154. o->end();
  155. }
  156. {
  157. Fl_Pack *o = pack = new Fl_Pack( width(), 0, 1006, 115 );
  158. o->labeltype( FL_NO_LABEL );
  159. o->resize( x() + width(), y(), w() - width(), h() );
  160. Fl_Group::current()->resizable( o );
  161. {
  162. Fl_Pack *o = control = new Fl_Pack( width(), 0, pack->w(), 115 );
  163. o->end();
  164. }
  165. {
  166. Fl_Pack *o = takes = new Fl_Pack( width(), 0, pack->w(), 115 );
  167. o->end();
  168. o->hide();
  169. }
  170. o->end();
  171. }
  172. end();
  173. if ( L )
  174. name( L );
  175. /* FIXME: should be configurable, but where? */
  176. create_outputs( 2 );
  177. create_inputs( 2 );
  178. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  179. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  180. log_create();
  181. }
  182. Track::~Track ( )
  183. {
  184. log_destroy();
  185. }
  186. static int pack_visible( Fl_Pack *p )
  187. {
  188. int v = 0;
  189. for ( int i = p->children(); i--; )
  190. if ( p->child( i )->visible() )
  191. v++;
  192. return v;
  193. }
  194. /* adjust size of widget and children */
  195. void
  196. Track::resize ( void )
  197. {
  198. for ( int i = takes->children(); i--; )
  199. takes->child( i )->size( w(), height() );
  200. for ( int i = control->children(); i--; )
  201. control->child( i )->size( w(), height() );
  202. if ( _show_all_takes )
  203. {
  204. takes->show();
  205. Fl_Group::size( w(), height() * ( 1 + takes->children() + pack_visible( control ) ) );
  206. }
  207. else
  208. {
  209. takes->hide();
  210. Fl_Group::size( w(), height() * ( 1 + pack_visible( control ) ) );
  211. }
  212. if ( track() )
  213. track()->size( w(), height() );
  214. if ( controls->y() + controls->h() > y() + h() )
  215. controls->hide();
  216. else
  217. controls->show();
  218. parent()->redraw();
  219. }
  220. void
  221. Track::size ( int v )
  222. {
  223. if ( v < 0 || v > 3 )
  224. return;
  225. _size = v;
  226. resize();
  227. }
  228. void
  229. Track::track( Sequence * t )
  230. {
  231. // t->size( 1, h() );
  232. if ( track() )
  233. add( track() );
  234. // takes->insert( *track(), 0 );
  235. _track = t;
  236. pack->insert( *t, 0 );
  237. resize();
  238. }
  239. void
  240. Track::add_control( Sequence *t )
  241. {
  242. control->add( t );
  243. resize();
  244. }
  245. /**********/
  246. /* Engine */
  247. /**********/
  248. bool
  249. Track::create_outputs ( int n )
  250. {
  251. char pname[256];
  252. for ( int i = 0; i < n; ++i )
  253. {
  254. snprintf( pname, sizeof( pname ), "%s/out-%d", name(), i + 1 );
  255. output.push_back( Port( strdup( pname ), Port::Output ) );
  256. }
  257. /* FIXME: bogus */
  258. return true;
  259. }
  260. bool
  261. Track::create_inputs ( int n )
  262. {
  263. char pname[256];
  264. for ( int i = 0; i < n; ++i )
  265. {
  266. snprintf( pname, sizeof( pname ), "%s/in-%d", name(), i + 1 );
  267. input.push_back( Port( strdup( pname ), Port::Input ) );
  268. }
  269. /* FIXME: bogus */
  270. return true;
  271. }
  272. /* THREAD: RT */
  273. nframes_t
  274. Track::process ( nframes_t nframes )
  275. {
  276. if ( playback_ds )
  277. {
  278. record_ds->process( nframes );
  279. return playback_ds->process( nframes );
  280. }
  281. else
  282. return 0;
  283. }
  284. /* THREAD: RT */
  285. void
  286. Track::seek ( nframes_t frame )
  287. {
  288. if ( playback_ds )
  289. return playback_ds->seek( frame );
  290. }
  291. /* FIXME: what about theading issues with this region/audiofile being
  292. accessible from the UI thread? Need locking? */
  293. #include "Region.H"
  294. #include <time.h>
  295. /** very cheap UUID generator... */
  296. unsigned long long
  297. uuid ( void )
  298. {
  299. time_t t = time( NULL );
  300. return (unsigned long long) t;
  301. }
  302. /* THREAD: IO */
  303. /** create capture region and prepare to record */
  304. void
  305. Track::record ( nframes_t frame )
  306. {
  307. assert( _capture == NULL );
  308. char pat[256];
  309. snprintf( pat, sizeof( pat ), "%s-%llu.wav", name(), uuid() );
  310. /* FIXME: hack */
  311. Audio_File *af = Audio_File_SF::create( pat, 48000, input.size(), "Wav/24" );
  312. _capture = new Region( af, track(), frame );
  313. /* FIXME: wrong place for this */
  314. _capture->_r->end = 0;
  315. }
  316. /* THREAD: IO */
  317. /** write a block to the (already opened) capture file */
  318. void
  319. Track::write ( sample_t *buf, nframes_t nframes )
  320. {
  321. _capture->write( buf, nframes );
  322. }
  323. #include <stdio.h>
  324. /* THREAD: IO */
  325. void
  326. Track::stop ( nframes_t nframes )
  327. {
  328. _capture->finalize();
  329. _capture = NULL;
  330. }