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.

161 lines
5.3KB

  1. #ifndef STK_NREV_H
  2. #define STK_NREV_H
  3. #include "Effect.h"
  4. #include "Delay.h"
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class NRev
  8. \brief CCRMA's NRev reverberator class.
  9. This class takes a monophonic input signal and produces a stereo
  10. output signal. It is derived from the CLM NRev function, which is
  11. based on the use of networks of simple allpass and comb delay
  12. filters. This particular arrangement consists of 6 comb filters
  13. in parallel, followed by 3 allpass filters, a lowpass filter, and
  14. another allpass in series, followed by two allpass filters in
  15. parallel with corresponding right and left outputs.
  16. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  17. */
  18. /***************************************************/
  19. class NRev : public Effect
  20. {
  21. public:
  22. //! Class constructor taking a T60 decay time argument (one second default value).
  23. NRev( StkFloat T60 = 1.0 );
  24. //! Reset and clear all internal state.
  25. void clear( void );
  26. //! Set the reverberation T60 decay time.
  27. void setT60( StkFloat T60 );
  28. //! Return the specified channel value of the last computed stereo frame.
  29. /*!
  30. Use the lastFrame() function to get both values of the last
  31. computed stereo frame. The \c channel argument must be 0 or 1
  32. (the first channel is specified by 0). However, range checking is
  33. only performed if _STK_DEBUG_ is defined during compilation, in
  34. which case an out-of-range value will trigger an StkError
  35. exception.
  36. */
  37. StkFloat lastOut( unsigned int channel = 0 );
  38. //! Input one sample to the effect and return the specified \c channel value of the computed stereo frame.
  39. /*!
  40. Use the lastFrame() function to get both values of the computed
  41. stereo output frame. The \c channel argument must be 0 or 1 (the
  42. first channel is specified by 0). However, range checking is only
  43. performed if _STK_DEBUG_ is defined during compilation, in which
  44. case an out-of-range value will trigger an StkError exception.
  45. */
  46. StkFloat tick( StkFloat input, unsigned int channel = 0 );
  47. //! Take a channel of the StkFrames object as inputs to the effect and replace with stereo outputs.
  48. /*!
  49. The StkFrames argument reference is returned. The stereo
  50. outputs are written to the StkFrames argument starting at the
  51. specified \c channel. Therefore, the \c channel argument must be
  52. less than ( channels() - 1 ) of the StkFrames argument (the first
  53. channel is specified by 0). However, range checking is only
  54. performed if _STK_DEBUG_ is defined during compilation, in which
  55. case an out-of-range value will trigger an StkError exception.
  56. */
  57. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  58. //! Take a channel of the \c iFrames object as inputs to the effect and write stereo outputs to the \c oFrames object.
  59. /*!
  60. The \c iFrames object reference is returned. The \c iChannel
  61. argument must be less than the number of channels in the \c
  62. iFrames argument (the first channel is specified by 0). The \c
  63. oChannel argument must be less than ( channels() - 1 ) of the \c
  64. oFrames argument. However, range checking is only performed if
  65. _STK_DEBUG_ is defined during compilation, in which case an
  66. out-of-range value will trigger an StkError exception.
  67. */
  68. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  69. protected:
  70. Delay allpassDelays_[8];
  71. Delay combDelays_[6];
  72. StkFloat allpassCoefficient_;
  73. StkFloat combCoefficient_[6];
  74. StkFloat lowpassState_;
  75. };
  76. inline StkFloat NRev :: lastOut( unsigned int channel )
  77. {
  78. #if defined(_STK_DEBUG_)
  79. if ( channel > 1 ) {
  80. oStream_ << "NRev::lastOut(): channel argument must be less than 2!";
  81. handleError( StkError::FUNCTION_ARGUMENT );
  82. }
  83. #endif
  84. return lastFrame_[channel];
  85. }
  86. inline StkFloat NRev :: tick( StkFloat input, unsigned int channel )
  87. {
  88. #if defined(_STK_DEBUG_)
  89. if ( channel > 1 ) {
  90. oStream_ << "NRev::tick(): channel argument must be less than 2!";
  91. handleError( StkError::FUNCTION_ARGUMENT );
  92. }
  93. #endif
  94. StkFloat temp, temp0, temp1, temp2, temp3;
  95. int i;
  96. temp0 = 0.0;
  97. for ( i=0; i<6; i++ ) {
  98. temp = input + (combCoefficient_[i] * combDelays_[i].lastOut());
  99. temp0 += combDelays_[i].tick(temp);
  100. }
  101. for ( i=0; i<3; i++ ) {
  102. temp = allpassDelays_[i].lastOut();
  103. temp1 = allpassCoefficient_ * temp;
  104. temp1 += temp0;
  105. allpassDelays_[i].tick(temp1);
  106. temp0 = -(allpassCoefficient_ * temp1) + temp;
  107. }
  108. // One-pole lowpass filter.
  109. lowpassState_ = 0.7 * lowpassState_ + 0.3 * temp0;
  110. temp = allpassDelays_[3].lastOut();
  111. temp1 = allpassCoefficient_ * temp;
  112. temp1 += lowpassState_;
  113. allpassDelays_[3].tick( temp1 );
  114. temp1 = -( allpassCoefficient_ * temp1 ) + temp;
  115. temp = allpassDelays_[4].lastOut();
  116. temp2 = allpassCoefficient_ * temp;
  117. temp2 += temp1;
  118. allpassDelays_[4].tick( temp2 );
  119. lastFrame_[0] = effectMix_*( -( allpassCoefficient_ * temp2 ) + temp );
  120. temp = allpassDelays_[5].lastOut();
  121. temp3 = allpassCoefficient_ * temp;
  122. temp3 += temp1;
  123. allpassDelays_[5].tick( temp3 );
  124. lastFrame_[1] = effectMix_*( - ( allpassCoefficient_ * temp3 ) + temp );
  125. temp = ( 1.0 - effectMix_ ) * input;
  126. lastFrame_[0] += temp;
  127. lastFrame_[1] += temp;
  128. return lastFrame_[channel];
  129. }
  130. } // stk namespace
  131. #endif