Audio plugin host https://kx.studio/carla
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.

116 lines
2.8KB

  1. /***************************************************/
  2. /*! \class Delay
  3. \brief STK non-interpolating delay line class.
  4. This class implements a non-interpolating digital delay-line. If
  5. the delay and maximum length are not specified during
  6. instantiation, a fixed maximum length of 4095 and a delay of zero
  7. is set.
  8. A non-interpolating delay line is typically used in fixed
  9. delay-length applications, such as for reverberation.
  10. by Perry R. Cook and Gary P. Scavone, 1995-2011.
  11. */
  12. /***************************************************/
  13. #include "Delay.h"
  14. namespace stk {
  15. Delay :: Delay( unsigned long delay, unsigned long maxDelay )
  16. {
  17. // Writing before reading allows delays from 0 to length-1.
  18. // If we want to allow a delay of maxDelay, we need a
  19. // delay-line of length = maxDelay+1.
  20. if ( delay > maxDelay ) {
  21. oStream_ << "Delay::Delay: maxDelay must be > than delay argument!\n";
  22. handleError( StkError::FUNCTION_ARGUMENT );
  23. }
  24. if ( ( maxDelay + 1 ) > inputs_.size() )
  25. inputs_.resize( maxDelay + 1, 1, 0.0 );
  26. inPoint_ = 0;
  27. this->setDelay( delay );
  28. }
  29. Delay :: ~Delay()
  30. {
  31. }
  32. void Delay :: setMaximumDelay( unsigned long delay )
  33. {
  34. if ( delay < inputs_.size() ) return;
  35. inputs_.resize( delay + 1 );
  36. }
  37. void Delay :: setDelay( unsigned long delay )
  38. {
  39. if ( delay > inputs_.size() - 1 ) { // The value is too big.
  40. oStream_ << "Delay::setDelay: argument (" << delay << ") greater than maximum!\n";
  41. handleError( StkError::WARNING ); return;
  42. }
  43. if ( delay < 0 ) {
  44. oStream_ << "Delay::setDelay: argument (" << delay << ") less than zero!\n";
  45. handleError( StkError::WARNING ); return;
  46. }
  47. // read chases write
  48. if ( inPoint_ >= delay ) outPoint_ = inPoint_ - delay;
  49. else outPoint_ = inputs_.size() + inPoint_ - delay;
  50. delay_ = delay;
  51. }
  52. StkFloat Delay :: energy( void ) const
  53. {
  54. unsigned long i;
  55. register StkFloat e = 0;
  56. if ( inPoint_ >= outPoint_ ) {
  57. for ( i=outPoint_; i<inPoint_; i++ ) {
  58. register StkFloat t = inputs_[i];
  59. e += t*t;
  60. }
  61. } else {
  62. for ( i=outPoint_; i<inputs_.size(); i++ ) {
  63. register StkFloat t = inputs_[i];
  64. e += t*t;
  65. }
  66. for ( i=0; i<inPoint_; i++ ) {
  67. register StkFloat t = inputs_[i];
  68. e += t*t;
  69. }
  70. }
  71. return e;
  72. }
  73. StkFloat Delay :: tapOut( unsigned long tapDelay )
  74. {
  75. long tap = inPoint_ - tapDelay - 1;
  76. while ( tap < 0 ) // Check for wraparound.
  77. tap += inputs_.size();
  78. return inputs_[tap];
  79. }
  80. void Delay :: tapIn( StkFloat value, unsigned long tapDelay )
  81. {
  82. long tap = inPoint_ - tapDelay - 1;
  83. while ( tap < 0 ) // Check for wraparound.
  84. tap += inputs_.size();
  85. inputs_[tap] = value;
  86. }
  87. StkFloat Delay :: addTo( StkFloat value, unsigned long tapDelay )
  88. {
  89. long tap = inPoint_ - tapDelay - 1;
  90. while ( tap < 0 ) // Check for wraparound.
  91. tap += inputs_.size();
  92. return inputs_[tap]+= value;
  93. }
  94. } // stk namespace