1 |
/* |
2 |
Shared includes and definitions for all LFO benchmarks |
3 |
(triang.cpp, saw.cpp, sine.cpp, square.cpp in this directory) |
4 |
|
5 |
Copyright (C) 2005 - 2019 Christian Schoenebeck <cuse@users.sf.net> |
6 |
*/ |
7 |
|
8 |
#ifndef LS_LFO_BENCHMARK_H |
9 |
#define LS_LFO_BENCHMARK_H |
10 |
|
11 |
#include <math.h> |
12 |
#include <time.h> |
13 |
#include <stdio.h> |
14 |
#include <vector> |
15 |
#include <string> |
16 |
#include <algorithm> |
17 |
#include <functional> |
18 |
|
19 |
#include "../src/engines/common/LFOBase.h" |
20 |
|
21 |
// whether we should not show any messages on the console |
22 |
#ifndef SILENT |
23 |
# define SILENT 0 |
24 |
#endif |
25 |
|
26 |
// set to 1 if you want to output the three calculated waves as RAW files |
27 |
// you can e.g. open it as RAW file in Rezound |
28 |
// (32 bit SP-FP PCM, mono, little endian, 44100kHz) |
29 |
#ifndef OUTPUT_AS_RAW_WAVE |
30 |
# define OUTPUT_AS_RAW_WAVE 0 |
31 |
#endif |
32 |
|
33 |
// how many sample points should we calculate in one sequence |
34 |
#ifndef STEPS |
35 |
# define STEPS 16384 |
36 |
#endif |
37 |
|
38 |
// how often should we repeat the benchmark loop of each solution |
39 |
#ifndef RUNS |
40 |
# define RUNS 6000 |
41 |
#endif |
42 |
|
43 |
// whether the wave should have positive and negative range (signed -> 1) or just positive (unsigned -> 0) |
44 |
#ifndef SIGNED |
45 |
# define SIGNED 1 |
46 |
#endif |
47 |
|
48 |
// maximum value of the LFO output (also depends on SIGNED above) |
49 |
#ifndef MAX |
50 |
# define MAX 1.0f |
51 |
#endif |
52 |
|
53 |
// pro forma |
54 |
#ifndef SAMPLING_RATE |
55 |
# define SAMPLING_RATE 44100.0f |
56 |
#endif |
57 |
|
58 |
// we use 32 bit single precision floating point as sample point format |
59 |
typedef float smpl_t; // (sample_t is already defined as int16_t by global_private.h) |
60 |
|
61 |
using namespace LinuxSampler; |
62 |
|
63 |
// output calculated values as RAW audio format (32 bit floating point, mono) file |
64 |
inline void output_as_raw_file(const char* filename, smpl_t* pOutputBuffer, int steps) { |
65 |
FILE* file = fopen(filename, "w"); |
66 |
if (file) { |
67 |
fwrite((void*) pOutputBuffer, sizeof(float), steps, file); |
68 |
fclose(file); |
69 |
} else { |
70 |
fprintf(stderr, "Could not open %s\n", filename); |
71 |
} |
72 |
} |
73 |
|
74 |
struct BenchRes { |
75 |
int algorithmID; ///< Numeric ID of the implementation been benchmarked. |
76 |
std::string algorithmName; ///< Human readable name of the algorithm (to be printed in the results summary). |
77 |
double timeMSecs; ///< How long the algorithm executed (in milliseconds). |
78 |
}; |
79 |
|
80 |
inline void sortResultsFirstToBeBest(std::vector<BenchRes>& results) { |
81 |
std::sort(results.begin(), results.end(), [](const BenchRes& a, const BenchRes& b) { |
82 |
return a.timeMSecs < b.timeMSecs; |
83 |
}); |
84 |
} |
85 |
|
86 |
inline void printResultSummary(const std::vector<BenchRes>& results) { |
87 |
#if ! SILENT |
88 |
printf("\n"); |
89 |
printf("RESULT\n"); |
90 |
printf("======\n\n"); |
91 |
for (int i = 0; i < results.size(); ++i) { |
92 |
const BenchRes& res = results[i]; |
93 |
printf("%d. %s solution: %.1fms%s\n", i+1, res.algorithmName.c_str(), res.timeMSecs, (i==0) ? " <-- [WINNER]" : ""); |
94 |
} |
95 |
printf("\n"); |
96 |
printf("[Exit Result (Winner's ID) = %d]\n\n", results[0].algorithmID); |
97 |
#endif |
98 |
} |
99 |
|
100 |
#endif // LS_LFO_BENCHMARK_H |