1 |
/* |
2 |
Saw wave generator benchmark |
3 |
|
4 |
This is a benchmark for comparison between two different integer math |
5 |
solutions. |
6 |
|
7 |
Copyright (C) 2019 Christian Schoenebeck <cuse@users.sf.net> |
8 |
*/ |
9 |
|
10 |
#include "lfobench.h" |
11 |
|
12 |
#include "../src/engines/common/LFOSawIntMath.h" |
13 |
#include "../src/engines/common/SawLFO.h" |
14 |
|
15 |
// return value of this benchmark |
16 |
// to indicate the best performing solution |
17 |
#define SAW_OLD_INT_MATH_SOLUTION 20 |
18 |
#define SAW_NEW_INT_MATH_SOLUTION 21 |
19 |
#define INVALID_RESULT -1 |
20 |
|
21 |
#if SIGNED |
22 |
LFOSawIntMath<LFO::range_signed>* pSawIntLFO = NULL; |
23 |
SawLFO<LFO::range_signed,true>* pSawLFOold = NULL; |
24 |
#else // unsigned |
25 |
LFOSawIntMath<LFO::range_unsigned>* pSawIntLFO = NULL; |
26 |
SawLFO<LFO::range_unsigned,true>* pSawLFOold = NULL; |
27 |
#endif |
28 |
|
29 |
double saw_new_int_math(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) { |
30 |
// pro forma |
31 |
pSawIntLFO->trigger(frequency, LFO::start_level_min, 0 /* max. internal depth */, 1200, false, (unsigned int) SAMPLING_RATE); |
32 |
//pSawIntLFO->setPhase(0); |
33 |
//pSawIntLFO->setFrequency(frequency*2, SAMPLING_RATE); |
34 |
|
35 |
clock_t stop_time; |
36 |
clock_t start_time = clock(); |
37 |
|
38 |
for (int run = 0; run < RUNS; run++) { |
39 |
pSawIntLFO->updateByMIDICtrlValue(127); // pro forma |
40 |
for (int i = 0; i < steps; ++i) { |
41 |
//pSawIntLFO->updateByMIDICtrlValue(float(i)/float(steps)*127.f); |
42 |
pDestinationBuffer[i] = pSawIntLFO->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load |
43 |
} |
44 |
} |
45 |
|
46 |
stop_time = clock(); |
47 |
double elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0); |
48 |
#if ! SILENT |
49 |
printf("New int math solution elapsed time: %.1f ms\n", elapsed_time); |
50 |
#endif |
51 |
|
52 |
return elapsed_time; |
53 |
} |
54 |
|
55 |
double saw_old_int_math(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) { |
56 |
// pro forma |
57 |
pSawLFOold->trigger(frequency, LFO::start_level_min, 0 /* max. internal depth */, 1200, false, (unsigned int) SAMPLING_RATE); |
58 |
//pSawLFOold->setPhase(0); |
59 |
//pSawLFOold->setFrequency(frequency*2, SAMPLING_RATE); |
60 |
|
61 |
clock_t stop_time; |
62 |
clock_t start_time = clock(); |
63 |
|
64 |
for (int run = 0; run < RUNS; run++) { |
65 |
pSawLFOold->updateByMIDICtrlValue(127); // pro forma |
66 |
for (int i = 0; i < steps; ++i) { |
67 |
//pSawLFOold->updateByMIDICtrlValue(float(i)/float(steps)*127.f); |
68 |
pDestinationBuffer[i] = pSawLFOold->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load |
69 |
} |
70 |
} |
71 |
|
72 |
stop_time = clock(); |
73 |
double elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0); |
74 |
#if ! SILENT |
75 |
printf("Old int math solution elapsed time: %.1f ms\n", elapsed_time); |
76 |
#endif |
77 |
|
78 |
return elapsed_time; |
79 |
} |
80 |
|
81 |
int main() { |
82 |
const int steps = STEPS; |
83 |
const int sinusoidFrequency = 100; // Hz |
84 |
|
85 |
#if ! SILENT |
86 |
printf("\n"); |
87 |
# if SIGNED |
88 |
printf("Signed saw wave benchmark\n"); |
89 |
# else |
90 |
printf("Unsigned saw wave benchmark\n"); |
91 |
# endif |
92 |
printf("----------------------------------\n"); |
93 |
printf("\n"); |
94 |
#endif |
95 |
|
96 |
#if SIGNED |
97 |
pSawIntLFO = new LFOSawIntMath<LFO::range_signed>(MAX); |
98 |
pSawLFOold = new SawLFO<LFO::range_signed,true>(MAX); |
99 |
#else // unsigned |
100 |
pSawIntLFO = new LFOSawIntMath<LFO::range_unsigned>(MAX); |
101 |
pSawLFOold = new SawLFO<LFO::range_unsigned,true>(MAX); |
102 |
#endif |
103 |
|
104 |
// output buffer for the calculated sinusoid wave |
105 |
smpl_t* pOutputBuffer = new smpl_t[steps]; |
106 |
// just an arbitrary amplitude envelope to simulate a bit higher memory bandwidth |
107 |
float* pAmplitude = new float[steps]; |
108 |
|
109 |
// pro forma - an arbitary amplitude envelope |
110 |
for (int i = 0; i < steps; ++i) |
111 |
pAmplitude[i] = (float) i / (float) steps; |
112 |
|
113 |
// going to store how long each solution took (in seconds) |
114 |
std::vector<BenchRes> results; |
115 |
|
116 |
|
117 |
results.push_back({ |
118 |
.algorithmID = SAW_NEW_INT_MATH_SOLUTION, |
119 |
.algorithmName = "New int math", |
120 |
.timeMSecs = saw_new_int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency) |
121 |
}); |
122 |
#if OUTPUT_AS_RAW_WAVE |
123 |
output_as_raw_file("saw_new_int_math.raw", pOutputBuffer, steps); |
124 |
#endif |
125 |
|
126 |
|
127 |
results.push_back({ |
128 |
.algorithmID = SAW_OLD_INT_MATH_SOLUTION, |
129 |
.algorithmName = "Old int math", |
130 |
.timeMSecs = saw_old_int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency) |
131 |
}); |
132 |
#if OUTPUT_AS_RAW_WAVE |
133 |
output_as_raw_file("saw_old_int_math.raw", pOutputBuffer, steps); |
134 |
#endif |
135 |
|
136 |
|
137 |
#if ! SILENT |
138 |
printf("\nOK, 2nd try\n\n"); |
139 |
#endif |
140 |
|
141 |
|
142 |
results[0].timeMSecs += saw_new_int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency); |
143 |
results[1].timeMSecs += saw_old_int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency); |
144 |
|
145 |
|
146 |
if (pAmplitude) delete[] pAmplitude; |
147 |
if (pOutputBuffer) delete[] pOutputBuffer; |
148 |
|
149 |
if (pSawIntLFO) delete pSawIntLFO; |
150 |
if (pSawLFOold) delete pSawLFOold; |
151 |
|
152 |
sortResultsFirstToBeBest(results); |
153 |
printResultSummary(results); |
154 |
|
155 |
return results[0].algorithmID; // return the winner's numeric algorithm ID |
156 |
} |