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.

923 lines
25KB

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