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.

173 lines
6.2KB

  1. #ifndef STK_CHORUS_H
  2. #define STK_CHORUS_H
  3. #include "Effect.h"
  4. #include "DelayL.h"
  5. #include "SineWave.h"
  6. namespace stk {
  7. /***************************************************/
  8. /*! \class Chorus
  9. \brief STK chorus effect class.
  10. This class implements a chorus effect. It takes a monophonic
  11. input signal and produces a stereo output signal.
  12. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  13. */
  14. /***************************************************/
  15. class Chorus : public Effect
  16. {
  17. public:
  18. //! Class constructor, taking the median desired delay length.
  19. /*!
  20. An StkError can be thrown if the rawwave path is incorrect.
  21. */
  22. Chorus( StkFloat baseDelay = 6000 );
  23. //! Reset and clear all internal state.
  24. void clear( void );
  25. //! Set modulation depth in range 0.0 - 1.0.
  26. void setModDepth( StkFloat depth );
  27. //! Set modulation frequency.
  28. void setModFrequency( StkFloat frequency );
  29. //! Return the specified channel value of the last computed stereo frame.
  30. /*!
  31. Use the lastFrame() function to get both values of the last
  32. computed stereo frame. The \c channel argument must be 0 or 1
  33. (the first channel is specified by 0). However, range checking is
  34. only performed if _STK_DEBUG_ is defined during compilation, in
  35. which case an out-of-range value will trigger an StkError
  36. exception.
  37. */
  38. StkFloat lastOut( unsigned int channel = 0 );
  39. //! Input one sample to the effect and return the specified \c channel value of the computed stereo frame.
  40. /*!
  41. Use the lastFrame() function to get both values of the computed
  42. stereo output frame. The \c channel argument must be 0 or 1 (the
  43. first channel is specified by 0). However, range checking is only
  44. performed if _STK_DEBUG_ is defined during compilation, in which
  45. case an out-of-range value will trigger an StkError exception.
  46. */
  47. StkFloat tick( StkFloat input, unsigned int channel = 0 );
  48. //! Take a channel of the StkFrames object as inputs to the effect and replace with stereo outputs.
  49. /*!
  50. The StkFrames argument reference is returned. The stereo
  51. outputs are written to the StkFrames argument starting at the
  52. specified \c channel. Therefore, the \c channel argument must be
  53. less than ( channels() - 1 ) of the StkFrames argument (the first
  54. channel is specified by 0). However, range checking is only
  55. performed if _STK_DEBUG_ is defined during compilation, in which
  56. case an out-of-range value will trigger an StkError exception.
  57. */
  58. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  59. //! Take a channel of the \c iFrames object as inputs to the effect and write stereo outputs to the \c oFrames object.
  60. /*!
  61. The \c iFrames object reference is returned. The \c iChannel
  62. argument must be less than the number of channels in the \c
  63. iFrames argument (the first channel is specified by 0). The \c
  64. oChannel argument must be less than ( channels() - 1 ) of the \c
  65. oFrames argument. However, range checking is only performed if
  66. _STK_DEBUG_ is defined during compilation, in which case an
  67. out-of-range value will trigger an StkError exception.
  68. */
  69. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  70. protected:
  71. DelayL delayLine_[2];
  72. SineWave mods_[2];
  73. StkFloat baseLength_;
  74. StkFloat modDepth_;
  75. };
  76. inline StkFloat Chorus :: lastOut( unsigned int channel )
  77. {
  78. #if defined(_STK_DEBUG_)
  79. if ( channel > 1 ) {
  80. oStream_ << "Chorus::lastOut(): channel argument must be less than 2!";
  81. handleError( StkError::FUNCTION_ARGUMENT );
  82. }
  83. #endif
  84. return lastFrame_[channel];
  85. }
  86. inline StkFloat Chorus :: tick( StkFloat input, unsigned int channel )
  87. {
  88. #if defined(_STK_DEBUG_)
  89. if ( channel > 1 ) {
  90. oStream_ << "Chorus::tick(): channel argument must be less than 2!";
  91. handleError( StkError::FUNCTION_ARGUMENT );
  92. }
  93. #endif
  94. delayLine_[0].setDelay( baseLength_ * 0.707 * ( 1.0 + modDepth_ * mods_[0].tick() ) );
  95. delayLine_[1].setDelay( baseLength_ * 0.5 * ( 1.0 - modDepth_ * mods_[1].tick() ) );
  96. lastFrame_[0] = effectMix_ * ( delayLine_[0].tick( input ) - input ) + input;
  97. lastFrame_[1] = effectMix_ * ( delayLine_[1].tick( input ) - input ) + input;
  98. return lastFrame_[channel];
  99. }
  100. inline StkFrames& Chorus :: tick( StkFrames& frames, unsigned int channel )
  101. {
  102. #if defined(_STK_DEBUG_)
  103. if ( channel >= frames.channels() - 1 ) {
  104. oStream_ << "Chorus::tick(): channel and StkFrames arguments are incompatible!";
  105. handleError( StkError::FUNCTION_ARGUMENT );
  106. }
  107. #endif
  108. StkFloat *samples = &frames[channel];
  109. unsigned int hop = frames.channels() - 1;
  110. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  111. delayLine_[0].setDelay( baseLength_ * 0.707 * ( 1.0 + modDepth_ * mods_[0].tick() ) );
  112. delayLine_[1].setDelay( baseLength_ * 0.5 * ( 1.0 - modDepth_ * mods_[1].tick() ) );
  113. *samples = effectMix_ * ( delayLine_[0].tick( *samples ) - *samples ) + *samples;
  114. samples++;
  115. *samples = effectMix_ * ( delayLine_[1].tick( *samples ) - *samples ) + *samples;
  116. }
  117. lastFrame_[0] = *(samples-hop);
  118. lastFrame_[1] = *(samples-hop+1);
  119. return frames;
  120. }
  121. inline StkFrames& Chorus :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  122. {
  123. #if defined(_STK_DEBUG_)
  124. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() - 1 ) {
  125. oStream_ << "Chorus::tick(): channel and StkFrames arguments are incompatible!";
  126. handleError( StkError::FUNCTION_ARGUMENT );
  127. }
  128. #endif
  129. StkFloat *iSamples = &iFrames[iChannel];
  130. StkFloat *oSamples = &oFrames[oChannel];
  131. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  132. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  133. delayLine_[0].setDelay( baseLength_ * 0.707 * ( 1.0 + modDepth_ * mods_[0].tick() ) );
  134. delayLine_[1].setDelay( baseLength_ * 0.5 * ( 1.0 - modDepth_ * mods_[1].tick() ) );
  135. *oSamples = effectMix_ * ( delayLine_[0].tick( *iSamples ) - *iSamples ) + *iSamples;
  136. *(oSamples+1) = effectMix_ * ( delayLine_[1].tick( *iSamples ) - *iSamples ) + *iSamples;
  137. }
  138. lastFrame_[0] = *(oSamples-oHop);
  139. lastFrame_[1] = *(oSamples-oHop+1);
  140. return iFrames;
  141. }
  142. } // stk namespace
  143. #endif