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.

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