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.

832 lines
21KB

  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 "Region.H"
  20. #include "Timeline.H"
  21. #include "Waveform.H"
  22. #include <FL/fl_draw.H>
  23. #include <FL/Fl.H>
  24. #include <FL/Fl_Group.H>
  25. #include <FL/Fl_Widget.H>
  26. #include <FL/Fl_Box.H>
  27. #include <stdio.h>
  28. #include <algorithm>
  29. // using std::algorithm;
  30. using namespace std;
  31. extern Timeline *timeline;
  32. Fl_Boxtype Region::_box = FL_UP_BOX;
  33. Fl_Color Region::_selection_color = FL_MAGENTA;
  34. static Fl_Color fl_invert_color ( Fl_Color c )
  35. {
  36. unsigned char r, g, b;
  37. Fl::get_color( c, r, g, b );
  38. return fl_rgb_color( 255 - r, 255 - g, 255 - b );
  39. }
  40. #if 0
  41. /* perhaps use map? */
  42. map_PRIM ( set )
  43. {
  44. /* if ( narg % 2 != 0 ) */
  45. /* printf( "invalid number of arguments\n" ); */
  46. int id = atoi( arg );
  47. map_ARG_NEXT( arg, end );
  48. Logable *l = Loggable::find( id );
  49. char **sa = malloc( sizeof( char * ) * narg + 1 );
  50. for ( int i = 0; i < narg; ++i )
  51. sa[ i ] = strdup( map_ARG_NEXT( arg, end ) );
  52. l->set( sa );
  53. map_RESULT( "" );
  54. }
  55. #endif
  56. void
  57. Region::init ( void )
  58. {
  59. _track = NULL;
  60. _r->offset = 0;
  61. _r->start = 0;
  62. _r->end = 0;
  63. _scale = 1.0f;
  64. _clip = NULL;
  65. _box_color = FL_CYAN;
  66. _color = FL_BLUE;
  67. _fade_in.length = 256;
  68. _fade_in.type = Fade::Sigmoid;
  69. _fade_out = _fade_in;
  70. }
  71. /* copy constructor */
  72. Region::Region ( const Region & rhs )
  73. {
  74. *((Track_Widget*)this) = (Track_Widget &)rhs;
  75. _clip = rhs._clip;
  76. _scale = rhs._scale;
  77. _fade_in = rhs._fade_in;
  78. _fade_out = rhs._fade_out;
  79. log_create();
  80. }
  81. Track_Widget *
  82. Region::clone ( const Track_Widget *r )
  83. {
  84. return new Region( *(Region*)r );
  85. }
  86. /* */
  87. Region::Region ( Audio_File *c )
  88. {
  89. init();
  90. _clip = c;
  91. _r->end = _clip->length();
  92. log_create();
  93. }
  94. /* used when DND importing */
  95. Region::Region ( Audio_File *c, Track *t, nframes_t o )
  96. {
  97. init();
  98. _clip = c;
  99. _r->end = _clip->length();
  100. _track = t;
  101. _r->offset = o;
  102. _track->add( this );
  103. int sum = 0;
  104. const char *s = rindex( _clip->name(), '/' );
  105. for ( int i = strlen( s ); i--; )
  106. sum += s[ i ];
  107. while ( sum >> 8 )
  108. sum = (sum & 0xFF) + (sum >> 8);
  109. _color = (Fl_Color)sum;
  110. /* _color = fl_color_average( FL_YELLOW, (Fl_Color)sum, 0.80 ); */
  111. // _color = FL_YELLOW;
  112. _box_color = FL_WHITE;
  113. log_create();
  114. }
  115. void
  116. Region::trim ( enum trim_e t, int X )
  117. {
  118. X -= _track->x();
  119. redraw();
  120. switch ( t )
  121. {
  122. case LEFT:
  123. {
  124. /* if ( d < 0 ) */
  125. /* // _track->damage( FL_DAMAGE_EXPOSE, x() + d, y(), 1 - d, h() ); */
  126. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() ); */
  127. /* else */
  128. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), d, h() ); */
  129. int d = X - ( abs_x() - scroll_x() );
  130. long td = timeline->x_to_ts( d );
  131. if ( td < 0 && _r->start < 0 - td )
  132. td = 0 - _r->start;
  133. if ( _r->start + td >= _r->end )
  134. td = (_r->end - _r->start) - timeline->x_to_ts( 1 );
  135. _r->start += td;
  136. _r->offset += td;
  137. break;
  138. }
  139. case RIGHT:
  140. {
  141. int d = (( abs_x() - scroll_x() ) + abs_w() ) - X;
  142. /* _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), d, h() ); */
  143. long td = timeline->x_to_ts( d );
  144. // printf( "%li %li\n", td, _r->end - _r->start );
  145. if ( td >= 0 && _r->end - _r->start < td )
  146. _r->end = _r->start + timeline->x_to_ts( 1 );
  147. else
  148. _r->end -= td;
  149. break;
  150. }
  151. default:
  152. return;
  153. }
  154. }
  155. int
  156. Region::handle ( int m )
  157. {
  158. static int ox, oy;
  159. static enum trim_e trimming;
  160. static bool copied = false;
  161. static nframes_t os;
  162. // int X = Fl::event_x() - _track->x();
  163. int X = Fl::event_x();
  164. int Y = Fl::event_y();
  165. int ret;
  166. Logger _log( this );
  167. //log_r->start();
  168. switch ( m )
  169. {
  170. case FL_ENTER:
  171. Track_Widget::handle( m );
  172. redraw();
  173. break;
  174. case FL_LEAVE:
  175. Track_Widget::handle( m );
  176. redraw();
  177. break;
  178. case FL_PUSH:
  179. {
  180. /* trimming / splitting */
  181. if ( Fl::event_shift() && ! Fl::event_ctrl() )
  182. {
  183. switch ( Fl::event_button() )
  184. {
  185. case 1:
  186. trim( trimming = LEFT, X );
  187. begin_drag( Drag( x() - X, y() - Y ) );
  188. _log.hold();
  189. break;
  190. case 3:
  191. trim( trimming = RIGHT, X );
  192. begin_drag( Drag( x() - X, y() - Y ) );
  193. _log.hold();
  194. break;
  195. case 2:
  196. {
  197. /* split */
  198. if ( ! copied )
  199. {
  200. Loggable::block_start();
  201. Region *copy = new Region( *this );
  202. trim( RIGHT, X );
  203. copy->trim( LEFT, X );
  204. _track->add( copy );
  205. log_end();
  206. Loggable::block_end();
  207. return 0;
  208. }
  209. }
  210. default:
  211. return 0;
  212. break;
  213. }
  214. fl_cursor( FL_CURSOR_WE );
  215. return 1;
  216. }
  217. else
  218. {
  219. ox = x() - X;
  220. oy = y() - Y;
  221. /* for panning */
  222. os = _r->start;
  223. /* normalization and selection */
  224. if ( Fl::event_button2() )
  225. {
  226. if ( Fl::event_ctrl() )
  227. normalize();
  228. else
  229. {
  230. if ( Track_Widget::current() == this )
  231. {
  232. if ( selected() )
  233. deselect();
  234. else
  235. select();
  236. }
  237. }
  238. redraw();
  239. goto changed;
  240. }
  241. if ( Fl::event_button1() && Fl::event_ctrl() )
  242. {
  243. /* duplication */
  244. return 1;
  245. }
  246. else
  247. return Track_Widget::handle( m );
  248. }
  249. break;
  250. }
  251. case FL_RELEASE:
  252. {
  253. Track_Widget::handle( m );
  254. copied = false;
  255. if ( trimming != NO )
  256. trimming = NO;
  257. goto changed;
  258. }
  259. case FL_DRAG:
  260. if ( ! _drag )
  261. {
  262. begin_drag( Drag( x() - X, y() - Y ) );
  263. _log.hold();
  264. }
  265. /* panning */
  266. if ( Fl::event_state() & FL_SHIFT &&
  267. Fl::event_state() & FL_CTRL )
  268. {
  269. int d = (ox + X) - x();
  270. long td = timeline->x_to_ts( d );
  271. nframes_t W = _r->end - _r->start;
  272. if ( td > 0 && os < td )
  273. _r->start = 0;
  274. else
  275. _r->start = os - td;
  276. _r->end = _r->start + W;
  277. _track->redraw();
  278. return 1;
  279. }
  280. /* trimming */
  281. if ( Fl::event_state() & FL_SHIFT )
  282. if ( trimming )
  283. {
  284. trim( trimming, X );
  285. return 1;
  286. }
  287. else
  288. return 0;
  289. /* duplication */
  290. if ( Fl::event_state() & FL_CTRL )
  291. {
  292. if ( _drag->state == 0 )
  293. {
  294. _track->add( new Region( *this ) );
  295. _drag->state = 1;
  296. return 1;
  297. }
  298. }
  299. /* track jumping */
  300. if ( ! selected() )
  301. {
  302. if ( Y > y() + h() )
  303. {
  304. Fl::copy( class_name(), strlen( class_name() ), 0 );
  305. Fl::dnd();
  306. }
  307. else
  308. if ( Y < y() )
  309. {
  310. Fl::copy( class_name(), strlen( class_name() ), 0 );
  311. Fl::dnd();
  312. }
  313. }
  314. ret = Track_Widget::handle( m );
  315. return ret | 1;
  316. default:
  317. return Track_Widget::handle( m );
  318. break;
  319. }
  320. changed:
  321. return 1;
  322. }
  323. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  324. portion of the region covered by this draw, which may or may not
  325. cover the fade in question. */
  326. void
  327. Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W )
  328. {
  329. const int dy = y() + Fl::box_dy( box() );
  330. const int dh = h() - Fl::box_dh( box() );
  331. const int height = dh;
  332. const int width = timeline->ts_to_x( fade.length );
  333. fl_color( fl_lighter( FL_BLACK ) );
  334. fl_push_matrix();
  335. if ( dir == Fade::In )
  336. {
  337. fl_translate( line_x(), dy );
  338. fl_scale( width, height );
  339. }
  340. else
  341. {
  342. // fl_translate( line_x() + abs_w(), dy + height );
  343. fl_translate( line_x() + abs_w(), dy );
  344. fl_scale( width, height );
  345. /* flip */
  346. fl_scale( -1.0, 1.0 );
  347. // fl_scale( 1.0, -1.0 );
  348. }
  349. if ( filled )
  350. fl_begin_polygon();
  351. else
  352. fl_begin_line();
  353. fl_vertex( 0.0, 0.0 );
  354. fl_vertex( 0.0, 1.0 );
  355. for ( int i = 0; i < width; ++i )
  356. {
  357. const float x = i / (float)width;
  358. const float y = 1.0f - fade.gain( timeline->x_to_ts( i ) );
  359. fl_vertex( x, y );
  360. }
  361. if ( filled )
  362. fl_end_polygon();
  363. else
  364. fl_end_line();
  365. fl_pop_matrix();
  366. }
  367. void
  368. Region::draw_box( int X, int Y, int W, int H )
  369. {
  370. if ( ! shown() )
  371. return;
  372. /* dirty hack to keep the box from flipping to vertical at small sizes */
  373. fl_push_clip( x(), Y, w(), H );
  374. if ( selected() )
  375. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
  376. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  377. else
  378. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
  379. /* draw fades */
  380. draw_fade( _fade_in, Fade::In, true, X, W );
  381. draw_fade( _fade_out, Fade::Out, true, X, W );
  382. fl_pop_clip();
  383. }
  384. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  385. Y W and H are the portion of the widget to draw (arrived at by
  386. intersection of the clip and relative to OX) */
  387. void
  388. Region::draw ( int X, int Y, int W, int H )
  389. {
  390. if ( ! shown() )
  391. return;
  392. if ( ! ( W > 0 && H > 0 ) )
  393. return;
  394. int OX = scroll_x();
  395. int ox = timeline->ts_to_x( _r->offset );
  396. if ( ox > OX + _track->w() ||
  397. ox < OX && ox + abs_w() < OX )
  398. return;
  399. int rw = timeline->ts_to_x( _r->end - _r->start );
  400. // nframes_t end = _r->offset + ( _r->end - _r->start );
  401. /* calculate waveform offset due to scrolling */
  402. nframes_t offset = 0;
  403. if ( ox < OX )
  404. {
  405. offset = timeline->x_to_ts( OX - ox );
  406. rw = timeline->ts_to_x( (_r->end - _r->start) - offset );
  407. }
  408. rw = min( rw, _track->w() );
  409. int rx = x();
  410. fl_push_clip( rx, Y, rw, H );
  411. /* get actual peak data */
  412. int channels;
  413. int peaks;
  414. Peak *pbuf;
  415. const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  416. _clip->read_peaks( timeline->fpp(),
  417. start,
  418. start + timeline->x_to_ts( W ),
  419. &peaks, &pbuf, &channels );
  420. assert( pbuf );
  421. /* draw fade curve outlines--this is only here because of crossfades */
  422. draw_fade( _fade_in, Fade::In, false, X, W );
  423. draw_fade( _fade_out, Fade::Out, false, X, W );
  424. int ch = (h() - Fl::box_dh( box() )) / channels;
  425. for ( int i = 0; i < channels; ++i )
  426. {
  427. Peak *pb = pbuf + (peaks * i);
  428. /* scale it */
  429. for ( int j = peaks; j--; )
  430. {
  431. pb[ j ].min *= _scale;
  432. pb[ j ].max *= _scale;
  433. }
  434. /* int fw = timeline->ts_to_x( fade.length ); */
  435. /* /\* if ( draw_fade_waveform ) *\/ */
  436. /* for ( int j = min( fw, peaks ); j--; ) */
  437. /* { */
  438. /* const float g = fade.gain( j * timeline->fpp() ); */
  439. /* pb[ j ].min *= g; */
  440. /* pb[ j ].max *= g; */
  441. /* } */
  442. Waveform::draw( X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch,
  443. pb, peaks,
  444. selected() ? fl_invert_color( _color ) : _color );
  445. }
  446. delete[] pbuf;
  447. /* for ( int i = _clip->channels(); i--; ) */
  448. /* Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, */
  449. /* ch, _clip, i, timeline->fpp(), */
  450. /* _r->start + offset, min( (_r->end - _r->start) - offset, _r->end), */
  451. /* _scale, selected() ? fl_invert_color( _color ) : _color ); */
  452. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  453. fl_color( FL_BLACK );
  454. fl_line( rx, Y, rx, Y + H );
  455. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  456. draw_label( _clip->name(), align() );
  457. if ( current() )
  458. {
  459. /* draw length bubble */
  460. char pat[40];
  461. snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
  462. draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
  463. }
  464. fl_pop_clip();
  465. }
  466. void
  467. Region::normalize ( void )
  468. {
  469. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  470. /* FIXME: figure out a way to do this via the peak server */
  471. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  472. }
  473. /**********/
  474. /* Engine */
  475. /**********/
  476. /** Apply a (portion of) fade-out from /start/ to /end/ assuming a
  477. * buffer size of /nframes/. /start/ and /end/ are relative to the
  478. * given buffer, and /start/ may be negative. */
  479. void
  480. Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  481. {
  482. printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  483. nframes_t i = start > 0 ? start : 0;
  484. nframes_t e = end > nframes ? nframes : end;
  485. if ( dir == Fade::Out )
  486. for ( ; i < e; ++i )
  487. buf[ i ] *= gain( (length - 1) - (i - start) );
  488. else
  489. for ( ; i < e; ++i )
  490. buf[ i ] *= gain( i - start );
  491. }
  492. #if 0
  493. /** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
  494. * (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
  495. * /offset/ */
  496. static inline
  497. float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
  498. {
  499. float a, b;
  500. /* FIXME: these first two sections should *definitely* be cached */
  501. /* calculate coefficients */
  502. if ( dir == FADE_OUT )
  503. {
  504. a = -1.0f / (double)nframes;
  505. /* fixme why would we need to know the clip length? */
  506. b = length / (double)nframes;
  507. // b = nframes;
  508. }
  509. else
  510. {
  511. a = 1.0f / (double)nframes;
  512. b = 0.0f;
  513. }
  514. float c[4];
  515. /* interpolate points */
  516. switch ( type )
  517. {
  518. case Linear:
  519. c[1] = a;
  520. c[0] = b;
  521. break;
  522. case Quadratic:
  523. c[2] = a * a;
  524. c[1] = 2.0f * a * b;
  525. c[0] = b * b;
  526. break;
  527. case Cubic:
  528. {
  529. const float a2 = a * a;
  530. const float b2 = b * b;
  531. c[3] = a * a2;
  532. c[2] = 3.0f * a2 * b;
  533. c[1] = 3.0f * a * b2;
  534. c[0] = b * b2;
  535. break;
  536. }
  537. default:
  538. printf( "unknown curve order\n" );
  539. }
  540. /* now get the gain for the given point */
  541. const float f = offset;
  542. const float f2 = f * f;
  543. float g = 1.0f;
  544. switch ( type )
  545. {
  546. case Linear:
  547. g *= c[1] * f + c[0];
  548. break;
  549. case Quadratic:
  550. g *= c[2] * f2 + c[1] * f + c[0];
  551. break;
  552. case Cubic:
  553. g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
  554. break;
  555. }
  556. printf( "gain for %lu is %f\n", offset, g );
  557. return g;
  558. }
  559. #endif
  560. /* THREAD: IO */
  561. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  562. this region into /buf/, where /pos/ is in timeline frames */
  563. /* this runs in the diskstream thread. */
  564. /* FIXME: it is far more efficient to read all the channels from a
  565. multichannel source at once... But how should we handle the case of a
  566. mismatch between the number of channels in this region's source and
  567. the number of channels on the track/buffer this data is being read
  568. for? Would it not be better to simply buffer and deinterlace the
  569. frames in the Audio_File class instead, so that sequential requests
  570. for different channels at the same position avoid hitting the disk
  571. again? */
  572. nframes_t
  573. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  574. {
  575. const Range r = _range;
  576. const nframes_t length = r.end - r.start;
  577. /* do nothing if we aren't covered by this frame range */
  578. if ( pos > r.offset + length || pos + nframes < r.offset )
  579. return 0;
  580. /* calculate offsets into file and sample buffer */
  581. nframes_t sofs, ofs, cnt;
  582. cnt = nframes;
  583. if ( pos < r.offset )
  584. {
  585. sofs = 0;
  586. ofs = r.offset - pos;
  587. cnt -= ofs;
  588. }
  589. else
  590. {
  591. ofs = 0;
  592. sofs = pos - r.offset;
  593. }
  594. if ( ofs >= nframes )
  595. return 0;
  596. // const nframes_t start = ofs + r.start + sofs;
  597. const nframes_t start = r.start + sofs;
  598. const nframes_t len = min( cnt, nframes - ofs );
  599. const nframes_t end = start + len;
  600. if ( len == 0 )
  601. return 0;
  602. /* now that we know how much and where to read, get on with it */
  603. /* FIXME: seeking can be very expensive. Esp. with compressed
  604. * formats. We should attempt to avoid it. But here or in the
  605. * Audio_File class? */
  606. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  607. cnt = _clip->read( buf + ofs, channel, start, end );
  608. /* apply gain */
  609. if ( _scale != 1.0f )
  610. for ( int i = cnt; i--; )
  611. buf[i] *= _scale;
  612. /* perform declicking if necessary */
  613. /* FIXME: keep the declick defults someplace else */
  614. Fade declick;
  615. declick.length = 256;
  616. declick.type = Fade::Linear;
  617. {
  618. Fade fade;
  619. fade = declick < _fade_in ? _fade_in : declick;
  620. /* do fade in if necessary */
  621. if ( sofs < fade.length )
  622. {
  623. const long d = 0 - sofs;
  624. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt - ofs );
  625. }
  626. fade = declick < _fade_out ? _fade_out : declick;
  627. /* do fade out if necessary */
  628. if ( start + cnt + fade.length > r.end )
  629. {
  630. const nframes_t d = r.end - start;
  631. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  632. }
  633. }
  634. // printf( "read %lu frames\n", cnt );
  635. return cnt;
  636. }