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.

514 lines
13KB

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