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.

831 lines
20KB

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