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.

208 lines
6.6KB

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