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.

892 lines
23KB

  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. nframes_t tsx = timeline->x_to_ts( 1 );
  407. nframes_t ts = 0;
  408. for ( int i = 0; i < width; ++i, ts += tsx )
  409. fl_vertex( i / (float)width, 1.0f - fade.gain( ts ) );
  410. fl_vertex( 1.0, 0.0 );
  411. if ( line )
  412. fl_end_line();
  413. else
  414. fl_end_polygon();
  415. fl_pop_matrix();
  416. }
  417. void
  418. Region::draw_box( int X, int Y, int W, int H )
  419. {
  420. if ( ! shown() )
  421. return;
  422. /* dirty hack to keep the box from flipping to vertical at small sizes */
  423. fl_push_clip( x(), Y, w(), H );
  424. if ( selected() )
  425. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
  426. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  427. else
  428. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
  429. /* draw fades */
  430. draw_fade( _fade_in, Fade::In, false, X, W );
  431. draw_fade( _fade_out, Fade::Out, false, X, W );
  432. fl_pop_clip();
  433. }
  434. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  435. Y W and H are the portion of the widget to draw (arrived at by
  436. intersection of the clip and relative to OX) */
  437. void
  438. Region::draw ( int X, int Y, int W, int H )
  439. {
  440. if ( ! shown() )
  441. return;
  442. if ( ! ( W > 0 && H > 0 ) )
  443. return;
  444. int OX = scroll_x();
  445. int ox = timeline->ts_to_x( _r->offset );
  446. if ( ox > OX + _track->w() ||
  447. ox < OX && ox + abs_w() < OX )
  448. return;
  449. int rw = timeline->ts_to_x( _r->end - _r->start );
  450. // nframes_t end = _r->offset + ( _r->end - _r->start );
  451. /* calculate waveform offset due to scrolling */
  452. nframes_t offset = 0;
  453. if ( ox < OX )
  454. {
  455. offset = timeline->x_to_ts( OX - ox );
  456. rw = timeline->ts_to_x( (_r->end - _r->start) - offset );
  457. }
  458. rw = min( rw, _track->w() );
  459. int rx = x();
  460. fl_push_clip( rx, Y, rw, H );
  461. /* get actual peak data */
  462. int channels;
  463. int peaks;
  464. Peak *pbuf;
  465. const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  466. _clip->read_peaks( timeline->fpp(),
  467. start,
  468. start + timeline->x_to_ts( W ),
  469. &peaks, &pbuf, &channels );
  470. assert( pbuf );
  471. /* draw fade curve outlines--this is only here because of crossfades */
  472. draw_fade( _fade_in, Fade::In, true, X, W );
  473. draw_fade( _fade_out, Fade::Out, true, X, W );
  474. int ch = (h() - Fl::box_dh( box() )) / channels;
  475. for ( int i = 0; i < channels; ++i )
  476. {
  477. Peak *pb = pbuf + (peaks * i);
  478. /* scale it */
  479. for ( int j = peaks; j--; )
  480. {
  481. pb[ j ].min *= _scale;
  482. pb[ j ].max *= _scale;
  483. }
  484. /* int fw = timeline->ts_to_x( fade.length ); */
  485. /* /\* if ( draw_fade_waveform ) *\/ */
  486. /* for ( int j = min( fw, peaks ); j--; ) */
  487. /* { */
  488. /* const float g = fade.gain( j * timeline->fpp() ); */
  489. /* pb[ j ].min *= g; */
  490. /* pb[ j ].max *= g; */
  491. /* } */
  492. Waveform::draw( X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch,
  493. pb, peaks,
  494. selected() ? fl_invert_color( _color ) : _color );
  495. }
  496. delete[] pbuf;
  497. /* for ( int i = _clip->channels(); i--; ) */
  498. /* Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, */
  499. /* ch, _clip, i, timeline->fpp(), */
  500. /* _r->start + offset, min( (_r->end - _r->start) - offset, _r->end), */
  501. /* _scale, selected() ? fl_invert_color( _color ) : _color ); */
  502. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  503. fl_color( FL_BLACK );
  504. fl_line( rx, Y, rx, Y + H );
  505. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  506. draw_label( _clip->name(), align() );
  507. if ( current() )
  508. {
  509. /* draw length bubble */
  510. char pat[40];
  511. snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
  512. draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
  513. }
  514. fl_pop_clip();
  515. }
  516. void
  517. Region::normalize ( void )
  518. {
  519. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  520. /* FIXME: figure out a way to do this via the peak server */
  521. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  522. }
  523. /**********/
  524. /* Engine */
  525. /**********/
  526. /** Apply a (portion of) fade-out from /start/ to /end/ assuming a
  527. * buffer size of /nframes/. /start/ and /end/ are relative to the
  528. * given buffer, and /start/ may be negative. */
  529. void
  530. Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  531. {
  532. printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  533. nframes_t i = start > 0 ? start : 0;
  534. nframes_t e = end > nframes ? nframes : end;
  535. if ( dir == Fade::Out )
  536. for ( ; i < e; ++i )
  537. buf[ i ] *= gain( (length - 1) - (i - start) );
  538. else
  539. for ( ; i < e; ++i )
  540. buf[ i ] *= gain( i - start );
  541. }
  542. #if 0
  543. /** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
  544. * (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
  545. * /offset/ */
  546. static inline
  547. float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
  548. {
  549. float a, b;
  550. /* FIXME: these first two sections should *definitely* be cached */
  551. /* calculate coefficients */
  552. if ( dir == FADE_OUT )
  553. {
  554. a = -1.0f / (double)nframes;
  555. /* fixme why would we need to know the clip length? */
  556. b = length / (double)nframes;
  557. // b = nframes;
  558. }
  559. else
  560. {
  561. a = 1.0f / (double)nframes;
  562. b = 0.0f;
  563. }
  564. float c[4];
  565. /* interpolate points */
  566. switch ( type )
  567. {
  568. case Linear:
  569. c[1] = a;
  570. c[0] = b;
  571. break;
  572. case Quadratic:
  573. c[2] = a * a;
  574. c[1] = 2.0f * a * b;
  575. c[0] = b * b;
  576. break;
  577. case Cubic:
  578. {
  579. const float a2 = a * a;
  580. const float b2 = b * b;
  581. c[3] = a * a2;
  582. c[2] = 3.0f * a2 * b;
  583. c[1] = 3.0f * a * b2;
  584. c[0] = b * b2;
  585. break;
  586. }
  587. default:
  588. printf( "unknown curve order\n" );
  589. }
  590. /* now get the gain for the given point */
  591. const float f = offset;
  592. const float f2 = f * f;
  593. float g = 1.0f;
  594. switch ( type )
  595. {
  596. case Linear:
  597. g *= c[1] * f + c[0];
  598. break;
  599. case Quadratic:
  600. g *= c[2] * f2 + c[1] * f + c[0];
  601. break;
  602. case Cubic:
  603. g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
  604. break;
  605. }
  606. printf( "gain for %lu is %f\n", offset, g );
  607. return g;
  608. }
  609. #endif
  610. /* THREAD: IO */
  611. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  612. this region into /buf/, where /pos/ is in timeline frames */
  613. /* this runs in the diskstream thread. */
  614. /* FIXME: it is far more efficient to read all the channels from a
  615. multichannel source at once... But how should we handle the case of a
  616. mismatch between the number of channels in this region's source and
  617. the number of channels on the track/buffer this data is being read
  618. for? Would it not be better to simply buffer and deinterlace the
  619. frames in the Audio_File class instead, so that sequential requests
  620. for different channels at the same position avoid hitting the disk
  621. again? */
  622. nframes_t
  623. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  624. {
  625. const Range r = _range;
  626. const nframes_t length = r.end - r.start;
  627. /* do nothing if we aren't covered by this frame range */
  628. if ( pos > r.offset + length || pos + nframes < r.offset )
  629. return 0;
  630. /* calculate offsets into file and sample buffer */
  631. nframes_t sofs, ofs, cnt;
  632. cnt = nframes;
  633. if ( pos < r.offset )
  634. {
  635. sofs = 0;
  636. ofs = r.offset - pos;
  637. cnt -= ofs;
  638. }
  639. else
  640. {
  641. ofs = 0;
  642. sofs = pos - r.offset;
  643. }
  644. if ( ofs >= nframes )
  645. return 0;
  646. // const nframes_t start = ofs + r.start + sofs;
  647. const nframes_t start = r.start + sofs;
  648. const nframes_t len = min( cnt, nframes - ofs );
  649. const nframes_t end = start + len;
  650. if ( len == 0 )
  651. return 0;
  652. /* now that we know how much and where to read, get on with it */
  653. /* FIXME: seeking can be very expensive. Esp. with compressed
  654. * formats. We should attempt to avoid it. But here or in the
  655. * Audio_File class? */
  656. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  657. cnt = _clip->read( buf + ofs, channel, start, end );
  658. /* apply gain */
  659. buffer_apply_gain( buf, cnt, _scale );
  660. /* perform declicking if necessary */
  661. /* FIXME: keep the declick defults someplace else */
  662. Fade declick;
  663. declick.length = 256;
  664. declick.type = Fade::Linear;
  665. {
  666. Fade fade;
  667. fade = declick < _fade_in ? _fade_in : declick;
  668. /* do fade in if necessary */
  669. if ( sofs < fade.length )
  670. {
  671. const long d = 0 - sofs;
  672. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt - ofs );
  673. }
  674. fade = declick < _fade_out ? _fade_out : declick;
  675. /* do fade out if necessary */
  676. if ( start + cnt + fade.length > r.end )
  677. {
  678. const nframes_t d = r.end - start;
  679. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  680. }
  681. }
  682. // printf( "read %lu frames\n", cnt );
  683. return cnt;
  684. }