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.

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