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.

121 lines
3.7KB

  1. #ifndef STK_ECHO_H
  2. #define STK_ECHO_H
  3. #include "Effect.h"
  4. #include "Delay.h"
  5. namespace stk {
  6. /***************************************************/
  7. /*! \class Echo
  8. \brief STK echo effect class.
  9. This class implements an echo effect.
  10. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  11. */
  12. /***************************************************/
  13. class Echo : public Effect
  14. {
  15. public:
  16. //! Class constructor, taking the longest desired delay length (one second default value).
  17. /*!
  18. The default delay value is set to 1/2 the maximum delay length.
  19. */
  20. Echo( unsigned long maximumDelay = (unsigned long) Stk::sampleRate() );
  21. //! Reset and clear all internal state.
  22. void clear();
  23. //! Set the maximum delay line length in samples.
  24. void setMaximumDelay( unsigned long delay );
  25. //! Set the delay line length in samples.
  26. void setDelay( unsigned long delay );
  27. //! Return the last computed output value.
  28. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  29. //! Input one sample to the effect and return one output.
  30. StkFloat tick( StkFloat input );
  31. //! Take a channel of the StkFrames object as inputs to the effect and replace with corresponding outputs.
  32. /*!
  33. The StkFrames argument reference is returned. The \c channel
  34. argument must be less than the number of channels in the
  35. StkFrames argument (the first channel is specified by 0).
  36. However, range checking is only performed if _STK_DEBUG_ is
  37. defined during compilation, in which case an out-of-range value
  38. will trigger an StkError exception.
  39. */
  40. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  41. //! Take a channel of the \c iFrames object as inputs to the effect and write outputs to the \c oFrames object.
  42. /*!
  43. The \c iFrames object reference is returned. Each channel
  44. argument must be less than the number of channels in the
  45. corresponding StkFrames argument (the first channel is specified
  46. by 0). However, range checking is only performed if _STK_DEBUG_
  47. is defined during compilation, in which case an out-of-range value
  48. will trigger an StkError exception.
  49. */
  50. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  51. protected:
  52. Delay delayLine_;
  53. unsigned long length_;
  54. };
  55. inline StkFloat Echo :: tick( StkFloat input )
  56. {
  57. lastFrame_[0] = effectMix_ * ( delayLine_.tick( input ) - input ) + input;
  58. return lastFrame_[0];
  59. }
  60. inline StkFrames& Echo :: tick( StkFrames& frames, unsigned int channel )
  61. {
  62. #if defined(_STK_DEBUG_)
  63. if ( channel >= frames.channels() ) {
  64. oStream_ << "Echo::tick(): channel and StkFrames arguments are incompatible!";
  65. handleError( StkError::FUNCTION_ARGUMENT );
  66. }
  67. #endif
  68. StkFloat *samples = &frames[channel];
  69. unsigned int hop = frames.channels();
  70. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  71. *samples = effectMix_ * ( delayLine_.tick( *samples ) - *samples ) + *samples;
  72. }
  73. lastFrame_[0] = *(samples-hop);
  74. return frames;
  75. }
  76. inline StkFrames& Echo :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  77. {
  78. #if defined(_STK_DEBUG_)
  79. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  80. oStream_ << "Echo::tick(): channel and StkFrames arguments are incompatible!";
  81. handleError( StkError::FUNCTION_ARGUMENT );
  82. }
  83. #endif
  84. StkFloat *iSamples = &iFrames[iChannel];
  85. StkFloat *oSamples = &oFrames[oChannel];
  86. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  87. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  88. *oSamples = effectMix_ * ( delayLine_.tick( *iSamples ) - *iSamples ) + *iSamples;
  89. }
  90. lastFrame_[0] = *(oSamples-oHop);
  91. return iFrames;
  92. }
  93. } // stk namespace
  94. #endif