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.

929 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 "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. redraw();
  195. return 1;
  196. }
  197. else if ( Fl::event_key() == FL_F + 4 )
  198. {
  199. long offset = length() - x_to_offset( X );
  200. if ( offset > 0 )
  201. _fade_out.length = offset;
  202. printf( "setting fade out length to %lu\n", _fade_in.length );
  203. redraw();
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. case FL_PUSH:
  209. {
  210. /* trimming / splitting */
  211. if ( Fl::event_shift() && ! Fl::event_ctrl() )
  212. {
  213. switch ( Fl::event_button() )
  214. {
  215. case 1:
  216. trim( trimming = LEFT, X );
  217. begin_drag( Drag( x() - X, y() - Y ) );
  218. _log.hold();
  219. break;
  220. case 3:
  221. trim( trimming = RIGHT, X );
  222. begin_drag( Drag( x() - X, y() - Y ) );
  223. _log.hold();
  224. break;
  225. case 2:
  226. {
  227. /* split */
  228. if ( ! copied )
  229. {
  230. Loggable::block_start();
  231. Region *copy = new Region( *this );
  232. trim( RIGHT, X );
  233. copy->trim( LEFT, X );
  234. _track->add( copy );
  235. log_end();
  236. Loggable::block_end();
  237. return 0;
  238. }
  239. }
  240. default:
  241. return 0;
  242. break;
  243. }
  244. fl_cursor( FL_CURSOR_WE );
  245. return 1;
  246. }
  247. else
  248. {
  249. ox = x() - X;
  250. oy = y() - Y;
  251. /* for panning */
  252. os = _r->start;
  253. /* normalization and selection */
  254. if ( Fl::event_button2() )
  255. {
  256. if ( Fl::event_ctrl() )
  257. normalize();
  258. else
  259. {
  260. if ( Sequence_Widget::current() == this )
  261. {
  262. if ( selected() )
  263. deselect();
  264. else
  265. select();
  266. }
  267. }
  268. redraw();
  269. goto changed;
  270. }
  271. else if ( Fl::event_button1() && Fl::event_ctrl() )
  272. {
  273. /* duplication */
  274. return 1;
  275. }
  276. else if ( Fl::event_button3() && ! Fl::event_ctrl() )
  277. {
  278. /* context menu */
  279. Fade::fade_type_e it = _fade_in.type;
  280. Fade::fade_type_e ot = _fade_out.type;
  281. Fl_Menu_Item menu[] =
  282. {
  283. { "Fade", 0, 0, 0, FL_SUBMENU },
  284. { "In", 0, 0, 0, FL_SUBMENU },
  285. { "Linear", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  286. { "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  287. { "Logarithmic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  288. { "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  289. { 0 },
  290. { "Out", 0, 0, 0, FL_SUBMENU },
  291. { "Linear", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  292. { "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  293. { "Logarothmic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  294. { "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  295. { 0 },
  296. { 0 },
  297. { 0 },
  298. };
  299. const Fl_Menu_Item *r = menu->popup( X, Y, "Region" );
  300. if ( r )
  301. {
  302. if ( r > &menu[1] && r < &menu[6] )
  303. _fade_in.type = (Fade::fade_type_e)(int)(r - &menu[2]);
  304. else if ( r > &menu[7] && r < &menu[12] )
  305. _fade_out.type = (Fade::fade_type_e)(int)(r - &menu[8]);
  306. redraw();
  307. }
  308. return 1;
  309. }
  310. else
  311. return Sequence_Widget::handle( m );
  312. }
  313. break;
  314. }
  315. case FL_RELEASE:
  316. {
  317. Sequence_Widget::handle( m );
  318. copied = false;
  319. if ( trimming != NO )
  320. trimming = NO;
  321. goto changed;
  322. }
  323. case FL_DRAG:
  324. if ( ! _drag )
  325. {
  326. begin_drag( Drag( x() - X, y() - Y ) );
  327. _log.hold();
  328. }
  329. /* panning */
  330. if ( Fl::event_state() & FL_SHIFT &&
  331. Fl::event_state() & FL_CTRL )
  332. {
  333. int d = (ox + X) - x();
  334. long td = timeline->x_to_ts( d );
  335. nframes_t W = _r->end - _r->start;
  336. if ( td > 0 && os < td )
  337. _r->start = 0;
  338. else
  339. _r->start = os - td;
  340. _r->end = _r->start + W;
  341. _track->redraw();
  342. return 1;
  343. }
  344. /* trimming */
  345. if ( Fl::event_state() & FL_SHIFT )
  346. if ( trimming )
  347. {
  348. trim( trimming, X );
  349. return 1;
  350. }
  351. else
  352. return 0;
  353. /* duplication */
  354. if ( Fl::event_state() & FL_CTRL )
  355. {
  356. if ( _drag->state == 0 )
  357. {
  358. _track->add( new Region( *this ) );
  359. _drag->state = 1;
  360. return 1;
  361. }
  362. }
  363. /* track jumping */
  364. if ( ! selected() )
  365. {
  366. if ( Y > y() + h() )
  367. {
  368. Fl::copy( class_name(), strlen( class_name() ), 0 );
  369. Fl::dnd();
  370. }
  371. else
  372. if ( Y < y() )
  373. {
  374. Fl::copy( class_name(), strlen( class_name() ), 0 );
  375. Fl::dnd();
  376. }
  377. }
  378. ret = Sequence_Widget::handle( m );
  379. return ret | 1;
  380. default:
  381. return Sequence_Widget::handle( m );
  382. break;
  383. }
  384. changed:
  385. return 1;
  386. }
  387. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  388. portion of the region covered by this draw, which may or may not
  389. cover the fade in question. */
  390. void
  391. Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
  392. {
  393. const int dy = y() + Fl::box_dy( box() );
  394. const int dh = h() - Fl::box_dh( box() );
  395. const int height = dh;
  396. const int width = timeline->ts_to_x( fade.length );
  397. fl_color( fl_lighter( FL_BLACK ) );
  398. fl_push_matrix();
  399. if ( dir == Fade::In )
  400. fl_translate( line_x(), dy );
  401. else
  402. {
  403. fl_translate( line_x() + abs_w(), dy );
  404. /* flip */
  405. fl_scale( -1.0, 1.0 );
  406. }
  407. fl_scale( width, height );
  408. if ( line )
  409. fl_begin_line();
  410. else
  411. fl_begin_polygon();
  412. fl_vertex( 0.0, 0.0 );
  413. fl_vertex( 0.0, 1.0 );
  414. // if ( draw_real_fade_curve )
  415. {
  416. nframes_t tsx = timeline->x_to_ts( 1 );
  417. nframes_t ts = 0;
  418. for ( int i = 0; i < width; ++i, ts += tsx )
  419. fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
  420. }
  421. fl_vertex( 1.0, 0.0 );
  422. if ( line )
  423. fl_end_line();
  424. else
  425. fl_end_polygon();
  426. fl_pop_matrix();
  427. }
  428. void
  429. Region::draw_box( void )
  430. {
  431. /* dirty hack to keep the box from flipping to vertical at small sizes */
  432. fl_push_clip( x(), y(), w(), h() );
  433. Fl_Color selection_color = _selection_color;
  434. Fl_Color color = Region::inherit_track_color ? track()->track()->color() : _box_color;
  435. color = fl_color_average( color, track()->color(), 0.75f );
  436. if ( this == ((Audio_Sequence*)track())->capture() )
  437. {
  438. color = FL_RED;
  439. }
  440. else if ( ! active_r() )
  441. {
  442. color = fl_inactive( color );
  443. selection_color = fl_inactive( selection_color );
  444. }
  445. if ( selected() )
  446. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), selection_color );
  447. else
  448. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), color );
  449. /* draw fades */
  450. draw_fade( _fade_in, Fade::In, false, x(), w() );
  451. draw_fade( _fade_out, Fade::Out, false, x(), w() );
  452. fl_pop_clip();
  453. }
  454. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  455. void
  456. Region::draw ( void )
  457. {
  458. /* intersect clip with region */
  459. int X, Y, W, H;
  460. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  461. if ( ! ( W > 0 && H > 0 ) )
  462. /* no coverage */
  463. return;
  464. int OX = scroll_x();
  465. int ox = timeline->ts_to_x( _r->offset );
  466. if ( ox > OX + _track->w() ||
  467. ox < OX && ox + abs_w() < OX )
  468. /* not in viewport */
  469. return;
  470. int rw = timeline->ts_to_x( _r->end - _r->start );
  471. // nframes_t end = _r->offset + ( _r->end - _r->start );
  472. /* calculate waveform offset due to scrolling */
  473. nframes_t offset = 0;
  474. if ( ox < OX )
  475. {
  476. offset = timeline->x_to_ts( OX - ox );
  477. rw -= OX - ox;
  478. }
  479. rw = min( rw, _track->w() );
  480. int rx = x();
  481. fl_push_clip( rx, Y, rw, H );
  482. /* get actual peak data */
  483. int channels;
  484. int peaks;
  485. Peak *pbuf;
  486. // const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  487. nframes_t start = _r->start + offset;
  488. /* compensate for ??? */
  489. if ( X - rx > 0 )
  490. start += timeline->x_to_ts( X - rx );
  491. if ( _clip->read_peaks( timeline->fpp(),
  492. start,
  493. start + timeline->x_to_ts( W ),
  494. &peaks, &pbuf, &channels ) )
  495. {
  496. assert( pbuf );
  497. /* draw fade curve outlines--this is only here because of crossfades */
  498. draw_fade( _fade_in, Fade::In, true, X, W );
  499. draw_fade( _fade_out, Fade::Out, true, X, W );
  500. int ch = (h() - Fl::box_dh( box() )) / channels;
  501. /* scale it */
  502. for ( int j = peaks * channels; j--; )
  503. {
  504. pbuf[ j ].min *= _scale;
  505. pbuf[ j ].max *= _scale;
  506. }
  507. for ( int i = 0; i < channels; ++i )
  508. {
  509. // Peak *pb = pbuf + (peaks * i);
  510. /* int fw = timeline->ts_to_x( fade.length ); */
  511. /* /\* if ( draw_fade_waveform ) *\/ */
  512. /* for ( int j = min( fw, peaks ); j--; ) */
  513. /* { */
  514. /* const float g = fade.gain( j * timeline->fpp() ); */
  515. /* pb[ j ].min *= g; */
  516. /* pb[ j ].max *= g; */
  517. /* } */
  518. Waveform::draw( X,
  519. (y() + Fl::box_dy( box() )) + (i * ch),
  520. W,
  521. ch,
  522. pbuf + i, peaks, channels,
  523. selected() ? fl_invert_color( _color ) : _color );
  524. }
  525. }
  526. /* FIXME: only draw as many as are necessary! */
  527. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  528. /* fl_color( FL_BLACK ); */
  529. /* fl_line( rx, Y, rx, Y + H ); */
  530. /* fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H ); */
  531. draw_label( _clip->name(), align() );
  532. /* if ( current() ) */
  533. /* { */
  534. /* /\* draw length bubble *\/ */
  535. /* char pat[40]; */
  536. /* snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() ); */
  537. /* draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN ); */
  538. /* } */
  539. fl_pop_clip();
  540. }
  541. void
  542. Region::normalize ( void )
  543. {
  544. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  545. /* FIXME: figure out a way to do this via the peak server */
  546. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  547. }
  548. /**********/
  549. /* Engine */
  550. /**********/
  551. /** Apply a (portion of) fade from /start/ to /end/ assuming a
  552. * buffer size of /nframes/. /start/ and /end/ are relative to the
  553. * given buffer, and /start/ may be negative. */
  554. void
  555. Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
  556. {
  557. // printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
  558. if ( ! nframes )
  559. return;
  560. const nframes_t i = start > 0 ? start : 0;
  561. const nframes_t e = end > nframes ? nframes : end;
  562. assert( i < nframes );
  563. const float inc = increment();
  564. float fi = ( i - start ) / (float)length;
  565. // buf += i;
  566. buf = &buf[ i ];
  567. nframes_t n = e - i;
  568. assert( i + n <= nframes );
  569. if ( dir == Fade::Out )
  570. {
  571. fi = 1.0f - fi;
  572. for ( ; n--; fi -= inc )
  573. *(buf++) *= gain( fi );
  574. }
  575. else
  576. for ( ; n--; fi += inc )
  577. *(buf++) *= gain( fi );
  578. }
  579. /* THREAD: IO */
  580. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  581. this region into /buf/, where /pos/ is in timeline frames */
  582. /* this runs in the diskstream thread. */
  583. /* FIXME: it is far more efficient to read all the channels from a
  584. multichannel source at once... But how should we handle the case of a
  585. mismatch between the number of channels in this region's source and
  586. the number of channels on the track/buffer this data is being read
  587. for? Would it not be better to simply buffer and deinterlace the
  588. frames in the Audio_File class instead, so that sequential requests
  589. for different channels at the same position avoid hitting the disk
  590. again? */
  591. nframes_t
  592. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  593. {
  594. const Range r = _range;
  595. const nframes_t length = r.end - r.start;
  596. /* do nothing if we aren't covered by this frame range */
  597. if ( pos > r.offset + length || pos + nframes < r.offset )
  598. return 0;
  599. /* calculate offsets into file and sample buffer */
  600. nframes_t sofs, ofs, cnt;
  601. cnt = nframes;
  602. if ( pos < r.offset )
  603. {
  604. sofs = 0;
  605. ofs = r.offset - pos;
  606. cnt -= ofs;
  607. }
  608. else
  609. {
  610. ofs = 0;
  611. sofs = pos - r.offset;
  612. }
  613. if ( ofs >= nframes )
  614. return 0;
  615. // const nframes_t start = ofs + r.start + sofs;
  616. const nframes_t start = r.start + sofs;
  617. const nframes_t len = min( cnt, nframes - ofs );
  618. const nframes_t end = start + len;
  619. if ( len == 0 )
  620. return 0;
  621. /* now that we know how much and where to read, get on with it */
  622. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  623. cnt = _clip->read( buf + ofs, channel, start, end );
  624. /* apply gain */
  625. buffer_apply_gain( buf + ofs, cnt, _scale );
  626. /* perform declicking if necessary */
  627. /* FIXME: keep the declick defults someplace else */
  628. Fade declick;
  629. declick.length = 256;
  630. declick.type = Fade::Linear;
  631. {
  632. Fade fade;
  633. fade = declick < _fade_in ? _fade_in : declick;
  634. /* do fade in if necessary */
  635. if ( sofs < fade.length )
  636. {
  637. const long d = 0 - sofs;
  638. assert( cnt <= nframes );
  639. fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt );
  640. }
  641. fade = declick < _fade_out ? _fade_out : declick;
  642. /* do fade out if necessary */
  643. // if ( start + cnt + fade.length > r.end )
  644. if ( start + fade.length > r.end )
  645. {
  646. const nframes_t d = r.end - start;
  647. assert( cnt <= nframes );
  648. fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
  649. }
  650. }
  651. // printf( "read %lu frames\n", cnt );
  652. return cnt;
  653. }
  654. /* THREAD: IO */
  655. /** write /nframes/ from /buf/ to source. /buf/ is interleaved and
  656. must match the channel layout of the write source! */
  657. nframes_t
  658. Region::write ( nframes_t nframes )
  659. {
  660. _range.end += nframes;
  661. /* FIXME: too much? */
  662. // _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
  663. if ( 0 == ( timeline->ts_to_x( _range.end ) % 20 ) )
  664. {
  665. nframes_t oldl = _clip->length();
  666. /* FIXME: hack */
  667. _clip->close();
  668. _clip->open();
  669. int W = timeline->ts_to_x( _clip->length() - oldl );
  670. /* why - 1? */
  671. if ( W )
  672. {
  673. ++W;
  674. track()->damage( FL_DAMAGE_EXPOSE, x() + w() - W, y(), W, h() );
  675. }
  676. }
  677. return nframes;
  678. }
  679. /** prepare for capturing */
  680. void
  681. Region::prepare ( void )
  682. {
  683. log_start();
  684. }
  685. /** finalize region capture. Assumes that this *is* a captured region
  686. and that no other regions refer to the same source */
  687. bool
  688. Region::finalize ( void )
  689. {
  690. log_end();
  691. _clip->close();
  692. _clip->open();
  693. _range.end = _clip->length();
  694. return true;
  695. }