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.

120 lines
3.5KB

  1. /***************************************************/
  2. /*! \class NRev
  3. \brief CCRMA's NRev reverberator class.
  4. This class takes a monophonic input signal and produces a stereo
  5. output signal. It is derived from the CLM NRev function, which is
  6. based on the use of networks of simple allpass and comb delay
  7. filters. This particular arrangement consists of 6 comb filters
  8. in parallel, followed by 3 allpass filters, a lowpass filter, and
  9. another allpass in series, followed by two allpass filters in
  10. parallel with corresponding right and left outputs.
  11. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  12. */
  13. /***************************************************/
  14. #include "NRev.h"
  15. #include <cmath>
  16. namespace stk {
  17. NRev :: NRev( StkFloat T60 )
  18. {
  19. if ( T60 <= 0.0 ) {
  20. oStream_ << "NRev::NRev: argument (" << T60 << ") must be positive!";
  21. handleError( StkError::FUNCTION_ARGUMENT );
  22. }
  23. lastFrame_.resize( 1, 2, 0.0 ); // resize lastFrame_ for stereo output
  24. int lengths[15] = {1433, 1601, 1867, 2053, 2251, 2399, 347, 113, 37, 59, 53, 43, 37, 29, 19};
  25. double scaler = Stk::sampleRate() / 25641.0;
  26. int delay, i;
  27. for ( i=0; i<15; i++ ) {
  28. delay = (int) floor(scaler * lengths[i]);
  29. if ( (delay & 1) == 0) delay++;
  30. while ( !this->isPrime(delay) ) delay += 2;
  31. lengths[i] = delay;
  32. }
  33. for ( i=0; i<6; i++ ) {
  34. combDelays_[i].setMaximumDelay( lengths[i] );
  35. combDelays_[i].setDelay( lengths[i] );
  36. combCoefficient_[i] = pow(10.0, (-3 * lengths[i] / (T60 * Stk::sampleRate())));
  37. }
  38. for ( i=0; i<8; i++ ) {
  39. allpassDelays_[i].setMaximumDelay( lengths[i+6] );
  40. allpassDelays_[i].setDelay( lengths[i+6] );
  41. }
  42. this->setT60( T60 );
  43. allpassCoefficient_ = 0.7;
  44. effectMix_ = 0.3;
  45. this->clear();
  46. }
  47. void NRev :: clear()
  48. {
  49. int i;
  50. for (i=0; i<6; i++) combDelays_[i].clear();
  51. for (i=0; i<8; i++) allpassDelays_[i].clear();
  52. lastFrame_[0] = 0.0;
  53. lastFrame_[1] = 0.0;
  54. lowpassState_ = 0.0;
  55. }
  56. void NRev :: setT60( StkFloat T60 )
  57. {
  58. if ( T60 <= 0.0 ) {
  59. oStream_ << "NRev::setT60: argument (" << T60 << ") must be positive!";
  60. handleError( StkError::WARNING ); return;
  61. }
  62. for ( int i=0; i<6; i++ )
  63. combCoefficient_[i] = pow(10.0, (-3.0 * combDelays_[i].getDelay() / (T60 * Stk::sampleRate())));
  64. }
  65. StkFrames& NRev :: tick( StkFrames& frames, unsigned int channel )
  66. {
  67. #if defined(_STK_DEBUG_)
  68. if ( channel >= frames.channels() - 1 ) {
  69. oStream_ << "NRev::tick(): channel and StkFrames arguments are incompatible!";
  70. handleError( StkError::FUNCTION_ARGUMENT );
  71. }
  72. #endif
  73. StkFloat *samples = &frames[channel];
  74. unsigned int hop = frames.channels();
  75. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  76. *samples = tick( *samples );
  77. *(samples+1) = lastFrame_[1];
  78. }
  79. return frames;
  80. }
  81. StkFrames& NRev :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  82. {
  83. #if defined(_STK_DEBUG_)
  84. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() - 1 ) {
  85. oStream_ << "NRev::tick(): channel and StkFrames arguments are incompatible!";
  86. handleError( StkError::FUNCTION_ARGUMENT );
  87. }
  88. #endif
  89. StkFloat *iSamples = &iFrames[iChannel];
  90. StkFloat *oSamples = &oFrames[oChannel];
  91. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  92. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  93. *oSamples = tick( *iSamples );
  94. *(oSamples+1) = lastFrame_[1];
  95. }
  96. return iFrames;
  97. }
  98. } // stk namespace