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.

693 lines
19KB

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