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.

137 lines
3.8KB

  1. /***************************************************/
  2. /*! \class JCRev
  3. \brief John Chowning's reverberator class.
  4. This class takes a monophonic input signal and
  5. produces a stereo output signal. It is derived
  6. from the CLM JCRev function, which is based on
  7. the use of networks of simple allpass and comb
  8. delay filters. This class implements three
  9. series allpass units, followed by four parallel
  10. comb filters, and two decorrelation delay lines
  11. in parallel at the output.
  12. Although not in the original JC reverberator,
  13. one-pole lowpass filters have been added inside
  14. the feedback comb filters.
  15. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  16. */
  17. /***************************************************/
  18. #include "JCRev.h"
  19. #include <cmath>
  20. namespace stk {
  21. JCRev :: JCRev( StkFloat T60 )
  22. {
  23. if ( T60 <= 0.0 ) {
  24. oStream_ << "JCRev::JCRev: argument (" << T60 << ") must be positive!";
  25. handleError( StkError::FUNCTION_ARGUMENT );
  26. }
  27. lastFrame_.resize( 1, 2, 0.0 ); // resize lastFrame_ for stereo output
  28. // Delay lengths for 44100 Hz sample rate.
  29. int lengths[9] = {1116, 1356, 1422, 1617, 225, 341, 441, 211, 179};
  30. double scaler = Stk::sampleRate() / 44100.0;
  31. int delay, i;
  32. if ( scaler != 1.0 ) {
  33. for ( i=0; i<9; i++ ) {
  34. delay = (int) floor( scaler * lengths[i] );
  35. if ( (delay & 1) == 0) delay++;
  36. while ( !this->isPrime(delay) ) delay += 2;
  37. lengths[i] = delay;
  38. }
  39. }
  40. for ( i=0; i<3; i++ ) {
  41. allpassDelays_[i].setMaximumDelay( lengths[i+4] );
  42. allpassDelays_[i].setDelay( lengths[i+4] );
  43. }
  44. for ( i=0; i<4; i++ ) {
  45. combDelays_[i].setMaximumDelay( lengths[i] );
  46. combDelays_[i].setDelay( lengths[i] );
  47. combFilters_[i].setPole( 0.2 );
  48. }
  49. this->setT60( T60 );
  50. outLeftDelay_.setMaximumDelay( lengths[7] );
  51. outLeftDelay_.setDelay( lengths[7] );
  52. outRightDelay_.setMaximumDelay( lengths[8] );
  53. outRightDelay_.setDelay( lengths[8] );
  54. allpassCoefficient_ = 0.7;
  55. effectMix_ = 0.3;
  56. this->clear();
  57. }
  58. void JCRev :: clear()
  59. {
  60. allpassDelays_[0].clear();
  61. allpassDelays_[1].clear();
  62. allpassDelays_[2].clear();
  63. combDelays_[0].clear();
  64. combDelays_[1].clear();
  65. combDelays_[2].clear();
  66. combDelays_[3].clear();
  67. outRightDelay_.clear();
  68. outLeftDelay_.clear();
  69. lastFrame_[0] = 0.0;
  70. lastFrame_[1] = 0.0;
  71. }
  72. void JCRev :: setT60( StkFloat T60 )
  73. {
  74. if ( T60 <= 0.0 ) {
  75. oStream_ << "JCRev::setT60: argument (" << T60 << ") must be positive!";
  76. handleError( StkError::WARNING ); return;
  77. }
  78. for ( int i=0; i<4; i++ )
  79. combCoefficient_[i] = pow(10.0, (-3.0 * combDelays_[i].getDelay() / (T60 * Stk::sampleRate())));
  80. }
  81. StkFrames& JCRev :: tick( StkFrames& frames, unsigned int channel )
  82. {
  83. #if defined(_STK_DEBUG_)
  84. if ( channel >= frames.channels() - 1 ) {
  85. oStream_ << "JCRev::tick(): channel and StkFrames arguments are incompatible!";
  86. handleError( StkError::FUNCTION_ARGUMENT );
  87. }
  88. #endif
  89. StkFloat *samples = &frames[channel];
  90. unsigned int hop = frames.channels();
  91. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  92. *samples = tick( *samples );
  93. *(samples+1) = lastFrame_[1];
  94. }
  95. return frames;
  96. }
  97. StkFrames& JCRev :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  98. {
  99. #if defined(_STK_DEBUG_)
  100. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() - 1 ) {
  101. oStream_ << "JCRev::tick(): channel and StkFrames arguments are incompatible!";
  102. handleError( StkError::FUNCTION_ARGUMENT );
  103. }
  104. #endif
  105. StkFloat *iSamples = &iFrames[iChannel];
  106. StkFloat *oSamples = &oFrames[oChannel];
  107. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  108. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  109. *oSamples = tick( *iSamples );
  110. *(oSamples+1) = lastFrame_[1];
  111. }
  112. return iFrames;
  113. }
  114. } // stk namespace