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.

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