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.

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