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.

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