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.

390 lines
9.7KB

  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_PLASTIC_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. void
  41. Region::init ( void )
  42. {
  43. /* align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_BOTTOM | FL_ALIGN_CLIP ); */
  44. /* labeltype( FL_SHADOW_LABEL ); */
  45. /* labelcolor( FL_WHITE ); */
  46. /* box( FL_PLASTIC_UP_BOX ); */
  47. _track = NULL;
  48. _offset = 0;
  49. _start = 0;
  50. _end = 0;
  51. _scale = 1.0f;
  52. _clip = NULL;
  53. _box_color = FL_CYAN;
  54. _color = FL_BLUE;
  55. }
  56. Region::Region ( const Region & rhs )
  57. {
  58. _offset = rhs._offset;
  59. _track = rhs._track;
  60. _clip = rhs._clip;
  61. _start = rhs._start;
  62. _end = rhs._end;
  63. _scale = rhs._scale;
  64. log_create();
  65. }
  66. Region::Region ( Audio_File *c )
  67. {
  68. init();
  69. _clip = c;
  70. _end = _clip->length();
  71. log_create();
  72. }
  73. void
  74. Region::trim ( enum trim_e t, int X )
  75. {
  76. redraw();
  77. switch ( t )
  78. {
  79. case LEFT:
  80. {
  81. int d = X - x();
  82. /* if ( d < 0 ) */
  83. /* // _track->damage( FL_DAMAGE_EXPOSE, x() + d, y(), 1 - d, h() ); */
  84. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() ); */
  85. /* else */
  86. /* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), d, h() ); */
  87. long td = timeline->x_to_ts( d );
  88. if ( td < 0 && _start < 0 - td )
  89. td = 0 - _start;
  90. _start += td;
  91. _offset += td;
  92. break;
  93. }
  94. case RIGHT:
  95. {
  96. int d = (x() + w()) - X;
  97. /* _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), d, h() ); */
  98. long td = timeline->x_to_ts( d );
  99. _end -= td;
  100. break;
  101. }
  102. default:
  103. return;
  104. }
  105. }
  106. int
  107. Region::handle ( int m )
  108. {
  109. static int ox, oy;
  110. static enum trim_e trimming;
  111. static bool copied = false;
  112. static nframes_t os;
  113. int X = Fl::event_x();
  114. int Y = Fl::event_y();
  115. int ret;
  116. switch ( m )
  117. {
  118. case FL_PUSH:
  119. {
  120. if ( Fl::event_state() & FL_SHIFT &&
  121. ! ( Fl::event_state() & FL_CTRL ))
  122. {
  123. switch ( Fl::event_button() )
  124. {
  125. case 1:
  126. trim( trimming = LEFT, X );
  127. break;
  128. case 3:
  129. trim( trimming = RIGHT, X );
  130. break;
  131. default:
  132. return 0;
  133. }
  134. fl_cursor( FL_CURSOR_WE );
  135. return 1;
  136. }
  137. else
  138. {
  139. ox = x() - X;
  140. oy = y() - Y;
  141. if ( Fl::event_state() && FL_CTRL )
  142. {
  143. os = _start;
  144. // Fl::local_grab( this );
  145. }
  146. if ( Fl::event_button() == 2 )
  147. {
  148. if ( Fl::event_state() & FL_CTRL )
  149. normalize();
  150. else
  151. _selected = ! _selected;
  152. log_change();
  153. redraw();
  154. }
  155. ret = Track_Widget::handle( m );
  156. return ret | 1;
  157. }
  158. break;
  159. }
  160. case FL_RELEASE:
  161. Track_Widget::handle( m );
  162. copied = false;
  163. if ( trimming != NO )
  164. {
  165. trimming = NO;
  166. log_change();
  167. }
  168. return 1;
  169. case FL_DRAG:
  170. if ( Fl::event_state() & FL_SHIFT &&
  171. Fl::event_state() & FL_CTRL )
  172. {
  173. int d = (ox + X) - x();
  174. long td = timeline->x_to_ts( d );
  175. nframes_t W = _end - _start;
  176. if ( td > 0 && os < td )
  177. _start = 0;
  178. else
  179. _start = os - td;
  180. _end = _start + W;
  181. _track->redraw();
  182. return 1;
  183. }
  184. if ( Fl::event_state() & FL_SHIFT )
  185. if ( trimming )
  186. {
  187. trim( trimming, X );
  188. return 1;
  189. }
  190. else
  191. return 0;
  192. if ( Fl::event_state() & FL_CTRL )
  193. {
  194. if ( ! copied )
  195. {
  196. _track->add( new Region( *this ) );
  197. copied = true;
  198. return 1;
  199. }
  200. }
  201. if ( Y > y() + h() )
  202. {
  203. if ( _track->next() )
  204. if ( Y > _track->next()->y() )
  205. _track->next()->add( this );
  206. }
  207. else
  208. if ( Y < y() )
  209. {
  210. if ( _track->prev() )
  211. if ( Y < _track->prev()->y() + _track->prev()->h() )
  212. _track->prev()->add( this );
  213. }
  214. // _track->redraw();
  215. // _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() );
  216. ret = Track_Widget::handle( m );
  217. return ret | 1;
  218. // _offset = timeline->x_to_ts( x() );
  219. default:
  220. return Track_Widget::handle( m );
  221. break;
  222. }
  223. }
  224. void
  225. Region::draw_box( int X, int Y, int W, int H )
  226. {
  227. /* dirty hack to keep the box from flipping to vertical at small sizes */
  228. fl_push_clip( x(), Y, w(), H );
  229. if ( _selected )
  230. fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, _selection_color );
  231. // fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
  232. else
  233. fl_draw_box( box(), x() - 10, Y, w() + 50, H, _box_color );
  234. fl_pop_clip();
  235. }
  236. /* Draw (part of) region. OX is pixel offset from start of timeline, X
  237. Y W and H are the portion of the widget to draw (arrived at by
  238. intersection of the clip and relative to OX) */
  239. void
  240. Region::draw ( int X, int Y, int W, int H )
  241. {
  242. if ( ! ( W > 0 && H > 0 ) )
  243. return;
  244. int OX = scroll_x();
  245. int ox = timeline->ts_to_x( _offset );
  246. if ( ox > OX + _track->w() ||
  247. ox < OX && ox + abs_w() < OX )
  248. return;
  249. int rw = timeline->ts_to_x( _end - _start );
  250. nframes_t end = _offset + ( _end - _start );
  251. /* calculate waveform offset due to scrolling */
  252. nframes_t offset = 0;
  253. if ( ox < OX )
  254. {
  255. offset = timeline->x_to_ts( OX - ox );
  256. rw = timeline->ts_to_x( (_end - _start) - offset );
  257. }
  258. rw = min( rw, _track->w() );
  259. int rx = x();
  260. fl_push_clip( rx, Y, rw, H );
  261. /* dirty hack to keep the box from flipping to vertical at small sizes */
  262. // fl_draw_box( box(), rx - 10, Y, rw + 50, H, _box_color );
  263. // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
  264. draw_waveform( rx, Y, rw, H, _clip, _start + offset, min( (_end - _start) - offset, _end), _scale, _selected ? _color : fl_invert_color( _color ) );
  265. timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
  266. fl_color( FL_BLACK );
  267. fl_line( rx, Y, rx, Y + H );
  268. fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
  269. draw_label( _clip->name(), align() );
  270. /* if ( _selected ) */
  271. /* { */
  272. /* fl_color( selection_color() ); */
  273. /* fl_line_style( FL_SOLID, 4 ); */
  274. /* fl_rect( x(), y(), w(), h() ); */
  275. /* fl_line_style( FL_SOLID, 0 ); */
  276. /* } */
  277. fl_pop_clip();
  278. }
  279. void
  280. Region::normalize ( void )
  281. {
  282. printf( "normalize: start=%lu end=%lu\n", _start, _end );
  283. _scale = _clip->peaks()->normalization_factor( _start, _end );
  284. }
  285. void
  286. Region::dump ( void )
  287. {
  288. // printf( "Region %p %lu { \"%s\" %lu %lu }\n", this, _offset, _clip->name(), _start, _end );
  289. /* how about in STD? */
  290. 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() );
  291. }