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.

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