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.

278 lines
7.8KB

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