]> git.sesse.net Git - nageru/blob - benchmark_audio_mixer.cpp
Various Makefile tweaks (mostly related to cleaning moc files).
[nageru] / benchmark_audio_mixer.cpp
1 // Rather simplistic benchmark of AudioMixer. Sets up a simple mapping
2 // with the default settings, feeds some white noise to the inputs and
3 // runs a while. Useful for e.g. profiling.
4
5 #include <assert.h>
6 #include <bmusb/bmusb.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <algorithm>
10 #include <chrono>
11 #include <cmath>
12 #include <ratio>
13 #include <vector>
14
15 #include "audio_mixer.h"
16 #include "db.h"
17 #include "defs.h"
18 #include "input_mapping.h"
19 #include "resampling_queue.h"
20 #include "timebase.h"
21
22 #define NUM_BENCHMARK_CARDS 4
23 #define NUM_WARMUP_FRAMES 100
24 #define NUM_BENCHMARK_FRAMES 1000
25 #define NUM_TEST_FRAMES 10
26 #define NUM_CHANNELS 8
27 #define NUM_SAMPLES 1024
28
29 using namespace std;
30 using namespace std::chrono;
31
32 // 16-bit samples, white noise at full volume.
33 uint8_t samples16[(NUM_SAMPLES * NUM_CHANNELS + 1024) * sizeof(uint16_t)];
34
35 // 24-bit samples, white noise at low volume (-48 dB).
36 uint8_t samples24[(NUM_SAMPLES * NUM_CHANNELS + 1024) * 3];
37
38 static uint32_t seed = 1234;
39
40 // We use our own instead of rand() to get deterministic behavior.
41 // Quality doesn't really matter much.
42 uint32_t lcgrand()
43 {
44         seed = seed * 1103515245u + 12345u;
45         return seed;
46 }
47
48 void reset_lcgrand()
49 {
50         seed = 1234;
51 }
52
53 void callback(float level_lufs, float peak_db,
54               std::vector<AudioMixer::BusLevel> bus_levels,
55               float global_level_lufs, float range_low_lufs, float range_high_lufs,
56               float final_makeup_gain_db,
57               float correlation)
58 {
59         // Empty.
60 }
61
62 vector<float> process_frame(unsigned frame_num, AudioMixer *mixer)
63 {
64         // Feed the inputs.
65         for (unsigned card_index = 0; card_index < NUM_BENCHMARK_CARDS; ++card_index) {
66                 bmusb::AudioFormat audio_format;
67                 audio_format.bits_per_sample = card_index == 3 ? 24 : 16;
68                 audio_format.num_channels = NUM_CHANNELS;
69                 
70                 unsigned num_samples = NUM_SAMPLES + (lcgrand() % 9) - 5;
71                 bool ok = mixer->add_audio(DeviceSpec{InputSourceType::CAPTURE_CARD, card_index},
72                         card_index == 3 ? samples24 : samples16, num_samples, audio_format,
73                         NUM_SAMPLES * TIMEBASE / OUTPUT_FREQUENCY);
74                 assert(ok);
75         }
76
77         double pts = double(frame_num) * NUM_SAMPLES / OUTPUT_FREQUENCY;
78         return mixer->get_output(pts, NUM_SAMPLES, ResamplingQueue::ADJUST_RATE);
79 }
80
81 void init_mapping(AudioMixer *mixer)
82 {
83         InputMapping mapping;
84
85         InputMapping::Bus bus1;
86         bus1.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 0};
87         bus1.source_channel[0] = 0;
88         bus1.source_channel[1] = 1;
89         mapping.buses.push_back(bus1);
90
91         InputMapping::Bus bus2;
92         bus2.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 3};
93         bus2.source_channel[0] = 6;
94         bus2.source_channel[1] = 4;
95         mapping.buses.push_back(bus2);
96
97         mixer->set_input_mapping(mapping);
98 }
99
100 void do_test(const char *filename)
101 {
102         AudioMixer mixer(NUM_BENCHMARK_CARDS);
103         mixer.set_audio_level_callback(callback);
104         init_mapping(&mixer);
105
106         reset_lcgrand();
107
108         vector<float> output;
109         for (unsigned i = 0; i < NUM_TEST_FRAMES; ++i) {
110                 vector<float> frame_output = process_frame(i, &mixer);
111                 output.insert(output.end(), frame_output.begin(), frame_output.end());
112         }
113
114         FILE *fp = fopen(filename, "rb");
115         if (fp == nullptr) {
116                 fprintf(stderr, "%s not found, writing new reference.\n", filename);
117                 fp = fopen(filename, "wb");
118                 fwrite(&output[0], output.size() * sizeof(float), 1, fp);
119                 fclose(fp);
120                 return;
121         }
122
123         vector<float> ref;
124         ref.resize(output.size());
125         fread(&ref[0], output.size() * sizeof(float), 1, fp);
126         fclose(fp);
127
128         float max_err = 0.0f, sum_sq_err = 0.0f;
129         for (unsigned i = 0; i < output.size(); ++i) {
130                 float err = output[i] - ref[i];
131                 max_err = max(max_err, fabs(err));
132                 sum_sq_err += err * err;
133         }
134
135         printf("Largest error: %.6f (%+.1f dB)\n", max_err, to_db(max_err));
136         printf("RMS error:     %+.1f dB\n", to_db(sqrt(sum_sq_err) / output.size()));
137 }
138
139 void do_benchmark()
140 {
141         AudioMixer mixer(NUM_BENCHMARK_CARDS);
142         mixer.set_audio_level_callback(callback);
143         init_mapping(&mixer);
144
145         size_t out_samples = 0;
146
147         reset_lcgrand();
148
149         steady_clock::time_point start, end;
150         for (unsigned i = 0; i < NUM_WARMUP_FRAMES + NUM_BENCHMARK_FRAMES; ++i) {
151                 if (i == NUM_WARMUP_FRAMES) {
152                         start = steady_clock::now();
153                 }
154                 vector<float> output = process_frame(i, &mixer);
155                 if (i >= NUM_WARMUP_FRAMES) {
156                         out_samples += output.size();
157                 }
158         }
159         end = steady_clock::now();
160
161         double elapsed = duration<double>(end - start).count();
162         double simulated = double(out_samples) / (OUTPUT_FREQUENCY * 2);
163         printf("%ld samples produced in %.1f ms (%.1f%% CPU, %.1fx realtime).\n",
164                 out_samples, elapsed * 1e3, 100.0 * elapsed / simulated, simulated / elapsed);
165 }
166
167 int main(int argc, char **argv)
168 {
169         for (unsigned i = 0; i < NUM_SAMPLES * NUM_CHANNELS + 1024; ++i) {
170                 samples16[i * 2] = lcgrand() & 0xff;
171                 samples16[i * 2 + 1] = lcgrand() & 0xff;
172
173                 samples24[i * 3] = lcgrand() & 0xff;
174                 samples24[i * 3 + 1] = lcgrand() & 0xff;
175                 samples24[i * 3 + 2] = 0;
176         }
177
178         if (argc == 2) {
179                 do_test(argv[1]);
180         }
181         do_benchmark();
182 }
183