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.

683 lines
18KB

  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 <FL/fl_draw.H>
  19. #include <FL/Fl.H>
  20. #include <FL/Fl_Group.H>
  21. #include <FL/Fl_Widget.H>
  22. #include <FL/Fl_Box.H>
  23. #include <FL/Fl_Menu_Item.H>
  24. #include <FL/fl_show_colormap.H>
  25. #include <stdio.h>
  26. #include "Sequence.H"
  27. #include "Audio_Region.H"
  28. #include "Timeline.H"
  29. #include "Waveform.H"
  30. #include "Audio_Sequence.H"
  31. #include "Track.H"
  32. #include "Engine/Audio_File.H"
  33. #include <algorithm>
  34. using std::min;
  35. using std::max;
  36. extern Timeline *timeline;
  37. bool Audio_Region::inherit_track_color = true;
  38. Fl_Boxtype Audio_Region::_box = FL_UP_BOX;
  39. Fl_Color Audio_Region::_selection_color = FL_MAGENTA;
  40. static Fl_Color fl_invert_color ( Fl_Color c )
  41. {
  42. unsigned char r, g, b;
  43. Fl::get_color( c, r, g, b );
  44. return fl_rgb_color( 255 - r, 255 - g, 255 - b );
  45. }
  46. void
  47. Audio_Region::get ( Log_Entry &e ) const
  48. {
  49. e.add( ":source", _clip ? _clip->name() : "" );
  50. e.add( ":gain", _scale );
  51. e.add( ":fade-in-type", _fade_in.type );
  52. e.add( ":fade-in-duration", _fade_in.length );
  53. e.add( ":fade-out-type", _fade_out.type );
  54. e.add( ":fade-out-duration", _fade_out.length );
  55. Sequence_Region::get( e );
  56. e.add( ":offset", _r->offset );
  57. }
  58. void
  59. Audio_Region::set ( Log_Entry &e )
  60. {
  61. for ( int i = 0; i < e.size(); ++i )
  62. {
  63. const char *s, *v;
  64. e.get( i, &s, &v );
  65. if ( ! strcmp( s, ":gain" ) )
  66. _scale = atof( v );
  67. else if ( ! strcmp( s, ":color" ) )
  68. _box_color = (Fl_Color)atoi( v );
  69. else if ( ! strcmp( s, ":fade-in-type" ) )
  70. _fade_in.type = (Fade::fade_type_e)atoi( v );
  71. else if ( ! strcmp( s, ":fade-in-duration" ) )
  72. _fade_in.length = atoll( v );
  73. else if ( ! strcmp( s, ":fade-out-type" ) )
  74. _fade_out.type = (Fade::fade_type_e)atoi( v );
  75. else if ( ! strcmp( s, ":fade-out-duration" ) )
  76. _fade_out.length = atoll( v );
  77. else if ( ! strcmp( s, ":offset" ) )
  78. _r->offset = atoll( v );
  79. else if ( ! strcmp( s, ":source" ) )
  80. {
  81. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  82. {
  83. printf( "Grave error: could not open source \"%s\"\n", v );
  84. }
  85. }
  86. }
  87. Sequence_Region::set( e );
  88. }
  89. void
  90. Audio_Region::init ( void )
  91. {
  92. _sequence = NULL;
  93. _scale = 1.0f;
  94. _clip = NULL;
  95. _box_color = FL_CYAN;
  96. _color = FL_BLUE;
  97. _fade_in.length = 256;
  98. _fade_in.type = Fade::Sigmoid;
  99. _fade_out = _fade_in;
  100. }
  101. /* copy constructor */
  102. Audio_Region::Audio_Region ( const Audio_Region & rhs ) : Sequence_Region( rhs )
  103. {
  104. // *((Sequence_Region*)this) = (Sequence_Region &)rhs;
  105. _clip = rhs._clip;
  106. _scale = rhs._scale;
  107. _fade_in = rhs._fade_in;
  108. _fade_out = rhs._fade_out;
  109. log_create();
  110. }
  111. /* */
  112. Audio_Region::Audio_Region ( Audio_File *c )
  113. {
  114. init();
  115. _clip = c;
  116. _r->length = _clip->length();
  117. log_create();
  118. }
  119. /* used when DND importing */
  120. Audio_Region::Audio_Region ( Audio_File *c, Sequence *t, nframes_t o )
  121. {
  122. init();
  123. _clip = c;
  124. _sequence = t;
  125. _r->offset = 0;
  126. _r->start = o;
  127. _r->length = _clip->length();
  128. sequence()->add( this );
  129. int sum = 0;
  130. const char *s = rindex( _clip->name(), '/' );
  131. if ( ! s )
  132. s = _clip->name();
  133. for ( int i = strlen( s ); i--; )
  134. sum += s[ i ];
  135. while ( sum >> 8 )
  136. sum = (sum & 0xFF) + (sum >> 8);
  137. _color = (Fl_Color)sum;
  138. /* _color = fl_color_average( FL_YELLOW, (Fl_Color)sum, 0.80 ); */
  139. // _color = FL_YELLOW;
  140. _box_color = FL_WHITE;
  141. log_create();
  142. }
  143. const char *
  144. Audio_Region::source_name ( void ) const
  145. {
  146. return _clip->name();
  147. }
  148. void
  149. Audio_Region::menu_cb ( Fl_Widget *w, void *v )
  150. {
  151. ((Audio_Region*)v)->menu_cb( (Fl_Menu_*) w );
  152. }
  153. void
  154. Audio_Region::menu_cb ( const Fl_Menu_ *m )
  155. {
  156. char picked[256];
  157. m->item_pathname( picked, sizeof( picked ) );
  158. Logger log( this );
  159. if ( ! strcmp( picked, "Fade/In/Linear" ) )
  160. _fade_in.type = Fade::Linear;
  161. else if ( ! strcmp( picked, "Fade/In/Sigmoid" ) )
  162. _fade_in.type = Fade::Sigmoid;
  163. else if ( ! strcmp( picked, "Fade/In/Logarithmic" ) )
  164. _fade_in.type = Fade::Logarithmic;
  165. else if ( ! strcmp( picked, "Fade/In/Parabolic" ) )
  166. _fade_in.type = Fade::Parabolic;
  167. else if ( ! strcmp( picked, "Fade/Out/Linear" ) )
  168. _fade_out.type = Fade::Linear;
  169. else if ( ! strcmp( picked, "Fade/Out/Sigmoid" ) )
  170. _fade_out.type = Fade::Sigmoid;
  171. else if ( ! strcmp( picked, "Fade/Out/Logarithmic" ) )
  172. _fade_out.type = Fade::Logarithmic;
  173. else if ( ! strcmp( picked, "Fade/Out/Parabolic" ) )
  174. _fade_out.type = Fade::Parabolic;
  175. else if ( ! strcmp( picked, "/Color" ) )
  176. box_color( fl_show_colormap( box_color() ) );
  177. else if ( ! strcmp( picked, "/Fade in to mouse" ) )
  178. {
  179. nframes_t offset = x_to_offset( Fl::event_x() );
  180. if ( offset < length() )
  181. _fade_in.length = offset;
  182. DMESSAGE( "set fade in duration" );
  183. }
  184. else if ( ! strcmp( picked, "/Fade out to mouse" ) )
  185. {
  186. long offset = length() - x_to_offset( Fl::event_x() );
  187. if ( offset > 0 )
  188. _fade_out.length = offset;
  189. }
  190. else
  191. FATAL( "Unknown menu choice \"%s\"", picked );
  192. redraw();
  193. }
  194. #include "FL/test_press.H"
  195. /** build the context menu for this region */
  196. Fl_Menu_Button &
  197. Audio_Region::menu ( void )
  198. {
  199. static Fl_Menu_Button m( 0, 0, 0, 0, "Region" );
  200. Fade::fade_type_e it = _fade_in.type;
  201. Fade::fade_type_e ot = _fade_out.type;
  202. Fl_Menu_Item items[] =
  203. {
  204. { "Fade", 0, &Audio_Region::menu_cb, 0, FL_SUBMENU },
  205. { "In", 0, &Audio_Region::menu_cb, 0, FL_SUBMENU },
  206. { "Linear", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( it == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  207. { "Sigmoid", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( it == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  208. { "Logarithmic", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( it == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  209. { "Parabolic", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( it == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  210. { 0 },
  211. { "Out", 0, &Audio_Region::menu_cb, 0, FL_SUBMENU },
  212. { "Linear", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( ot == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  213. { "Sigmoid", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( ot == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  214. { "Logarothmic", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( ot == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  215. { "Parabolic", 0, &Audio_Region::menu_cb, 0, FL_MENU_RADIO | ( ot == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  216. { 0 },
  217. { 0 },
  218. { "Color", 0, &Audio_Region::menu_cb, 0, inherit_track_color ? FL_MENU_INACTIVE : 0 },
  219. { "Fade in to mouse", FL_F + 3, &Audio_Region::menu_cb, 0 },
  220. { "Fade out to mouse", FL_F + 4, &Audio_Region::menu_cb, 0 },
  221. { 0 },
  222. };
  223. m.copy( items, (void*)this );
  224. return m;
  225. }
  226. #include "FL/menu_popup.H"
  227. int
  228. Audio_Region::handle ( int m )
  229. {
  230. static int ox, oy;
  231. static bool copied = false;
  232. static nframes_t os;
  233. int X = Fl::event_x();
  234. int Y = Fl::event_y();
  235. Logger _log( this );
  236. switch ( m )
  237. {
  238. case FL_FOCUS:
  239. case FL_UNFOCUS:
  240. return 1;
  241. case FL_KEYBOARD:
  242. return menu().test_shortcut() != 0;
  243. case FL_ENTER:
  244. return Sequence_Region::handle( m );
  245. case FL_LEAVE:
  246. return Sequence_Region::handle( m );
  247. case FL_PUSH:
  248. {
  249. /* splitting */
  250. if ( test_press( FL_BUTTON2 | FL_SHIFT ) )
  251. {
  252. /* split */
  253. if ( ! copied )
  254. {
  255. Loggable::block_start();
  256. Audio_Region *copy = new Audio_Region( *this );
  257. trim( RIGHT, X );
  258. copy->trim( LEFT, X );
  259. sequence()->add( copy );
  260. log_end();
  261. Loggable::block_end();
  262. }
  263. return 0;
  264. }
  265. else
  266. {
  267. ox = x() - X;
  268. oy = y() - Y;
  269. /* for panning */
  270. os = _r->offset;
  271. if ( test_press( FL_BUTTON2 | FL_CTRL ) )
  272. {
  273. normalize();
  274. redraw();
  275. return 1;
  276. }
  277. else if ( test_press( FL_BUTTON3 ) )
  278. {
  279. /* context menu */
  280. menu_popup( &menu() );
  281. return 1;
  282. }
  283. else
  284. return Sequence_Region::handle( m );
  285. }
  286. break;
  287. }
  288. case FL_RELEASE:
  289. {
  290. Sequence_Region::handle( m );
  291. copied = false;
  292. return 1;
  293. }
  294. case FL_DRAG:
  295. if ( ! _drag )
  296. {
  297. begin_drag( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
  298. _log.hold();
  299. }
  300. if ( test_press( FL_BUTTON1 | FL_SHIFT | FL_CTRL ) )
  301. {
  302. /* panning */
  303. int d = (ox + X) - x();
  304. long td = timeline->x_to_ts( d );
  305. if ( td > 0 && os < (nframes_t)td )
  306. _r->offset = 0;
  307. else
  308. _r->offset = os - td;
  309. redraw();
  310. return 1;
  311. }
  312. return Sequence_Region::handle( m );
  313. default:
  314. return Sequence_Region::handle( m );
  315. break;
  316. }
  317. return 0;
  318. }
  319. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  320. portion of the region covered by this draw, which may or may not
  321. cover the fade in question. */
  322. void
  323. Audio_Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
  324. {
  325. const int dy = y() + Fl::box_dy( box() );
  326. const int dh = h() - Fl::box_dh( box() );
  327. const int height = dh;
  328. const int width = timeline->ts_to_x( fade.length );
  329. fl_color( fl_lighter( FL_BLACK ) );
  330. fl_push_matrix();
  331. if ( dir == Fade::In )
  332. fl_translate( line_x(), dy );
  333. else
  334. {
  335. fl_translate( line_x() + abs_w(), dy );
  336. /* flip */
  337. fl_scale( -1.0, 1.0 );
  338. }
  339. fl_scale( width, height );
  340. if ( line )
  341. fl_begin_line();
  342. else
  343. fl_begin_polygon();
  344. fl_vertex( 0.0, 0.0 );
  345. fl_vertex( 0.0, 1.0 );
  346. // if ( draw_real_fade_curve )
  347. {
  348. nframes_t tsx = timeline->x_to_ts( 1 );
  349. nframes_t ts = 0;
  350. for ( int i = 0; i < width; ++i, ts += tsx )
  351. fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
  352. }
  353. fl_vertex( 1.0, 0.0 );
  354. if ( line )
  355. fl_end_line();
  356. else
  357. fl_end_polygon();
  358. fl_pop_matrix();
  359. }
  360. struct Peaks_Redraw_Request {
  361. Audio_Region *region;
  362. nframes_t start;
  363. nframes_t end;
  364. Peaks_Redraw_Request ( Audio_Region *region, nframes_t start, nframes_t end ) : region( region ), start( start), end( end )
  365. {
  366. }
  367. };
  368. /* static wrapper */
  369. void
  370. Audio_Region::peaks_pending_cb ( void *v )
  371. {
  372. Peaks_Redraw_Request *r = (Peaks_Redraw_Request*)v;
  373. r->region->peaks_pending_cb( r );
  374. }
  375. void
  376. Audio_Region::peaks_pending_cb ( Peaks_Redraw_Request *r )
  377. {
  378. int npeaks = timeline->ts_to_x( r->end - r->start );
  379. if ( _clip->peaks()->ready( r->start, npeaks, timeline->fpp() ) )
  380. {
  381. printf( "damaging from timeout\n" );
  382. /* FIXME: only need to damage the affected area! */
  383. timeline->damage( FL_DAMAGE_ALL, x(), y(), w(), h() );
  384. delete r;
  385. }
  386. else
  387. Fl::repeat_timeout( 0.1f, &Audio_Region::peaks_pending_cb, (void*)r );
  388. }
  389. void
  390. Audio_Region::draw_box( void )
  391. {
  392. /* dirty hack to keep the box from flipping to vertical at small sizes */
  393. fl_push_clip( x(), y(), w(), h() );
  394. Fl_Color selection_color = _selection_color;
  395. Fl_Color color = Audio_Region::inherit_track_color ? sequence()->track()->color() : _box_color;
  396. color = fl_color_average( color, sequence()->color(), 0.75f );
  397. if ( this == ((Audio_Sequence*)sequence())->capture_region() )
  398. {
  399. color = FL_RED;
  400. }
  401. else if ( ! active_r() )
  402. {
  403. color = fl_inactive( color );
  404. selection_color = fl_inactive( selection_color );
  405. }
  406. if ( selected() )
  407. fl_draw_box( fl_down( box() ), x() - ( h() >> 1 ), y(), w() + ( h() >> 1 ) + 50, h(), selection_color );
  408. else
  409. fl_draw_box( box(), x() - ( h() >> 1 ), y(), w() + ( h() >> 1 ) + 50, h(), color );
  410. /* draw fades */
  411. draw_fade( _fade_in, Fade::In, false, x(), w() );
  412. draw_fade( _fade_out, Fade::Out, false, x(), w() );
  413. fl_pop_clip();
  414. }
  415. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  416. void
  417. Audio_Region::draw ( void )
  418. {
  419. /* intersect clip with region */
  420. int X, Y, W, H;
  421. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  422. if ( ! ( W > 0 && H > 0 ) )
  423. /* no coverage */
  424. return;
  425. /* account for waveform outlines... */
  426. X -= 2;
  427. W += 4;
  428. int OX = scroll_x();
  429. int ox = timeline->ts_to_x( _r->start );
  430. if ( ox > OX + sequence()->w() ||
  431. ox < OX && ox + abs_w() < OX )
  432. /* not in viewport */
  433. return;
  434. int rw = timeline->ts_to_x( _r->length );
  435. /* calculate waveform offset due to scrolling */
  436. nframes_t offset = 0;
  437. if ( ox < OX )
  438. {
  439. offset = timeline->x_to_ts( OX - ox );
  440. rw -= OX - ox;
  441. }
  442. rw = min( rw, sequence()->w() );
  443. int rx = x();
  444. fl_push_clip( rx, Y, rw, H );
  445. /* get actual peak data */
  446. int channels;
  447. int peaks;
  448. Peak *pbuf;
  449. // const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  450. // nframes_t start = _r->start + offset;
  451. nframes_t start = _r->offset + offset;
  452. /* compensate for ??? */
  453. if ( X - rx > 0 )
  454. start += timeline->x_to_ts( X - rx );
  455. const int peaks_needed = min( timeline->ts_to_x( _clip->length() - start ), W );
  456. const nframes_t end = start + timeline->x_to_ts( peaks_needed );
  457. if ( _clip->read_peaks( timeline->fpp(),
  458. start,
  459. end,
  460. &peaks, &pbuf, &channels ) &&
  461. peaks )
  462. {
  463. assert( pbuf );
  464. /* draw fade curve outlines--this is only here because of crossfades */
  465. draw_fade( _fade_in, Fade::In, true, X, W );
  466. draw_fade( _fade_out, Fade::Out, true, X, W );
  467. int ch = (h() - Fl::box_dh( box() )) / channels;
  468. Waveform::scale( pbuf, peaks * channels, _scale );
  469. for ( int i = 0; i < channels; ++i )
  470. {
  471. // Peak *pb = pbuf + (peaks * i);
  472. /* int fw = timeline->ts_to_x( fade.length ); */
  473. /* /\* if ( draw_fade_waveform ) *\/ */
  474. /* for ( int j = min( fw, peaks ); j--; ) */
  475. /* { */
  476. /* const float g = fade.gain( j * timeline->fpp() ); */
  477. /* pb[ j ].min *= g; */
  478. /* pb[ j ].max *= g; */
  479. /* } */
  480. Waveform::draw( X,
  481. (y() + Fl::box_dy( box() )) + (i * ch),
  482. W,
  483. ch,
  484. pbuf + i, peaks, channels,
  485. selected() ? fl_invert_color( _color ) : _color );
  486. }
  487. }
  488. if ( peaks < peaks_needed )
  489. {
  490. /* couldn't read peaks--perhaps they're being generated. Try again later. */
  491. Fl::add_timeout( 0.1f, &Audio_Region::peaks_pending_cb,
  492. new Peaks_Redraw_Request( this, start + timeline->x_to_ts( peaks ), end ) );
  493. }
  494. timeline->draw_measure_lines( X, Y, W, H, _box_color );
  495. /* fl_color( FL_BLACK ); */
  496. /* fl_line( rx, Y, rx, Y + H ); */
  497. /* fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H ); */
  498. if ( _clip->dummy() )
  499. {
  500. char pat[256];
  501. snprintf( pat, sizeof( pat ), "Missing Source!: %s", _clip->name() );
  502. draw_label( pat, align() );
  503. }
  504. else
  505. draw_label( _clip->name(), align() );
  506. /* if ( current() ) */
  507. /* { */
  508. /* /\* draw length bubble *\/ */
  509. /* char pat[40]; */
  510. /* snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() ); */
  511. /* draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN ); */
  512. /* } */
  513. fl_pop_clip();
  514. }
  515. void
  516. Audio_Region::normalize ( void )
  517. {
  518. int peaks, channels;
  519. Peak *pbuf;
  520. if ( _clip->read_peaks( length(), offset(), offset() + length(), &peaks, &pbuf, &channels ) &&
  521. peaks )
  522. _scale = pbuf->normalization_factor();
  523. }