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.

192 lines
5.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 "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. void
  84. get ( Log_Entry &e )
  85. {
  86. e.add( ":source", _clip ? _clip->name() : "" );
  87. e.add( ":gain", _scale );
  88. Sequence_Widget::get( e );
  89. }
  90. void
  91. set ( Log_Entry &e )
  92. {
  93. for ( int i = 0; i < e.size(); ++i )
  94. {
  95. const char *s, *v;
  96. e.get( i, &s, &v );
  97. if ( ! strcmp( s, ":gain" ) )
  98. _scale = atof( v );
  99. else if ( ! strcmp( s, ":source" ) )
  100. {
  101. if ( ! ( _clip = Audio_File::from_file( v ) ) )
  102. {
  103. printf( "Grave error: could not open source \"%s\"\n", v );
  104. }
  105. }
  106. }
  107. Sequence_Widget::set( e );
  108. }
  109. public:
  110. static Fl_Boxtype _box;
  111. static Fl_Color _selection_color;
  112. static Fl_Color selection_color ( void ) { return _selection_color; }
  113. static void selection_color ( Fl_Color v ) { _selection_color = v; }
  114. enum trim_e { NO, LEFT, RIGHT };
  115. void trim ( enum trim_e t, int X );
  116. void init ( void );
  117. Region ( )
  118. {
  119. init();
  120. }
  121. bool current ( void ) const { return this == belowmouse(); }
  122. public:
  123. const char *source_name ( void ) const { return _clip->name(); }
  124. LOG_CREATE_FUNC( Region );
  125. Sequence_Widget *clone ( const Sequence_Widget *r );
  126. ~Region ( )
  127. {
  128. log_destroy();
  129. }
  130. Fl_Boxtype box ( void ) const { return Region::_box; }
  131. Fl_Align align ( void ) const { return (Fl_Align)(FL_ALIGN_LEFT | FL_ALIGN_BOTTOM /*| FL_ALIGN_CLIP*/ | FL_ALIGN_INSIDE); }
  132. Region ( const Region & rhs );
  133. Region ( Audio_File *c );
  134. Region ( Audio_File *c, Sequence *t, nframes_t o );
  135. int handle ( int m );
  136. void draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool filled, int X, int W );
  137. void draw_box( void );
  138. void draw ( void );
  139. void resize ( void );
  140. void normalize ( void );
  141. /* Engine */
  142. nframes_t read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const;
  143. nframes_t write ( sample_t *buf, nframes_t nframes );
  144. bool finalize ( void );
  145. };