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.

853 lines
23KB

  1. #include <functional>
  2. #include <time.h>
  3. #include <cmath>
  4. #include <limits>
  5. #include "EvenVCO.h"
  6. //#include "EvenVCO_orig.h"
  7. #include "AudioMath.h"
  8. #include "BiquadParams.h"
  9. #include "BiquadFilter.h"
  10. #include "BiquadState.h"
  11. #include "ColoredNoise.h"
  12. #include "FrequencyShifter.h"
  13. #include "HilbertFilterDesigner.h"
  14. #include "LookupTableFactory.h"
  15. #include "TestComposite.h"
  16. #include "Tremolo.h"
  17. #include "VocalAnimator.h"
  18. #include "VocalFilter.h"
  19. #include "LFN.h"
  20. #include "GMR.h"
  21. #include "CHB.h"
  22. #include "FunVCOComposite.h"
  23. #include "EV3.h"
  24. #include "daveguide.h"
  25. #include "Shaper.h"
  26. #include "Super.h"
  27. using Shifter = FrequencyShifter<TestComposite>;
  28. using Animator = VocalAnimator<TestComposite>;
  29. using VocFilter = VocalFilter<TestComposite>;
  30. using Colors = ColoredNoise<TestComposite>;
  31. using Trem = Tremolo<TestComposite>;
  32. #include "MeasureTime.h"
  33. #if defined(_MSC_VER) || defined(ARCH_WIN)
  34. double SqTime::frequency = 0;
  35. #endif
  36. // There are many tests that are disabled with #if 0.
  37. // In most cases they still work, but don't need to be run regularly
  38. #if 0
  39. static void test1()
  40. {
  41. double d = .1;
  42. srand(57);
  43. const double scale = 1.0 / RAND_MAX;
  44. MeasureTime<float>::run("test1 sin", []() {
  45. float x = std::sin(TestBuffers<float>::get());
  46. return x;
  47. }, 1);
  48. MeasureTime<double>::run("test1 sin double", []() {
  49. float x = std::sin(TestBuffers<float>::get());
  50. return x;
  51. }, 1);
  52. MeasureTime<float>::run("test1 sinx2 float", []() {
  53. float x = std::sin(TestBuffers<float>::get());
  54. x = std::sin(x);
  55. return x;
  56. }, 1);
  57. MeasureTime<float>::run("mult float-10", []() {
  58. float x = TestBuffers<float>::get();
  59. float y = TestBuffers<float>::get();
  60. return x * y;
  61. }, 10);
  62. MeasureTime<double>::run("mult dbl", []() {
  63. double x = TestBuffers<double>::get();
  64. double y = TestBuffers<double>::get();
  65. return x * y;
  66. }, 1);
  67. MeasureTime<float>::run("div float", []() {
  68. float x = TestBuffers<float>::get();
  69. float y = TestBuffers<float>::get();
  70. return x / y;
  71. }, 1);
  72. MeasureTime<double>::run("div dbl", []() {
  73. double x = TestBuffers<double>::get();
  74. double y = TestBuffers<double>::get();
  75. return x / y;
  76. }, 1);
  77. MeasureTime<float>::run("test1 (do nothing)", [&d, scale]() {
  78. return TestBuffers<float>::get();
  79. }, 1);
  80. MeasureTime<float>::run("test1 pow2 float", []() {
  81. float x = std::pow(2, TestBuffers<float>::get());
  82. return x;
  83. }, 1);
  84. MeasureTime<float>::run("test1 pow rnd float", []() {
  85. float x = std::pow(TestBuffers<float>::get(), TestBuffers<float>::get());
  86. return x;
  87. }, 1);
  88. MeasureTime<float>::run("test1 exp float", []() {
  89. float x = std::exp(TestBuffers<float>::get());
  90. return x;
  91. }, 1);
  92. }
  93. #endif
  94. double overheadInOut = 0;
  95. double overheadOutOnly = 0;
  96. static void setup()
  97. {
  98. #ifdef _DEBUG
  99. // assert(false); // don't run this in debug
  100. #endif
  101. double d = .1;
  102. const double scale = 1.0 / RAND_MAX;
  103. overheadInOut = MeasureTime<float>::run(0.0, "test1 (do nothing i/o)", [&d, scale]() {
  104. return TestBuffers<float>::get();
  105. }, 1);
  106. overheadOutOnly = MeasureTime<float>::run(0.0, "test1 (do nothing oo)", [&d, scale]() {
  107. return 0.0f;
  108. }, 1);
  109. }
  110. template <typename T>
  111. static void testHilbert()
  112. {
  113. BiquadParams<T, 3> paramsSin;
  114. BiquadParams<T, 3> paramsCos;
  115. BiquadState<T, 3> state;
  116. HilbertFilterDesigner<T>::design(44100, paramsSin, paramsCos);
  117. MeasureTime<T>::run("hilbert", [&state, &paramsSin]() {
  118. T d = BiquadFilter<T>::run(TestBuffers<T>::get(), state, paramsSin);
  119. return d;
  120. }, 1);
  121. }
  122. #if 0
  123. static void testExpRange()
  124. {
  125. using T = float;
  126. LookupTableParams<T> table;
  127. LookupTableFactory<T>::makeExp2(table);
  128. MeasureTime<T>::run("exp lookup", [&table]() {
  129. T d = LookupTable<T>::lookup(table, TestBuffers<T>::get());
  130. return d;
  131. }, 1);
  132. }
  133. #endif
  134. static void testShifter()
  135. {
  136. Shifter fs;
  137. fs.setSampleRate(44100);
  138. fs.init();
  139. fs.inputs[Shifter::AUDIO_INPUT].value = 0;
  140. assert(overheadInOut >= 0);
  141. MeasureTime<float>::run(overheadInOut, "shifter", [&fs]() {
  142. fs.inputs[Shifter::AUDIO_INPUT].value = TestBuffers<float>::get();
  143. fs.step();
  144. return fs.outputs[Shifter::SIN_OUTPUT].value;
  145. }, 1);
  146. }
  147. static void testAnimator()
  148. {
  149. Animator an;
  150. an.setSampleRate(44100);
  151. an.init();
  152. an.inputs[Shifter::AUDIO_INPUT].value = 0;
  153. MeasureTime<float>::run(overheadInOut, "animator", [&an]() {
  154. an.inputs[Shifter::AUDIO_INPUT].value = TestBuffers<float>::get();
  155. an.step();
  156. return an.outputs[Shifter::SIN_OUTPUT].value;
  157. }, 1);
  158. }
  159. static void testVocalFilter()
  160. {
  161. VocFilter an;
  162. an.setSampleRate(44100);
  163. an.init();
  164. an.inputs[Shifter::AUDIO_INPUT].value = 0;
  165. MeasureTime<float>::run(overheadInOut, "vocal filter", [&an]() {
  166. an.inputs[Shifter::AUDIO_INPUT].value = TestBuffers<float>::get();
  167. an.step();
  168. return an.outputs[Shifter::SIN_OUTPUT].value;
  169. }, 1);
  170. }
  171. static void testColors()
  172. {
  173. Colors co;
  174. co.setSampleRate(44100);
  175. co.init();
  176. MeasureTime<float>::run(overheadInOut, "colors", [&co]() {
  177. co.step();
  178. return co.outputs[Colors::AUDIO_OUTPUT].value;
  179. }, 1);
  180. }
  181. static void testTremolo()
  182. {
  183. Trem tr;
  184. tr.setSampleRate(44100);
  185. tr.init();
  186. MeasureTime<float>::run(overheadInOut, "trem", [&tr]() {
  187. tr.inputs[Trem::AUDIO_INPUT].value = TestBuffers<float>::get();
  188. tr.step();
  189. return tr.outputs[Trem::AUDIO_OUTPUT].value;
  190. }, 1);
  191. }
  192. static void testLFN()
  193. {
  194. LFN<TestComposite> lfn;
  195. lfn.setSampleTime(1.0f / 44100.f);
  196. lfn.init();
  197. MeasureTime<float>::run(overheadOutOnly, "lfn", [&lfn]() {
  198. lfn.step();
  199. return lfn.outputs[LFN<TestComposite>::OUTPUT].value;
  200. }, 1);
  201. }
  202. #if 0
  203. static void testEvenOrig()
  204. {
  205. EvenVCO_orig<TestComposite> lfn;
  206. lfn.outputs[EvenVCO_orig<TestComposite>::EVEN_OUTPUT].active = true;
  207. lfn.outputs[EvenVCO_orig<TestComposite>::SINE_OUTPUT].active = true;
  208. lfn.outputs[EvenVCO_orig<TestComposite>::TRI_OUTPUT].active = true;
  209. lfn.outputs[EvenVCO_orig<TestComposite>::SQUARE_OUTPUT].active = true;
  210. lfn.outputs[EvenVCO_orig<TestComposite>::SAW_OUTPUT].active = true;
  211. for (int i = 0; i < 100; ++i) lfn.step();
  212. MeasureTime<float>::run(overheadOutOnly, "Even orig", [&lfn]() {
  213. lfn.inputs[EvenVCO_orig<TestComposite>::PITCH1_INPUT].value = TestBuffers<float>::get();
  214. lfn.step();
  215. return lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].value;
  216. }, 1);
  217. }
  218. #endif
  219. static void testEven()
  220. {
  221. EvenVCO<TestComposite> lfn;
  222. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = true;
  223. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = true;
  224. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = true;
  225. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = true;
  226. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = true;
  227. MeasureTime<float>::run(overheadOutOnly, "Even, all outs", [&lfn]() {
  228. lfn.step();
  229. return lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].value;
  230. }, 1);
  231. }
  232. static void testEvenEven()
  233. {
  234. EvenVCO<TestComposite> lfn;
  235. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = true;
  236. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = false;
  237. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = false;
  238. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = false;
  239. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = false;
  240. MeasureTime<float>::run(overheadOutOnly, "Even, even only", [&lfn]() {
  241. lfn.step();
  242. return lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].value;
  243. }, 1);
  244. }
  245. static void testEvenSin()
  246. {
  247. EvenVCO<TestComposite> lfn;
  248. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = false;
  249. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = true;
  250. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = false;
  251. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = false;
  252. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = false;
  253. MeasureTime<float>::run(overheadOutOnly, "Even, sin only", [&lfn]() {
  254. lfn.step();
  255. return lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].value;
  256. }, 1);
  257. }
  258. static void testEvenSaw()
  259. {
  260. EvenVCO<TestComposite> lfn;
  261. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = false;
  262. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = false;
  263. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = false;
  264. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = false;
  265. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = true;
  266. for (int i = 0; i < 100; ++i) lfn.step();
  267. MeasureTime<float>::run(overheadOutOnly, "Even, saw only", [&lfn]() {
  268. lfn.inputs[EvenVCO<TestComposite>::PITCH1_INPUT].value = TestBuffers<float>::get();
  269. lfn.step();
  270. return lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].value;
  271. }, 1);
  272. }
  273. static void testEvenTri()
  274. {
  275. EvenVCO<TestComposite> lfn;
  276. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = false;
  277. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = false;
  278. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = true;
  279. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = false;
  280. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = false;
  281. MeasureTime<float>::run(overheadOutOnly, "Even, tri only", [&lfn]() {
  282. lfn.step();
  283. return lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].value;
  284. }, 1);
  285. }
  286. static void testEvenSq()
  287. {
  288. EvenVCO<TestComposite> lfn;
  289. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = false;
  290. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = false;
  291. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = false;
  292. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = true;
  293. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = false;
  294. MeasureTime<float>::run(overheadOutOnly, "Even, Sq only", [&lfn]() {
  295. lfn.step();
  296. return lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].value;
  297. }, 1);
  298. }
  299. static void testEvenSqSaw()
  300. {
  301. EvenVCO<TestComposite> lfn;
  302. lfn.outputs[EvenVCO<TestComposite>::EVEN_OUTPUT].active = false;
  303. lfn.outputs[EvenVCO<TestComposite>::SINE_OUTPUT].active = false;
  304. lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].active = false;
  305. lfn.outputs[EvenVCO<TestComposite>::SQUARE_OUTPUT].active = true;
  306. lfn.outputs[EvenVCO<TestComposite>::SAW_OUTPUT].active = true;
  307. MeasureTime<float>::run(overheadOutOnly, "Even, Sq Saw", [&lfn]() {
  308. lfn.step();
  309. return lfn.outputs[EvenVCO<TestComposite>::TRI_OUTPUT].value;
  310. }, 1);
  311. }
  312. static void testFun()
  313. {
  314. FunVCOComposite<TestComposite> lfn;
  315. for (int i = 0; i < lfn.NUM_OUTPUTS; ++i) {
  316. lfn.outputs[i].active = true;
  317. }
  318. lfn.setSampleRate(44100.f);
  319. const bool isAnalog = false;
  320. lfn.params[FunVCOComposite<TestComposite>::MODE_PARAM].value = isAnalog ? 1.0f : 0.f;
  321. MeasureTime<float>::run(overheadOutOnly, "Fun all on, digital", [&lfn]() {
  322. lfn.step();
  323. return lfn.outputs[FunVCOComposite<TestComposite>::TRI_OUTPUT].value +
  324. lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].value +
  325. lfn.outputs[FunVCOComposite<TestComposite>::SIN_OUTPUT].value +
  326. lfn.outputs[FunVCOComposite<TestComposite>::SQR_OUTPUT].value;
  327. }, 1);
  328. }
  329. static void testFunNone()
  330. {
  331. FunVCOComposite<TestComposite> lfn;
  332. for (int i = 0; i < lfn.NUM_OUTPUTS; ++i) {
  333. lfn.outputs[i].active = false;
  334. }
  335. lfn.setSampleRate(44100.f);
  336. MeasureTime<float>::run(overheadOutOnly, "Fun all off", [&lfn]() {
  337. lfn.step();
  338. return lfn.outputs[FunVCOComposite<TestComposite>::TRI_OUTPUT].value;
  339. }, 1);
  340. }
  341. static void testFunSaw(bool isAnalog)
  342. {
  343. FunVCOComposite<TestComposite> lfn;
  344. lfn.outputs[FunVCOComposite<TestComposite>::SIN_OUTPUT].active = false;
  345. lfn.outputs[FunVCOComposite<TestComposite>::TRI_OUTPUT].active = false;
  346. lfn.outputs[FunVCOComposite<TestComposite>::SQR_OUTPUT].active = false;
  347. lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].active = true;
  348. // oscillator.analog = TBase::params[MODE_PARAM].value > 0.0f;
  349. lfn.params[FunVCOComposite<TestComposite>::MODE_PARAM].value = isAnalog ? 1.0f : 0.f;
  350. lfn.setSampleRate(44100.f);
  351. std::string title = isAnalog ? "Fun Saw Analog" : "Fun Saw Digital";
  352. MeasureTime<float>::run(overheadOutOnly, title.c_str(), [&lfn]() {
  353. lfn.step();
  354. return lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].value;
  355. }, 1);
  356. }
  357. static void testFunSin(bool isAnalog)
  358. {
  359. FunVCOComposite<TestComposite> lfn;
  360. lfn.outputs[FunVCOComposite<TestComposite>::SIN_OUTPUT].active = true;
  361. lfn.outputs[FunVCOComposite<TestComposite>::TRI_OUTPUT].active = false;
  362. lfn.outputs[FunVCOComposite<TestComposite>::SQR_OUTPUT].active = false;
  363. lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].active = false;
  364. lfn.params[FunVCOComposite<TestComposite>::MODE_PARAM].value = isAnalog ? 1.0f : 0.f;
  365. lfn.setSampleRate(44100.f);
  366. std::string title = isAnalog ? "Fun Sin Analog" : "Fun Sin Digital";
  367. MeasureTime<float>::run(overheadOutOnly, title.c_str(), [&lfn]() {
  368. lfn.step();
  369. return lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].value;
  370. }, 1);
  371. }
  372. static void testFunSq()
  373. {
  374. FunVCOComposite<TestComposite> lfn;
  375. lfn.outputs[FunVCOComposite<TestComposite>::SIN_OUTPUT].active = false;
  376. lfn.outputs[FunVCOComposite<TestComposite>::TRI_OUTPUT].active = false;
  377. lfn.outputs[FunVCOComposite<TestComposite>::SQR_OUTPUT].active = true;
  378. lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].active = false;
  379. lfn.setSampleRate(44100.f);
  380. MeasureTime<float>::run(overheadOutOnly, "Fun sq", [&lfn]() {
  381. lfn.step();
  382. return lfn.outputs[FunVCOComposite<TestComposite>::SAW_OUTPUT].value;
  383. }, 1);
  384. }
  385. static void testCHB(bool econ)
  386. {
  387. CHB<TestComposite> chb;
  388. // chb.init();
  389. chb.setEconomy(econ);
  390. std::string name = "chb ";
  391. name += econ ? "econ" : "full";
  392. MeasureTime<float>::run(overheadOutOnly, name.c_str(), [&chb]() {
  393. chb.step();
  394. return chb.outputs[CHB<TestComposite>::MIX_OUTPUT].value;
  395. }, 1);
  396. }
  397. static void testCHBdef()
  398. {
  399. CHB<TestComposite> chb;
  400. std::string name = "chbdef ";
  401. MeasureTime<float>::run(overheadOutOnly, name.c_str(), [&chb]() {
  402. chb.step();
  403. return chb.outputs[CHB<TestComposite>::MIX_OUTPUT].value;
  404. }, 1);
  405. }
  406. static void testEV3()
  407. {
  408. EV3<TestComposite> ev3;
  409. // chb.init();
  410. MeasureTime<float>::run(overheadOutOnly, "ev3", [&ev3]() {
  411. ev3.step();
  412. return ev3.outputs[EV3<TestComposite>::MIX_OUTPUT].value;
  413. }, 1);
  414. }
  415. static void testGMR()
  416. {
  417. GMR<TestComposite> gmr;
  418. gmr.setSampleRate(44100);
  419. gmr.init();
  420. MeasureTime<float>::run(overheadOutOnly, "gmr", [&gmr]() {
  421. gmr.step();
  422. return gmr.outputs[GMR<TestComposite>::TRIGGER_OUTPUT].value;
  423. }, 1);
  424. }
  425. static void testDG()
  426. {
  427. Daveguide<TestComposite> gmr;
  428. // gmr.setSampleRate(44100);
  429. // gmr.init();
  430. MeasureTime<float>::run(overheadOutOnly, "dg", [&gmr]() {
  431. gmr.step();
  432. return gmr.outputs[GMR<TestComposite>::TRIGGER_OUTPUT].value;
  433. }, 1);
  434. }
  435. // 95
  436. // down to 67 for just the oversampler.
  437. static void testShaper1a()
  438. {
  439. Shaper<TestComposite> gmr;
  440. // gmr.setSampleRate(44100);
  441. // gmr.init();
  442. gmr.params[Shaper<TestComposite>::PARAM_OVERSAMPLE].value = 0;
  443. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::FullWave;
  444. MeasureTime<float>::run(overheadOutOnly, "shaper fw 16X", [&gmr]() {
  445. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  446. gmr.step();
  447. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  448. }, 1);
  449. }
  450. static void testShaper1b()
  451. {
  452. Shaper<TestComposite> gmr;
  453. // gmr.setSampleRate(44100);
  454. // gmr.init();
  455. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::FullWave;
  456. gmr.params[Shaper<TestComposite>::PARAM_OVERSAMPLE].value = 1;
  457. MeasureTime<float>::run(overheadOutOnly, "shaper fw 4X", [&gmr]() {
  458. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  459. gmr.step();
  460. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  461. }, 1);
  462. }
  463. static void testSuper()
  464. {
  465. Super<TestComposite> gmr;
  466. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::FullWave;
  467. gmr.params[Shaper<TestComposite>::PARAM_OVERSAMPLE].value = 2;
  468. MeasureTime<float>::run(overheadOutOnly, "super", [&gmr]() {
  469. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  470. gmr.step();
  471. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  472. }, 1);
  473. }
  474. static void testShaper1c()
  475. {
  476. Shaper<TestComposite> gmr;
  477. // gmr.setSampleRate(44100);
  478. // gmr.init();
  479. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::FullWave;
  480. gmr.params[Shaper<TestComposite>::PARAM_OVERSAMPLE].value = 2;
  481. MeasureTime<float>::run(overheadOutOnly, "shaper fw 1X", [&gmr]() {
  482. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  483. gmr.step();
  484. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  485. }, 1);
  486. }
  487. // 284
  488. static void testShaper2()
  489. {
  490. Shaper<TestComposite> gmr;
  491. // gmr.setSampleRate(44100);
  492. // gmr.init();
  493. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::Crush;
  494. MeasureTime<float>::run(overheadOutOnly, "shaper crush", [&gmr]() {
  495. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  496. gmr.step();
  497. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  498. }, 1);
  499. }
  500. // 143
  501. static void testShaper3()
  502. {
  503. Shaper<TestComposite> gmr;
  504. // gmr.setSampleRate(44100);
  505. // gmr.init();
  506. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::AsymSpline;
  507. MeasureTime<float>::run(overheadOutOnly, "shaper asy", [&gmr]() {
  508. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  509. gmr.step();
  510. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  511. }, 1);
  512. }
  513. static void testShaper4()
  514. {
  515. Shaper<TestComposite> gmr;
  516. // gmr.setSampleRate(44100);
  517. // gmr.init();
  518. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::Fold;
  519. MeasureTime<float>::run(overheadOutOnly, "folder", [&gmr]() {
  520. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  521. gmr.step();
  522. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  523. }, 1);
  524. }
  525. static void testShaper5()
  526. {
  527. Shaper<TestComposite> gmr;
  528. // gmr.setSampleRate(44100);
  529. // gmr.init();
  530. gmr.params[Shaper<TestComposite>::PARAM_SHAPE].value = (float) Shaper<TestComposite>::Shapes::Fold2;
  531. MeasureTime<float>::run(overheadOutOnly, "folder II", [&gmr]() {
  532. gmr.inputs[Shaper<TestComposite>::INPUT_AUDIO].value = TestBuffers<float>::get();
  533. gmr.step();
  534. return gmr.outputs[Shaper<TestComposite>::OUTPUT_AUDIO].value;
  535. }, 1);
  536. }
  537. #if 0
  538. static void testAttenuverters()
  539. {
  540. auto scaler = AudioMath::makeLinearScaler<float>(-2, 2);
  541. MeasureTime<float>::run("linear scaler", [&scaler]() {
  542. float cv = TestBuffers<float>::get();
  543. float knob = TestBuffers<float>::get();
  544. float trim = TestBuffers<float>::get();
  545. return scaler(cv, knob, trim);
  546. }, 1);
  547. LookupTableParams<float> lookup;
  548. LookupTableFactory<float>::makeBipolarAudioTaper(lookup);
  549. MeasureTime<float>::run("bipolar lookup", [&lookup]() {
  550. float x = TestBuffers<float>::get();
  551. return LookupTable<float>::lookup(lookup, x);
  552. }, 1);
  553. // auto refFuncPos = AudioMath::makeFunc_AudioTaper(LookupTableFactory<T>::audioTaperKnee());
  554. {
  555. auto bipolarScaler = [&lookup, &scaler](float cv, float knob, float trim) {
  556. float scaledTrim = LookupTable<float>::lookup(lookup, cv);
  557. return scaler(cv, knob, scaledTrim);
  558. };
  559. MeasureTime<float>::run("bipolar scaler", [&bipolarScaler]() {
  560. float cv = TestBuffers<float>::get();
  561. float knob = TestBuffers<float>::get();
  562. float trim = TestBuffers<float>::get();
  563. return bipolarScaler(cv, knob, trim);
  564. }, 1);
  565. }
  566. }
  567. #endif
  568. #if 0
  569. static void testNoise(bool useDefault)
  570. {
  571. std::default_random_engine defaultGenerator{99};
  572. std::random_device rd{};
  573. std::mt19937 gen{rd()};
  574. std::normal_distribution<double> distribution{0, 1.0};
  575. std::string title = useDefault ? "default random" : "fancy random";
  576. MeasureTime<float>::run(overheadOutOnly, title.c_str(), [useDefault, &distribution, &defaultGenerator, &gen]() {
  577. if (useDefault) return distribution(defaultGenerator);
  578. else return distribution(gen);
  579. }, 1);
  580. }
  581. static uint64_t xoroshiro128plus_state[2] = {};
  582. static uint64_t rotl(const uint64_t x, int k)
  583. {
  584. return (x << k) | (x >> (64 - k));
  585. }
  586. static uint64_t xoroshiro128plus_next(void)
  587. {
  588. const uint64_t s0 = xoroshiro128plus_state[0];
  589. uint64_t s1 = xoroshiro128plus_state[1];
  590. const uint64_t result = s0 + s1;
  591. s1 ^= s0;
  592. xoroshiro128plus_state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
  593. xoroshiro128plus_state[1] = rotl(s1, 36); // c
  594. return result;
  595. }
  596. float randomUniformX()
  597. {
  598. // 24 bits of granularity is the best that can be done with floats while ensuring that the return value lies in [0.0, 1.0).
  599. return (xoroshiro128plus_next() >> (64 - 24)) / powf(2, 24);
  600. }
  601. float randomNormalX()
  602. {
  603. // Box-Muller transform
  604. float radius = sqrtf(-2.f * logf(1.f - randomUniformX()));
  605. float theta = 2.f * M_PI * randomUniformX();
  606. return radius * sinf(theta);
  607. // // Central Limit Theorem
  608. // const int n = 8;
  609. // float sum = 0.0;
  610. // for (int i = 0; i < n; i++) {
  611. // sum += randomUniform();
  612. // }
  613. // return (sum - n / 2.f) / sqrtf(n / 12.f);
  614. }
  615. static void testNormal()
  616. {
  617. MeasureTime<float>::run(overheadOutOnly, "normal", []() {
  618. return randomNormalX();
  619. }, 1);
  620. }
  621. #endif
  622. void perfTest()
  623. {
  624. printf("starting perf test\n");
  625. fflush(stdout);
  626. setup();
  627. #if 0
  628. testAttenuverters();
  629. testExpRange();
  630. #endif
  631. #if 0
  632. testNoise(false);
  633. testNoise(true);
  634. testNormal();
  635. #endif
  636. testSuper();
  637. testShaper1a();
  638. testShaper1b();
  639. testShaper1c();
  640. testShaper2();
  641. testShaper3();
  642. testShaper4();
  643. testShaper5();
  644. testEV3();
  645. testCHBdef();
  646. testFunSaw(true);
  647. #if 0
  648. testFunSaw(false);
  649. testFunSin(true);
  650. testFunSin(false);
  651. testFunSq();
  652. testFun();
  653. testFunNone();
  654. #endif
  655. // testEvenOrig();
  656. testEvenSaw();
  657. #if 0
  658. testEven();
  659. testEvenEven();
  660. testEvenSin();
  661. testEvenSaw();
  662. testEvenTri();
  663. testEvenSq();
  664. testEvenSqSaw();
  665. #endif
  666. #if 0
  667. testVocalFilter();
  668. testAnimator();
  669. testShifter();
  670. testColors();
  671. testTremolo();
  672. testLFN();
  673. testGMR();
  674. #endif
  675. // test1();
  676. #if 0
  677. testHilbert<float>();
  678. testHilbert<double>();
  679. #endif
  680. }