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.

247 lines
7.3KB

  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. #pragma once
  19. #include "Audio_File.H"
  20. #include "Track.H"
  21. #include "Timeline.H"
  22. /* Regions are "virtual" FLTK widgets; this is necessary because the
  23. * dimensions of real FLTK widgets are limited to 16-bits, which is
  24. * far too little for our purposes */
  25. #include "Track_Widget.H"
  26. #include "Loggable.H"
  27. class Region : public Track_Widget
  28. {
  29. public:
  30. struct Fade
  31. {
  32. enum fade_type_e { Linear = 0, Sigmoid, Logarithmic, Parabolic };
  33. enum fade_dir_e { In, Out };
  34. fade_type_e type;
  35. nframes_t length;
  36. Fade ( )
  37. {
  38. type = Linear;
  39. length = 0;
  40. }
  41. bool
  42. operator< ( const Fade &rhs ) const
  43. {
  44. return length < rhs.length;
  45. }
  46. float increment ( void ) const
  47. {
  48. return 1.0f / (float)length;
  49. }
  50. /** Return gain for frame /index/ of /nframes/ on a gain curve
  51. * of type /type/.*/
  52. /* FIXME: calling a function per sample is bad, switching on
  53. * type mid fade is bad. */
  54. inline float
  55. gain ( const float fi ) const
  56. {
  57. switch ( type )
  58. {
  59. case Linear:
  60. return fi;
  61. case Sigmoid:
  62. return (1.0f - cos( fi * M_PI )) / 2.0f;
  63. case Logarithmic:
  64. return pow( 0.1f, (1.0f - fi) * 3.0f );
  65. case Parabolic:
  66. return 1.0f - (1.0f - fi) * (1.0f - fi);
  67. default:
  68. return 1.0f;
  69. }
  70. }
  71. void apply ( sample_t *buf, fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const;
  72. };
  73. /* struct Fade_In : public Fade; */
  74. /* struct Fade_Out : public Fade; */
  75. private:
  76. Audio_File *_clip; /* clip this region represents */
  77. float _scale; /* amplitude adjustment */
  78. Fade _fade_in;
  79. Fade _fade_out;
  80. friend class Track_Header; /* for _clip */
  81. protected:
  82. const char *class_name ( void ) { return "Region"; }
  83. char ** get ( void )
  84. {
  85. // char *r;
  86. char **sa = (char**)malloc( sizeof( char* ) * 8 );
  87. int i = 0;
  88. asprintf( &sa[ i++ ], ":source \"%s\"", _clip ? _clip->name() : "" );
  89. asprintf( &sa[ i++ ], ":track 0x%X", _track ? _track->id() : 0 );
  90. asprintf( &sa[ i++ ], ":x %lu", _r->offset );
  91. asprintf( &sa[ i++ ], ":l %lu", _r->start );
  92. asprintf( &sa[ i++ ], ":r %lu", _r->end );
  93. asprintf( &sa[ i++ ], ":selected %d", selected() );
  94. asprintf( &sa[ i++ ], ":gain %f", _scale );
  95. sa[ i ] = NULL;
  96. return sa;
  97. }
  98. void
  99. set ( char **sa )
  100. {
  101. for ( int i = 0; sa[i]; ++i )
  102. {
  103. char *s = sa[i];
  104. strtok( s, " " );
  105. char *v = s + strlen( s ) + 1;
  106. if ( *v == '"' )
  107. {
  108. v++;
  109. v[ strlen( v ) - 2 ] = '\0';
  110. }
  111. if ( ! strcmp( s, ":x" ) )
  112. _r->offset = atol( v );
  113. else if ( ! strcmp( s, ":l" ) )
  114. _r->start = atol( v );
  115. else if ( ! strcmp( s, ":r" ) )
  116. _r->end = atol( v );
  117. else if ( ! strcmp( s, ":selected" ) )
  118. {
  119. if ( atoi( v ) )
  120. select();
  121. else
  122. deselect();
  123. }
  124. else if ( ! strcmp( s, ":gain" ) )
  125. _scale = atof( v );
  126. else if ( ! strcmp( s, ":source" ) )
  127. {
  128. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  129. {
  130. printf( "Grave error: could not open source \"%s\"\n", v );
  131. }
  132. }
  133. else if ( ! strcmp( s, ":track" ) )
  134. {
  135. int i;
  136. sscanf( v, "%X", &i );
  137. Track *t = (Track*)Loggable::find( i );
  138. assert( t );
  139. t->add( this );
  140. }
  141. free( s );
  142. }
  143. free( sa );
  144. if ( _track )
  145. _track->redraw();
  146. }
  147. public:
  148. static Fl_Boxtype _box;
  149. static Fl_Color _selection_color;
  150. static Fl_Color selection_color ( void ) { return _selection_color; }
  151. static void selection_color ( Fl_Color v ) { _selection_color = v; }
  152. enum trim_e { NO, LEFT, RIGHT };
  153. void trim ( enum trim_e t, int X );
  154. void init ( void );
  155. Region ( )
  156. {
  157. init();
  158. }
  159. bool current ( void ) const { return this == belowmouse(); }
  160. friend class Track_Header; /* for _clip in Track_Header::write() */
  161. public:
  162. static Loggable *
  163. create ( char **sa )
  164. {
  165. Region *r = new Region;
  166. r->set( sa );
  167. return (Loggable *)r;
  168. }
  169. Track_Widget *clone ( const Track_Widget *r );
  170. ~Region ( )
  171. {
  172. log_destroy();
  173. }
  174. Fl_Boxtype box ( void ) const { return Region::_box; }
  175. Fl_Align align ( void ) const { return (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE); }
  176. Region ( const Region & rhs );
  177. Region ( Audio_File *c );
  178. Region ( Audio_File *c, Track *t, nframes_t o );
  179. int handle ( int m );
  180. void draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W );
  181. void draw_box( void );
  182. void draw ( void );
  183. void resize ( void );
  184. void normalize ( void );
  185. /* Engine */
  186. nframes_t read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const;
  187. nframes_t write ( sample_t *buf, nframes_t nframes );
  188. };