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.

305 lines
7.2KB

  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. /* An Audio_Sequence is a sequence of Audio_Regions. Takes and 'track
  19. * contents' consist of these objects */
  20. #include <sys/time.h>
  21. #include <Fl/fl_ask.H>
  22. #include "Audio_Sequence.H"
  23. #include "Waveform.H"
  24. #include <list>
  25. using namespace std;
  26. #include "Track.H"
  27. #include "Engine/Audio_File.H" // for ::from_file()
  28. #include "Transport.H" // for locate()
  29. Audio_Sequence::Audio_Sequence ( Track *track, const char *name ) : Sequence( track )
  30. {
  31. _track = track;
  32. if ( name )
  33. Audio_Sequence::name( name );
  34. else
  35. {
  36. struct timeval tv;
  37. gettimeofday( &tv, NULL );
  38. time_t t = tv.tv_sec;
  39. char s[40];
  40. ctime_r( &t, s );
  41. s[ strlen( s ) - 1 ] = 0;
  42. Audio_Sequence::name( s );
  43. }
  44. if ( track )
  45. track->add( this );
  46. log_create();
  47. /* FIXME: temporary */
  48. labeltype( FL_NO_LABEL );
  49. }
  50. Audio_Sequence::~Audio_Sequence ( )
  51. {
  52. Loggable::block_start();
  53. clear();
  54. log_destroy();
  55. track()->remove( this );
  56. Loggable::block_end();
  57. }
  58. /** return a pointer to the current capture region for this sequence */
  59. const Audio_Region *
  60. Audio_Sequence::capture_region ( void ) const
  61. {
  62. return track()->capture_region();
  63. }
  64. void
  65. Audio_Sequence::get ( Log_Entry &e ) const
  66. {
  67. e.add( ":track", _track );
  68. e.add( ":name", name() );
  69. }
  70. void
  71. Audio_Sequence::set ( Log_Entry &e )
  72. {
  73. for ( int i = 0; i < e.size(); ++i )
  74. {
  75. const char *s, *v;
  76. e.get( i, &s, &v );
  77. if ( ! strcmp( ":track", s ) )
  78. {
  79. int i;
  80. sscanf( v, "%X", &i );
  81. Track *t = (Track*)Loggable::find( i );
  82. assert( t );
  83. t->sequence( this );
  84. }
  85. else if ( ! strcmp( ":name", s ) )
  86. name( v );
  87. }
  88. }
  89. static
  90. void
  91. deurlify ( char *url )
  92. {
  93. char *r, *w;
  94. r = w = url;
  95. for ( ; *r; r++, w++ )
  96. {
  97. if ( *r == '%' )
  98. {
  99. char data[3] = { *(r + 1), *(r + 2), 0 };
  100. int c;
  101. sscanf( data, "%2X", &c );
  102. *w = c;
  103. r += 2;
  104. }
  105. else
  106. *w = *r;
  107. }
  108. *w = NULL;
  109. }
  110. void
  111. Audio_Sequence::handle_widget_change ( nframes_t start, nframes_t length )
  112. {
  113. Sequence::handle_widget_change( start, length );
  114. /* a region has changed. we may need to rebuffer... */
  115. /* trigger rebuffer */
  116. /* FIXME: we really only need to rebuffer *this* sequence! */
  117. /* FIXME: how does this fit into the selection? */
  118. if ( transport->rolling && ( start > transport->frame || start + length > transport->frame ) )
  119. transport->locate( transport->frame );
  120. }
  121. void
  122. Audio_Sequence::draw ( void )
  123. {
  124. Sequence::draw();
  125. int xfades = 0;
  126. /* draw crossfades */
  127. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  128. {
  129. Sequence_Widget *o = overlaps( *r );
  130. if ( o )
  131. {
  132. if ( *o <= **r )
  133. {
  134. /* if ( o->x() == (*r)->x() && o->w() == (*r)->w() ) */
  135. /* printf( "complete superposition\n" ); */
  136. if ( o->contains( *r ) )
  137. /* completely inside */
  138. continue;
  139. ++xfades;
  140. Rectangle b( (*r)->x(),
  141. o->y(),
  142. (o->x() + o->w()) - (*r)->x(),
  143. o->h() );
  144. Fl_Color c = fl_color_average( o->box_color(), (*r)->box_color(), 0.50f );
  145. c = fl_color_average( c, FL_YELLOW, 0.30f );
  146. fl_push_clip( b.x, b.y, b.w, b.h );
  147. draw_box( FL_FLAT_BOX, b.x - 100, b.y, b.w + 200, b.h, c );
  148. draw_box( FL_UP_FRAME, b.x - 100, b.y, b.w + 200, b.h, c );
  149. fl_pop_clip();
  150. }
  151. }
  152. }
  153. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  154. {
  155. Sequence_Widget *o = overlaps( *r );
  156. if ( o )
  157. {
  158. if ( *o <= **r )
  159. {
  160. if ( o->contains( *r ) )
  161. /* completely inside */
  162. continue;
  163. Rectangle b( (*r)->x(), o->y(), (o->x() + o->w()) - (*r)->x(), o->h() );
  164. /* draw overlapping waveforms in X-ray style. */
  165. bool t = Waveform::fill;
  166. Waveform::fill = false;
  167. fl_push_clip( b.x, b.y, b.w, b.h );
  168. o->draw();
  169. (*r)->draw();
  170. fl_pop_clip();
  171. Waveform::fill = t;
  172. }
  173. }
  174. }
  175. }
  176. /** event handler that supports DND of audio clips */
  177. int
  178. Audio_Sequence::handle ( int m )
  179. {
  180. switch ( m )
  181. {
  182. case FL_PASTE:
  183. {
  184. const char *text = Fl::event_text();
  185. if ( ! strcmp( text, "Audio_Region" ) )
  186. return 1;
  187. char *file;
  188. if ( ! sscanf( text, "file://%a[^\r\n]\n", &file ) )
  189. {
  190. printf( "invalid drop \"%s\"\n", text );
  191. return 0;
  192. }
  193. deurlify( file );
  194. printf( "pasted file \"%s\"\n", file );
  195. fl_cursor( FL_CURSOR_WAIT );
  196. Fl::check();
  197. Audio_File *c = Audio_File::from_file( file );
  198. fl_cursor( FL_CURSOR_DEFAULT );
  199. if ( ! c )
  200. {
  201. fl_alert( "Could not import file \"%s\": Unsupported file type.", file );
  202. printf( "could not open file\n" );
  203. free( file );
  204. return 0;
  205. }
  206. free( file );
  207. // Audio_Region *r =
  208. new Audio_Region( c, this, timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() ) );
  209. redraw();
  210. return 1;
  211. }
  212. default:
  213. return Sequence::handle( m );
  214. }
  215. }