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.

915 lines
24KB

  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 "Transport.H"
  31. #include "const.h"
  32. #include "debug.h"
  33. #include <algorithm>
  34. using std::min;
  35. using std::max;
  36. /* defined in timeline.C */
  37. extern void draw_full_arrow_symbol ( Fl_Color color );
  38. extern Timeline *timeline;
  39. extern Transport *transport;
  40. bool Audio_Region::inherit_track_color = true;
  41. bool Audio_Region::show_box = true;
  42. Fl_Boxtype Audio_Region::_box = FL_FLAT_BOX;
  43. Fl_Color Audio_Region::_selection_color = FL_MAGENTA;
  44. static Fl_Color fl_invert_color ( Fl_Color c )
  45. {
  46. unsigned char r, g, b;
  47. Fl::get_color( c, r, g, b );
  48. return fl_rgb_color( 255 - r, 255 - g, 255 - b );
  49. }
  50. void
  51. Audio_Region::get ( Log_Entry &e ) const
  52. {
  53. e.add( ":source", _clip ? _clip->name() : "" );
  54. e.add( ":gain", _scale );
  55. e.add( ":fade-in-type", _fade_in.type );
  56. e.add( ":fade-in-duration", _fade_in.length );
  57. e.add( ":fade-out-type", _fade_out.type );
  58. e.add( ":fade-out-duration", _fade_out.length );
  59. Sequence_Region::get( e );
  60. e.add( ":offset", _r->offset );
  61. e.add( ":loop", _loop );
  62. }
  63. void
  64. Audio_Region::set ( Log_Entry &e )
  65. {
  66. for ( int i = 0; i < e.size(); ++i )
  67. {
  68. const char *s, *v;
  69. e.get( i, &s, &v );
  70. if ( ! strcmp( s, ":gain" ) )
  71. _scale = atof( v );
  72. else if ( ! strcmp( s, ":fade-in-type" ) )
  73. _fade_in.type = (Fade::fade_type_e)atoi( v );
  74. else if ( ! strcmp( s, ":fade-in-duration" ) )
  75. _fade_in.length = atoll( v );
  76. else if ( ! strcmp( s, ":fade-out-type" ) )
  77. _fade_out.type = (Fade::fade_type_e)atoi( v );
  78. else if ( ! strcmp( s, ":fade-out-duration" ) )
  79. _fade_out.length = atoll( v );
  80. else if ( ! strcmp( s, ":offset" ) )
  81. _r->offset = atoll( v );
  82. else if ( ! strcmp( s, ":loop" ) )
  83. _loop = atoll( v );
  84. else if ( ! strcmp( s, ":source" ) )
  85. {
  86. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  87. {
  88. printf( "Grave error: could not open source \"%s\"\n", v );
  89. }
  90. }
  91. }
  92. Sequence_Region::set( e );
  93. }
  94. void
  95. Audio_Region::init ( void )
  96. {
  97. _adjusting_gain = 0;
  98. _loop = 0;
  99. _sequence = NULL;
  100. _scale = 1.0f;
  101. _clip = NULL;
  102. _color = FL_FOREGROUND_COLOR;
  103. _box_color = FL_GRAY;
  104. _fade_in.length = 256;
  105. _fade_in.type = Fade::Sigmoid;
  106. _fade_out = _fade_in;
  107. }
  108. /* copy constructor */
  109. Audio_Region::Audio_Region ( const Audio_Region & rhs ) : Sequence_Region( rhs )
  110. {
  111. // *((Sequence_Region*)this) = (Sequence_Region &)rhs;
  112. _clip = rhs._clip->duplicate();
  113. _scale = rhs._scale;
  114. _fade_in = rhs._fade_in;
  115. _fade_out = rhs._fade_out;
  116. _loop = rhs._loop;
  117. _box_color = rhs._box_color;
  118. _color = rhs._color;
  119. _adjusting_gain = false;
  120. log_create();
  121. }
  122. /* */
  123. Audio_Region::Audio_Region ( Audio_File *c )
  124. {
  125. init();
  126. _clip = c;
  127. _r->length = _clip->length();
  128. log_create();
  129. }
  130. /* used when DND importing */
  131. Audio_Region::Audio_Region ( Audio_File *c, Sequence *t, nframes_t o )
  132. {
  133. init();
  134. _clip = c;
  135. _sequence = t;
  136. _r->offset = 0;
  137. _r->start = o;
  138. _r->length = _clip->length();
  139. sequence()->add( this );
  140. int sum = 0;
  141. const char *s = rindex( _clip->name(), '/' );
  142. if ( ! s )
  143. s = _clip->name();
  144. for ( int i = strlen( s ); i--; )
  145. sum += s[ i ];
  146. while ( sum >> 8 )
  147. sum = (sum & 0xFF) + (sum >> 8);
  148. _box_color = (Fl_Color)sum;
  149. log_create();
  150. }
  151. Audio_Region::~Audio_Region ( )
  152. {
  153. log_destroy();
  154. _clip->release();
  155. }
  156. void
  157. Audio_Region::menu_cb ( Fl_Widget *w, void *v )
  158. {
  159. ((Audio_Region*)v)->menu_cb( (Fl_Menu_*) w );
  160. }
  161. void
  162. Audio_Region::menu_cb ( const Fl_Menu_ *m )
  163. {
  164. char picked[256];
  165. m->item_pathname( picked, sizeof( picked ) );
  166. Logger log( this );
  167. if ( ! strcmp( picked, "Fade/In/Linear" ) )
  168. _fade_in.type = Fade::Linear;
  169. else if ( ! strcmp( picked, "Fade/In/Sigmoid" ) )
  170. _fade_in.type = Fade::Sigmoid;
  171. else if ( ! strcmp( picked, "Fade/In/Logarithmic" ) )
  172. _fade_in.type = Fade::Logarithmic;
  173. else if ( ! strcmp( picked, "Fade/In/Parabolic" ) )
  174. _fade_in.type = Fade::Parabolic;
  175. else if ( ! strcmp( picked, "Fade/Out/Linear" ) )
  176. _fade_out.type = Fade::Linear;
  177. else if ( ! strcmp( picked, "Fade/Out/Sigmoid" ) )
  178. _fade_out.type = Fade::Sigmoid;
  179. else if ( ! strcmp( picked, "Fade/Out/Logarithmic" ) )
  180. _fade_out.type = Fade::Logarithmic;
  181. else if ( ! strcmp( picked, "Fade/Out/Parabolic" ) )
  182. _fade_out.type = Fade::Parabolic;
  183. else if ( ! strcmp( picked, "/Color" ) )
  184. box_color( fl_show_colormap( box_color() ) );
  185. else if ( ! strcmp( picked, "/Split at mouse" ) )
  186. {
  187. Loggable::block_start();
  188. split( timeline->x_to_offset( Fl::event_x() ) );
  189. log_end();
  190. Loggable::block_end();
  191. log_start();
  192. }
  193. else if ( ! strcmp( picked, "/Crop to range" ) )
  194. {
  195. trim_left( timeline->range_start() );
  196. trim_right( timeline->range_end() );
  197. }
  198. else if ( ! strcmp( picked, "/Fade in to mouse" ) )
  199. {
  200. nframes_t offset = x_to_offset( Fl::event_x() );
  201. if ( offset < length() )
  202. _fade_in.length = offset;
  203. DMESSAGE( "set fade in duration" );
  204. }
  205. else if ( ! strcmp( picked, "/Fade out to mouse" ) )
  206. {
  207. long offset = length() - x_to_offset( Fl::event_x() );
  208. if ( offset > 0 )
  209. _fade_out.length = offset;
  210. }
  211. else if ( ! strcmp( picked, "/Gain with mouse vertical drag" ) )
  212. {
  213. /* float g = h() / (y() - Fl::event_y() ); */
  214. /* _scale = g; */
  215. }
  216. else if ( ! strcmp( picked, "/Loop point to mouse" ) )
  217. {
  218. nframes_t offset = x_to_offset( Fl::event_x() );
  219. if ( offset > 0 )
  220. {
  221. nframes_t f = offset + _r->start;
  222. if ( timeline->nearest_line( &f, false ) )
  223. _loop = f - _r->start;
  224. else
  225. _loop = offset;
  226. }
  227. }
  228. else if ( ! strcmp( picked, "/Clear loop point" ) )
  229. _loop = 0;
  230. else if ( ! strcmp( picked, "/Normalize" ) )
  231. normalize();
  232. else if ( ! strcmp( picked, "/Denormalize" ) )
  233. _scale = 1.0;
  234. else if ( ! strcmp( picked, "/Range from" ) )
  235. timeline->range( start(), length() );
  236. else if ( ! strcmp( picked, "/Remove" ) )
  237. remove();
  238. else
  239. FATAL( "Unknown menu choice \"%s\"", picked );
  240. redraw();
  241. }
  242. #include "FL/test_press.H"
  243. #include "FL/menu_popup.H"
  244. /** build the context menu for this region */
  245. Fl_Menu_Button &
  246. Audio_Region::menu ( void )
  247. {
  248. static Fl_Menu_Button m( 0, 0, 0, 0, "Region" );
  249. Fade::fade_type_e it = _fade_in.type;
  250. Fade::fade_type_e ot = _fade_out.type;
  251. Fl_Menu_Item items[] =
  252. {
  253. { "Fade", 0, 0, 0, FL_SUBMENU },
  254. { "In", 0, 0, 0, FL_SUBMENU },
  255. { "Linear", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  256. { "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  257. { "Logarithmic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  258. { "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  259. { 0 },
  260. { "Out", 0, 0, 0, FL_SUBMENU },
  261. { "Linear", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Linear ? FL_MENU_VALUE : 0 ) },
  262. { "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
  263. { "Logarithmic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
  264. { "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
  265. { 0 },
  266. { 0 },
  267. { "Color", 0, 0, 0, inherit_track_color ? FL_MENU_INACTIVE : 0 },
  268. { "Split at mouse", 's', 0, 0 },
  269. { "Crop to range", 'c', 0, 0 },
  270. { "Gain with mouse vertical drag", 'g', 0, 0 },
  271. { "Fade in to mouse", FL_F + 3, 0, 0 },
  272. { "Fade out to mouse", FL_F + 4, 0, 0 },
  273. { "Loop point to mouse", 'l', 0, 0 },
  274. { "Clear loop point", FL_SHIFT + 'l', 0, 0, 0 == _loop ? FL_MENU_INACTIVE : 0 },
  275. { "Normalize", 'n', 0, 0 },
  276. { "Denormalize", FL_SHIFT + 'n', 0, 0, 1.0 == _scale ? FL_MENU_INACTIVE : 0 },
  277. { "Range from", FL_CTRL + 'r', 0, 0 },
  278. { "Remove", 0, 0, 0 },
  279. { 0 },
  280. };
  281. menu_set_callback( items, &Audio_Region::menu_cb, (void*)this );
  282. m.copy( items, (void*)this );
  283. return m;
  284. }
  285. /** Draws the curve for a single fade. /X/ and /W/ repersent the
  286. portion of the region covered by this draw, which may or may not
  287. cover the fade in question. */
  288. void
  289. Audio_Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
  290. {
  291. const int dy = y() + Fl::box_dy( box() );
  292. const int dh = h() - Fl::box_dh( box() );
  293. const int height = dh;
  294. const int width = timeline->ts_to_x( fade.length );
  295. if ( width < 4 )
  296. /* too small to draw */
  297. return;
  298. int fx;
  299. if ( dir == Fade::In )
  300. {
  301. fx = line_x();
  302. if ( fx + width < X ||
  303. fx > X + W )
  304. /* clipped */
  305. return;
  306. }
  307. else
  308. {
  309. fx = line_x() + abs_w();
  310. if ( fx - width > X + W ||
  311. fx < X )
  312. /* clipped */
  313. return;
  314. }
  315. if ( line )
  316. fl_begin_line();
  317. else
  318. fl_begin_polygon();
  319. fl_vertex( fx, dy );
  320. fl_vertex( fx, dy + height );
  321. {
  322. nframes_t tsx = timeline->x_to_ts( 1 );
  323. if ( dir == Fade::In )
  324. {
  325. nframes_t ts = 0;
  326. for ( int i = 0; i < width; ++i, ts += tsx )
  327. fl_vertex( fx + i, dy + height - ( height * fade.gain( ts / (double)fade.length ) ));
  328. }
  329. else
  330. {
  331. nframes_t ts = tsx * width;
  332. for ( int i = 0; i < width; ++i, ts -= tsx )
  333. fl_vertex( fx - i, dy + ( height * fade.gain( ts / (double)fade.length ) ));
  334. }
  335. }
  336. if ( dir == Fade::In )
  337. fl_vertex( fx + width, dy );
  338. else
  339. fl_vertex( fx - width, dy );
  340. if ( line )
  341. fl_end_line();
  342. else
  343. fl_end_polygon();
  344. }
  345. Fl_Color
  346. Audio_Region::actual_box_color ( void ) const
  347. {
  348. return Audio_Region::inherit_track_color ? sequence()->track()->color() : _box_color;
  349. }
  350. void
  351. Audio_Region::draw_box( void )
  352. {
  353. fl_push_clip( x(), y(), w(), h() );
  354. Fl_Color selection_color = _selection_color;
  355. Fl_Color color = actual_box_color();
  356. color = fl_color_average( color, sequence()->color(), 0.75f );
  357. if ( recording() )
  358. {
  359. color = FL_RED;
  360. }
  361. else if ( ! active_r() )
  362. {
  363. color = fl_inactive( color );
  364. selection_color = fl_inactive( selection_color );
  365. }
  366. Fl_Boxtype b;
  367. Fl_Color c = selected() ? selection_color : color;
  368. if ( Audio_Region::show_box )
  369. {
  370. b = box();
  371. }
  372. else
  373. {
  374. b = FL_DOWN_FRAME;
  375. }
  376. fl_draw_box( b, x(), y(), w(), h(), c );
  377. fl_pop_clip();
  378. }
  379. void
  380. Audio_Region::peaks_ready_callback ( void *v )
  381. {
  382. DMESSAGE("Damaging region from peaks ready callback");
  383. Fl::lock();
  384. ((Audio_Region*)v)->redraw();
  385. Fl::unlock();
  386. Fl::awake();
  387. }
  388. bool
  389. Audio_Region::recording ( void ) const
  390. {
  391. return this == sequence()->track()->capture_region();
  392. }
  393. /** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
  394. void
  395. Audio_Region::draw ( void )
  396. {
  397. /* intersect clip with region */
  398. int X, Y, W, H;
  399. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  400. if ( ! ( W > 0 && H > 0 ) )
  401. /* no coverage */
  402. return;
  403. if ( start() > timeline->xoffset + timeline->x_to_ts( sequence()->w() ) ||
  404. start() + length() < timeline->xoffset )
  405. /* not in viewport */
  406. return;
  407. fl_push_clip( X, Y, W, H );
  408. /* overdraw a little to avoid artifacts when scrolling */
  409. W += 2;
  410. Fl_Color c = selected() ? fl_invert_color( _color ) : _color;
  411. if ( sequence()->damage() & FL_DAMAGE_USER1 &&
  412. recording() )
  413. {
  414. /* TODO: limit drawing. */
  415. }
  416. /* calculate waveform offset due to scrolling */
  417. /* offset is the number of frames into the waveform the value of X translates to */
  418. nframes_t x_frame = timeline->xoffset + timeline->x_to_ts( X - _sequence->x() );
  419. nframes_t offset = 0;
  420. if ( x_frame < start() )
  421. /* sometimes X is one pixel too soon... */
  422. offset = 0;
  423. else
  424. offset = x_frame - start();
  425. nframes_t fo = 0;
  426. nframes_t ostart = 0, oend = 0;
  427. const int total_peaks_needed = W;
  428. nframes_t total_frames_needed = timeline->x_to_ts( total_peaks_needed );
  429. {
  430. Fl_Color c = fl_color_average( FL_DARK1,
  431. Audio_Region::inherit_track_color ? sequence()->track()->color() : _box_color,
  432. 0.75f );
  433. fl_color( fl_color_add_alpha( c, 127 ) );
  434. draw_fade( _fade_in, Fade::In, false, X, W );
  435. draw_fade( _fade_out, Fade::Out, false, X, W );
  436. }
  437. int channels = 0;
  438. int peaks = 0;
  439. Peak *pbuf = NULL;
  440. do {
  441. nframes_t start = _r->offset;
  442. nframes_t loop_frames_needed = _loop ? _loop : total_frames_needed;
  443. int loop_peaks_needed = timeline->ts_to_x( loop_frames_needed );
  444. Fl_Color c = Fl::get_color( _color );
  445. if ( recording() )
  446. {
  447. // loop_peaks_needed = timeline->ts_to_x( _range.length );
  448. c = FL_BLACK;
  449. }
  450. c = fl_color_add_alpha( c, 220 );
  451. if ( ! fo ) /* first loop... */
  452. {
  453. if ( _loop )
  454. start += offset % _loop;
  455. else
  456. start += offset;
  457. /* DMESSAGE( "offset = %lu", (unsigned long) offset ); */
  458. /* DMESSAGE( "loop peaks needed = %d", loop_peaks_needed ); */
  459. if ( _loop )
  460. {
  461. loop_frames_needed -= offset % loop_frames_needed;
  462. loop_peaks_needed = timeline->ts_to_x( loop_frames_needed );
  463. }
  464. /* DMESSAGE( "loop peaks needed = %d", loop_peaks_needed ); */
  465. assert( loop_peaks_needed >= 0 );
  466. }
  467. if ( fo + loop_frames_needed > total_frames_needed )
  468. {
  469. loop_frames_needed -= ( fo + loop_frames_needed ) - total_frames_needed;
  470. loop_peaks_needed = timeline->ts_to_x( loop_frames_needed );
  471. }
  472. if ( !loop_peaks_needed )
  473. break;
  474. const nframes_t end = start + loop_frames_needed;
  475. if ( start != ostart || end != oend )
  476. {
  477. if ( _clip->peaks()->peakfile_ready() )
  478. {
  479. if ( _clip->read_peaks( timeline->fpp(),
  480. start,
  481. end,
  482. &peaks, &pbuf, &channels ) )
  483. {
  484. Waveform::scale( pbuf, peaks * channels, _scale );
  485. ostart = start;
  486. oend = end;
  487. }
  488. }
  489. else
  490. {
  491. if ( ! transport->rolling )
  492. {
  493. /* create a thread to make the peaks */
  494. _clip->peaks()->make_peaks_asynchronously( Audio_Region::peaks_ready_callback, this );
  495. }
  496. }
  497. }
  498. else
  499. {
  500. // DMESSAGE( "using cached peaks" );
  501. }
  502. if ( peaks && pbuf )
  503. {
  504. int ch = (h() - Fl::box_dh( box() )) / channels;
  505. int xo = timeline->ts_to_x( fo );
  506. for ( int i = 0; i < channels; ++i )
  507. {
  508. Waveform::draw( X + xo,
  509. (y() + Fl::box_dy( box() )) + (i * ch),
  510. loop_peaks_needed,
  511. ch,
  512. pbuf + i, peaks, channels,
  513. c );
  514. }
  515. }
  516. else
  517. WARNING( "Pbuf == %p, peaks = %lu", pbuf, (unsigned long)peaks );
  518. if ( peaks < loop_peaks_needed )
  519. {
  520. DMESSAGE( "Peak read came up %lu peaks short", (unsigned long)loop_peaks_needed - peaks );
  521. }
  522. fo += loop_frames_needed;
  523. }
  524. while ( _loop && fo < total_frames_needed );
  525. if ( _loop && offset < _loop )
  526. {
  527. const int lx = get_x( start() + _loop );
  528. if ( lx < X + W )
  529. {
  530. fl_color( FL_RED );
  531. fl_line_style( FL_DASH, 0 );
  532. fl_line( lx, y(), lx, y() + h() );
  533. fl_line_style( FL_SOLID, 0 );
  534. }
  535. }
  536. if ( _adjusting_gain )
  537. {
  538. fl_color( fl_color_add_alpha( FL_DARK1, 127 ) );
  539. fl_rectf( X, ( y() + h() ) - ( h() * ( _scale * 0.25 ) ), X + W, y() + h() );
  540. fl_line_style( FL_DASH, 1 );
  541. fl_color( fl_color_add_alpha( FL_GREEN, 200 ) );
  542. float j = 5;
  543. for ( int i = y() + h(); i > y(); i -= j, j *= 1.2 )
  544. {
  545. fl_line( X, i, X + W, i );
  546. }
  547. fl_line_style( FL_SOLID, 0 );
  548. }
  549. /* if ( current() ) */
  550. /* { */
  551. /* /\* draw length bubble *\/ */
  552. /* char pat[40]; */
  553. /* snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() ); */
  554. /* draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN ); */
  555. /* } */
  556. fl_pop_clip();
  557. }
  558. void
  559. Audio_Region::draw_label ( void )
  560. {
  561. if ( _clip->dummy() )
  562. {
  563. char pat[256];
  564. snprintf( pat, sizeof( pat ), "Missing Source!: %s", _clip->name() );
  565. draw_label( pat, align() );
  566. }
  567. else
  568. draw_label( _clip->name(), align() );
  569. }
  570. /** split region at absolute frame /where/ */
  571. void
  572. Audio_Region::split ( nframes_t where )
  573. {
  574. nframes_t old_fade_in = _fade_in.length;
  575. _fade_in.length = 256;
  576. Audio_Region *copy = new Audio_Region( *this );
  577. Logger _log( copy );
  578. _fade_in.length = old_fade_in;
  579. _fade_out.length = 256;
  580. Sequence_Region::split( copy, where );
  581. }
  582. int
  583. Audio_Region::handle ( int m )
  584. {
  585. static int ox, oy;
  586. static bool copied = false;
  587. static nframes_t os;
  588. int X = Fl::event_x();
  589. int Y = Fl::event_y();
  590. Logger _log( this );
  591. switch ( m )
  592. {
  593. case FL_FOCUS:
  594. case FL_UNFOCUS:
  595. return 1;
  596. case FL_KEYUP:
  597. if ( Fl::event_key() == 'g' )
  598. {
  599. _adjusting_gain = false;
  600. redraw();
  601. return 1;
  602. }
  603. break;
  604. case FL_KEYBOARD:
  605. if ( Fl::event_key() == 'g' )
  606. {
  607. _adjusting_gain = true;
  608. redraw();
  609. return 1;
  610. }
  611. return menu().test_shortcut() != 0;
  612. case FL_ENTER:
  613. return Sequence_Region::handle( m );
  614. case FL_LEAVE:
  615. return Sequence_Region::handle( m );
  616. case FL_PUSH:
  617. {
  618. if ( Fl::event_key() == 'g' )
  619. return 1;
  620. /* splitting */
  621. if ( test_press( FL_BUTTON2 | FL_SHIFT ) )
  622. {
  623. /* split */
  624. if ( ! copied )
  625. {
  626. Loggable::block_start();
  627. split( timeline->x_to_offset( X ) );
  628. log_end();
  629. Loggable::block_end();
  630. log_start();
  631. }
  632. return 0;
  633. }
  634. else
  635. {
  636. ox = x() - X;
  637. oy = y() - Y;
  638. /* for panning */
  639. os = _r->offset;
  640. if ( test_press( FL_BUTTON2 | FL_CTRL ) )
  641. {
  642. normalize();
  643. return 1;
  644. }
  645. else if ( test_press( FL_BUTTON3 ) )
  646. {
  647. /* context menu */
  648. menu_popup( &menu() );
  649. return 1;
  650. }
  651. else
  652. return Sequence_Region::handle( m );
  653. }
  654. break;
  655. }
  656. case FL_RELEASE:
  657. {
  658. Sequence_Region::handle( m );
  659. copied = false;
  660. return 1;
  661. }
  662. case FL_DRAG:
  663. if ( ! _drag )
  664. {
  665. begin_drag( Drag( x() - X, y() - Y, x_to_offset( X ) ) );
  666. _log.hold();
  667. }
  668. if ( Fl::event_key() == 'g' )
  669. {
  670. float d = (float)h() / ( y() - Fl::event_y() );
  671. _scale = -0.5f * d;
  672. redraw();
  673. return 1;
  674. }
  675. if ( test_press( FL_BUTTON1 | FL_SHIFT | FL_CTRL ) )
  676. {
  677. /* panning */
  678. int d = (ox + X) - x();
  679. bool negative = d < 0;
  680. if ( d < 0 )
  681. _r->offset = os + timeline->x_to_ts( 0 - d );
  682. else
  683. {
  684. if ( os < timeline->x_to_ts( d ) )
  685. _r->offset = 0;
  686. else
  687. _r->offset = os - timeline->x_to_ts( d );
  688. }
  689. redraw();
  690. return 1;
  691. }
  692. return Sequence_Region::handle( m );
  693. default:
  694. return Sequence_Region::handle( m );
  695. break;
  696. }
  697. return 0;
  698. }
  699. /**********/
  700. /* Public */
  701. /**********/
  702. /** return the name of the audio source this region represents */
  703. const char *
  704. Audio_Region::source_name ( void ) const
  705. {
  706. return _clip->name();
  707. }
  708. /** set the amplitude scaling for this region from the normalization
  709. * factor for the range of samples represented by this region */
  710. void
  711. Audio_Region::normalize ( void )
  712. {
  713. int peaks, channels;
  714. Peak *pbuf;
  715. const nframes_t npeaks = _loop ? _loop : length();
  716. if ( _clip->read_peaks( npeaks, offset(), offset() + npeaks, &peaks, &pbuf, &channels ) &&
  717. peaks )
  718. _scale = pbuf->normalization_factor();
  719. /* FIXME: wrong place for this? */
  720. sequence()->handle_widget_change( start(), length() );
  721. redraw();
  722. }