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.

722 lines
19KB

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