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.

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