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.

419 lines
11KB

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