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.

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