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.

98 lines
2.3KB

  1. #include "FractionalDelay.h"
  2. #include <assert.h>
  3. float FractionalDelay::getOutput()
  4. {
  5. #ifdef _LOG
  6. printf("\n");
  7. #endif
  8. int delayTimeSamples = (int) delayTime;
  9. const double x = delayTime - delayTimeSamples;
  10. const double y0 = getDelayedOutput(delayTimeSamples - 1);
  11. const double y1 = getDelayedOutput(delayTimeSamples);
  12. const double y2 = getDelayedOutput(delayTimeSamples + 1);
  13. const double y3 = getDelayedOutput(delayTimeSamples + 2);
  14. #ifdef _LOG
  15. printf("dt=%.2f, dts=%d x=%.2f ", delayTime, delayTimeSamples, x);
  16. printf("y0=%.2f y1=%.2f y2=%.2f y3=%.2f\n", y0, y1, y2, y3);
  17. #endif
  18. const double x0 = -1.0;
  19. const double x1 = 0.0;
  20. const double x2 = 1.0;
  21. const double x3 = 2.0;
  22. assert(x >= x1);
  23. assert(x <= x2);
  24. double dRet = -(1.0 / 6.0)*y0*(x - x1)*(x - x2)*(x - x3);
  25. dRet += (1.0 / 2.0)* y1*(x - x0) * (x - x2) * (x - x3);
  26. dRet += (-1.0 / 2.0)*y2*(x - x0) * (x - x1) * (x - x3);
  27. dRet += (1.0 / 6.0) * y3*(x - x0) * (x - x1) * (x - x2);
  28. #if 0
  29. static int ct = 0;
  30. if (++ct > 100) {
  31. ct = 0;
  32. char buf[500];
  33. sprintf(buf, "del = %f int=%d, rem=%f ret=%f\n", time, delayTimeSamples, x, dRet);
  34. DebugUtil::trace(buf);
  35. sprintf(buf, " y0=%f y1=%f y2=%f y3=%f\n", y0, y1, y2, y3);
  36. DebugUtil::trace(buf);
  37. }
  38. #endif
  39. return float(dRet);
  40. }
  41. float FractionalDelay::getDelayedOutput(int delaySamples)
  42. {
  43. int index = inputPointerIndex;
  44. index -= delaySamples; // indexes increase as time goes by,
  45. // to the output (in the past), is at a lower index
  46. if (index < 0) {
  47. //int n = state.numSamples'
  48. index += numSamples;
  49. assert(index >= 0 && index < numSamples);
  50. }
  51. #ifdef _LOG
  52. printf("getting output from area of %d\n", index);
  53. #endif
  54. return delayMemory[index];
  55. }
  56. void FractionalDelay::setInput(float input)
  57. {
  58. //printf("setting input at %d\n", inputPointerIndex);
  59. delayMemory[inputPointerIndex++] = input;
  60. if (inputPointerIndex >= numSamples) {
  61. inputPointerIndex = 0;
  62. }
  63. }
  64. /*
  65. float run(float input)
  66. {
  67. float ret = getOutput();
  68. setInput(input);
  69. return ret;
  70. }
  71. */
  72. float RecirculatingFractionalDelay::run(float input)
  73. {
  74. float output = getOutput();
  75. input += (output * feedback);
  76. setInput(input);
  77. return output;
  78. }