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.

240 lines
6.5KB

  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. enum fade_type_e { Linear, Cosine, Logarithmic, Parabolic };
  34. enum fade_dir_e { FADE_IN, FADE_OUT };
  35. struct Fade
  36. {
  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 )
  46. {
  47. return length < rhs.length;
  48. }
  49. };
  50. private:
  51. Audio_File *_clip; /* clip this region represents */
  52. float _scale; /* amplitude adjustment */
  53. Fade _fade_in;
  54. Fade _fade_out;
  55. protected:
  56. const char *class_name ( void ) { return "Region"; }
  57. char ** get ( void )
  58. {
  59. // char *r;
  60. char **sa = (char**)malloc( sizeof( char* ) * 8 );
  61. int i = 0;
  62. asprintf( &sa[ i++ ], ":source \"%s\"", _clip ? _clip->name() : "" );
  63. asprintf( &sa[ i++ ], ":track 0x%X", _track ? _track->id() : 0 );
  64. asprintf( &sa[ i++ ], ":x %lu", _r->offset );
  65. asprintf( &sa[ i++ ], ":l %lu", _r->start );
  66. asprintf( &sa[ i++ ], ":r %lu", _r->end );
  67. asprintf( &sa[ i++ ], ":selected %d", selected() );
  68. asprintf( &sa[ i++ ], ":gain %f", _scale );
  69. sa[ i ] = NULL;
  70. return sa;
  71. }
  72. void
  73. set ( char **sa )
  74. {
  75. for ( int i = 0; sa[i]; ++i )
  76. {
  77. char *s = sa[i];
  78. strtok( s, " " );
  79. char *v = s + strlen( s ) + 1;
  80. if ( *v == '"' )
  81. {
  82. v++;
  83. v[ strlen( v ) - 2 ] = '\0';
  84. }
  85. if ( ! strcmp( s, ":x" ) )
  86. _r->offset = atol( v );
  87. else if ( ! strcmp( s, ":l" ) )
  88. _r->start = atol( v );
  89. else if ( ! strcmp( s, ":r" ) )
  90. _r->end = atol( v );
  91. else if ( ! strcmp( s, ":selected" ) )
  92. {
  93. if ( atoi( v ) )
  94. select();
  95. else
  96. deselect();
  97. }
  98. else if ( ! strcmp( s, ":gain" ) )
  99. _scale = atof( v );
  100. else if ( ! strcmp( s, ":source" ) )
  101. {
  102. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  103. {
  104. printf( "Grave error: could not open source \"%s\"\n", v );
  105. }
  106. }
  107. else if ( ! strcmp( s, ":track" ) )
  108. {
  109. int i;
  110. sscanf( v, "%X", &i );
  111. Track *t = (Track*)Loggable::find( i );
  112. assert( t );
  113. t->add( this );
  114. }
  115. free( s );
  116. }
  117. free( sa );
  118. #ifndef ENGINE
  119. if ( _track )
  120. _track->redraw();
  121. #endif
  122. }
  123. public:
  124. Region_Base ( )
  125. {
  126. _clip = NULL;
  127. _scale = 1.0f;
  128. }
  129. #ifdef ENGINE
  130. /* for loggable */
  131. static Loggable *
  132. create ( char **sa )
  133. {
  134. Region_Base *r = new Region_Base;
  135. r->set( sa );
  136. return (Loggable *)r;
  137. }
  138. #else
  139. friend class Region;
  140. #endif
  141. };
  142. #ifndef ENGINE
  143. class Region : public Region_Base
  144. {
  145. static Fl_Boxtype _box;
  146. static Fl_Color _selection_color;
  147. static Fl_Color selection_color ( void ) { return _selection_color; }
  148. static void selection_color ( Fl_Color v ) { _selection_color = v; }
  149. enum trim_e { NO, LEFT, RIGHT };
  150. void trim ( enum trim_e t, int X );
  151. void init ( void );
  152. Region ( )
  153. {
  154. init();
  155. }
  156. bool current ( void ) const { return this == belowmouse(); }
  157. public:
  158. static Loggable *
  159. create ( char **sa )
  160. {
  161. Region *r = new Region;
  162. r->set( sa );
  163. return (Loggable *)r;
  164. }
  165. Track_Widget *clone ( const Track_Widget *r );
  166. ~Region ( )
  167. {
  168. log_destroy();
  169. }
  170. Fl_Boxtype box ( void ) const { return Region::_box; }
  171. Fl_Align align ( void ) const { return (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE); }
  172. Region ( const Region & rhs );
  173. Region ( Audio_File *c );
  174. Region ( Audio_File *c, Track *t, nframes_t o );
  175. int handle ( int m );
  176. void draw_box( int X, int Y, int W, int H );
  177. void draw ( int X, int Y, int W, int H );
  178. void resize ( void );
  179. void normalize ( void );
  180. nframes_t read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const;
  181. };
  182. #endif