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.

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