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