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.

604 lines
15KB

  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. _r->offset = rhs._r->offset;
  72. _track = rhs._track;
  73. // _track = NULL;
  74. _clip = rhs._clip;
  75. _r->start = rhs._r->start;
  76. _r->end = rhs._r->end;
  77. _scale = rhs._scale;
  78. _box_color = rhs._box_color;
  79. _color = rhs._color;
  80. log_create();
  81. }
  82. Track_Widget *
  83. Region::clone ( const Track_Widget *r )
  84. {
  85. return new Region( *(Region*)r );
  86. }
  87. /* */
  88. Region::Region ( Audio_File *c )
  89. {
  90. init();
  91. _clip = c;
  92. _r->end = _clip->length();
  93. log_create();
  94. }
  95. /* used when DND importing */
  96. Region::Region ( Audio_File *c, Track *t, nframes_t o )
  97. {
  98. init();
  99. _clip = c;
  100. _r->end = _clip->length();
  101. _track = t;
  102. _r->offset = o;
  103. _track->add( this );
  104. int sum = 0;
  105. const char *s = rindex( _clip->name(), '/' );
  106. for ( int i = strlen( s ); i--; )
  107. sum += s[ i ];
  108. while ( sum >> 8 )
  109. sum = (sum & 0xFF) + (sum >> 8);
  110. _color = (Fl_Color)sum;
  111. /* _color = fl_color_average( FL_YELLOW, (Fl_Color)sum, 0.80 ); */
  112. // _color = FL_YELLOW;
  113. _box_color = FL_WHITE;
  114. log_create();
  115. }
  116. void
  117. Region::trim ( enum trim_e t, int X )
  118. {
  119. X -= _track->x();
  120. redraw();
  121. switch ( t )
  122. {
  123. case LEFT:
  124. {
  125. /* if ( d < 0 ) */
  126. /* // _track->damage( FL_DAMAGE_EXPOSE, x() + d, y(), 1 - d, h() ); */
  127. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() ); */
  128. /* else */
  129. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), d, h() ); */
  130. int d = X - ( abs_x() - scroll_x() );
  131. long td = timeline->x_to_ts( d );
  132. if ( td < 0 && _r->start < 0 - td )
  133. td = 0 - _r->start;
  134. if ( _r->start + td >= _r->end )
  135. td = (_r->end - _r->start) - timeline->x_to_ts( 1 );
  136. _r->start += td;
  137. _r->offset += td;
  138. break;
  139. }
  140. case RIGHT:
  141. {
  142. int d = (( abs_x() - scroll_x() ) + abs_w() ) - X;
  143. /* _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), d, h() ); */
  144. long td = timeline->x_to_ts( d );
  145. // printf( "%li %li\n", td, _r->end - _r->start );
  146. if ( td >= 0 && _r->end - _r->start < td )
  147. _r->end = _r->start + timeline->x_to_ts( 1 );
  148. else
  149. _r->end -= td;
  150. break;
  151. }
  152. default:
  153. return;
  154. }
  155. }
  156. int
  157. Region::handle ( int m )
  158. {
  159. static int ox, oy;
  160. static enum trim_e trimming;
  161. static bool copied = false;
  162. static nframes_t os;
  163. // int X = Fl::event_x() - _track->x();
  164. int X = Fl::event_x();
  165. int Y = Fl::event_y();
  166. int ret;
  167. Logger _log( this );
  168. //log_r->start();
  169. switch ( m )
  170. {
  171. case FL_ENTER:
  172. Track_Widget::handle( m );
  173. redraw();
  174. break;
  175. case FL_LEAVE:
  176. Track_Widget::handle( m );
  177. redraw();
  178. break;
  179. case FL_PUSH:
  180. {
  181. /* trimming / splitting */
  182. if ( Fl::event_shift() && ! Fl::event_ctrl() )
  183. {
  184. switch ( Fl::event_button() )
  185. {
  186. case 1:
  187. trim( trimming = LEFT, X );
  188. begin_drag( Drag( x() - X, y() - Y ) );
  189. _log.hold();
  190. break;
  191. case 3:
  192. trim( trimming = RIGHT, X );
  193. begin_drag( Drag( x() - X, y() - Y ) );
  194. _log.hold();
  195. break;
  196. case 2:
  197. {
  198. /* split */
  199. if ( ! copied )
  200. {
  201. Loggable::block_start();
  202. Region *copy = new Region( *this );
  203. trim( RIGHT, X );
  204. copy->trim( LEFT, X );
  205. _track->add( copy );
  206. log_end();
  207. Loggable::block_end();
  208. return 0;
  209. }
  210. }
  211. default:
  212. return 0;
  213. break;
  214. }
  215. fl_cursor( FL_CURSOR_WE );
  216. return 1;
  217. }
  218. else
  219. {
  220. ox = x() - X;
  221. oy = y() - Y;
  222. /* for panning */
  223. os = _r->start;
  224. /* normalization and selection */
  225. if ( Fl::event_button2() )
  226. {
  227. if ( Fl::event_ctrl() )
  228. normalize();
  229. else
  230. {
  231. if ( Track_Widget::current() == this )
  232. {
  233. if ( selected() )
  234. deselect();
  235. else
  236. select();
  237. }
  238. }
  239. redraw();
  240. goto changed;
  241. }
  242. if ( Fl::event_button1() && Fl::event_ctrl() )
  243. {
  244. /* duplication */
  245. return 1;
  246. }
  247. else
  248. return Track_Widget::handle( m );
  249. }
  250. break;
  251. }
  252. case FL_RELEASE:
  253. {
  254. Track_Widget::handle( m );
  255. copied = false;
  256. if ( trimming != NO )
  257. trimming = NO;
  258. goto changed;
  259. }
  260. case FL_DRAG:
  261. if ( ! _drag )
  262. {
  263. begin_drag( Drag( x() - X, y() - Y ) );
  264. _log.hold();
  265. }
  266. /* panning */
  267. if ( Fl::event_state() & FL_SHIFT &&
  268. Fl::event_state() & FL_CTRL )
  269. {
  270. int d = (ox + X) - x();
  271. long td = timeline->x_to_ts( d );
  272. nframes_t W = _r->end - _r->start;
  273. if ( td > 0 && os < td )
  274. _r->start = 0;
  275. else
  276. _r->start = os - td;
  277. _r->end = _r->start + W;
  278. _track->redraw();
  279. return 1;
  280. }
  281. /* trimming */
  282. if ( Fl::event_state() & FL_SHIFT )
  283. if ( trimming )
  284. {
  285. trim( trimming, X );
  286. return 1;
  287. }
  288. else
  289. return 0;
  290. /* duplication */
  291. if ( Fl::event_state() & FL_CTRL )
  292. {
  293. if ( _drag->state == 0 )
  294. {
  295. _track->add( new Region( *this ) );
  296. _drag->state = 1;
  297. return 1;
  298. }
  299. }
  300. /* track jumping */
  301. if ( ! selected() )
  302. {
  303. if ( Y > y() + h() )
  304. {
  305. Fl::copy( class_name(), strlen( class_name() ), 0 );
  306. Fl::dnd();
  307. }
  308. else
  309. if ( Y < y() )
  310. {
  311. Fl::copy( class_name(), strlen( class_name() ), 0 );
  312. Fl::dnd();
  313. }
  314. }
  315. ret = Track_Widget::handle( m );
  316. return ret | 1;
  317. default:
  318. return Track_Widget::handle( m );
  319. break;
  320. }
  321. changed:
  322. return 1;
  323. }
  324. void
  325. Region::draw_box( int X, int Y, int W, int H )
  326. {
  327. if ( ! shown() )
  328. return;
  329. /* dirty hack to keep the box from flipping to vertical at small sizes */
  330. fl_push_clip( x(), Y, w(), H );
  331. if ( selected() )
  332. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
  333. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  334. else
  335. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
  336. fl_pop_clip();
  337. }
  338. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  339. Y W and H are the portion of the widget to draw (arrived at by
  340. intersection of the clip and relative to OX) */
  341. void
  342. Region::draw ( int X, int Y, int W, int H )
  343. {
  344. if ( ! shown() )
  345. return;
  346. if ( ! ( W > 0 && H > 0 ) )
  347. return;
  348. int OX = scroll_x();
  349. int ox = timeline->ts_to_x( _r->offset );
  350. if ( ox > OX + _track->w() ||
  351. ox < OX && ox + abs_w() < OX )
  352. return;
  353. int rw = timeline->ts_to_x( _r->end - _r->start );
  354. // nframes_t end = _r->offset + ( _r->end - _r->start );
  355. /* calculate waveform offset due to scrolling */
  356. nframes_t offset = 0;
  357. if ( ox < OX )
  358. {
  359. offset = timeline->x_to_ts( OX - ox );
  360. rw = timeline->ts_to_x( (_r->end - _r->start) - offset );
  361. }
  362. rw = min( rw, _track->w() );
  363. int rx = x();
  364. fl_push_clip( rx, Y, rw, H );
  365. /* get actual peak data */
  366. int channels;
  367. int peaks;
  368. Peak *pbuf;
  369. const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  370. _clip->read_peaks( timeline->fpp(),
  371. start,
  372. start + timeline->x_to_ts( W ),
  373. &peaks, &pbuf, &channels );
  374. assert( pbuf );
  375. int ch = (h() - Fl::box_dh( box() )) / channels;
  376. for ( int i = 0; i < channels; ++i )
  377. {
  378. Peak *pb = pbuf + (peaks * i);
  379. /* scale it */
  380. for ( int j = peaks; j--; )
  381. {
  382. pb[ j ].min *= _scale;
  383. pb[ j ].max *= _scale;
  384. }
  385. Waveform::draw( X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch,
  386. pb, peaks,
  387. selected() ? fl_invert_color( _color ) : _color );
  388. }
  389. delete pbuf;
  390. /* for ( int i = _clip->channels(); i--; ) */
  391. /* Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, */
  392. /* ch, _clip, i, timeline->fpp(), */
  393. /* _r->start + offset, min( (_r->end - _r->start) - offset, _r->end), */
  394. /* _scale, selected() ? fl_invert_color( _color ) : _color ); */
  395. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  396. fl_color( FL_BLACK );
  397. fl_line( rx, Y, rx, Y + H );
  398. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  399. draw_label( _clip->name(), align() );
  400. if ( current() )
  401. {
  402. /* draw length bubble */
  403. char pat[40];
  404. snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
  405. draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
  406. }
  407. fl_pop_clip();
  408. }
  409. void
  410. Region::normalize ( void )
  411. {
  412. printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
  413. /* FIXME: figure out a way to do this via the peak server */
  414. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
  415. }
  416. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  417. this region into /buf/, where /pos/ is in timeline frames */
  418. /* this runs in the diskstream thread. */
  419. /* FIXME: it is far more efficient to read all the channels from a
  420. multichannel source at once... But how should we handle the case of a
  421. mismatch between the number of channels in this region's source and
  422. the number of channels on the track/buffer this data is being read
  423. for? Would it not be better to simply buffer and deinterlace the
  424. frames in the Audio_File class instead, so that sequential requests
  425. for different channels at the same position avoid hitting the disk
  426. again? */
  427. /* FIXME: should fade-out/fade-ins not be handled here? */
  428. nframes_t
  429. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  430. {
  431. const Range &r = _range;
  432. const nframes_t length = r.end - r.start;
  433. /* do nothing if we aren't covered by this frame range */
  434. if ( pos > r.offset + length || pos + nframes < r.offset )
  435. return 0;
  436. /* calculate offsets into file and sample buffer */
  437. nframes_t sofs, ofs, cnt;
  438. if ( pos < r.offset )
  439. {
  440. sofs = 0;
  441. ofs = r.offset - pos;
  442. cnt = nframes - ofs;
  443. }
  444. else
  445. {
  446. ofs = 0;
  447. sofs = pos - r.offset;
  448. }
  449. if ( ofs > nframes )
  450. return 0;
  451. const nframes_t start = ofs + r.start + sofs;
  452. const nframes_t len = min( cnt, nframes - ofs );
  453. const nframes_t end = start + len;
  454. if ( len == 0 )
  455. return 0;
  456. /* now that we know how much and where to read, get on with it */
  457. /* FIXME: seeking can be very expensive. Esp. with compressed
  458. * formats. We should attempt to avoid it. But here or in the
  459. * Audio_File class? */
  460. cnt = _clip->read( buf + ofs, channel, start, end );
  461. /* apply gain */
  462. for ( int i = cnt; i--; )
  463. buf[i] *= _scale;
  464. return cnt;
  465. }