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.

189 lines
6.2KB

  1. #ifndef STK_DELAY_H
  2. #define STK_DELAY_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class Delay
  7. \brief STK non-interpolating delay line class.
  8. This class implements a non-interpolating digital delay-line. If
  9. the delay and maximum length are not specified during
  10. instantiation, a fixed maximum length of 4095 and a delay of zero
  11. is set.
  12. A non-interpolating delay line is typically used in fixed
  13. delay-length applications, such as for reverberation.
  14. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  15. */
  16. /***************************************************/
  17. class Delay : public Filter
  18. {
  19. public:
  20. //! The default constructor creates a delay-line with maximum length of 4095 samples and zero delay.
  21. /*!
  22. An StkError will be thrown if the delay parameter is less than
  23. zero, the maximum delay parameter is less than one, or the delay
  24. parameter is greater than the maxDelay value.
  25. */
  26. Delay( unsigned long delay = 0, unsigned long maxDelay = 4095 );
  27. //! Class destructor.
  28. ~Delay();
  29. //! Get the maximum delay-line length.
  30. unsigned long getMaximumDelay( void ) { return inputs_.size() - 1; };
  31. //! Set the maximum delay-line length.
  32. /*!
  33. This method should generally only be used during initial setup
  34. of the delay line. If it is used between calls to the tick()
  35. function, without a call to clear(), a signal discontinuity will
  36. likely occur. If the current maximum length is greater than the
  37. new length, no memory allocation change is made.
  38. */
  39. void setMaximumDelay( unsigned long delay );
  40. //! Set the delay-line length.
  41. /*!
  42. The valid range for \e delay is from 0 to the maximum delay-line length.
  43. */
  44. void setDelay( unsigned long delay );
  45. //! Return the current delay-line length.
  46. unsigned long getDelay( void ) const { return delay_; };
  47. //! Return the value at \e tapDelay samples from the delay-line input.
  48. /*!
  49. The tap point is determined modulo the delay-line length and is
  50. relative to the last input value (i.e., a tapDelay of zero returns
  51. the last input value).
  52. */
  53. StkFloat tapOut( unsigned long tapDelay );
  54. //! Set the \e value at \e tapDelay samples from the delay-line input.
  55. void tapIn( StkFloat value, unsigned long tapDelay );
  56. //! Sum the provided \e value into the delay line at \e tapDelay samples from the input.
  57. /*!
  58. The new value is returned. The tap point is determined modulo
  59. the delay-line length and is relative to the last input value
  60. (i.e., a tapDelay of zero sums into the last input value).
  61. */
  62. StkFloat addTo( StkFloat value, unsigned long tapDelay );
  63. //! Return the last computed output value.
  64. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  65. //! Return the value that will be output by the next call to tick().
  66. /*!
  67. This method is valid only for delay settings greater than zero!
  68. */
  69. StkFloat nextOut( void ) { return inputs_[outPoint_]; };
  70. //! Calculate and return the signal energy in the delay-line.
  71. StkFloat energy( void ) const;
  72. //! Input one sample to the filter and return one output.
  73. StkFloat tick( StkFloat input );
  74. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  75. /*!
  76. The StkFrames argument reference is returned. The \c channel
  77. argument must be less than the number of channels in the
  78. StkFrames argument (the first channel is specified by 0).
  79. However, range checking is only performed if _STK_DEBUG_ is
  80. defined during compilation, in which case an out-of-range value
  81. will trigger an StkError exception.
  82. */
  83. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  84. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  85. /*!
  86. The \c iFrames object reference is returned. Each channel
  87. argument must be less than the number of channels in the
  88. corresponding StkFrames argument (the first channel is specified
  89. by 0). However, range checking is only performed if _STK_DEBUG_
  90. is defined during compilation, in which case an out-of-range value
  91. will trigger an StkError exception.
  92. */
  93. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  94. protected:
  95. unsigned long inPoint_;
  96. unsigned long outPoint_;
  97. unsigned long delay_;
  98. };
  99. inline StkFloat Delay :: tick( StkFloat input )
  100. {
  101. inputs_[inPoint_++] = input * gain_;
  102. // Check for end condition
  103. if ( inPoint_ == inputs_.size() )
  104. inPoint_ = 0;
  105. // Read out next value
  106. lastFrame_[0] = inputs_[outPoint_++];
  107. if ( outPoint_ == inputs_.size() )
  108. outPoint_ = 0;
  109. return lastFrame_[0];
  110. }
  111. inline StkFrames& Delay :: tick( StkFrames& frames, unsigned int channel )
  112. {
  113. #if defined(_STK_DEBUG_)
  114. if ( channel >= frames.channels() ) {
  115. oStream_ << "Delay::tick(): channel and StkFrames arguments are incompatible!";
  116. handleError( StkError::FUNCTION_ARGUMENT );
  117. }
  118. #endif
  119. StkFloat *samples = &frames[channel];
  120. unsigned int hop = frames.channels();
  121. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  122. inputs_[inPoint_++] = *samples * gain_;
  123. if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
  124. *samples = inputs_[outPoint_++];
  125. if ( outPoint_ == inputs_.size() ) outPoint_ = 0;
  126. }
  127. lastFrame_[0] = *(samples-hop);
  128. return frames;
  129. }
  130. inline StkFrames& Delay :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  131. {
  132. #if defined(_STK_DEBUG_)
  133. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  134. oStream_ << "Delay::tick(): channel and StkFrames arguments are incompatible!";
  135. handleError( StkError::FUNCTION_ARGUMENT );
  136. }
  137. #endif
  138. StkFloat *iSamples = &iFrames[iChannel];
  139. StkFloat *oSamples = &oFrames[oChannel];
  140. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  141. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  142. inputs_[inPoint_++] = *iSamples * gain_;
  143. if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
  144. *oSamples = inputs_[outPoint_++];
  145. if ( outPoint_ == inputs_.size() ) outPoint_ = 0;
  146. }
  147. lastFrame_[0] = *(oSamples-oHop);
  148. return iFrames;
  149. }
  150. } // stk namespace
  151. #endif