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.

953 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 "Sequence.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. *((Sequence_Widget*)this) = (Sequence_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. Sequence_Widget *
  84. Region::clone ( const Sequence_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, Sequence *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. Sequence_Widget::handle( m );
  178. redraw();
  179. break;
  180. case FL_LEAVE:
  181. Sequence_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 ( Sequence_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() && ! Fl::event_ctrl() )
  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. return 1;
  304. }
  305. else
  306. return Sequence_Widget::handle( m );
  307. }
  308. break;
  309. }
  310. case FL_RELEASE:
  311. {
  312. Sequence_Widget::handle( m );
  313. copied = false;
  314. if ( trimming != NO )
  315. trimming = NO;
  316. goto changed;
  317. }
  318. case FL_DRAG:
  319. if ( ! _drag )
  320. {
  321. begin_drag( Drag( x() - X, y() - Y ) );
  322. _log.hold();
  323. }
  324. /* panning */
  325. if ( Fl::event_state() & FL_SHIFT &&
  326. Fl::event_state() & FL_CTRL )
  327. {
  328. int d = (ox + X) - x();
  329. long td = timeline->x_to_ts( d );
  330. nframes_t W = _r->end - _r->start;
  331. if ( td > 0 && os < td )
  332. _r->start = 0;
  333. else
  334. _r->start = os - td;
  335. _r->end = _r->start + W;
  336. _track->redraw();
  337. return 1;
  338. }
  339. /* trimming */
  340. if ( Fl::event_state() & FL_SHIFT )
  341. if ( trimming )
  342. {
  343. trim( trimming, X );
  344. return 1;
  345. }
  346. else
  347. return 0;
  348. /* duplication */
  349. if ( Fl::event_state() & FL_CTRL )
  350. {
  351. if ( _drag->state == 0 )
  352. {
  353. _track->add( new Region( *this ) );
  354. _drag->state = 1;
  355. return 1;
  356. }
  357. }
  358. /* track jumping */
  359. if ( ! selected() )
  360. {
  361. if ( Y > y() + h() )
  362. {
  363. Fl::copy( class_name(), strlen( class_name() ), 0 );
  364. Fl::dnd();
  365. }
  366. else
  367. if ( Y < y() )
  368. {
  369. Fl::copy( class_name(), strlen( class_name() ), 0 );
  370. Fl::dnd();
  371. }
  372. }
  373. ret = Sequence_Widget::handle( m );
  374. return ret | 1;
  375. default:
  376. return Sequence_Widget::handle( m );
  377. break;
  378. }
  379. changed:
  380. return 1;
  381. }
  382. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  383. portion of the region covered by this draw, which may or may not
  384. cover the fade in question. */
  385. void
  386. Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
  387. {
  388. const int dy = y() + Fl::box_dy( box() );
  389. const int dh = h() - Fl::box_dh( box() );
  390. const int height = dh;
  391. const int width = timeline->ts_to_x( fade.length );
  392. fl_color( fl_lighter( FL_BLACK ) );
  393. fl_push_matrix();
  394. if ( dir == Fade::In )
  395. fl_translate( line_x(), dy );
  396. else
  397. {
  398. fl_translate( line_x() + abs_w(), dy );
  399. /* flip */
  400. fl_scale( -1.0, 1.0 );
  401. }
  402. fl_scale( width, height );
  403. if ( line )
  404. fl_begin_line();
  405. else
  406. fl_begin_polygon();
  407. fl_vertex( 0.0, 0.0 );
  408. fl_vertex( 0.0, 1.0 );
  409. // if ( draw_real_fade_curve )
  410. {
  411. nframes_t tsx = timeline->x_to_ts( 1 );
  412. nframes_t ts = 0;
  413. for ( int i = 0; i < width; ++i, ts += tsx )
  414. fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
  415. }
  416. fl_vertex( 1.0, 0.0 );
  417. if ( line )
  418. fl_end_line();
  419. else
  420. fl_end_polygon();
  421. fl_pop_matrix();
  422. }
  423. void
  424. Region::draw_box( void )
  425. {
  426. /* dirty hack to keep the box from flipping to vertical at small sizes */
  427. fl_push_clip( x(), y(), w(), h() );
  428. if ( selected() )
  429. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
  430. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  431. else
  432. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
  433. /* draw fades */
  434. draw_fade( _fade_in, Fade::In, false, x(), w() );
  435. draw_fade( _fade_out, Fade::Out, false, x(), w() );
  436. fl_pop_clip();
  437. }
  438. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  439. void
  440. Region::draw ( void )
  441. {
  442. /* intersect clip with region */
  443. int X, Y, W, H;
  444. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  445. if ( ! ( W > 0 && H > 0 ) )
  446. /* no coverage */
  447. return;
  448. int OX = scroll_x();
  449. int ox = timeline->ts_to_x( _r->offset );
  450. if ( ox > OX + _track->w() ||
  451. ox < OX && ox + abs_w() < OX )
  452. /* not in viewport */
  453. return;
  454. int rw = timeline->ts_to_x( _r->end - _r->start );
  455. // nframes_t end = _r->offset + ( _r->end - _r->start );
  456. /* calculate waveform offset due to scrolling */
  457. nframes_t offset = 0;
  458. if ( ox < OX )
  459. {
  460. offset = timeline->x_to_ts( OX - ox );
  461. rw -= OX - ox;
  462. }
  463. rw = min( rw, _track->w() );
  464. int rx = x();
  465. fl_push_clip( rx, Y, rw, H );
  466. /* get actual peak data */
  467. int channels;
  468. int peaks;
  469. Peak *pbuf;
  470. // const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  471. nframes_t start = _r->start + offset;
  472. /* compensate for ??? */
  473. if ( X - rx > 0 )
  474. start += timeline->x_to_ts( X - rx );
  475. if ( _clip->read_peaks( timeline->fpp(),
  476. start,
  477. start + timeline->x_to_ts( W ),
  478. &peaks, &pbuf, &channels ) )
  479. {
  480. assert( pbuf );
  481. /* draw fade curve outlines--this is only here because of crossfades */
  482. draw_fade( _fade_in, Fade::In, true, X, W );
  483. draw_fade( _fade_out, Fade::Out, true, X, W );
  484. int ch = (h() - Fl::box_dh( box() )) / channels;
  485. /* scale it */
  486. for ( int j = peaks * channels; j--; )
  487. {
  488. pbuf[ j ].min *= _scale;
  489. pbuf[ j ].max *= _scale;
  490. }
  491. for ( int i = 0; i < channels; ++i )
  492. {
  493. // Peak *pb = pbuf + (peaks * i);
  494. /* int fw = timeline->ts_to_x( fade.length ); */
  495. /* /\* if ( draw_fade_waveform ) *\/ */
  496. /* for ( int j = min( fw, peaks ); j--; ) */
  497. /* { */
  498. /* const float g = fade.gain( j * timeline->fpp() ); */
  499. /* pb[ j ].min *= g; */
  500. /* pb[ j ].max *= g; */
  501. /* } */
  502. Waveform::draw( X,
  503. (y() + Fl::box_dy( box() )) + (i * ch),
  504. W,
  505. ch,
  506. pbuf + i, peaks, channels,
  507. selected() ? fl_invert_color( _color ) : _color );
  508. }
  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 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. if ( ! nframes )
  542. return;
  543. const nframes_t i = start > 0 ? start : 0;
  544. const nframes_t e = end > nframes ? nframes : end;
  545. assert( i < nframes );
  546. const float inc = increment();
  547. float fi = ( i - start ) / (float)length;
  548. // buf += i;
  549. buf = &buf[ i ];
  550. nframes_t n = e - i;
  551. assert( i + n <= nframes );
  552. if ( dir == Fade::Out )
  553. {
  554. fi = 1.0f - fi;
  555. for ( ; n--; fi -= inc )
  556. *(buf++) *= gain( fi );
  557. }
  558. else
  559. for ( ; n--; fi += inc )
  560. *(buf++) *= gain( fi );
  561. }
  562. #if 0
  563. /** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
  564. * (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
  565. * /offset/ */
  566. static inline
  567. float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
  568. {
  569. float a, b;
  570. /* FIXME: these first two sections should *definitely* be cached */
  571. /* calculate coefficients */
  572. if ( dir == FADE_OUT )
  573. {
  574. a = -1.0f / (double)nframes;
  575. /* fixme why would we need to know the clip length? */
  576. b = length / (double)nframes;
  577. // b = nframes;
  578. }
  579. else
  580. {
  581. a = 1.0f / (double)nframes;
  582. b = 0.0f;
  583. }
  584. float c[4];
  585. /* interpolate points */
  586. switch ( type )
  587. {
  588. case Linear:
  589. c[1] = a;
  590. c[0] = b;
  591. break;
  592. case Quadratic:
  593. c[2] = a * a;
  594. c[1] = 2.0f * a * b;
  595. c[0] = b * b;
  596. break;
  597. case Cubic:
  598. {
  599. const float a2 = a * a;
  600. const float b2 = b * b;
  601. c[3] = a * a2;
  602. c[2] = 3.0f * a2 * b;
  603. c[1] = 3.0f * a * b2;
  604. c[0] = b * b2;
  605. break;
  606. }
  607. default:
  608. printf( "unknown curve order\n" );
  609. }
  610. /* now get the gain for the given point */
  611. const float f = offset;
  612. const float f2 = f * f;
  613. float g = 1.0f;
  614. switch ( type )
  615. {
  616. case Linear:
  617. g *= c[1] * f + c[0];
  618. break;
  619. case Quadratic:
  620. g *= c[2] * f2 + c[1] * f + c[0];
  621. break;
  622. case Cubic:
  623. g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
  624. break;
  625. }
  626. printf( "gain for %lu is %f\n", offset, g );
  627. return g;
  628. }
  629. #endif
  630. /* THREAD: IO */
  631. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  632. this region into /buf/, where /pos/ is in timeline frames */
  633. /* this runs in the diskstream thread. */
  634. /* FIXME: it is far more efficient to read all the channels from a
  635. multichannel source at once... But how should we handle the case of a
  636. mismatch between the number of channels in this region's source and
  637. the number of channels on the track/buffer this data is being read
  638. for? Would it not be better to simply buffer and deinterlace the
  639. frames in the Audio_File class instead, so that sequential requests
  640. for different channels at the same position avoid hitting the disk
  641. again? */
  642. nframes_t
  643. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  644. {
  645. const Range r = _range;
  646. const nframes_t length = r.end - r.start;
  647. /* do nothing if we aren't covered by this frame range */
  648. if ( pos > r.offset + length || pos + nframes < r.offset )
  649. return 0;
  650. /* calculate offsets into file and sample buffer */
  651. nframes_t sofs, ofs, cnt;
  652. cnt = nframes;
  653. if ( pos < r.offset )
  654. {
  655. sofs = 0;
  656. ofs = r.offset - pos;
  657. cnt -= ofs;
  658. }
  659. else
  660. {
  661. ofs = 0;
  662. sofs = pos - r.offset;
  663. }
  664. if ( ofs >= nframes )
  665. return 0;
  666. // const nframes_t start = ofs + r.start + sofs;
  667. const nframes_t start = r.start + sofs;
  668. const nframes_t len = min( cnt, nframes - ofs );
  669. const nframes_t end = start + len;
  670. if ( len == 0 )
  671. return 0;
  672. /* now that we know how much and where to read, get on with it */
  673. /* FIXME: seeking can be very expensive. Esp. with compressed
  674. * formats. We should attempt to avoid it. But here or in the
  675. * Audio_File class? */
  676. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  677. cnt = _clip->read( buf + ofs, channel, start, end );
  678. /* apply gain */
  679. buffer_apply_gain( buf, cnt, _scale );
  680. /* perform declicking if necessary */
  681. /* FIXME: keep the declick defults someplace else */
  682. Fade declick;
  683. declick.length = 256;
  684. declick.type = Fade::Linear;
  685. {
  686. Fade fade;
  687. fade = declick < _fade_in ? _fade_in : declick;
  688. /* do fade in if necessary */
  689. if ( sofs < fade.length )
  690. {
  691. const long d = 0 - sofs;
  692. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt - ofs );
  693. }
  694. fade = declick < _fade_out ? _fade_out : declick;
  695. /* do fade out if necessary */
  696. // if ( start + cnt + fade.length > r.end )
  697. if ( start + fade.length > r.end )
  698. {
  699. const nframes_t d = r.end - start;
  700. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  701. }
  702. }
  703. // printf( "read %lu frames\n", cnt );
  704. return cnt;
  705. }
  706. /* THREAD: IO */
  707. /** write /nframes/ from /buf/ to source. /buf/ is interleaved and
  708. must match the channel layout of the write source! */
  709. nframes_t
  710. Region::write ( sample_t *buf, nframes_t nframes )
  711. {
  712. nframes_t l = _clip->write( buf, nframes );
  713. _range.end += l;
  714. /* FIXME: too much? */
  715. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  716. redraw();
  717. return l;
  718. }
  719. /** finalize region capture. Assumes that this *is* a captured region
  720. and that no other regions refer to the same source */
  721. bool
  722. Region::finalize ( void )
  723. {
  724. _clip->close();
  725. _clip->open();
  726. return true;
  727. }