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.

221 lines
7.8KB

  1. #ifndef STK_TAPDELAY_H
  2. #define STK_TAPDELAY_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class TapDelay
  7. \brief STK non-interpolating tapped delay line class.
  8. This class implements a non-interpolating digital delay-line with
  9. an arbitrary number of output "taps". If the maximum length and
  10. tap delays are not specified during instantiation, a fixed maximum
  11. length of 4095 and a single tap delay of zero 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 TapDelay : public Filter
  18. {
  19. public:
  20. //! The default constructor creates a delay-line with maximum length of 4095 samples and a single tap at delay = 0.
  21. /*!
  22. An StkError will be thrown if any tap delay parameter is less
  23. than zero, the maximum delay parameter is less than one, or any
  24. tap delay parameter is greater than the maxDelay value.
  25. */
  26. TapDelay( std::vector<unsigned long> taps = std::vector<unsigned long>( 1, 0 ), unsigned long maxDelay = 4095 );
  27. //! Class destructor.
  28. ~TapDelay();
  29. //! Set the maximum delay-line length.
  30. /*!
  31. This method should generally only be used during initial setup
  32. of the delay line. If it is used between calls to the tick()
  33. function, without a call to clear(), a signal discontinuity will
  34. likely occur. If the current maximum length is greater than the
  35. new length, no change will be made.
  36. */
  37. void setMaximumDelay( unsigned long delay );
  38. //! Set the delay-line tap lengths.
  39. /*!
  40. The valid range for each tap length is from 0 to the maximum delay-line length.
  41. */
  42. void setTapDelays( std::vector<unsigned long> taps );
  43. //! Return the current delay-line length.
  44. std::vector<unsigned long> getTapDelays( void ) const { return delays_; };
  45. //! Return the specified tap value of the last computed frame.
  46. /*!
  47. Use the lastFrame() function to get all tap values from the
  48. last computed frame. The \c tap argument must be less than the
  49. number of delayline taps (the first tap is specified by 0).
  50. However, range checking is only performed if _STK_DEBUG_ is
  51. defined during compilation, in which case an out-of-range value
  52. will trigger an StkError exception.
  53. */
  54. StkFloat lastOut( unsigned int tap = 0 ) const;
  55. //! Input one sample to the delayline and return outputs at all tap positions.
  56. /*!
  57. The StkFrames argument reference is returned. The output
  58. values are ordered according to the tap positions set using the
  59. setTapDelays() function (no sorting is performed). The StkFrames
  60. argument must contain at least as many channels as the number of
  61. taps. However, range checking is only performed if _STK_DEBUG_ is
  62. defined during compilation, in which case an out-of-range value
  63. will trigger an StkError exception.
  64. */
  65. StkFrames& tick( StkFloat input, StkFrames& outputs );
  66. //! Take a channel of the StkFrames object as inputs to the filter and write outputs back to the same object.
  67. /*!
  68. The StkFrames argument reference is returned. The output
  69. values are ordered according to the tap positions set using the
  70. setTapDelays() function (no sorting is performed). The StkFrames
  71. argument must contain at least as many channels as the number of
  72. taps. However, range checking is only performed if _STK_DEBUG_ is
  73. defined during compilation, in which case an out-of-range value
  74. will trigger an StkError exception.
  75. */
  76. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  77. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  78. /*!
  79. The \c iFrames object reference is returned. The output values
  80. are ordered according to the tap positions set using the
  81. setTapDelays() function (no sorting is performed). The \c
  82. iChannel argument must be less than the number of channels in
  83. the \c iFrames argument (the first channel is specified by 0).
  84. The \c oFrames argument must contain at least as many channels as
  85. the number of taps. However, range checking is only performed if
  86. _STK_DEBUG_ is defined during compilation, in which case an
  87. out-of-range value will trigger an StkError exception.
  88. */
  89. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0 );
  90. protected:
  91. unsigned long inPoint_;
  92. std::vector<unsigned long> outPoint_;
  93. std::vector<unsigned long> delays_;
  94. };
  95. inline StkFloat TapDelay :: lastOut( unsigned int tap ) const
  96. {
  97. #if defined(_STK_DEBUG_)
  98. if ( tap >= lastFrame_.size() ) {
  99. oStream_ << "TapDelay::lastOut(): tap argument and number of taps are incompatible!";
  100. handleError( StkError::FUNCTION_ARGUMENT );
  101. }
  102. #endif
  103. return lastFrame_[tap];
  104. }
  105. inline StkFrames& TapDelay :: tick( StkFloat input, StkFrames& outputs )
  106. {
  107. #if defined(_STK_DEBUG_)
  108. if ( outputs.channels() < outPoint_.size() ) {
  109. oStream_ << "TapDelay::tick(): number of taps > channels in StkFrames argument!";
  110. handleError( StkError::FUNCTION_ARGUMENT );
  111. }
  112. #endif
  113. inputs_[inPoint_++] = input * gain_;
  114. // Check for end condition
  115. if ( inPoint_ == inputs_.size() )
  116. inPoint_ = 0;
  117. // Read out next values
  118. StkFloat *outs = &outputs[0];
  119. for ( unsigned int i=0; i<outPoint_.size(); i++ ) {
  120. *outs++ = inputs_[outPoint_[i]];
  121. lastFrame_[i] = *outs;
  122. if ( ++outPoint_[i] == inputs_.size() )
  123. outPoint_[i] = 0;
  124. }
  125. return outputs;
  126. }
  127. inline StkFrames& TapDelay :: tick( StkFrames& frames, unsigned int channel )
  128. {
  129. #if defined(_STK_DEBUG_)
  130. if ( channel >= frames.channels() ) {
  131. oStream_ << "TapDelay::tick(): channel and StkFrames arguments are incompatible!";
  132. handleError( StkError::FUNCTION_ARGUMENT );
  133. }
  134. if ( frames.channels() < outPoint_.size() ) {
  135. oStream_ << "TapDelay::tick(): number of taps > channels in StkFrames argument!";
  136. handleError( StkError::FUNCTION_ARGUMENT );
  137. }
  138. #endif
  139. StkFloat *iSamples = &frames[channel];
  140. StkFloat *oSamples = &frames[0];
  141. std::size_t j;
  142. unsigned int iHop = frames.channels();
  143. std::size_t oHop = frames.channels() - outPoint_.size();
  144. for ( unsigned long i=0; i<frames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  145. inputs_[inPoint_++] = *iSamples * gain_;
  146. if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
  147. for ( j=0; j<outPoint_.size(); j++ ) {
  148. *oSamples++ = inputs_[outPoint_[j]];
  149. if ( ++outPoint_[j] == inputs_.size() ) outPoint_[j] = 0;
  150. }
  151. }
  152. oSamples -= frames.channels();
  153. for ( j=0; j<outPoint_.size(); j++ ) lastFrame_[j] = *oSamples++;
  154. return frames;
  155. }
  156. inline StkFrames& TapDelay :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel )
  157. {
  158. #if defined(_STK_DEBUG_)
  159. if ( iChannel >= iFrames.channels() ) {
  160. oStream_ << "TapDelay::tick(): channel and StkFrames arguments are incompatible!";
  161. handleError( StkError::FUNCTION_ARGUMENT );
  162. }
  163. if ( oFrames.channels() < outPoint_.size() ) {
  164. oStream_ << "TapDelay::tick(): number of taps > channels in output StkFrames argument!";
  165. handleError( StkError::FUNCTION_ARGUMENT );
  166. }
  167. #endif
  168. StkFloat *iSamples = &iFrames[iChannel];
  169. StkFloat *oSamples = &oFrames[0];
  170. std::size_t j;
  171. unsigned int iHop = iFrames.channels();
  172. std::size_t oHop = oFrames.channels() - outPoint_.size();
  173. for ( unsigned long i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  174. inputs_[inPoint_++] = *iSamples * gain_;
  175. if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
  176. for ( j=0; j<outPoint_.size(); j++ ) {
  177. *oSamples++ = inputs_[outPoint_[j]];
  178. if ( ++outPoint_[j] == inputs_.size() ) outPoint_[j] = 0;
  179. }
  180. }
  181. oSamples -= oFrames.channels();
  182. for ( j=0; j<outPoint_.size(); j++ ) lastFrame_[j] = *oSamples++;
  183. return iFrames;
  184. }
  185. } // stk namespace
  186. #endif