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.

160 lines
3.7KB

  1. #include <benchmark/benchmark.h>
  2. #include "dsp/oscillator.hpp"
  3. using namespace bogaudio::dsp;
  4. static void BM_Phasor(benchmark::State& state) {
  5. Phasor p(44100.0, 440.0);
  6. for (auto _ : state) {
  7. p.next();
  8. }
  9. }
  10. BENCHMARK(BM_Phasor);
  11. static void BM_SineOscillator(benchmark::State& state) {
  12. SineOscillator o(44100.0, 440.0);
  13. for (auto _ : state) {
  14. o.next();
  15. }
  16. }
  17. BENCHMARK(BM_SineOscillator);
  18. static void BM_SineTableOscillator(benchmark::State& state) {
  19. SineTableOscillator o(44100.0, 440.0);
  20. for (auto _ : state) {
  21. o.next();
  22. }
  23. }
  24. BENCHMARK(BM_SineTableOscillator);
  25. static void BM_SineOscillatorFM(benchmark::State& state) {
  26. const float baseHz = 440.0;
  27. SineOscillator m(44100.0, baseHz);
  28. SineOscillator c(44100.0, baseHz);
  29. for (auto _ : state) {
  30. c.setFrequency(baseHz + m.next());
  31. c.next();
  32. }
  33. }
  34. BENCHMARK(BM_SineOscillatorFM);
  35. static void BM_SineTableOscillatorPM(benchmark::State& state) {
  36. SineTableOscillator m(44100.0, 440.0);
  37. SineTableOscillator c(44100.0, 440.0);
  38. for (auto _ : state) {
  39. c.advancePhase();
  40. c.nextFromPhasor(c, Phasor::radiansToPhase(m.next()));
  41. }
  42. }
  43. BENCHMARK(BM_SineTableOscillatorPM);
  44. static void BM_SawOscillator(benchmark::State& state) {
  45. SawOscillator o(44100.0, 440.0);
  46. for (auto _ : state) {
  47. o.next();
  48. }
  49. }
  50. BENCHMARK(BM_SawOscillator);
  51. static void BM_SaturatingSawOscillator(benchmark::State& state) {
  52. SaturatingSawOscillator o(44100.0, 440.0);
  53. o.setSaturation(0.9);
  54. for (auto _ : state) {
  55. o.next();
  56. }
  57. }
  58. BENCHMARK(BM_SaturatingSawOscillator);
  59. static void BM_BandLimitedSawOscillator(benchmark::State& state) {
  60. BandLimitedSawOscillator o(44100.0, 440.0);
  61. o.setQuality(12);
  62. for (auto _ : state) {
  63. o.next();
  64. }
  65. }
  66. BENCHMARK(BM_BandLimitedSawOscillator);
  67. static void BM_SquareOscillator(benchmark::State& state) {
  68. SquareOscillator o(44100.0, 440.0);
  69. for (auto _ : state) {
  70. o.next();
  71. }
  72. }
  73. BENCHMARK(BM_SquareOscillator);
  74. static void BM_BandLimitedSquareOscillator(benchmark::State& state) {
  75. BandLimitedSquareOscillator o(44100.0, 440.0);
  76. o.setQuality(12);
  77. for (auto _ : state) {
  78. o.next();
  79. }
  80. }
  81. BENCHMARK(BM_BandLimitedSquareOscillator);
  82. static void BM_TriangleOscillator(benchmark::State& state) {
  83. TriangleOscillator o(44100.0, 440.0);
  84. for (auto _ : state) {
  85. o.next();
  86. }
  87. }
  88. BENCHMARK(BM_TriangleOscillator);
  89. static void BM_SampledTriangleOscillator(benchmark::State& state) {
  90. TriangleOscillator o(44100.0, 440.0);
  91. o.setSampleWidth(Phasor::maxSampleWidth / 2.0);
  92. for (auto _ : state) {
  93. o.next();
  94. }
  95. }
  96. BENCHMARK(BM_SampledTriangleOscillator);
  97. static void BM_SineBankOscillator100(benchmark::State& state) {
  98. SineBankOscillator o(44100.0, 100.0, 100);
  99. for (int i = 1, n = o.partialCount(); i <= n; ++i) {
  100. o.setPartial(i, i, 1.0 / (float)i);
  101. }
  102. for (auto _ : state) {
  103. o.next();
  104. }
  105. }
  106. BENCHMARK(BM_SineBankOscillator100);
  107. static void BM_SineBankOscillator500(benchmark::State& state) {
  108. SineBankOscillator o(44100.0, 500.0, 100);
  109. for (int i = 1, n = o.partialCount(); i <= n; ++i) {
  110. o.setPartial(i, i, 1.0 / (float)i);
  111. }
  112. for (auto _ : state) {
  113. o.next();
  114. }
  115. }
  116. BENCHMARK(BM_SineBankOscillator500);
  117. static void BM_SineBankOscillator5000(benchmark::State& state) {
  118. SineBankOscillator o(44100.0, 5000.0, 100);
  119. for (int i = 1, n = o.partialCount(); i <= n; ++i) {
  120. o.setPartial(i, i, 1.0 / (float)i);
  121. }
  122. for (auto _ : state) {
  123. o.next();
  124. }
  125. }
  126. BENCHMARK(BM_SineBankOscillator5000);
  127. static void BM_SineBankOscillator15000(benchmark::State& state) {
  128. SineBankOscillator o(44100.0, 15000.0, 100);
  129. for (int i = 1, n = o.partialCount(); i <= n; ++i) {
  130. o.setPartial(i, i, 1.0 / (float)i);
  131. }
  132. for (auto _ : state) {
  133. o.next();
  134. }
  135. }
  136. BENCHMARK(BM_SineBankOscillator15000);