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.

275 lines
7.6KB

  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. protected:
  84. const char *class_name ( void ) { return "Region"; }
  85. char ** get ( void )
  86. {
  87. // char *r;
  88. char **sa = (char**)malloc( sizeof( char* ) * 8 );
  89. int i = 0;
  90. asprintf( &sa[ i++ ], ":source \"%s\"", _clip ? _clip->name() : "" );
  91. asprintf( &sa[ i++ ], ":track 0x%X", _track ? _track->id() : 0 );
  92. asprintf( &sa[ i++ ], ":x %lu", _r->offset );
  93. asprintf( &sa[ i++ ], ":l %lu", _r->start );
  94. asprintf( &sa[ i++ ], ":r %lu", _r->end );
  95. asprintf( &sa[ i++ ], ":selected %d", selected() );
  96. asprintf( &sa[ i++ ], ":gain %f", _scale );
  97. sa[ i ] = NULL;
  98. return sa;
  99. }
  100. void
  101. set ( char **sa )
  102. {
  103. for ( int i = 0; sa[i]; ++i )
  104. {
  105. char *s = sa[i];
  106. strtok( s, " " );
  107. char *v = s + strlen( s ) + 1;
  108. if ( *v == '"' )
  109. {
  110. v++;
  111. v[ strlen( v ) - 2 ] = '\0';
  112. }
  113. if ( ! strcmp( s, ":x" ) )
  114. _r->offset = atol( v );
  115. else if ( ! strcmp( s, ":l" ) )
  116. _r->start = atol( v );
  117. else if ( ! strcmp( s, ":r" ) )
  118. _r->end = atol( v );
  119. else if ( ! strcmp( s, ":selected" ) )
  120. {
  121. if ( atoi( v ) )
  122. select();
  123. else
  124. deselect();
  125. }
  126. else if ( ! strcmp( s, ":gain" ) )
  127. _scale = atof( v );
  128. else if ( ! strcmp( s, ":source" ) )
  129. {
  130. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  131. {
  132. printf( "Grave error: could not open source \"%s\"\n", v );
  133. }
  134. }
  135. else if ( ! strcmp( s, ":track" ) )
  136. {
  137. int i;
  138. sscanf( v, "%X", &i );
  139. Track *t = (Track*)Loggable::find( i );
  140. assert( t );
  141. t->add( this );
  142. }
  143. free( s );
  144. }
  145. free( sa );
  146. #ifndef ENGINE
  147. if ( _track )
  148. _track->redraw();
  149. #endif
  150. }
  151. public:
  152. Region_Base ( )
  153. {
  154. _clip = NULL;
  155. _scale = 1.0f;
  156. }
  157. #ifdef ENGINE
  158. /* for loggable */
  159. static Loggable *
  160. create ( char **sa )
  161. {
  162. Region_Base *r = new Region_Base;
  163. r->set( sa );
  164. return (Loggable *)r;
  165. }
  166. #else
  167. friend class Region;
  168. #endif
  169. };
  170. #ifndef ENGINE
  171. class Region : public Region_Base
  172. {
  173. static Fl_Boxtype _box;
  174. static Fl_Color _selection_color;
  175. static Fl_Color selection_color ( void ) { return _selection_color; }
  176. static void selection_color ( Fl_Color v ) { _selection_color = v; }
  177. enum trim_e { NO, LEFT, RIGHT };
  178. void trim ( enum trim_e t, int X );
  179. void init ( void );
  180. Region ( )
  181. {
  182. init();
  183. }
  184. bool current ( void ) const { return this == belowmouse(); }
  185. public:
  186. static Loggable *
  187. create ( char **sa )
  188. {
  189. Region *r = new Region;
  190. r->set( sa );
  191. return (Loggable *)r;
  192. }
  193. Track_Widget *clone ( const Track_Widget *r );
  194. ~Region ( )
  195. {
  196. log_destroy();
  197. }
  198. Fl_Boxtype box ( void ) const { return Region::_box; }
  199. Fl_Align align ( void ) const { return (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE); }
  200. Region ( const Region & rhs );
  201. Region ( Audio_File *c );
  202. Region ( Audio_File *c, Track *t, nframes_t o );
  203. int handle ( int m );
  204. void draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W );
  205. void draw_box( void );
  206. void draw ( void );
  207. void resize ( void );
  208. void normalize ( void );
  209. nframes_t read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const;
  210. };
  211. #endif