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.

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