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.

483 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. 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. void
  82. Track::init ( void )
  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( timeline->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. /* FIXME: should be configurable, but where? */
  174. create_outputs( 2 );
  175. create_inputs( 2 );
  176. playback_ds = new Playback_DS( this, engine->frame_rate(), engine->nframes(), output.size() );
  177. record_ds = new Record_DS( this, engine->frame_rate(), engine->nframes(), input.size() );
  178. }
  179. Track::Track ( const char *L ) :
  180. Fl_Group ( 0, 0, 0, 0, L )
  181. {
  182. init();
  183. if ( L )
  184. name( L );
  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::track ( Sequence * t )
  235. {
  236. t->track( this );
  237. if ( track() )
  238. add( track() );
  239. _track = t;
  240. pack->insert( *t, 0 );
  241. resize();
  242. }
  243. void
  244. Track::add ( Control_Sequence *t )
  245. {
  246. t->track( this );
  247. control->add( t );
  248. resize();
  249. }
  250. void
  251. Track::draw ( void )
  252. {
  253. if ( _selected )
  254. {
  255. Fl_Color c = color();
  256. color( FL_RED );
  257. Fl_Group::draw();
  258. color( c );
  259. }
  260. else
  261. Fl_Group::draw();
  262. if ( ! name_field->visible() )
  263. {
  264. fl_color( FL_WHITE );
  265. fl_font( FL_HELVETICA, 14 );
  266. fl_draw( name_field->value(), name_field->x(), name_field->y(), name_field->w(), name_field->h(), FL_ALIGN_CENTER );
  267. }
  268. }
  269. int
  270. Track::handle ( int m )
  271. {
  272. Logger log( this );
  273. switch ( m )
  274. {
  275. case FL_MOUSEWHEEL:
  276. {
  277. if ( ! Fl::event_shift() )
  278. return 0;
  279. int d = Fl::event_dy();
  280. printf( "%d\n", d );
  281. if ( d < 0 )
  282. size( size() - 1 );
  283. else
  284. size( size() + 1 );
  285. return 1;
  286. }
  287. default:
  288. return Fl_Group::handle( m );
  289. }
  290. }
  291. /**********/
  292. /* Engine */
  293. /**********/
  294. bool
  295. Track::create_outputs ( int n )
  296. {
  297. char pname[256];
  298. for ( int i = 0; i < n; ++i )
  299. {
  300. snprintf( pname, sizeof( pname ), "%s/out-%d", name(), i + 1 );
  301. output.push_back( Port( strdup( pname ), Port::Output ) );
  302. }
  303. /* FIXME: bogus */
  304. return true;
  305. }
  306. bool
  307. Track::create_inputs ( int n )
  308. {
  309. char pname[256];
  310. for ( int i = 0; i < n; ++i )
  311. {
  312. snprintf( pname, sizeof( pname ), "%s/in-%d", name(), i + 1 );
  313. input.push_back( Port( strdup( pname ), Port::Input ) );
  314. }
  315. /* FIXME: bogus */
  316. return true;
  317. }
  318. /* THREAD: RT */
  319. nframes_t
  320. Track::process ( nframes_t nframes )
  321. {
  322. if ( playback_ds )
  323. {
  324. record_ds->process( nframes );
  325. return playback_ds->process( nframes );
  326. }
  327. else
  328. return 0;
  329. }
  330. /* THREAD: RT */
  331. void
  332. Track::seek ( nframes_t frame )
  333. {
  334. if ( playback_ds )
  335. return playback_ds->seek( frame );
  336. }
  337. /* FIXME: what about theading issues with this region/audiofile being
  338. accessible from the UI thread? Need locking? */
  339. #include "Region.H"
  340. #include <time.h>
  341. /** very cheap UUID generator... */
  342. unsigned long long
  343. uuid ( void )
  344. {
  345. time_t t = time( NULL );
  346. return (unsigned long long) t;
  347. }
  348. /* THREAD: IO */
  349. /** create capture region and prepare to record */
  350. void
  351. Track::record ( nframes_t frame )
  352. {
  353. assert( _capture == NULL );
  354. char pat[256];
  355. snprintf( pat, sizeof( pat ), "%s-%llu.wav", name(), uuid() );
  356. /* FIXME: hack */
  357. Audio_File *af = Audio_File_SF::create( pat, 48000, input.size(), "Wav/24" );
  358. _capture = new Region( af, track(), frame );
  359. /* FIXME: wrong place for this */
  360. _capture->_r->end = 0;
  361. }
  362. /* THREAD: IO */
  363. /** write a block to the (already opened) capture file */
  364. void
  365. Track::write ( sample_t *buf, nframes_t nframes )
  366. {
  367. _capture->write( buf, nframes );
  368. }
  369. #include <stdio.h>
  370. /* THREAD: IO */
  371. void
  372. Track::stop ( nframes_t nframes )
  373. {
  374. _capture->finalize();
  375. _capture = NULL;
  376. }