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.

861 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. /* convert a screen x coord into an offset into the region */
  156. #define x_to_offset( X ) ( timeline->x_to_ts( scroll_x() + ( (X) - _track->x() ) ) - _r->offset )
  157. int
  158. Region::handle ( int m )
  159. {
  160. static int ox, oy;
  161. static enum trim_e trimming;
  162. static bool copied = false;
  163. static nframes_t os;
  164. // int X = Fl::event_x() - _track->x();
  165. int X = Fl::event_x();
  166. int Y = Fl::event_y();
  167. int ret;
  168. Logger _log( this );
  169. //log_r->start();
  170. switch ( m )
  171. {
  172. case FL_ENTER:
  173. Track_Widget::handle( m );
  174. redraw();
  175. break;
  176. case FL_LEAVE:
  177. Track_Widget::handle( m );
  178. redraw();
  179. break;
  180. case FL_KEYBOARD:
  181. {
  182. if ( Fl::event_key() == FL_F + 3 )
  183. {
  184. nframes_t offset = x_to_offset( X );
  185. if ( offset < length() )
  186. _fade_in.length = offset;
  187. printf( "setting fade in length to %lu\n", _fade_in.length );
  188. }
  189. else
  190. if ( Fl::event_key() == FL_F + 4 )
  191. {
  192. long offset = length() - x_to_offset( X );
  193. if ( offset > 0 )
  194. _fade_out.length = offset;
  195. printf( "setting fade out length to %lu\n", _fade_in.length );
  196. }
  197. redraw();
  198. return 1;
  199. }
  200. case FL_PUSH:
  201. {
  202. /* trimming / splitting */
  203. if ( Fl::event_shift() && ! Fl::event_ctrl() )
  204. {
  205. switch ( Fl::event_button() )
  206. {
  207. case 1:
  208. trim( trimming = LEFT, X );
  209. begin_drag( Drag( x() - X, y() - Y ) );
  210. _log.hold();
  211. break;
  212. case 3:
  213. trim( trimming = RIGHT, X );
  214. begin_drag( Drag( x() - X, y() - Y ) );
  215. _log.hold();
  216. break;
  217. case 2:
  218. {
  219. /* split */
  220. if ( ! copied )
  221. {
  222. Loggable::block_start();
  223. Region *copy = new Region( *this );
  224. trim( RIGHT, X );
  225. copy->trim( LEFT, X );
  226. _track->add( copy );
  227. log_end();
  228. Loggable::block_end();
  229. return 0;
  230. }
  231. }
  232. default:
  233. return 0;
  234. break;
  235. }
  236. fl_cursor( FL_CURSOR_WE );
  237. return 1;
  238. }
  239. else
  240. {
  241. ox = x() - X;
  242. oy = y() - Y;
  243. /* for panning */
  244. os = _r->start;
  245. /* normalization and selection */
  246. if ( Fl::event_button2() )
  247. {
  248. if ( Fl::event_ctrl() )
  249. normalize();
  250. else
  251. {
  252. if ( Track_Widget::current() == this )
  253. {
  254. if ( selected() )
  255. deselect();
  256. else
  257. select();
  258. }
  259. }
  260. redraw();
  261. goto changed;
  262. }
  263. if ( Fl::event_button1() && Fl::event_ctrl() )
  264. {
  265. /* duplication */
  266. return 1;
  267. }
  268. else
  269. return Track_Widget::handle( m );
  270. }
  271. break;
  272. }
  273. case FL_RELEASE:
  274. {
  275. Track_Widget::handle( m );
  276. copied = false;
  277. if ( trimming != NO )
  278. trimming = NO;
  279. goto changed;
  280. }
  281. case FL_DRAG:
  282. if ( ! _drag )
  283. {
  284. begin_drag( Drag( x() - X, y() - Y ) );
  285. _log.hold();
  286. }
  287. /* panning */
  288. if ( Fl::event_state() & FL_SHIFT &&
  289. Fl::event_state() & FL_CTRL )
  290. {
  291. int d = (ox + X) - x();
  292. long td = timeline->x_to_ts( d );
  293. nframes_t W = _r->end - _r->start;
  294. if ( td > 0 && os < td )
  295. _r->start = 0;
  296. else
  297. _r->start = os - td;
  298. _r->end = _r->start + W;
  299. _track->redraw();
  300. return 1;
  301. }
  302. /* trimming */
  303. if ( Fl::event_state() & FL_SHIFT )
  304. if ( trimming )
  305. {
  306. trim( trimming, X );
  307. return 1;
  308. }
  309. else
  310. return 0;
  311. /* duplication */
  312. if ( Fl::event_state() & FL_CTRL )
  313. {
  314. if ( _drag->state == 0 )
  315. {
  316. _track->add( new Region( *this ) );
  317. _drag->state = 1;
  318. return 1;
  319. }
  320. }
  321. /* track jumping */
  322. if ( ! selected() )
  323. {
  324. if ( Y > y() + h() )
  325. {
  326. Fl::copy( class_name(), strlen( class_name() ), 0 );
  327. Fl::dnd();
  328. }
  329. else
  330. if ( Y < y() )
  331. {
  332. Fl::copy( class_name(), strlen( class_name() ), 0 );
  333. Fl::dnd();
  334. }
  335. }
  336. ret = Track_Widget::handle( m );
  337. return ret | 1;
  338. default:
  339. return Track_Widget::handle( m );
  340. break;
  341. }
  342. changed:
  343. return 1;
  344. }
  345. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  346. portion of the region covered by this draw, which may or may not
  347. cover the fade in question. */
  348. void
  349. Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W )
  350. {
  351. const int dy = y() + Fl::box_dy( box() );
  352. const int dh = h() - Fl::box_dh( box() );
  353. const int height = dh;
  354. const int width = timeline->ts_to_x( fade.length );
  355. fl_color( fl_lighter( FL_BLACK ) );
  356. fl_push_matrix();
  357. if ( dir == Fade::In )
  358. {
  359. fl_translate( line_x(), dy );
  360. fl_scale( width, height );
  361. }
  362. else
  363. {
  364. // fl_translate( line_x() + abs_w(), dy + height );
  365. fl_translate( line_x() + abs_w(), dy );
  366. fl_scale( width, height );
  367. /* flip */
  368. fl_scale( -1.0, 1.0 );
  369. // fl_scale( 1.0, -1.0 );
  370. }
  371. if ( filled )
  372. fl_begin_polygon();
  373. else
  374. fl_begin_line();
  375. fl_vertex( 0.0, 0.0 );
  376. fl_vertex( 0.0, 1.0 );
  377. for ( int i = 0; i < width; ++i )
  378. {
  379. const float x = i / (float)width;
  380. const float y = 1.0f - fade.gain( timeline->x_to_ts( i ) );
  381. fl_vertex( x, y );
  382. }
  383. if ( filled )
  384. fl_end_polygon();
  385. else
  386. fl_end_line();
  387. fl_pop_matrix();
  388. }
  389. void
  390. Region::draw_box( int X, int Y, int W, int H )
  391. {
  392. if ( ! shown() )
  393. return;
  394. /* dirty hack to keep the box from flipping to vertical at small sizes */
  395. fl_push_clip( x(), Y, w(), H );
  396. if ( selected() )
  397. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
  398. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  399. else
  400. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
  401. /* draw fades */
  402. draw_fade( _fade_in, Fade::In, true, X, W );
  403. draw_fade( _fade_out, Fade::Out, true, X, W );
  404. fl_pop_clip();
  405. }
  406. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  407. Y W and H are the portion of the widget to draw (arrived at by
  408. intersection of the clip and relative to OX) */
  409. void
  410. Region::draw ( int X, int Y, int W, int H )
  411. {
  412. if ( ! shown() )
  413. return;
  414. if ( ! ( W > 0 && H > 0 ) )
  415. return;
  416. int OX = scroll_x();
  417. int ox = timeline->ts_to_x( _r->offset );
  418. if ( ox > OX + _track->w() ||
  419. ox < OX && ox + abs_w() < OX )
  420. return;
  421. int rw = timeline->ts_to_x( _r->end - _r->start );
  422. // nframes_t end = _r->offset + ( _r->end - _r->start );
  423. /* calculate waveform offset due to scrolling */
  424. nframes_t offset = 0;
  425. if ( ox < OX )
  426. {
  427. offset = timeline->x_to_ts( OX - ox );
  428. rw = timeline->ts_to_x( (_r->end - _r->start) - offset );
  429. }
  430. rw = min( rw, _track->w() );
  431. int rx = x();
  432. fl_push_clip( rx, Y, rw, H );
  433. /* get actual peak data */
  434. int channels;
  435. int peaks;
  436. Peak *pbuf;
  437. const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  438. _clip->read_peaks( timeline->fpp(),
  439. start,
  440. start + timeline->x_to_ts( W ),
  441. &peaks, &pbuf, &channels );
  442. assert( pbuf );
  443. /* draw fade curve outlines--this is only here because of crossfades */
  444. draw_fade( _fade_in, Fade::In, false, X, W );
  445. draw_fade( _fade_out, Fade::Out, false, X, W );
  446. int ch = (h() - Fl::box_dh( box() )) / channels;
  447. for ( int i = 0; i < channels; ++i )
  448. {
  449. Peak *pb = pbuf + (peaks * i);
  450. /* scale it */
  451. for ( int j = peaks; j--; )
  452. {
  453. pb[ j ].min *= _scale;
  454. pb[ j ].max *= _scale;
  455. }
  456. /* int fw = timeline->ts_to_x( fade.length ); */
  457. /* /\* if ( draw_fade_waveform ) *\/ */
  458. /* for ( int j = min( fw, peaks ); j--; ) */
  459. /* { */
  460. /* const float g = fade.gain( j * timeline->fpp() ); */
  461. /* pb[ j ].min *= g; */
  462. /* pb[ j ].max *= g; */
  463. /* } */
  464. Waveform::draw( X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch,
  465. pb, peaks,
  466. selected() ? fl_invert_color( _color ) : _color );
  467. }
  468. delete[] pbuf;
  469. /* for ( int i = _clip->channels(); i--; ) */
  470. /* Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, */
  471. /* ch, _clip, i, timeline->fpp(), */
  472. /* _r->start + offset, min( (_r->end - _r->start) - offset, _r->end), */
  473. /* _scale, selected() ? fl_invert_color( _color ) : _color ); */
  474. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  475. fl_color( FL_BLACK );
  476. fl_line( rx, Y, rx, Y + H );
  477. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  478. draw_label( _clip->name(), align() );
  479. if ( current() )
  480. {
  481. /* draw length bubble */
  482. char pat[40];
  483. snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
  484. draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
  485. }
  486. fl_pop_clip();
  487. }
  488. void
  489. Region::normalize ( void )
  490. {
  491. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  492. /* FIXME: figure out a way to do this via the peak server */
  493. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  494. }
  495. /**********/
  496. /* Engine */
  497. /**********/
  498. /** Apply a (portion of) fade-out from /start/ to /end/ assuming a
  499. * buffer size of /nframes/. /start/ and /end/ are relative to the
  500. * given buffer, and /start/ may be negative. */
  501. void
  502. Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  503. {
  504. printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  505. nframes_t i = start > 0 ? start : 0;
  506. nframes_t e = end > nframes ? nframes : end;
  507. if ( dir == Fade::Out )
  508. for ( ; i < e; ++i )
  509. buf[ i ] *= gain( (length - 1) - (i - start) );
  510. else
  511. for ( ; i < e; ++i )
  512. buf[ i ] *= gain( i - start );
  513. }
  514. #if 0
  515. /** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
  516. * (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
  517. * /offset/ */
  518. static inline
  519. float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
  520. {
  521. float a, b;
  522. /* FIXME: these first two sections should *definitely* be cached */
  523. /* calculate coefficients */
  524. if ( dir == FADE_OUT )
  525. {
  526. a = -1.0f / (double)nframes;
  527. /* fixme why would we need to know the clip length? */
  528. b = length / (double)nframes;
  529. // b = nframes;
  530. }
  531. else
  532. {
  533. a = 1.0f / (double)nframes;
  534. b = 0.0f;
  535. }
  536. float c[4];
  537. /* interpolate points */
  538. switch ( type )
  539. {
  540. case Linear:
  541. c[1] = a;
  542. c[0] = b;
  543. break;
  544. case Quadratic:
  545. c[2] = a * a;
  546. c[1] = 2.0f * a * b;
  547. c[0] = b * b;
  548. break;
  549. case Cubic:
  550. {
  551. const float a2 = a * a;
  552. const float b2 = b * b;
  553. c[3] = a * a2;
  554. c[2] = 3.0f * a2 * b;
  555. c[1] = 3.0f * a * b2;
  556. c[0] = b * b2;
  557. break;
  558. }
  559. default:
  560. printf( "unknown curve order\n" );
  561. }
  562. /* now get the gain for the given point */
  563. const float f = offset;
  564. const float f2 = f * f;
  565. float g = 1.0f;
  566. switch ( type )
  567. {
  568. case Linear:
  569. g *= c[1] * f + c[0];
  570. break;
  571. case Quadratic:
  572. g *= c[2] * f2 + c[1] * f + c[0];
  573. break;
  574. case Cubic:
  575. g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
  576. break;
  577. }
  578. printf( "gain for %lu is %f\n", offset, g );
  579. return g;
  580. }
  581. #endif
  582. /* THREAD: IO */
  583. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  584. this region into /buf/, where /pos/ is in timeline frames */
  585. /* this runs in the diskstream thread. */
  586. /* FIXME: it is far more efficient to read all the channels from a
  587. multichannel source at once... But how should we handle the case of a
  588. mismatch between the number of channels in this region's source and
  589. the number of channels on the track/buffer this data is being read
  590. for? Would it not be better to simply buffer and deinterlace the
  591. frames in the Audio_File class instead, so that sequential requests
  592. for different channels at the same position avoid hitting the disk
  593. again? */
  594. nframes_t
  595. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  596. {
  597. const Range r = _range;
  598. const nframes_t length = r.end - r.start;
  599. /* do nothing if we aren't covered by this frame range */
  600. if ( pos > r.offset + length || pos + nframes < r.offset )
  601. return 0;
  602. /* calculate offsets into file and sample buffer */
  603. nframes_t sofs, ofs, cnt;
  604. cnt = nframes;
  605. if ( pos < r.offset )
  606. {
  607. sofs = 0;
  608. ofs = r.offset - pos;
  609. cnt -= ofs;
  610. }
  611. else
  612. {
  613. ofs = 0;
  614. sofs = pos - r.offset;
  615. }
  616. if ( ofs >= nframes )
  617. return 0;
  618. // const nframes_t start = ofs + r.start + sofs;
  619. const nframes_t start = r.start + sofs;
  620. const nframes_t len = min( cnt, nframes - ofs );
  621. const nframes_t end = start + len;
  622. if ( len == 0 )
  623. return 0;
  624. /* now that we know how much and where to read, get on with it */
  625. /* FIXME: seeking can be very expensive. Esp. with compressed
  626. * formats. We should attempt to avoid it. But here or in the
  627. * Audio_File class? */
  628. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  629. cnt = _clip->read( buf + ofs, channel, start, end );
  630. /* apply gain */
  631. if ( _scale != 1.0f )
  632. for ( int i = cnt; i--; )
  633. buf[i] *= _scale;
  634. /* perform declicking if necessary */
  635. /* FIXME: keep the declick defults someplace else */
  636. Fade declick;
  637. declick.length = 256;
  638. declick.type = Fade::Linear;
  639. {
  640. Fade fade;
  641. fade = declick < _fade_in ? _fade_in : declick;
  642. /* do fade in if necessary */
  643. if ( sofs < fade.length )
  644. {
  645. const long d = 0 - sofs;
  646. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt - ofs );
  647. }
  648. fade = declick < _fade_out ? _fade_out : declick;
  649. /* do fade out if necessary */
  650. if ( start + cnt + fade.length > r.end )
  651. {
  652. const nframes_t d = r.end - start;
  653. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  654. }
  655. }
  656. // printf( "read %lu frames\n", cnt );
  657. return cnt;
  658. }