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.

869 lines
23KB

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