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.

608 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. *((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. /** read the overlapping part of /channel/ at /pos/ for /nframes/ of
  411. this region into /buf/, where /pos/ is in timeline frames */
  412. /* this runs in the diskstream thread. */
  413. /* FIXME: it is far more efficient to read all the channels from a
  414. multichannel source at once... But how should we handle the case of a
  415. mismatch between the number of channels in this region's source and
  416. the number of channels on the track/buffer this data is being read
  417. for? Would it not be better to simply buffer and deinterlace the
  418. frames in the Audio_File class instead, so that sequential requests
  419. for different channels at the same position avoid hitting the disk
  420. again? */
  421. /* FIXME: should fade-out/fade-ins not be handled here? */
  422. nframes_t
  423. Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
  424. {
  425. const Range r = _range;
  426. const nframes_t length = r.end - r.start;
  427. /* do nothing if we aren't covered by this frame range */
  428. if ( pos > r.offset + length || pos + nframes < r.offset )
  429. return 0;
  430. /* calculate offsets into file and sample buffer */
  431. nframes_t sofs, ofs, cnt;
  432. cnt = nframes;
  433. if ( pos < r.offset )
  434. {
  435. sofs = 0;
  436. ofs = r.offset - pos;
  437. cnt -= ofs;
  438. }
  439. else
  440. {
  441. ofs = 0;
  442. sofs = pos - r.offset;
  443. }
  444. if ( ofs >= nframes )
  445. return 0;
  446. // const nframes_t start = ofs + r.start + sofs;
  447. const nframes_t start = r.start + sofs;
  448. const nframes_t len = min( cnt, nframes - ofs );
  449. const nframes_t end = start + len;
  450. if ( len == 0 )
  451. return 0;
  452. /* now that we know how much and where to read, get on with it */
  453. /* FIXME: seeking can be very expensive. Esp. with compressed
  454. * formats. We should attempt to avoid it. But here or in the
  455. * Audio_File class? */
  456. // printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
  457. cnt = _clip->read( buf + ofs, channel, start, end );
  458. /* apply gain */
  459. if ( _scale != 1.0f )
  460. for ( int i = cnt; i--; )
  461. buf[i] *= _scale;
  462. // printf( "read %lu frames\n", cnt );
  463. return cnt;
  464. }