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.

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