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.

404 lines
10KB

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