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.

242 lines
5.5KB

  1. #include "asserts.h"
  2. #include "ObjectCache.h"
  3. extern int _numLookupParams;
  4. template <typename T>
  5. static void testBipolar()
  6. {
  7. assertEQ(_numLookupParams, 0);
  8. auto test = ObjectCache<T>::getBipolarAudioTaper();
  9. assertEQ(_numLookupParams, 1);
  10. auto test2 = ObjectCache<T>::getBipolarAudioTaper();
  11. assertEQ(_numLookupParams, 1);
  12. test.reset();
  13. assertEQ(_numLookupParams, 1);
  14. test2.reset();
  15. assertEQ(_numLookupParams, 0);
  16. {
  17. // simple test that bipolar audio scalers use cached lookups, and they work
  18. AudioMath::ScaleFun<float> f = AudioMath::makeScalerWithBipolarAudioTrim(3, 4);
  19. assertEQ(f(0, -5, 0), 3.);
  20. assertEQ(_numLookupParams, 1);
  21. }
  22. assertEQ(_numLookupParams, 0);
  23. // make again
  24. test = ObjectCache<T>::getBipolarAudioTaper();
  25. assertEQ(_numLookupParams, 1);
  26. }
  27. template <typename T>
  28. static void testAudioTaper()
  29. {
  30. assertEQ(_numLookupParams, 0);
  31. auto test = ObjectCache<T>::getAudioTaper();
  32. assertEQ(_numLookupParams, 1);
  33. auto test2 = ObjectCache<T>::getAudioTaper();
  34. assertEQ(_numLookupParams, 1);
  35. test.reset();
  36. assertEQ(_numLookupParams, 1);
  37. test2.reset();
  38. assertEQ(_numLookupParams, 0);
  39. {
  40. // simple test that bipolar audio scalers use cached lookups, and they work
  41. AudioMath::SimpleScaleFun<float> f = AudioMath::makeSimpleScalerAudioTaper(3, 4);
  42. assertEQ(f(0, -5), 3.);
  43. assertEQ(_numLookupParams, 1);
  44. assertEQ(f(5, 5), 4.);
  45. }
  46. assertEQ(_numLookupParams, 0);
  47. // make again
  48. test = ObjectCache<T>::getAudioTaper();
  49. assertEQ(_numLookupParams, 1);
  50. }
  51. template <typename T>
  52. static void testSin()
  53. {
  54. assertEQ(_numLookupParams, 0);
  55. auto test = ObjectCache<T>::getSinLookup();
  56. assertEQ(_numLookupParams, 1);
  57. auto test2 = ObjectCache<T>::getSinLookup();
  58. assertEQ(_numLookupParams, 1);
  59. test.reset();
  60. assertEQ(_numLookupParams, 1);
  61. test2.reset();
  62. assertEQ(_numLookupParams, 0);
  63. {
  64. // // simple test that bipolar audio scalers use cached lookups, and they work
  65. AudioMath::ScaleFun<float> f = AudioMath::makeScalerWithBipolarAudioTrim(3, 4);
  66. assertEQ(f(0, -5, 0), 3.);
  67. assertEQ(_numLookupParams, 1);
  68. }
  69. assertEQ(_numLookupParams, 0);
  70. // make again
  71. test = ObjectCache<T>::getSinLookup();
  72. assertEQ(_numLookupParams, 1);
  73. }
  74. template <typename T>
  75. static void testExp2()
  76. {
  77. assertEQ(_numLookupParams, 0);
  78. auto test = ObjectCache<T>::getExp2();
  79. assertEQ(_numLookupParams, 1);
  80. auto test2 = ObjectCache<T>::getExp2();
  81. assertEQ(_numLookupParams, 1);
  82. test.reset();
  83. assertEQ(_numLookupParams, 1);
  84. test2.reset();
  85. assertEQ(_numLookupParams, 0);
  86. {
  87. auto test3 = ObjectCache<T>::getExp2();
  88. const double x = LookupTable<T>::lookup(*test3, (T)3.2);
  89. const double y = std::pow(2, 3.2);
  90. assertClose(x, y, .001);
  91. assertEQ(_numLookupParams, 1);
  92. }
  93. assertEQ(_numLookupParams, 0);
  94. // make again
  95. test = ObjectCache<T>::getExp2();
  96. assertEQ(_numLookupParams, 1);
  97. }
  98. template <typename T>
  99. static void testExp2b()
  100. {
  101. {
  102. // make sure exp2 is really 1V/octave
  103. auto ex2 = ObjectCache<T>::getExp2();
  104. const T a = LookupTable<T>::lookup(*ex2, 5);
  105. const T b = LookupTable<T>::lookup(*ex2, 6);
  106. assertClose(b / a, 2, .001);
  107. }
  108. }
  109. template <typename T>
  110. static void testDb2Gain()
  111. {
  112. assertEQ(_numLookupParams, 0);
  113. auto test = ObjectCache<T>::getDb2Gain();
  114. assertEQ(_numLookupParams, 1);
  115. auto test2 = ObjectCache<T>::getDb2Gain();
  116. assertEQ(_numLookupParams, 1);
  117. test.reset();
  118. assertEQ(_numLookupParams, 1);
  119. test2.reset();
  120. assertEQ(_numLookupParams, 0);
  121. {
  122. auto test3 = ObjectCache<T>::getDb2Gain();
  123. const double x = LookupTable<T>::lookup(*test3, (T) -12);
  124. const double y = AudioMath::gainFromDb(-12);
  125. assertClose(x, y, .1);
  126. assertEQ(_numLookupParams, 1);
  127. }
  128. assertEQ(_numLookupParams, 0);
  129. // make again
  130. test = ObjectCache<T>::getDb2Gain();
  131. assertEQ(_numLookupParams, 1);
  132. }
  133. template <typename T>
  134. static void testDb2Gain2()
  135. {
  136. assertEQ(_numLookupParams, 0);
  137. auto test = ObjectCache<T>::getDb2Gain();
  138. // .1 db from -80 to +20
  139. for (double db = -80; db < 20; db += .1) {
  140. const double x = LookupTable<T>::lookup(*test, (T) db);
  141. const double y = AudioMath::gainFromDb(db);
  142. assertClose(x, y, .2);
  143. }
  144. }
  145. template <typename T>
  146. static void testTanh5()
  147. {
  148. auto test = ObjectCache<T>::getTanh5();
  149. auto test2 = ObjectCache<T>::getTanh5();
  150. assertEQ(_numLookupParams, 1);
  151. for (double x = -5; x <= 5; x += .1) {
  152. const double y = LookupTable<T>::lookup(*test, (T) x);
  153. const double y0 = std::tanh(x);
  154. assertClose(y, y0, .01);
  155. }
  156. }
  157. template <typename T>
  158. static void testExp2Ex()
  159. {
  160. auto f = ObjectCache<T>::getExp2Ex();
  161. assertEQ(_numLookupParams, 2);
  162. }
  163. template <typename T>
  164. static void testLPF()
  165. {
  166. // make sure we can get the two supported cutoffs
  167. auto f16 = ObjectCache<T>::get6PLPParams(.25f / 16);
  168. assert(f16);
  169. auto f4 = ObjectCache<T>::get6PLPParams(.25f / 4);
  170. assert(f4);
  171. }
  172. template <typename T>
  173. static void test()
  174. {
  175. testBipolar<T>();
  176. testAudioTaper<T>();
  177. testSin<T>();
  178. testExp2<T>();
  179. testExp2b<T>();
  180. testDb2Gain<T>();
  181. testDb2Gain2<T>();
  182. testTanh5<T>();
  183. testExp2Ex<T>();
  184. testLPF<T>();
  185. }
  186. void testObjectCache()
  187. {
  188. assertEQ(_numLookupParams, 0);
  189. test<float>();
  190. test<double>();
  191. assertEQ(_numLookupParams, 0);
  192. }