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.

652 lines
17KB

  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 <FL/fl_draw.H>
  19. #include <FL/Fl.H>
  20. #include <FL/Fl_Group.H>
  21. #include <FL/Fl_Widget.H>
  22. #include <FL/Fl_Box.H>
  23. #include <FL/Fl_Menu_Item.H>
  24. #include <stdio.h>
  25. #include "Sequence.H"
  26. #include "Audio_Region.H"
  27. #include "Timeline.H"
  28. #include "Waveform.H"
  29. #include "Audio_Sequence.H"
  30. #include "Track.H"
  31. #include "Engine/Audio_File.H"
  32. #include <algorithm>
  33. using std::min;
  34. using std::max;
  35. extern Timeline *timeline;
  36. bool Audio_Region::inherit_track_color = true;
  37. Fl_Boxtype Audio_Region::_box = FL_UP_BOX;
  38. Fl_Color Audio_Region::_selection_color = FL_MAGENTA;
  39. static Fl_Color fl_invert_color ( Fl_Color c )
  40. {
  41. unsigned char r, g, b;
  42. Fl::get_color( c, r, g, b );
  43. return fl_rgb_color( 255 - r, 255 - g, 255 - b );
  44. }
  45. void
  46. Audio_Region::get ( Log_Entry &e ) const
  47. {
  48. e.add( ":source", _clip ? _clip->name() : "" );
  49. e.add( ":gain", _scale );
  50. e.add( ":fade-in-type", _fade_in.type );
  51. e.add( ":fade-in-duration", _fade_in.length );
  52. e.add( ":fade-out-type", _fade_out.type );
  53. e.add( ":fade-out-duration", _fade_out.length );
  54. Sequence_Region::get( e );
  55. e.add( ":offset", _r->offset );
  56. }
  57. void
  58. Audio_Region::set ( Log_Entry &e )
  59. {
  60. for ( int i = 0; i < e.size(); ++i )
  61. {
  62. const char *s, *v;
  63. e.get( i, &s, &v );
  64. if ( ! strcmp( s, ":gain" ) )
  65. _scale = atof( v );
  66. else if ( ! strcmp( s, ":color" ) )
  67. _box_color = (Fl_Color)atoi( v );
  68. else if ( ! strcmp( s, ":fade-in-type" ) )
  69. _fade_in.type = (Fade::fade_type_e)atoi( v );
  70. else if ( ! strcmp( s, ":fade-in-duration" ) )
  71. _fade_in.length = atoll( v );
  72. else if ( ! strcmp( s, ":fade-out-type" ) )
  73. _fade_out.type = (Fade::fade_type_e)atoi( v );
  74. else if ( ! strcmp( s, ":fade-out-duration" ) )
  75. _fade_out.length = atoll( v );
  76. else if ( ! strcmp( s, ":offset" ) )
  77. _r->offset = atoll( v );
  78. else if ( ! strcmp( s, ":source" ) )
  79. {
  80. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  81. {
  82. printf( "Grave error: could not open source \"%s\"\n", v );
  83. }
  84. }
  85. }
  86. Sequence_Region::set( e );
  87. }
  88. void
  89. Audio_Region::init ( void )
  90. {
  91. _sequence = NULL;
  92. _scale = 1.0f;
  93. _clip = NULL;
  94. _box_color = FL_CYAN;
  95. _color = FL_BLUE;
  96. _fade_in.length = 256;
  97. _fade_in.type = Fade::Sigmoid;
  98. _fade_out = _fade_in;
  99. }
  100. /* copy constructor */
  101. Audio_Region::Audio_Region ( const Audio_Region & rhs ) : Sequence_Region( rhs )
  102. {
  103. // *((Sequence_Region*)this) = (Sequence_Region &)rhs;
  104. _clip = rhs._clip;
  105. _scale = rhs._scale;
  106. _fade_in = rhs._fade_in;
  107. _fade_out = rhs._fade_out;
  108. log_create();
  109. }
  110. /* */
  111. Audio_Region::Audio_Region ( Audio_File *c )
  112. {
  113. init();
  114. _clip = c;
  115. _r->length = _clip->length();
  116. log_create();
  117. }
  118. /* used when DND importing */
  119. Audio_Region::Audio_Region ( Audio_File *c, Sequence *t, nframes_t o )
  120. {
  121. init();
  122. _clip = c;
  123. _sequence = t;
  124. _r->offset = 0;
  125. _r->start = o;
  126. _r->length = _clip->length();
  127. sequence()->add( this );
  128. int sum = 0;
  129. const char *s = rindex( _clip->name(), '/' );
  130. if ( ! s )
  131. s = _clip->name();
  132. for ( int i = strlen( s ); i--; )
  133. sum += s[ i ];
  134. while ( sum >> 8 )
  135. sum = (sum & 0xFF) + (sum >> 8);
  136. _color = (Fl_Color)sum;
  137. /* _color = fl_color_average( FL_YELLOW, (Fl_Color)sum, 0.80 ); */
  138. // _color = FL_YELLOW;
  139. _box_color = FL_WHITE;
  140. log_create();
  141. }
  142. const char *
  143. Audio_Region::source_name ( void ) const
  144. {
  145. return _clip->name();
  146. }
  147. #include <FL/fl_show_colormap.H>
  148. #include "FL/test_press.H"
  149. int
  150. Audio_Region::handle ( int m )
  151. {
  152. static int ox, oy;
  153. static bool copied = false;
  154. static nframes_t os;
  155. int X = Fl::event_x();
  156. int Y = Fl::event_y();
  157. Logger _log( this );
  158. switch ( m )
  159. {
  160. case FL_KEYBOARD:
  161. {
  162. if ( Fl::event_key() == FL_F + 3 )
  163. {
  164. nframes_t offset = x_to_offset( X );
  165. if ( offset < length() )
  166. _fade_in.length = offset;
  167. DMESSAGE( "setting fade in length to %lu", _fade_in.length );
  168. redraw();
  169. return 1;
  170. }
  171. else if ( Fl::event_key() == FL_F + 4 )
  172. {
  173. long offset = length() - x_to_offset( X );
  174. if ( offset > 0 )
  175. _fade_out.length = offset;
  176. DMESSAGE( "setting fade out length to %lu", _fade_in.length );
  177. redraw();
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. case FL_PUSH:
  183. {
  184. /* splitting */
  185. if ( test_press( FL_BUTTON2 | FL_SHIFT ) )
  186. {
  187. /* split */
  188. if ( ! copied )
  189. {
  190. Loggable::block_start();
  191. Audio_Region *copy = new Audio_Region( *this );
  192. trim( RIGHT, X );
  193. copy->trim( LEFT, X );
  194. sequence()->add( copy );
  195. log_end();
  196. Loggable::block_end();
  197. }
  198. return 0;
  199. }
  200. else
  201. {
  202. ox = x() - X;
  203. oy = y() - Y;
  204. /* for panning */
  205. os = _r->offset;
  206. if ( test_press( FL_BUTTON2 | FL_CTRL ) )
  207. {
  208. normalize();
  209. redraw();
  210. return 1;
  211. }
  212. else if ( test_press( FL_BUTTON3 ) )
  213. {
  214. /* context menu */
  215. Fade::fade_type_e it = _fade_in.type;
  216. Fade::fade_type_e ot = _fade_out.type;
  217. Fl_Menu_Item menu[] =
  218. {
  219. { "Fade", 0, 0, 0, FL_SUBMENU },
  220. { "In", 0, 0, 0, FL_SUBMENU },
  221. { "Linear", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  222. { "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  223. { "Logarithmic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  224. { "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  225. { 0 },
  226. { "Out", 0, 0, 0, FL_SUBMENU },
  227. { "Linear", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  228. { "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  229. { "Logarothmic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  230. { "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  231. { 0 },
  232. { 0 },
  233. { "Color", 0, 0, 0, inherit_track_color ? FL_MENU_INACTIVE : 0 },
  234. { 0 },
  235. };
  236. const Fl_Menu_Item *r = menu->popup( X, Y, "Audio_Region" );
  237. if ( r )
  238. {
  239. if ( r > &menu[1] && r < &menu[6] )
  240. _fade_in.type = (Fade::fade_type_e)(int)(r - &menu[2]);
  241. else if ( r > &menu[7] && r < &menu[12] )
  242. _fade_out.type = (Fade::fade_type_e)(int)(r - &menu[8]);
  243. else if ( r == &menu[ 14 ] )
  244. {
  245. box_color( fl_show_colormap( box_color() ) );
  246. }
  247. redraw();
  248. }
  249. return 1;
  250. }
  251. else
  252. return Sequence_Region::handle( m );
  253. }
  254. break;
  255. }
  256. case FL_RELEASE:
  257. {
  258. Sequence_Region::handle( m );
  259. copied = false;
  260. return 1;
  261. }
  262. case FL_DRAG:
  263. if ( ! _drag )
  264. {
  265. begin_drag( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
  266. _log.hold();
  267. }
  268. if ( test_press( FL_BUTTON1 | FL_SHIFT | FL_CTRL ) )
  269. {
  270. /* panning */
  271. int d = (ox + X) - x();
  272. long td = timeline->x_to_ts( d );
  273. if ( td > 0 && os < (nframes_t)td )
  274. _r->offset = 0;
  275. else
  276. _r->offset = os - td;
  277. redraw();
  278. return 1;
  279. }
  280. return Sequence_Region::handle( m );
  281. default:
  282. return Sequence_Region::handle( m );
  283. break;
  284. }
  285. return 0;
  286. }
  287. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  288. portion of the region covered by this draw, which may or may not
  289. cover the fade in question. */
  290. void
  291. Audio_Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
  292. {
  293. const int dy = y() + Fl::box_dy( box() );
  294. const int dh = h() - Fl::box_dh( box() );
  295. const int height = dh;
  296. const int width = timeline->ts_to_x( fade.length );
  297. fl_color( fl_lighter( FL_BLACK ) );
  298. fl_push_matrix();
  299. if ( dir == Fade::In )
  300. fl_translate( line_x(), dy );
  301. else
  302. {
  303. fl_translate( line_x() + abs_w(), dy );
  304. /* flip */
  305. fl_scale( -1.0, 1.0 );
  306. }
  307. fl_scale( width, height );
  308. if ( line )
  309. fl_begin_line();
  310. else
  311. fl_begin_polygon();
  312. fl_vertex( 0.0, 0.0 );
  313. fl_vertex( 0.0, 1.0 );
  314. // if ( draw_real_fade_curve )
  315. {
  316. nframes_t tsx = timeline->x_to_ts( 1 );
  317. nframes_t ts = 0;
  318. for ( int i = 0; i < width; ++i, ts += tsx )
  319. fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
  320. }
  321. fl_vertex( 1.0, 0.0 );
  322. if ( line )
  323. fl_end_line();
  324. else
  325. fl_end_polygon();
  326. fl_pop_matrix();
  327. }
  328. struct Peaks_Redraw_Request {
  329. Audio_Region *region;
  330. nframes_t start;
  331. nframes_t end;
  332. Peaks_Redraw_Request ( Audio_Region *region, nframes_t start, nframes_t end ) : region( region ), start( start), end( end )
  333. {
  334. }
  335. };
  336. /* static wrapper */
  337. void
  338. Audio_Region::peaks_pending_cb ( void *v )
  339. {
  340. Peaks_Redraw_Request *r = (Peaks_Redraw_Request*)v;
  341. r->region->peaks_pending_cb( r );
  342. }
  343. void
  344. Audio_Region::peaks_pending_cb ( Peaks_Redraw_Request *r )
  345. {
  346. int npeaks = timeline->ts_to_x( r->end - r->start );
  347. if ( _clip->peaks()->ready( r->start, npeaks, timeline->fpp() ) )
  348. {
  349. printf( "damaging from timeout\n" );
  350. /* FIXME: only need to damage the affected area! */
  351. timeline->damage( FL_DAMAGE_ALL, x(), y(), w(), h() );
  352. delete r;
  353. }
  354. else
  355. Fl::repeat_timeout( 0.1f, &Audio_Region::peaks_pending_cb, (void*)r );
  356. }
  357. void
  358. Audio_Region::draw_box( void )
  359. {
  360. /* dirty hack to keep the box from flipping to vertical at small sizes */
  361. fl_push_clip( x(), y(), w(), h() );
  362. Fl_Color selection_color = _selection_color;
  363. Fl_Color color = Audio_Region::inherit_track_color ? sequence()->track()->color() : _box_color;
  364. color = fl_color_average( color, sequence()->color(), 0.75f );
  365. if ( this == ((Audio_Sequence*)sequence())->capture_region() )
  366. {
  367. color = FL_RED;
  368. }
  369. else if ( ! active_r() )
  370. {
  371. color = fl_inactive( color );
  372. selection_color = fl_inactive( selection_color );
  373. }
  374. if ( selected() )
  375. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), selection_color );
  376. else
  377. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), color );
  378. /* draw fades */
  379. draw_fade( _fade_in, Fade::In, false, x(), w() );
  380. draw_fade( _fade_out, Fade::Out, false, x(), w() );
  381. fl_pop_clip();
  382. }
  383. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  384. void
  385. Audio_Region::draw ( void )
  386. {
  387. /* intersect clip with region */
  388. int X, Y, W, H;
  389. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  390. if ( ! ( W > 0 && H > 0 ) )
  391. /* no coverage */
  392. return;
  393. /* account for waveform outlines... */
  394. X -= 2;
  395. W += 4;
  396. int OX = scroll_x();
  397. int ox = timeline->ts_to_x( _r->start );
  398. if ( ox > OX + sequence()->w() ||
  399. ox < OX && ox + abs_w() < OX )
  400. /* not in viewport */
  401. return;
  402. int rw = timeline->ts_to_x( _r->length );
  403. /* calculate waveform offset due to scrolling */
  404. nframes_t offset = 0;
  405. if ( ox < OX )
  406. {
  407. offset = timeline->x_to_ts( OX - ox );
  408. rw -= OX - ox;
  409. }
  410. rw = min( rw, sequence()->w() );
  411. int rx = x();
  412. fl_push_clip( rx, Y, rw, H );
  413. /* get actual peak data */
  414. int channels;
  415. int peaks;
  416. Peak *pbuf;
  417. // const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  418. // nframes_t start = _r->start + offset;
  419. nframes_t start = _r->offset + offset;
  420. /* compensate for ??? */
  421. if ( X - rx > 0 )
  422. start += timeline->x_to_ts( X - rx );
  423. const int peaks_needed = min( timeline->ts_to_x( _clip->length() - start ), W );
  424. const nframes_t end = start + timeline->x_to_ts( peaks_needed );
  425. if ( _clip->read_peaks( timeline->fpp(),
  426. start,
  427. end,
  428. &peaks, &pbuf, &channels ) &&
  429. peaks )
  430. {
  431. assert( pbuf );
  432. /* draw fade curve outlines--this is only here because of crossfades */
  433. draw_fade( _fade_in, Fade::In, true, X, W );
  434. draw_fade( _fade_out, Fade::Out, true, X, W );
  435. int ch = (h() - Fl::box_dh( box() )) / channels;
  436. Waveform::scale( pbuf, peaks * channels, _scale );
  437. for ( int i = 0; i < channels; ++i )
  438. {
  439. // Peak *pb = pbuf + (peaks * i);
  440. /* int fw = timeline->ts_to_x( fade.length ); */
  441. /* /\* if ( draw_fade_waveform ) *\/ */
  442. /* for ( int j = min( fw, peaks ); j--; ) */
  443. /* { */
  444. /* const float g = fade.gain( j * timeline->fpp() ); */
  445. /* pb[ j ].min *= g; */
  446. /* pb[ j ].max *= g; */
  447. /* } */
  448. Waveform::draw( X,
  449. (y() + Fl::box_dy( box() )) + (i * ch),
  450. W,
  451. ch,
  452. pbuf + i, peaks, channels,
  453. selected() ? fl_invert_color( _color ) : _color );
  454. }
  455. }
  456. if ( peaks < peaks_needed )
  457. {
  458. /* couldn't read peaks--perhaps they're being generated. Try again later. */
  459. Fl::add_timeout( 0.1f, &Audio_Region::peaks_pending_cb,
  460. new Peaks_Redraw_Request( this, start + timeline->x_to_ts( peaks ), end ) );
  461. }
  462. timeline->draw_measure_lines( X, Y, W, H, _box_color );
  463. /* fl_color( FL_BLACK ); */
  464. /* fl_line( rx, Y, rx, Y + H ); */
  465. /* fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H ); */
  466. if ( _clip->dummy() )
  467. {
  468. char pat[256];
  469. snprintf( pat, sizeof( pat ), "Missing Source!: %s", _clip->name() );
  470. draw_label( pat, align() );
  471. }
  472. else
  473. draw_label( _clip->name(), align() );
  474. /* if ( current() ) */
  475. /* { */
  476. /* /\* draw length bubble *\/ */
  477. /* char pat[40]; */
  478. /* snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() ); */
  479. /* draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN ); */
  480. /* } */
  481. fl_pop_clip();
  482. }
  483. void
  484. Audio_Region::normalize ( void )
  485. {
  486. int peaks, channels;
  487. Peak *pbuf;
  488. if ( _clip->read_peaks( length(), offset(), offset() + length(), &peaks, &pbuf, &channels ) &&
  489. peaks )
  490. _scale = pbuf->normalization_factor();
  491. }