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. color = fl_color_average( color, track()->color(), 0.75f );
  434. if ( this == ((Audio_Sequence*)track())->capture() )
  435. {
  436. color = FL_RED;
  437. }
  438. else if ( ! active_r() )
  439. {
  440. color = fl_inactive( color );
  441. selection_color = fl_inactive( selection_color );
  442. }
  443. if ( selected() )
  444. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), selection_color );
  445. else
  446. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), color );
  447. /* draw fades */
  448. draw_fade( _fade_in, Fade::In, false, x(), w() );
  449. draw_fade( _fade_out, Fade::Out, false, x(), w() );
  450. fl_pop_clip();
  451. }
  452. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  453. void
  454. Region::draw ( void )
  455. {
  456. /* intersect clip with region */
  457. int X, Y, W, H;
  458. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  459. if ( ! ( W > 0 && H > 0 ) )
  460. /* no coverage */
  461. return;
  462. int OX = scroll_x();
  463. int ox = timeline->ts_to_x( _r->offset );
  464. if ( ox > OX + _track->w() ||
  465. ox < OX && ox + abs_w() < OX )
  466. /* not in viewport */
  467. return;
  468. int rw = timeline->ts_to_x( _r->end - _r->start );
  469. // nframes_t end = _r->offset + ( _r->end - _r->start );
  470. /* calculate waveform offset due to scrolling */
  471. nframes_t offset = 0;
  472. if ( ox < OX )
  473. {
  474. offset = timeline->x_to_ts( OX - ox );
  475. rw -= OX - ox;
  476. }
  477. rw = min( rw, _track->w() );
  478. int rx = x();
  479. fl_push_clip( rx, Y, rw, H );
  480. /* get actual peak data */
  481. int channels;
  482. int peaks;
  483. Peak *pbuf;
  484. // const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  485. nframes_t start = _r->start + offset;
  486. /* compensate for ??? */
  487. if ( X - rx > 0 )
  488. start += timeline->x_to_ts( X - rx );
  489. if ( _clip->read_peaks( timeline->fpp(),
  490. start,
  491. start + timeline->x_to_ts( W ),
  492. &peaks, &pbuf, &channels ) )
  493. {
  494. assert( pbuf );
  495. /* draw fade curve outlines--this is only here because of crossfades */
  496. draw_fade( _fade_in, Fade::In, true, X, W );
  497. draw_fade( _fade_out, Fade::Out, true, X, W );
  498. int ch = (h() - Fl::box_dh( box() )) / channels;
  499. /* scale it */
  500. for ( int j = peaks * channels; j--; )
  501. {
  502. pbuf[ j ].min *= _scale;
  503. pbuf[ j ].max *= _scale;
  504. }
  505. for ( int i = 0; i < channels; ++i )
  506. {
  507. // Peak *pb = pbuf + (peaks * i);
  508. /* int fw = timeline->ts_to_x( fade.length ); */
  509. /* /\* if ( draw_fade_waveform ) *\/ */
  510. /* for ( int j = min( fw, peaks ); j--; ) */
  511. /* { */
  512. /* const float g = fade.gain( j * timeline->fpp() ); */
  513. /* pb[ j ].min *= g; */
  514. /* pb[ j ].max *= g; */
  515. /* } */
  516. Waveform::draw( X,
  517. (y() + Fl::box_dy( box() )) + (i * ch),
  518. W,
  519. ch,
  520. pbuf + i, peaks, channels,
  521. selected() ? fl_invert_color( _color ) : _color );
  522. }
  523. }
  524. /* FIXME: only draw as many as are necessary! */
  525. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  526. /* fl_color( FL_BLACK ); */
  527. /* fl_line( rx, Y, rx, Y + H ); */
  528. /* fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H ); */
  529. draw_label( _clip->name(), align() );
  530. /* if ( current() ) */
  531. /* { */
  532. /* /\* draw length bubble *\/ */
  533. /* char pat[40]; */
  534. /* snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() ); */
  535. /* draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN ); */
  536. /* } */
  537. fl_pop_clip();
  538. }
  539. void
  540. Region::normalize ( void )
  541. {
  542. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  543. /* FIXME: figure out a way to do this via the peak server */
  544. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  545. }
  546. /**********/
  547. /* Engine */
  548. /**********/
  549. /** Apply a (portion of) fade from /start/ to /end/ assuming a
  550. * buffer size of /nframes/. /start/ and /end/ are relative to the
  551. * given buffer, and /start/ may be negative. */
  552. void
  553. Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  554. {
  555. // printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  556. if ( ! nframes )
  557. return;
  558. const nframes_t i = start > 0 ? start : 0;
  559. const nframes_t e = end > nframes ? nframes : end;
  560. assert( i < nframes );
  561. const float inc = increment();
  562. float fi = ( i - start ) / (float)length;
  563. // buf += i;
  564. buf = &buf[ i ];
  565. nframes_t n = e - i;
  566. assert( i + n <= nframes );
  567. if ( dir == Fade::Out )
  568. {
  569. fi = 1.0f - fi;
  570. for ( ; n--; fi -= inc )
  571. *(buf++) *= gain( fi );
  572. }
  573. else
  574. for ( ; n--; fi += inc )
  575. *(buf++) *= gain( fi );
  576. }
  577. #if 0
  578. /** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
  579. * (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
  580. * /offset/ */
  581. static inline
  582. float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
  583. {
  584. float a, b;
  585. /* FIXME: these first two sections should *definitely* be cached */
  586. /* calculate coefficients */
  587. if ( dir == FADE_OUT )
  588. {
  589. a = -1.0f / (double)nframes;
  590. /* fixme why would we need to know the clip length? */
  591. b = length / (double)nframes;
  592. // b = nframes;
  593. }
  594. else
  595. {
  596. a = 1.0f / (double)nframes;
  597. b = 0.0f;
  598. }
  599. float c[4];
  600. /* interpolate points */
  601. switch ( type )
  602. {
  603. case Linear:
  604. c[1] = a;
  605. c[0] = b;
  606. break;
  607. case Quadratic:
  608. c[2] = a * a;
  609. c[1] = 2.0f * a * b;
  610. c[0] = b * b;
  611. break;
  612. case Cubic:
  613. {
  614. const float a2 = a * a;
  615. const float b2 = b * b;
  616. c[3] = a * a2;
  617. c[2] = 3.0f * a2 * b;
  618. c[1] = 3.0f * a * b2;
  619. c[0] = b * b2;
  620. break;
  621. }
  622. default:
  623. printf( "unknown curve order\n" );
  624. }
  625. /* now get the gain for the given point */
  626. const float f = offset;
  627. const float f2 = f * f;
  628. float g = 1.0f;
  629. switch ( type )
  630. {
  631. case Linear:
  632. g *= c[1] * f + c[0];
  633. break;
  634. case Quadratic:
  635. g *= c[2] * f2 + c[1] * f + c[0];
  636. break;
  637. case Cubic:
  638. g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
  639. break;
  640. }
  641. printf( "gain for %lu is %f\n", offset, g );
  642. return g;
  643. }
  644. #endif
  645. /* THREAD: IO */
  646. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  647. this region into /buf/, where /pos/ is in timeline frames */
  648. /* this runs in the diskstream thread. */
  649. /* FIXME: it is far more efficient to read all the channels from a
  650. multichannel source at once... But how should we handle the case of a
  651. mismatch between the number of channels in this region's source and
  652. the number of channels on the track/buffer this data is being read
  653. for? Would it not be better to simply buffer and deinterlace the
  654. frames in the Audio_File class instead, so that sequential requests
  655. for different channels at the same position avoid hitting the disk
  656. again? */
  657. nframes_t
  658. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  659. {
  660. const Range r = _range;
  661. const nframes_t length = r.end - r.start;
  662. /* do nothing if we aren't covered by this frame range */
  663. if ( pos > r.offset + length || pos + nframes < r.offset )
  664. return 0;
  665. /* calculate offsets into file and sample buffer */
  666. nframes_t sofs, ofs, cnt;
  667. cnt = nframes;
  668. if ( pos < r.offset )
  669. {
  670. sofs = 0;
  671. ofs = r.offset - pos;
  672. cnt -= ofs;
  673. }
  674. else
  675. {
  676. ofs = 0;
  677. sofs = pos - r.offset;
  678. }
  679. if ( ofs >= nframes )
  680. return 0;
  681. // const nframes_t start = ofs + r.start + sofs;
  682. const nframes_t start = r.start + sofs;
  683. const nframes_t len = min( cnt, nframes - ofs );
  684. const nframes_t end = start + len;
  685. if ( len == 0 )
  686. return 0;
  687. /* now that we know how much and where to read, get on with it */
  688. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  689. cnt = _clip->read( buf + ofs, channel, start, end );
  690. /* apply gain */
  691. buffer_apply_gain( buf + ofs, cnt, _scale );
  692. /* perform declicking if necessary */
  693. /* FIXME: keep the declick defults someplace else */
  694. Fade declick;
  695. declick.length = 256;
  696. declick.type = Fade::Linear;
  697. {
  698. Fade fade;
  699. fade = declick < _fade_in ? _fade_in : declick;
  700. /* do fade in if necessary */
  701. if ( sofs < fade.length )
  702. {
  703. const long d = 0 - sofs;
  704. assert( cnt <= nframes );
  705. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt );
  706. }
  707. fade = declick < _fade_out ? _fade_out : declick;
  708. /* do fade out if necessary */
  709. // if ( start + cnt + fade.length > r.end )
  710. if ( start + fade.length > r.end )
  711. {
  712. const nframes_t d = r.end - start;
  713. assert( cnt <= nframes );
  714. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  715. }
  716. }
  717. // printf( "read %lu frames\n", cnt );
  718. return cnt;
  719. }
  720. /* THREAD: IO */
  721. /** write /nframes/ from /buf/ to source. /buf/ is interleaved and
  722. must match the channel layout of the write source! */
  723. nframes_t
  724. Region::write ( nframes_t nframes )
  725. {
  726. _range.end += nframes;
  727. /* FIXME: too much? */
  728. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  729. if ( 0 == ( timeline->ts_to_x( _range.end ) % 20 ) )
  730. {
  731. /* FIXME: hack to get new size */
  732. _clip->close();
  733. _clip->open();
  734. redraw();
  735. }
  736. return nframes;
  737. }
  738. /** prepare for capturing */
  739. void
  740. Region::prepare ( void )
  741. {
  742. log_start();
  743. }
  744. /** finalize region capture. Assumes that this *is* a captured region
  745. and that no other regions refer to the same source */
  746. bool
  747. Region::finalize ( void )
  748. {
  749. log_end();
  750. _clip->close();
  751. _clip->open();
  752. return true;
  753. }