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.

169 lines
3.0KB

  1. #include "FractionalDelay.h"
  2. #include "asserts.h"
  3. // test that we can set up and get zero output
  4. static void test0()
  5. {
  6. FractionalDelay f(100);
  7. f.setDelay(50);
  8. float x = f.run(1);
  9. assertEQ(x, 0);
  10. }
  11. // test that the output is perhaps a delayed version of the input
  12. static void test1()
  13. {
  14. printf("test1\n\n");
  15. FractionalDelay f(100);
  16. f.setDelay(10);
  17. float x = 0;
  18. for (int i = 0; i < 20; ++i) {
  19. x = f.run(1);
  20. }
  21. assertEQ(x, 1);
  22. }
  23. static void testTime(float delayTime, float expectedValue, float tolerance)
  24. {
  25. int iTime = int(delayTime);
  26. if ((delayTime - iTime) > .0000000001) {
  27. iTime++;
  28. }
  29. FractionalDelay f(100);
  30. f.setDelay(delayTime);
  31. float x = 0;
  32. float lastX = 0;
  33. for (int i = 0; i < 20; ++i) {
  34. x = f.run(1);
  35. // printf("in loop i=%d, x=%f\n", i, x);
  36. if (x > .99f) {
  37. assertEQ(i, iTime); // delay amount should be >= 10 < 11;
  38. assertClose(lastX, expectedValue, tolerance);
  39. return;
  40. }
  41. lastX = x;
  42. }
  43. assert(false); // we should have exited already
  44. }
  45. static void test2()
  46. {
  47. testTime(10.0f, 0, .0001f);
  48. }
  49. static void test3()
  50. {
  51. testTime(10.5f, 0.5f, .0001f);
  52. }
  53. static void test4()
  54. {
  55. testTime(10.25f, 0.75f, .1f);
  56. }
  57. static void test5()
  58. {
  59. // MAKE THIS WORK
  60. // testTime(10.05f, 0, .1f);
  61. }
  62. static void test6()
  63. {
  64. testTime(10.75f, .25, .05f);
  65. }
  66. static void test10()
  67. {
  68. RecirculatingFractionalDelay d(1000);
  69. d.setDelay(100);
  70. assertEQ(d.run(0), 0);
  71. }
  72. static void test11()
  73. {
  74. RecirculatingFractionalDelay d(1000);
  75. d.setDelay(100);
  76. for (int i = 0; ; ++i) {
  77. assertLT(i, 110); // too far with no sound
  78. float x = d.run(1);
  79. if (x > .5) {
  80. return; //we found sound!
  81. }
  82. }
  83. }
  84. static void testRecirc(float delayTime, float feedback, float expectedDuration, bool expectDurationLonger)
  85. {
  86. RecirculatingFractionalDelay d(1000);
  87. d.setDelay(delayTime);
  88. d.setFeedback(feedback);
  89. const int maxDur = 10000;
  90. assert(expectedDuration < maxDur);
  91. // feed a blast into it
  92. for (int i = 0; i < 10; ++i) {
  93. d.run(1);
  94. }
  95. int lastSound = 0;
  96. for (int i = 0; ; ++i) {
  97. if (i > maxDur) {
  98. // we have hit end of test
  99. // If we expect infinit, that's ok
  100. if (!expectDurationLonger) {
  101. assertLT(lastSound, expectedDuration);
  102. } else {
  103. assertGT(lastSound, expectedDuration);
  104. }
  105. break;
  106. }
  107. float x = d.run(0);
  108. if (x > .1) {
  109. lastSound = i;
  110. }
  111. if (lastSound > expectedDuration && !expectDurationLonger) {
  112. assert(false);
  113. }
  114. }
  115. }
  116. static void test12()
  117. {
  118. testRecirc(20, 0, 22, false);
  119. }
  120. static void test13()
  121. {
  122. testRecirc(20, .9f, 100, true);
  123. }
  124. void testDelay()
  125. {
  126. test0();
  127. test1();
  128. test2();
  129. test3();
  130. test4();
  131. test5();
  132. test6();
  133. test10();
  134. test11();
  135. test12();
  136. test13();
  137. }