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.

909 lines
24KB

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