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.

884 lines
24KB

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