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.

111 lines
2.7KB

  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--2017.
  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, 1, 0.0 );
  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. // read chases write
  44. if ( inPoint_ >= delay ) outPoint_ = inPoint_ - delay;
  45. else outPoint_ = inputs_.size() + inPoint_ - delay;
  46. delay_ = delay;
  47. }
  48. StkFloat Delay :: energy( void ) const
  49. {
  50. unsigned long i;
  51. StkFloat e = 0;
  52. if ( inPoint_ >= outPoint_ ) {
  53. for ( i=outPoint_; i<inPoint_; i++ ) {
  54. StkFloat t = inputs_[i];
  55. e += t*t;
  56. }
  57. } else {
  58. for ( i=outPoint_; i<inputs_.size(); i++ ) {
  59. StkFloat t = inputs_[i];
  60. e += t*t;
  61. }
  62. for ( i=0; i<inPoint_; i++ ) {
  63. StkFloat t = inputs_[i];
  64. e += t*t;
  65. }
  66. }
  67. return e;
  68. }
  69. StkFloat Delay :: tapOut( unsigned long tapDelay )
  70. {
  71. long tap = inPoint_ - tapDelay - 1;
  72. while ( tap < 0 ) // Check for wraparound.
  73. tap += inputs_.size();
  74. return inputs_[tap];
  75. }
  76. void Delay :: tapIn( StkFloat value, unsigned long tapDelay )
  77. {
  78. long tap = inPoint_ - tapDelay - 1;
  79. while ( tap < 0 ) // Check for wraparound.
  80. tap += inputs_.size();
  81. inputs_[tap] = value;
  82. }
  83. StkFloat Delay :: addTo( StkFloat value, unsigned long tapDelay )
  84. {
  85. long tap = inPoint_ - tapDelay - 1;
  86. while ( tap < 0 ) // Check for wraparound.
  87. tap += inputs_.size();
  88. return inputs_[tap]+= value;
  89. }
  90. } // stk namespace