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.

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