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.

252 lines
8.2KB

  1. #ifndef STK_FREEVERB_H
  2. #define STK_FREEVERB_H
  3. #include "Effect.h"
  4. #include "Delay.h"
  5. #include "OnePole.h"
  6. namespace stk {
  7. /***********************************************************************/
  8. /*! \class FreeVerb
  9. \brief Jezar at Dreampoint's FreeVerb, implemented in STK.
  10. Freeverb is a free and open-source Schroeder reverberator
  11. originally implemented in C++. The parameters of the reverberation
  12. model are exceptionally well tuned. FreeVerb uses 8
  13. lowpass-feedback-comb-filters in parallel, followed by 4 Schroeder
  14. allpass filters in series. The input signal can be either mono or
  15. stereo, and the output signal is stereo. The delay lengths are
  16. optimized for a sample rate of 44100 Hz.
  17. Ported to STK by Gregory Burlet, 2012.
  18. */
  19. /***********************************************************************/
  20. class FreeVerb : public Effect
  21. {
  22. public:
  23. //! FreeVerb Constructor
  24. /*!
  25. Initializes the effect with default parameters. Note that these defaults
  26. are slightly different than those in the original implementation of
  27. FreeVerb [Effect Mix: 0.75; Room Size: 0.75; Damping: 0.25; Width: 1.0;
  28. Mode: freeze mode off].
  29. */
  30. FreeVerb();
  31. //! Destructor
  32. ~FreeVerb();
  33. //! Set the effect mix [0 = mostly dry, 1 = mostly wet].
  34. void setEffectMix( StkFloat mix );
  35. //! Set the room size (comb filter feedback gain) parameter [0,1].
  36. void setRoomSize( StkFloat value );
  37. //! Get the room size (comb filter feedback gain) parameter.
  38. StkFloat getRoomSize( void );
  39. //! Set the damping parameter [0=low damping, 1=higher damping].
  40. void setDamping( StkFloat value );
  41. //! Get the damping parameter.
  42. StkFloat getDamping( void );
  43. //! Set the width (left-right mixing) parameter [0,1].
  44. void setWidth( StkFloat value );
  45. //! Get the width (left-right mixing) parameter.
  46. StkFloat getWidth( void );
  47. //! Set the mode [frozen = 1, unfrozen = 0].
  48. void setMode( bool isFrozen );
  49. //! Get the current freeze mode [frozen = 1, unfrozen = 0].
  50. StkFloat getMode( void );
  51. //! Clears delay lines, etc.
  52. void clear( void );
  53. //! Return the specified channel value of the last computed stereo frame.
  54. /*!
  55. Use the lastFrame() function to get both values of the last
  56. computed stereo frame. The \c channel argument must be 0 or 1
  57. (the first channel is specified by 0). However, range checking is
  58. only performed if _STK_DEBUG_ is defined during compilation, in
  59. which case an out-of-range value will trigger an StkError
  60. exception.
  61. */
  62. StkFloat lastOut( unsigned int channel = 0 );
  63. //! Input one or two samples to the effect and return the specified \c channel value of the computed stereo frame.
  64. /*!
  65. Use the lastFrame() function to get both values of the computed
  66. stereo output frame. The \c channel argument must be 0 or 1 (the
  67. first channel is specified by 0). However, range checking is only
  68. performed if _STK_DEBUG_ is defined during compilation, in which
  69. case an out-of-range value will trigger an StkError exception.
  70. */
  71. StkFloat tick( StkFloat inputL, StkFloat inputR = 0.0, unsigned int channel = 0 );
  72. //! Take two channels of the StkFrames object as inputs to the effect and replace with stereo outputs.
  73. /*!
  74. The StkFrames argument reference is returned. The stereo
  75. inputs are taken from (and written back to) the StkFrames argument
  76. starting at the specified \c channel. Therefore, the \c channel
  77. argument must be less than ( channels() - 1 ) of the StkFrames
  78. argument (the first channel is specified by 0). However, range
  79. checking is only performed if _STK_DEBUG_ is defined during
  80. compilation, in which case an out-of-range value will trigger an
  81. StkError exception.
  82. */
  83. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  84. //! Take one or two channels of the \c iFrames object as inputs to the effect and write stereo outputs to the \c oFrames object.
  85. /*!
  86. The \c iFrames object reference is returned. The \c iChannel
  87. argument must be less than the number of channels in the \c
  88. iFrames argument (the first channel is specified by 0). If more
  89. than one channel of data exists in \c iFrames starting from \c
  90. iChannel, stereo data is input to the effect. The \c oChannel
  91. argument must be less than ( channels() - 1 ) of the \c oFrames
  92. argument. However, range checking is only performed if
  93. _STK_DEBUG_ is defined during compilation, in which case an
  94. out-of-range value will trigger an StkError exception.
  95. */
  96. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  97. protected:
  98. //! Update interdependent parameters.
  99. void update( void );
  100. // Clamp very small floats to zero, version from
  101. // http://music.columbia.edu/pipermail/linux-audio-user/2004-July/013489.html .
  102. // However, this is for 32-bit floats only.
  103. //static inline StkFloat undenormalize( volatile StkFloat s ) {
  104. // s += 9.8607615E-32f;
  105. // return s - 9.8607615E-32f;
  106. //}
  107. static const int nCombs = 8;
  108. static const int nAllpasses = 4;
  109. static const int stereoSpread = 23;
  110. static const StkFloat fixedGain;
  111. static const StkFloat scaleWet;
  112. static const StkFloat scaleDry;
  113. static const StkFloat scaleDamp;
  114. static const StkFloat scaleRoom;
  115. static const StkFloat offsetRoom;
  116. // Delay line lengths for 44100Hz sampling rate.
  117. static int cDelayLengths[nCombs];
  118. static int aDelayLengths[nAllpasses];
  119. StkFloat g_; // allpass coefficient
  120. StkFloat gain_;
  121. StkFloat roomSizeMem_, roomSize_;
  122. StkFloat dampMem_, damp_;
  123. StkFloat wet1_, wet2_;
  124. StkFloat dry_;
  125. StkFloat width_;
  126. bool frozenMode_;
  127. // LBFC: Lowpass Feedback Comb Filters
  128. Delay combDelayL_[nCombs];
  129. Delay combDelayR_[nCombs];
  130. OnePole combLPL_[nCombs];
  131. OnePole combLPR_[nCombs];
  132. // AP: Allpass Filters
  133. Delay allPassDelayL_[nAllpasses];
  134. Delay allPassDelayR_[nAllpasses];
  135. };
  136. inline StkFloat FreeVerb :: lastOut( unsigned int channel )
  137. {
  138. #if defined(_STK_DEBUG_)
  139. if ( channel > 1 ) {
  140. oStream_ << "FreeVerb::lastOut(): channel argument must be less than 2!";
  141. handleError( StkError::FUNCTION_ARGUMENT );
  142. }
  143. #endif
  144. return lastFrame_[channel];
  145. }
  146. inline StkFloat FreeVerb::tick( StkFloat inputL, StkFloat inputR, unsigned int channel )
  147. {
  148. #if defined(_STK_DEBUG_)
  149. if ( channel > 1 ) {
  150. oStream_ << "FreeVerb::tick(): channel argument must be less than 2!";
  151. handleError(StkError::FUNCTION_ARGUMENT);
  152. }
  153. #endif
  154. StkFloat fInput = (inputL + inputR) * gain_;
  155. StkFloat outL = 0.0;
  156. StkFloat outR = 0.0;
  157. // Parallel LBCF filters
  158. for ( int i = 0; i < nCombs; i++ ) {
  159. // Left channel
  160. //StkFloat yn = fInput + (roomSize_ * FreeVerb::undenormalize(combLPL_[i].tick(FreeVerb::undenormalize(combDelayL_[i].nextOut()))));
  161. StkFloat yn = fInput + (roomSize_ * combLPL_[i].tick( combDelayL_[i].nextOut() ) );
  162. combDelayL_[i].tick(yn);
  163. outL += yn;
  164. // Right channel
  165. //yn = fInput + (roomSize_ * FreeVerb::undenormalize(combLPR_[i].tick(FreeVerb::undenormalize(combDelayR_[i].nextOut()))));
  166. yn = fInput + (roomSize_ * combLPR_[i].tick( combDelayR_[i].nextOut() ) );
  167. combDelayR_[i].tick(yn);
  168. outR += yn;
  169. }
  170. // Series allpass filters
  171. for ( int i = 0; i < nAllpasses; i++ ) {
  172. // Left channel
  173. //StkFloat vn_m = FreeVerb::undenormalize(allPassDelayL_[i].nextOut());
  174. StkFloat vn_m = allPassDelayL_[i].nextOut();
  175. StkFloat vn = outL + (g_ * vn_m);
  176. allPassDelayL_[i].tick(vn);
  177. // calculate output
  178. outL = -vn + (1.0 + g_)*vn_m;
  179. // Right channel
  180. //vn_m = FreeVerb::undenormalize(allPassDelayR_[i].nextOut());
  181. vn_m = allPassDelayR_[i].nextOut();
  182. vn = outR + (g_ * vn_m);
  183. allPassDelayR_[i].tick(vn);
  184. // calculate output
  185. outR = -vn + (1.0 + g_)*vn_m;
  186. }
  187. // Mix output
  188. lastFrame_[0] = outL*wet1_ + outR*wet2_ + inputL*dry_;
  189. lastFrame_[1] = outR*wet1_ + outL*wet2_ + inputR*dry_;
  190. /*
  191. // Hard limiter ... there's not much else we can do at this point
  192. if ( lastFrame_[0] >= 1.0 ) {
  193. lastFrame_[0] = 0.9999;
  194. }
  195. if ( lastFrame_[0] <= -1.0 ) {
  196. lastFrame_[0] = -0.9999;
  197. }
  198. if ( lastFrame_[1] >= 1.0 ) {
  199. lastFrame_[1] = 0.9999;
  200. }
  201. if ( lastFrame_[1] <= -1.0 ) {
  202. lastFrame_[1] = -0.9999;
  203. }
  204. */
  205. return lastFrame_[channel];
  206. }
  207. }
  208. #endif