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