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.

945 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() )
  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 Sequence_Widget::handle( m );
  306. }
  307. break;
  308. }
  309. case FL_RELEASE:
  310. {
  311. Sequence_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 = Sequence_Widget::handle( m );
  373. return ret | 1;
  374. default:
  375. return Sequence_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. /* scale it */
  485. for ( int j = peaks * channels; j--; )
  486. {
  487. pbuf[ j ].min *= _scale;
  488. pbuf[ j ].max *= _scale;
  489. }
  490. for ( int i = 0; i < channels; ++i )
  491. {
  492. // Peak *pb = pbuf + (peaks * i);
  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. pbuf + i, peaks, channels,
  506. selected() ? fl_invert_color( _color ) : _color );
  507. }
  508. }
  509. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  510. fl_color( FL_BLACK );
  511. fl_line( rx, Y, rx, Y + H );
  512. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  513. draw_label( _clip->name(), align() );
  514. if ( current() )
  515. {
  516. /* draw length bubble */
  517. char pat[40];
  518. snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
  519. draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
  520. }
  521. fl_pop_clip();
  522. }
  523. void
  524. Region::normalize ( void )
  525. {
  526. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  527. /* FIXME: figure out a way to do this via the peak server */
  528. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  529. }
  530. /**********/
  531. /* Engine */
  532. /**********/
  533. /** Apply a (portion of) fade-out from /start/ to /end/ assuming a
  534. * buffer size of /nframes/. /start/ and /end/ are relative to the
  535. * given buffer, and /start/ may be negative. */
  536. void
  537. Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  538. {
  539. // printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  540. const nframes_t i = start > 0 ? start : 0;
  541. const nframes_t e = end > nframes ? nframes : end;
  542. const float inc = increment();
  543. float fi = ( i - start ) / (float)length;
  544. // buf += i;
  545. buf = &buf[ i ];
  546. nframes_t n = e - i;
  547. assert( i + n <= nframes );
  548. if ( dir == Fade::Out )
  549. {
  550. fi = 1.0f - fi;
  551. for ( ; n--; fi -= inc )
  552. *(buf++) *= gain( fi );
  553. }
  554. else
  555. for ( ; n--; fi += inc )
  556. *(buf++) *= gain( fi );
  557. }
  558. #if 0
  559. /** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
  560. * (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
  561. * /offset/ */
  562. static inline
  563. float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
  564. {
  565. float a, b;
  566. /* FIXME: these first two sections should *definitely* be cached */
  567. /* calculate coefficients */
  568. if ( dir == FADE_OUT )
  569. {
  570. a = -1.0f / (double)nframes;
  571. /* fixme why would we need to know the clip length? */
  572. b = length / (double)nframes;
  573. // b = nframes;
  574. }
  575. else
  576. {
  577. a = 1.0f / (double)nframes;
  578. b = 0.0f;
  579. }
  580. float c[4];
  581. /* interpolate points */
  582. switch ( type )
  583. {
  584. case Linear:
  585. c[1] = a;
  586. c[0] = b;
  587. break;
  588. case Quadratic:
  589. c[2] = a * a;
  590. c[1] = 2.0f * a * b;
  591. c[0] = b * b;
  592. break;
  593. case Cubic:
  594. {
  595. const float a2 = a * a;
  596. const float b2 = b * b;
  597. c[3] = a * a2;
  598. c[2] = 3.0f * a2 * b;
  599. c[1] = 3.0f * a * b2;
  600. c[0] = b * b2;
  601. break;
  602. }
  603. default:
  604. printf( "unknown curve order\n" );
  605. }
  606. /* now get the gain for the given point */
  607. const float f = offset;
  608. const float f2 = f * f;
  609. float g = 1.0f;
  610. switch ( type )
  611. {
  612. case Linear:
  613. g *= c[1] * f + c[0];
  614. break;
  615. case Quadratic:
  616. g *= c[2] * f2 + c[1] * f + c[0];
  617. break;
  618. case Cubic:
  619. g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
  620. break;
  621. }
  622. printf( "gain for %lu is %f\n", offset, g );
  623. return g;
  624. }
  625. #endif
  626. /* THREAD: IO */
  627. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  628. this region into /buf/, where /pos/ is in timeline frames */
  629. /* this runs in the diskstream thread. */
  630. /* FIXME: it is far more efficient to read all the channels from a
  631. multichannel source at once... But how should we handle the case of a
  632. mismatch between the number of channels in this region's source and
  633. the number of channels on the track/buffer this data is being read
  634. for? Would it not be better to simply buffer and deinterlace the
  635. frames in the Audio_File class instead, so that sequential requests
  636. for different channels at the same position avoid hitting the disk
  637. again? */
  638. nframes_t
  639. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  640. {
  641. const Range r = _range;
  642. const nframes_t length = r.end - r.start;
  643. /* do nothing if we aren't covered by this frame range */
  644. if ( pos > r.offset + length || pos + nframes < r.offset )
  645. return 0;
  646. /* calculate offsets into file and sample buffer */
  647. nframes_t sofs, ofs, cnt;
  648. cnt = nframes;
  649. if ( pos < r.offset )
  650. {
  651. sofs = 0;
  652. ofs = r.offset - pos;
  653. cnt -= ofs;
  654. }
  655. else
  656. {
  657. ofs = 0;
  658. sofs = pos - r.offset;
  659. }
  660. if ( ofs >= nframes )
  661. return 0;
  662. // const nframes_t start = ofs + r.start + sofs;
  663. const nframes_t start = r.start + sofs;
  664. const nframes_t len = min( cnt, nframes - ofs );
  665. const nframes_t end = start + len;
  666. if ( len == 0 )
  667. return 0;
  668. /* now that we know how much and where to read, get on with it */
  669. /* FIXME: seeking can be very expensive. Esp. with compressed
  670. * formats. We should attempt to avoid it. But here or in the
  671. * Audio_File class? */
  672. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  673. cnt = _clip->read( buf + ofs, channel, start, end );
  674. /* apply gain */
  675. buffer_apply_gain( buf, cnt, _scale );
  676. /* perform declicking if necessary */
  677. /* FIXME: keep the declick defults someplace else */
  678. Fade declick;
  679. declick.length = 256;
  680. declick.type = Fade::Linear;
  681. {
  682. Fade fade;
  683. fade = declick < _fade_in ? _fade_in : declick;
  684. /* do fade in if necessary */
  685. if ( sofs < fade.length )
  686. {
  687. const long d = 0 - sofs;
  688. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt - ofs );
  689. }
  690. fade = declick < _fade_out ? _fade_out : declick;
  691. /* do fade out if necessary */
  692. if ( start + cnt + fade.length > r.end )
  693. {
  694. const nframes_t d = r.end - start;
  695. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  696. }
  697. }
  698. // printf( "read %lu frames\n", cnt );
  699. return cnt;
  700. }
  701. /* THREAD: IO */
  702. /** write /nframes/ from /buf/ to source. /buf/ is interleaved and
  703. must match the channel layout of the write source! */
  704. nframes_t
  705. Region::write ( sample_t *buf, nframes_t nframes )
  706. {
  707. nframes_t l = _clip->write( buf, nframes );
  708. _range.end += l;
  709. /* FIXME: too much? */
  710. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  711. redraw();
  712. return l;
  713. }
  714. /** finalize region capture. Assumes that this *is* a captured region
  715. and that no other regions refer to the same source */
  716. bool
  717. Region::finalize ( void )
  718. {
  719. _clip->close();
  720. _clip->open();
  721. return true;
  722. }