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.

694 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. return _menu->test_shortcut() != 0;
  243. case FL_ENTER:
  244. {
  245. update_menu();
  246. return Sequence_Region::handle( m );
  247. }
  248. case FL_LEAVE:
  249. return Sequence_Region::handle( m );
  250. case FL_PUSH:
  251. {
  252. /* splitting */
  253. if ( test_press( FL_BUTTON2 | FL_SHIFT ) )
  254. {
  255. /* split */
  256. if ( ! copied )
  257. {
  258. Loggable::block_start();
  259. Audio_Region *copy = new Audio_Region( *this );
  260. trim( RIGHT, X );
  261. copy->trim( LEFT, X );
  262. sequence()->add( copy );
  263. log_end();
  264. Loggable::block_end();
  265. }
  266. return 0;
  267. }
  268. else
  269. {
  270. ox = x() - X;
  271. oy = y() - Y;
  272. /* for panning */
  273. os = _r->offset;
  274. if ( test_press( FL_BUTTON2 | FL_CTRL ) )
  275. {
  276. normalize();
  277. redraw();
  278. return 1;
  279. }
  280. else if ( test_press( FL_BUTTON3 ) )
  281. {
  282. /* context menu */
  283. update_menu();
  284. const Fl_Menu_Item *r = _menu->menu()->popup( X, Y, _menu->label() );
  285. if ( r )
  286. {
  287. _menu->value( r );
  288. r->do_callback( static_cast<Fl_Widget*>(_menu) );
  289. }
  290. return 1;
  291. }
  292. else
  293. return Sequence_Region::handle( m );
  294. }
  295. break;
  296. }
  297. case FL_RELEASE:
  298. {
  299. Sequence_Region::handle( m );
  300. copied = false;
  301. return 1;
  302. }
  303. case FL_DRAG:
  304. if ( ! _drag )
  305. {
  306. begin_drag( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
  307. _log.hold();
  308. }
  309. if ( test_press( FL_BUTTON1 | FL_SHIFT | FL_CTRL ) )
  310. {
  311. /* panning */
  312. int d = (ox + X) - x();
  313. long td = timeline->x_to_ts( d );
  314. if ( td > 0 && os < (nframes_t)td )
  315. _r->offset = 0;
  316. else
  317. _r->offset = os - td;
  318. redraw();
  319. return 1;
  320. }
  321. return Sequence_Region::handle( m );
  322. default:
  323. return Sequence_Region::handle( m );
  324. break;
  325. }
  326. return 0;
  327. }
  328. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  329. portion of the region covered by this draw, which may or may not
  330. cover the fade in question. */
  331. void
  332. Audio_Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
  333. {
  334. const int dy = y() + Fl::box_dy( box() );
  335. const int dh = h() - Fl::box_dh( box() );
  336. const int height = dh;
  337. const int width = timeline->ts_to_x( fade.length );
  338. fl_color( fl_lighter( FL_BLACK ) );
  339. fl_push_matrix();
  340. if ( dir == Fade::In )
  341. fl_translate( line_x(), dy );
  342. else
  343. {
  344. fl_translate( line_x() + abs_w(), dy );
  345. /* flip */
  346. fl_scale( -1.0, 1.0 );
  347. }
  348. fl_scale( width, height );
  349. if ( line )
  350. fl_begin_line();
  351. else
  352. fl_begin_polygon();
  353. fl_vertex( 0.0, 0.0 );
  354. fl_vertex( 0.0, 1.0 );
  355. // if ( draw_real_fade_curve )
  356. {
  357. nframes_t tsx = timeline->x_to_ts( 1 );
  358. nframes_t ts = 0;
  359. for ( int i = 0; i < width; ++i, ts += tsx )
  360. fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
  361. }
  362. fl_vertex( 1.0, 0.0 );
  363. if ( line )
  364. fl_end_line();
  365. else
  366. fl_end_polygon();
  367. fl_pop_matrix();
  368. }
  369. struct Peaks_Redraw_Request {
  370. Audio_Region *region;
  371. nframes_t start;
  372. nframes_t end;
  373. Peaks_Redraw_Request ( Audio_Region *region, nframes_t start, nframes_t end ) : region( region ), start( start), end( end )
  374. {
  375. }
  376. };
  377. /* static wrapper */
  378. void
  379. Audio_Region::peaks_pending_cb ( void *v )
  380. {
  381. Peaks_Redraw_Request *r = (Peaks_Redraw_Request*)v;
  382. r->region->peaks_pending_cb( r );
  383. }
  384. void
  385. Audio_Region::peaks_pending_cb ( Peaks_Redraw_Request *r )
  386. {
  387. int npeaks = timeline->ts_to_x( r->end - r->start );
  388. if ( _clip->peaks()->ready( r->start, npeaks, timeline->fpp() ) )
  389. {
  390. printf( "damaging from timeout\n" );
  391. /* FIXME: only need to damage the affected area! */
  392. timeline->damage( FL_DAMAGE_ALL, x(), y(), w(), h() );
  393. delete r;
  394. }
  395. else
  396. Fl::repeat_timeout( 0.1f, &Audio_Region::peaks_pending_cb, (void*)r );
  397. }
  398. void
  399. Audio_Region::draw_box( void )
  400. {
  401. /* dirty hack to keep the box from flipping to vertical at small sizes */
  402. fl_push_clip( x(), y(), w(), h() );
  403. Fl_Color selection_color = _selection_color;
  404. Fl_Color color = Audio_Region::inherit_track_color ? sequence()->track()->color() : _box_color;
  405. color = fl_color_average( color, sequence()->color(), 0.75f );
  406. if ( this == ((Audio_Sequence*)sequence())->capture_region() )
  407. {
  408. color = FL_RED;
  409. }
  410. else if ( ! active_r() )
  411. {
  412. color = fl_inactive( color );
  413. selection_color = fl_inactive( selection_color );
  414. }
  415. if ( selected() )
  416. fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), selection_color );
  417. else
  418. fl_draw_box( box(), x() - 10, y(), w() + 50, h(), color );
  419. /* draw fades */
  420. draw_fade( _fade_in, Fade::In, false, x(), w() );
  421. draw_fade( _fade_out, Fade::Out, false, x(), w() );
  422. fl_pop_clip();
  423. }
  424. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  425. void
  426. Audio_Region::draw ( void )
  427. {
  428. /* intersect clip with region */
  429. int X, Y, W, H;
  430. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  431. if ( ! ( W > 0 && H > 0 ) )
  432. /* no coverage */
  433. return;
  434. /* account for waveform outlines... */
  435. X -= 2;
  436. W += 4;
  437. int OX = scroll_x();
  438. int ox = timeline->ts_to_x( _r->start );
  439. if ( ox > OX + sequence()->w() ||
  440. ox < OX && ox + abs_w() < OX )
  441. /* not in viewport */
  442. return;
  443. int rw = timeline->ts_to_x( _r->length );
  444. /* calculate waveform offset due to scrolling */
  445. nframes_t offset = 0;
  446. if ( ox < OX )
  447. {
  448. offset = timeline->x_to_ts( OX - ox );
  449. rw -= OX - ox;
  450. }
  451. rw = min( rw, sequence()->w() );
  452. int rx = x();
  453. fl_push_clip( rx, Y, rw, H );
  454. /* get actual peak data */
  455. int channels;
  456. int peaks;
  457. Peak *pbuf;
  458. // const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
  459. // nframes_t start = _r->start + offset;
  460. nframes_t start = _r->offset + offset;
  461. /* compensate for ??? */
  462. if ( X - rx > 0 )
  463. start += timeline->x_to_ts( X - rx );
  464. const int peaks_needed = min( timeline->ts_to_x( _clip->length() - start ), W );
  465. const nframes_t end = start + timeline->x_to_ts( peaks_needed );
  466. if ( _clip->read_peaks( timeline->fpp(),
  467. start,
  468. end,
  469. &peaks, &pbuf, &channels ) &&
  470. peaks )
  471. {
  472. assert( pbuf );
  473. /* draw fade curve outlines--this is only here because of crossfades */
  474. draw_fade( _fade_in, Fade::In, true, X, W );
  475. draw_fade( _fade_out, Fade::Out, true, X, W );
  476. int ch = (h() - Fl::box_dh( box() )) / channels;
  477. Waveform::scale( pbuf, peaks * channels, _scale );
  478. for ( int i = 0; i < channels; ++i )
  479. {
  480. // Peak *pb = pbuf + (peaks * i);
  481. /* int fw = timeline->ts_to_x( fade.length ); */
  482. /* /\* if ( draw_fade_waveform ) *\/ */
  483. /* for ( int j = min( fw, peaks ); j--; ) */
  484. /* { */
  485. /* const float g = fade.gain( j * timeline->fpp() ); */
  486. /* pb[ j ].min *= g; */
  487. /* pb[ j ].max *= g; */
  488. /* } */
  489. Waveform::draw( X,
  490. (y() + Fl::box_dy( box() )) + (i * ch),
  491. W,
  492. ch,
  493. pbuf + i, peaks, channels,
  494. selected() ? fl_invert_color( _color ) : _color );
  495. }
  496. }
  497. if ( peaks < peaks_needed )
  498. {
  499. /* couldn't read peaks--perhaps they're being generated. Try again later. */
  500. Fl::add_timeout( 0.1f, &Audio_Region::peaks_pending_cb,
  501. new Peaks_Redraw_Request( this, start + timeline->x_to_ts( peaks ), end ) );
  502. }
  503. timeline->draw_measure_lines( X, Y, W, H, _box_color );
  504. /* fl_color( FL_BLACK ); */
  505. /* fl_line( rx, Y, rx, Y + H ); */
  506. /* fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H ); */
  507. if ( _clip->dummy() )
  508. {
  509. char pat[256];
  510. snprintf( pat, sizeof( pat ), "Missing Source!: %s", _clip->name() );
  511. draw_label( pat, align() );
  512. }
  513. else
  514. draw_label( _clip->name(), align() );
  515. /* if ( current() ) */
  516. /* { */
  517. /* /\* draw length bubble *\/ */
  518. /* char pat[40]; */
  519. /* snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() ); */
  520. /* draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN ); */
  521. /* } */
  522. fl_pop_clip();
  523. }
  524. void
  525. Audio_Region::normalize ( void )
  526. {
  527. int peaks, channels;
  528. Peak *pbuf;
  529. if ( _clip->read_peaks( length(), offset(), offset() + length(), &peaks, &pbuf, &channels ) &&
  530. peaks )
  531. _scale = pbuf->normalization_factor();
  532. }