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.

518 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::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. if ( ! name_field->visible() )
  289. {
  290. fl_color( FL_WHITE );
  291. fl_font( FL_HELVETICA, 14 );
  292. fl_draw( name_field->value(), name_field->x(), name_field->y(), name_field->w(), name_field->h(), FL_ALIGN_CENTER );
  293. }
  294. }
  295. int
  296. Track::handle ( int m )
  297. {
  298. Logger log( this );
  299. switch ( m )
  300. {
  301. case FL_MOUSEWHEEL:
  302. {
  303. if ( ! Fl::event_shift() )
  304. return 0;
  305. int d = Fl::event_dy();
  306. printf( "%d\n", d );
  307. if ( d < 0 )
  308. size( size() - 1 );
  309. else
  310. size( size() + 1 );
  311. return 1;
  312. }
  313. default:
  314. return Fl_Group::handle( m );
  315. }
  316. }
  317. /**********/
  318. /* Engine */
  319. /**********/
  320. bool
  321. Track::create_outputs ( int n )
  322. {
  323. char pname[256];
  324. for ( int i = 0; i < n; ++i )
  325. {
  326. snprintf( pname, sizeof( pname ), "%s/out-%d", name(), i + 1 );
  327. output.push_back( Port( strdup( pname ), Port::Output ) );
  328. }
  329. /* FIXME: bogus */
  330. return true;
  331. }
  332. bool
  333. Track::create_inputs ( int n )
  334. {
  335. char pname[256];
  336. for ( int i = 0; i < n; ++i )
  337. {
  338. snprintf( pname, sizeof( pname ), "%s/in-%d", name(), i + 1 );
  339. input.push_back( Port( strdup( pname ), Port::Input ) );
  340. }
  341. /* FIXME: bogus */
  342. return true;
  343. }
  344. /* THREAD: RT */
  345. nframes_t
  346. Track::process ( nframes_t nframes )
  347. {
  348. if ( playback_ds )
  349. {
  350. record_ds->process( nframes );
  351. return playback_ds->process( nframes );
  352. }
  353. else
  354. return 0;
  355. }
  356. /* THREAD: RT */
  357. void
  358. Track::seek ( nframes_t frame )
  359. {
  360. if ( playback_ds )
  361. return playback_ds->seek( frame );
  362. }
  363. /* FIXME: what about theading issues with this region/audiofile being
  364. accessible from the UI thread? Need locking? */
  365. #include "Region.H"
  366. #include <time.h>
  367. /** very cheap UUID generator... */
  368. unsigned long long
  369. uuid ( void )
  370. {
  371. time_t t = time( NULL );
  372. return (unsigned long long) t;
  373. }
  374. /* THREAD: IO */
  375. /** create capture region and prepare to record */
  376. void
  377. Track::record ( nframes_t frame )
  378. {
  379. assert( _capture == NULL );
  380. char pat[256];
  381. snprintf( pat, sizeof( pat ), "%s-%llu.wav", name(), uuid() );
  382. /* FIXME: hack */
  383. Audio_File *af = Audio_File_SF::create( pat, 48000, input.size(), "Wav/24" );
  384. _capture = new Region( af, track(), frame );
  385. /* FIXME: wrong place for this */
  386. _capture->_r->end = 0;
  387. }
  388. /* THREAD: IO */
  389. /** write a block to the (already opened) capture file */
  390. void
  391. Track::write ( sample_t *buf, nframes_t nframes )
  392. {
  393. _capture->write( buf, nframes );
  394. }
  395. #include <stdio.h>
  396. /* THREAD: IO */
  397. void
  398. Track::stop ( nframes_t nframes )
  399. {
  400. _capture->finalize();
  401. _capture = NULL;
  402. }