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.

733 lines
20KB

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