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.

529 lines
13KB

  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. #include "Peak_Client.H"
  32. extern Timeline *timeline;
  33. Fl_Boxtype Region::_box = FL_UP_BOX;
  34. Fl_Color Region::_selection_color = FL_MAGENTA;
  35. static Fl_Color fl_invert_color ( Fl_Color c )
  36. {
  37. unsigned char r, g, b;
  38. Fl::get_color( c, r, g, b );
  39. return fl_rgb_color( 255 - r, 255 - g, 255 - b );
  40. }
  41. #if 0
  42. /* perhaps use map? */
  43. map_PRIM ( set )
  44. {
  45. /* if ( narg % 2 != 0 ) */
  46. /* printf( "invalid number of arguments\n" ); */
  47. int id = atoi( arg );
  48. map_ARG_NEXT( arg, end );
  49. Logable *l = Loggable::find( id );
  50. char **sa = malloc( sizeof( char * ) * narg + 1 );
  51. for ( int i = 0; i < narg; ++i )
  52. sa[ i ] = strdup( map_ARG_NEXT( arg, end ) );
  53. l->set( sa );
  54. map_RESULT( "" );
  55. }
  56. #endif
  57. void
  58. Region::init ( void )
  59. {
  60. _track = NULL;
  61. _offset = 0;
  62. _start = 0;
  63. _end = 0;
  64. _scale = 1.0f;
  65. _clip = NULL;
  66. _box_color = FL_CYAN;
  67. _color = FL_BLUE;
  68. }
  69. /* copy constructor */
  70. Region::Region ( const Region & rhs )
  71. {
  72. _offset = rhs._offset;
  73. // _track = rhs._track;
  74. _track = NULL;
  75. _clip = rhs._clip;
  76. _start = rhs._start;
  77. _end = rhs._end;
  78. _scale = rhs._scale;
  79. _box_color = rhs._box_color;
  80. _color = rhs._color;
  81. log_create();
  82. }
  83. /* */
  84. Region::Region ( Clip *c )
  85. {
  86. init();
  87. _clip = c;
  88. _end = _clip->length();
  89. log_create();
  90. }
  91. /* used when DND importing */
  92. Region::Region ( Clip *c, Track *t, nframes_t o )
  93. {
  94. init();
  95. _clip = c;
  96. _end = _clip->length();
  97. _track = t;
  98. _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 && _start < 0 - td )
  129. td = 0 - _start;
  130. if ( _start + td >= _end )
  131. td = (_end - _start) - timeline->x_to_ts( 1 );
  132. _start += td;
  133. _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, _end - _start );
  142. if ( td >= 0 && _end - _start < td )
  143. _end = _start + timeline->x_to_ts( 1 );
  144. else
  145. _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_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. _drag = new Drag( x() - X, y() - Y );
  185. _log.hold();
  186. break;
  187. case 3:
  188. trim( trimming = RIGHT, X );
  189. _drag = new 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 = _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. _drag = new 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 = _end - _start;
  269. if ( td > 0 && os < td )
  270. _start = 0;
  271. else
  272. _start = os - td;
  273. _end = _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. /* 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 ( ! ( W > 0 && H > 0 ) )
  339. return;
  340. int OX = scroll_x();
  341. int ox = timeline->ts_to_x( _offset );
  342. if ( ox > OX + _track->w() ||
  343. ox < OX && ox + abs_w() < OX )
  344. return;
  345. int rw = timeline->ts_to_x( _end - _start );
  346. nframes_t end = _offset + ( _end - _start );
  347. /* calculate waveform offset due to scrolling */
  348. nframes_t offset = 0;
  349. if ( ox < OX )
  350. {
  351. offset = timeline->x_to_ts( OX - ox );
  352. rw = timeline->ts_to_x( (_end - _start) - offset );
  353. }
  354. rw = min( rw, _track->w() );
  355. int rx = x();
  356. fl_push_clip( rx, Y, rw, H );
  357. /* get actual peak data */
  358. int channels;
  359. int peaks;
  360. Peak *pbuf;
  361. _clip->read_peaks( timeline->fpp(),
  362. // _start + offset, min( (_end - _start) - offset, _end),
  363. _start + offset, _start + offset + timeline->x_to_ts( W ),
  364. &peaks, &pbuf, &channels );
  365. assert( pbuf );
  366. int ch = (h() - Fl::box_dh( box() )) / channels;
  367. for ( int i = 0; i < channels; ++i )
  368. {
  369. Peak *pb = pbuf + (peaks * i);
  370. /* scale it */
  371. for ( int j = peaks; j--; )
  372. {
  373. pb[ j ].min *= _scale;
  374. pb[ j ].max *= _scale;
  375. }
  376. Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch,
  377. pb, peaks,
  378. selected() ? fl_invert_color( _color ) : _color );
  379. }
  380. delete pbuf;
  381. /* for ( int i = _clip->channels(); i--; ) */
  382. /* Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, */
  383. /* ch, _clip, i, timeline->fpp(), */
  384. /* _start + offset, min( (_end - _start) - offset, _end), */
  385. /* _scale, selected() ? fl_invert_color( _color ) : _color ); */
  386. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  387. fl_color( FL_BLACK );
  388. fl_line( rx, Y, rx, Y + H );
  389. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  390. draw_label( _clip->name(), align() );
  391. if ( current() )
  392. {
  393. /* draw length bubble */
  394. char pat[40];
  395. snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
  396. draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
  397. }
  398. fl_pop_clip();
  399. }
  400. void
  401. Region::normalize ( void )
  402. {
  403. printf( "normalize: start=%lu end=%lu\n", _start, _end );
  404. /* FIXME: figure out a way to do this via the peak server */
  405. /* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _start, _end ); */
  406. }