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.

986 lines
25KB

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