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.

805 lines
22KB

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