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.

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