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.

502 lines
12KB

  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. _offset = 0;
  61. _start = 0;
  62. _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. _offset = rhs._offset;
  72. _track = rhs._track;
  73. _clip = rhs._clip;
  74. _start = rhs._start;
  75. _end = rhs._end;
  76. _scale = rhs._scale;
  77. _box_color = rhs._box_color;
  78. _color = rhs._color;
  79. log_create();
  80. }
  81. /* */
  82. Region::Region ( Audio_File *c )
  83. {
  84. init();
  85. _clip = c;
  86. _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. _end = _clip->length();
  95. _track = t;
  96. _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. redraw();
  114. switch ( t )
  115. {
  116. case LEFT:
  117. {
  118. /* if ( d < 0 ) */
  119. /* // _track->damage( FL_DAMAGE_EXPOSE, x() + d, y(), 1 - d, h() ); */
  120. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() ); */
  121. /* else */
  122. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), d, h() ); */
  123. int d = X - ( abs_x() - scroll_x() );
  124. long td = timeline->x_to_ts( d );
  125. if ( td < 0 && _start < 0 - td )
  126. td = 0 - _start;
  127. if ( _start + td >= _end )
  128. td = (_end - _start) - timeline->x_to_ts( 1 );
  129. _start += td;
  130. _offset += td;
  131. break;
  132. }
  133. case RIGHT:
  134. {
  135. int d = (( abs_x() - scroll_x() ) + abs_w() ) - X;
  136. /* _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), d, h() ); */
  137. long td = timeline->x_to_ts( d );
  138. // printf( "%li %li\n", td, _end - _start );
  139. if ( td >= 0 && _end - _start < td )
  140. _end = _start + timeline->x_to_ts( 1 );
  141. else
  142. _end -= td;
  143. break;
  144. }
  145. default:
  146. return;
  147. }
  148. }
  149. int
  150. Region::handle ( int m )
  151. {
  152. static bool dragging = false;
  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 Y = Fl::event_y();
  159. int ret;
  160. Logger _log( this );
  161. //log_start();
  162. switch ( m )
  163. {
  164. case FL_PUSH:
  165. {
  166. if ( Fl::event_state() & FL_SHIFT &&
  167. ! ( Fl::event_state() & FL_CTRL ))
  168. {
  169. switch ( Fl::event_button() )
  170. {
  171. case 1:
  172. trim( trimming = LEFT, X );
  173. _log.hold();
  174. break;
  175. case 3:
  176. trim( trimming = RIGHT, X );
  177. _log.hold();
  178. break;
  179. case 2:
  180. {
  181. /* split */
  182. if ( ! copied )
  183. {
  184. Loggable::block_start();
  185. Region *copy = new Region( *this );
  186. trim( RIGHT, X );
  187. copy->trim( LEFT, X );
  188. _track->add( copy );
  189. log_end();
  190. Loggable::block_end();
  191. return 1;
  192. }
  193. }
  194. default:
  195. return 0;
  196. break;
  197. }
  198. fl_cursor( FL_CURSOR_WE );
  199. return 1;
  200. }
  201. else
  202. {
  203. ox = x() - X;
  204. oy = y() - Y;
  205. if ( Fl::event_state() && FL_CTRL )
  206. {
  207. os = _start;
  208. // Fl::local_grab( this );
  209. }
  210. if ( Fl::event_button() == 2 )
  211. {
  212. if ( Fl::event_state() & FL_CTRL )
  213. normalize();
  214. else
  215. {
  216. if ( selected() )
  217. deselect();
  218. else
  219. select();
  220. }
  221. redraw();
  222. goto changed;
  223. }
  224. ret = Track_Widget::handle( m );
  225. return ret | 1;
  226. }
  227. break;
  228. }
  229. case FL_RELEASE:
  230. Track_Widget::handle( m );
  231. copied = false;
  232. if ( trimming != NO )
  233. {
  234. trimming = NO;
  235. _log.release();
  236. }
  237. if ( dragging )
  238. _log.release();
  239. dragging = false;
  240. goto changed;
  241. case FL_DRAG:
  242. if ( ! dragging )
  243. {
  244. _log.hold();
  245. dragging = true;
  246. }
  247. if ( Fl::event_state() & FL_SHIFT &&
  248. Fl::event_state() & FL_CTRL )
  249. {
  250. int d = (ox + X) - x();
  251. long td = timeline->x_to_ts( d );
  252. nframes_t W = _end - _start;
  253. if ( td > 0 && os < td )
  254. _start = 0;
  255. else
  256. _start = os - td;
  257. _end = _start + W;
  258. _track->redraw();
  259. return 1;
  260. }
  261. if ( Fl::event_state() & FL_SHIFT )
  262. if ( trimming )
  263. {
  264. trim( trimming, X );
  265. return 1;
  266. }
  267. else
  268. return 0;
  269. if ( Fl::event_state() & FL_CTRL )
  270. {
  271. if ( ! copied )
  272. {
  273. _track->add( new Region( *this ) );
  274. copied = true;
  275. return 1;
  276. }
  277. }
  278. if ( ! selected() )
  279. {
  280. if ( Y > y() + h() )
  281. {
  282. if ( _track->next() )
  283. if ( Y > _track->next()->y() )
  284. _track->next()->add( this );
  285. }
  286. else
  287. if ( Y < y() )
  288. {
  289. if ( _track->prev() )
  290. if ( Y < _track->prev()->y() + _track->prev()->h() )
  291. _track->prev()->add( this );
  292. }
  293. }
  294. ret = Track_Widget::handle( m );
  295. return ret | 1;
  296. default:
  297. return Track_Widget::handle( m );
  298. break;
  299. }
  300. changed:
  301. // log_end();
  302. return 1;
  303. }
  304. void
  305. Region::draw_box( int X, int Y, int W, int H )
  306. {
  307. /* dirty hack to keep the box from flipping to vertical at small sizes */
  308. fl_push_clip( x(), Y, w(), H );
  309. if ( selected() )
  310. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
  311. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  312. else
  313. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
  314. fl_pop_clip();
  315. }
  316. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  317. Y W and H are the portion of the widget to draw (arrived at by
  318. intersection of the clip and relative to OX) */
  319. void
  320. Region::draw ( int X, int Y, int W, int H )
  321. {
  322. if ( ! ( W > 0 && H > 0 ) )
  323. return;
  324. int OX = scroll_x();
  325. int ox = timeline->ts_to_x( _offset );
  326. if ( ox > OX + _track->w() ||
  327. ox < OX && ox + abs_w() < OX )
  328. return;
  329. int rw = timeline->ts_to_x( _end - _start );
  330. nframes_t end = _offset + ( _end - _start );
  331. /* calculate waveform offset due to scrolling */
  332. nframes_t offset = 0;
  333. if ( ox < OX )
  334. {
  335. offset = timeline->x_to_ts( OX - ox );
  336. rw = timeline->ts_to_x( (_end - _start) - offset );
  337. }
  338. rw = min( rw, _track->w() );
  339. int rx = x();
  340. fl_push_clip( rx, Y, rw, H );
  341. /* dirty hack to keep the box from flipping to vertical at small sizes */
  342. // fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  343. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  344. int ch = (h() - Fl::box_dh( box() )) / _clip->channels();
  345. for ( int i = _clip->channels(); i--; )
  346. // draw_waveform( rx, y() + (i * ch), rw, ch, _clip, i,
  347. // _start + offset, min( (_end - _start) - offset, _end),
  348. // _scale, _selected ? _color : fl_invert_color( _color ) );
  349. draw_waveform( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch, _clip, i,
  350. _start + offset, min( (_end - _start) - offset, _end),
  351. _scale, selected() ? fl_invert_color( _color ) : _color );
  352. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  353. fl_color( FL_BLACK );
  354. fl_line( rx, Y, rx, Y + H );
  355. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  356. draw_label( _clip->name(), align() );
  357. /* if ( _selected ) */
  358. /* { */
  359. /* fl_color( selection_color() ); */
  360. /* fl_line_style( FL_SOLID, 4 ); */
  361. /* fl_rect( x(), y(), w(), h() ); */
  362. /* fl_line_style( FL_SOLID, 0 ); */
  363. /* } */
  364. fl_pop_clip();
  365. }
  366. void
  367. Region::normalize ( void )
  368. {
  369. printf( "normalize: start=%lu end=%lu\n", _start, _end );
  370. /* FIXME: punt */
  371. _scale = _clip->peaks( 0 )->normalization_factor( _start, _end );
  372. }
  373. void
  374. Region::dump ( void )
  375. {
  376. // printf( "Region %p %lu { \"%s\" %lu %lu }\n", this, _offset, _clip->name(), _start, _end );
  377. /* how about in STD? */
  378. printf( "Region\n\t%p\n\toffset\n\t\t%lu\n\tranage\n\t\t%lu\n\t\t%lu\n\tsource\n\t\t\"%s\"\n\n", this, _offset, _start, _end, _clip->name() );
  379. }