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.

678 lines
18KB

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