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.

210 lines
6.7KB

  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 "Sequence.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 "Sequence_Widget.H"
  26. #include "Loggable.H"
  27. class Region : public Sequence_Widget
  28. {
  29. public:
  30. static bool inherit_track_color;
  31. struct Fade
  32. {
  33. enum fade_type_e { Linear = 0, Sigmoid, Logarithmic, Parabolic };
  34. enum fade_dir_e { In, Out };
  35. fade_type_e type;
  36. nframes_t length;
  37. Fade ( )
  38. {
  39. type = Linear;
  40. length = 0;
  41. }
  42. bool
  43. operator< ( const Fade &rhs ) const
  44. {
  45. return length < rhs.length;
  46. }
  47. float increment ( void ) const
  48. {
  49. return 1.0f / (float)length;
  50. }
  51. /** Return gain for frame /index/ of /nframes/ on a gain curve
  52. * of type /type/.*/
  53. /* FIXME: calling a function per sample is bad, switching on
  54. * type mid fade is bad. */
  55. inline float
  56. gain ( const float fi ) const
  57. {
  58. switch ( type )
  59. {
  60. case Linear:
  61. return fi;
  62. case Sigmoid:
  63. return (1.0f - cos( fi * M_PI )) / 2.0f;
  64. case Logarithmic:
  65. return pow( 0.1f, (1.0f - fi) * 3.0f );
  66. case Parabolic:
  67. return 1.0f - (1.0f - fi) * (1.0f - fi);
  68. default:
  69. return 1.0f;
  70. }
  71. }
  72. void apply ( sample_t *buf, fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const;
  73. };
  74. /* struct Fade_In : public Fade; */
  75. /* struct Fade_Out : public Fade; */
  76. private:
  77. Audio_File *_clip; /* clip this region represents */
  78. float _scale; /* amplitude adjustment */
  79. Fade _fade_in;
  80. Fade _fade_out;
  81. friend class Track; /* for _clip */
  82. protected:
  83. // const char *class_name ( void ) { return "Region"; }
  84. virtual void get ( Log_Entry &e ) const
  85. {
  86. e.add( ":source", _clip ? _clip->name() : "" );
  87. e.add( ":gain", _scale );
  88. e.add( ":fade-in-type", _fade_in.type );
  89. e.add( ":fade-in-duration", _fade_in.length );
  90. e.add( ":fade-out-type", _fade_out.type );
  91. e.add( ":fade-out-duration", _fade_out.length );
  92. e.add( ":color", (int)_box_color );
  93. Sequence_Widget::get( e );
  94. }
  95. void
  96. set ( Log_Entry &e )
  97. {
  98. for ( int i = 0; i < e.size(); ++i )
  99. {
  100. const char *s, *v;
  101. e.get( i, &s, &v );
  102. if ( ! strcmp( s, ":gain" ) )
  103. _scale = atof( v );
  104. else if ( ! strcmp( s, ":color" ) )
  105. _box_color = (Fl_Color)atoi( v );
  106. else if ( ! strcmp( s, ":fade-in-type" ) )
  107. _fade_in.type = (Fade::fade_type_e)atoi( v );
  108. else if ( ! strcmp( s, ":fade-in-duration" ) )
  109. _fade_in.length = atoll( v );
  110. else if ( ! strcmp( s, ":fade-out-type" ) )
  111. _fade_out.type = (Fade::fade_type_e)atoi( v );
  112. else if ( ! strcmp( s, ":fade-out-duration" ) )
  113. _fade_out.length = atoll( v );
  114. else if ( ! strcmp( s, ":source" ) )
  115. {
  116. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  117. {
  118. printf( "Grave error: could not open source \"%s\"\n", v );
  119. }
  120. }
  121. }
  122. Sequence_Widget::set( e );
  123. }
  124. public:
  125. static Fl_Boxtype _box;
  126. static Fl_Color _selection_color;
  127. Fl_Color selection_color ( void ) const { return _selection_color; }
  128. void selection_color ( Fl_Color v ) { _selection_color = v; }
  129. enum trim_e { NO, LEFT, RIGHT };
  130. void trim ( enum trim_e t, int X );
  131. void init ( void );
  132. Region ( )
  133. {
  134. init();
  135. }
  136. bool current ( void ) const { return this == belowmouse(); }
  137. public:
  138. const char *source_name ( void ) const { return _clip->name(); }
  139. LOG_CREATE_FUNC( Region );
  140. Sequence_Widget *clone ( const Sequence_Widget *r );
  141. ~Region ( )
  142. {
  143. log_destroy();
  144. }
  145. Fl_Boxtype box ( void ) const { return Region::_box; }
  146. Fl_Align align ( void ) const { return (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE); }
  147. Region ( const Region & rhs );
  148. Region ( Audio_File *c );
  149. Region ( Audio_File *c, Sequence *t, nframes_t o );
  150. int handle ( int m );
  151. void draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W );
  152. void draw_box( void );
  153. void draw ( void );
  154. void resize ( void );
  155. void normalize ( void );
  156. /* Engine */
  157. nframes_t read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const;
  158. nframes_t write ( nframes_t nframes );
  159. void prepare ( void );
  160. bool finalize ( void );
  161. };