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.

231 lines
7.3KB

  1. #ifndef STK_DELAYL_H
  2. #define STK_DELAYL_H
  3. #include "Filter.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class DelayL
  7. \brief STK linear interpolating delay line class.
  8. This class implements a fractional-length digital delay-line using
  9. first-order linear interpolation. If the delay and maximum length
  10. are not specified during instantiation, a fixed maximum length of
  11. 4095 and a delay of zero is set.
  12. Linear interpolation is an efficient technique for achieving
  13. fractional delay lengths, though it does introduce high-frequency
  14. signal attenuation to varying degrees depending on the fractional
  15. delay setting. The use of higher order Lagrange interpolators can
  16. typically improve (minimize) this attenuation characteristic.
  17. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  18. */
  19. /***************************************************/
  20. class DelayL : public Filter
  21. {
  22. public:
  23. //! Default constructor creates a delay-line with maximum length of 4095 samples and zero delay.
  24. /*!
  25. An StkError will be thrown if the delay parameter is less than
  26. zero, the maximum delay parameter is less than one, or the delay
  27. parameter is greater than the maxDelay value.
  28. */
  29. DelayL( StkFloat delay = 0.0, unsigned long maxDelay = 4095 );
  30. //! Class destructor.
  31. ~DelayL();
  32. //! Get the maximum delay-line length.
  33. unsigned long getMaximumDelay( void ) { return inputs_.size() - 1; };
  34. //! Set the maximum delay-line length.
  35. /*!
  36. This method should generally only be used during initial setup
  37. of the delay line. If it is used between calls to the tick()
  38. function, without a call to clear(), a signal discontinuity will
  39. likely occur. If the current maximum length is greater than the
  40. new length, no memory allocation change is made.
  41. */
  42. void setMaximumDelay( unsigned long delay );
  43. //! Set the delay-line length.
  44. /*!
  45. The valid range for \e delay is from 0 to the maximum delay-line length.
  46. */
  47. void setDelay( StkFloat delay );
  48. //! Return the current delay-line length.
  49. StkFloat getDelay( void ) const { return delay_; };
  50. //! Return the value at \e tapDelay samples from the delay-line input.
  51. /*!
  52. The tap point is determined modulo the delay-line length and is
  53. relative to the last input value (i.e., a tapDelay of zero returns
  54. the last input value).
  55. */
  56. StkFloat tapOut( unsigned long tapDelay );
  57. //! Set the \e value at \e tapDelay samples from the delay-line input.
  58. void tapIn( StkFloat value, unsigned long tapDelay );
  59. //! Return the last computed output value.
  60. StkFloat lastOut( void ) const { return lastFrame_[0]; };
  61. //! Return the value which will be output by the next call to tick().
  62. /*!
  63. This method is valid only for delay settings greater than zero!
  64. */
  65. StkFloat nextOut( void );
  66. //! Input one sample to the filter and return one output.
  67. StkFloat tick( StkFloat input );
  68. //! Take a channel of the StkFrames object as inputs to the filter and replace with corresponding outputs.
  69. /*!
  70. The StkFrames argument reference is returned. The \c channel
  71. argument must be less than the number of channels in the
  72. StkFrames argument (the first channel is specified by 0).
  73. However, range checking is only performed if _STK_DEBUG_ is
  74. defined during compilation, in which case an out-of-range value
  75. will trigger an StkError exception.
  76. */
  77. StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
  78. //! Take a channel of the \c iFrames object as inputs to the filter and write outputs to the \c oFrames object.
  79. /*!
  80. The \c iFrames object reference is returned. Each channel
  81. argument must be less than the number of channels in the
  82. corresponding StkFrames argument (the first channel is specified
  83. by 0). However, range checking is only performed if _STK_DEBUG_
  84. is defined during compilation, in which case an out-of-range value
  85. will trigger an StkError exception.
  86. */
  87. StkFrames& tick( StkFrames& iFrames, StkFrames &oFrames, unsigned int iChannel = 0, unsigned int oChannel = 0 );
  88. protected:
  89. unsigned long inPoint_;
  90. unsigned long outPoint_;
  91. StkFloat delay_;
  92. StkFloat alpha_;
  93. StkFloat omAlpha_;
  94. StkFloat nextOutput_;
  95. bool doNextOut_;
  96. };
  97. inline StkFloat DelayL :: nextOut( void )
  98. {
  99. if ( doNextOut_ ) {
  100. // First 1/2 of interpolation
  101. nextOutput_ = inputs_[outPoint_] * omAlpha_;
  102. // Second 1/2 of interpolation
  103. if (outPoint_+1 < inputs_.size())
  104. nextOutput_ += inputs_[outPoint_+1] * alpha_;
  105. else
  106. nextOutput_ += inputs_[0] * alpha_;
  107. doNextOut_ = false;
  108. }
  109. return nextOutput_;
  110. }
  111. inline void DelayL :: setDelay( StkFloat delay )
  112. {
  113. if ( delay + 1 > inputs_.size() ) { // The value is too big.
  114. oStream_ << "DelayL::setDelay: argument (" << delay << ") greater than maximum!";
  115. handleError( StkError::WARNING ); return;
  116. }
  117. if (delay < 0 ) {
  118. oStream_ << "DelayL::setDelay: argument (" << delay << ") less than zero!";
  119. handleError( StkError::WARNING ); return;
  120. }
  121. StkFloat outPointer = inPoint_ - delay; // read chases write
  122. delay_ = delay;
  123. while ( outPointer < 0 )
  124. outPointer += inputs_.size(); // modulo maximum length
  125. outPoint_ = (long) outPointer; // integer part
  126. alpha_ = outPointer - outPoint_; // fractional part
  127. omAlpha_ = (StkFloat) 1.0 - alpha_;
  128. if ( outPoint_ == inputs_.size() ) outPoint_ = 0;
  129. doNextOut_ = true;
  130. }
  131. inline StkFloat DelayL :: tick( StkFloat input )
  132. {
  133. inputs_[inPoint_++] = input * gain_;
  134. // Increment input pointer modulo length.
  135. if ( inPoint_ == inputs_.size() )
  136. inPoint_ = 0;
  137. lastFrame_[0] = nextOut();
  138. doNextOut_ = true;
  139. // Increment output pointer modulo length.
  140. if ( ++outPoint_ == inputs_.size() )
  141. outPoint_ = 0;
  142. return lastFrame_[0];
  143. }
  144. inline StkFrames& DelayL :: tick( StkFrames& frames, unsigned int channel )
  145. {
  146. #if defined(_STK_DEBUG_)
  147. if ( channel >= frames.channels() ) {
  148. oStream_ << "DelayL::tick(): channel and StkFrames arguments are incompatible!";
  149. handleError( StkError::FUNCTION_ARGUMENT );
  150. }
  151. #endif
  152. StkFloat *samples = &frames[channel];
  153. unsigned int hop = frames.channels();
  154. for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
  155. inputs_[inPoint_++] = *samples * gain_;
  156. if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
  157. *samples = nextOut();
  158. doNextOut_ = true;
  159. if ( ++outPoint_ == inputs_.size() ) outPoint_ = 0;
  160. }
  161. lastFrame_[0] = *(samples-hop);
  162. return frames;
  163. }
  164. inline StkFrames& DelayL :: tick( StkFrames& iFrames, StkFrames& oFrames, unsigned int iChannel, unsigned int oChannel )
  165. {
  166. #if defined(_STK_DEBUG_)
  167. if ( iChannel >= iFrames.channels() || oChannel >= oFrames.channels() ) {
  168. oStream_ << "DelayL::tick(): channel and StkFrames arguments are incompatible!";
  169. handleError( StkError::FUNCTION_ARGUMENT );
  170. }
  171. #endif
  172. StkFloat *iSamples = &iFrames[iChannel];
  173. StkFloat *oSamples = &oFrames[oChannel];
  174. unsigned int iHop = iFrames.channels(), oHop = oFrames.channels();
  175. for ( unsigned int i=0; i<iFrames.frames(); i++, iSamples += iHop, oSamples += oHop ) {
  176. inputs_[inPoint_++] = *iSamples * gain_;
  177. if ( inPoint_ == inputs_.size() ) inPoint_ = 0;
  178. *oSamples = nextOut();
  179. doNextOut_ = true;
  180. if ( ++outPoint_ == inputs_.size() ) outPoint_ = 0;
  181. }
  182. lastFrame_[0] = *(oSamples-oHop);
  183. return iFrames;
  184. }
  185. } // stk namespace
  186. #endif