]> git.sesse.net Git - nageru/blob - benchmark_audio_mixer.cpp
Add some low-volume 24-bit data to the benchmark, for variety.
[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 <stdio.h>
6 #include <stdint.h>
7 #include <vector>
8 #include <chrono>
9 #include "audio_mixer.h"
10 #include "timebase.h"
11
12 #define NUM_BENCHMARK_CARDS 4
13 #define NUM_WARMUP_FRAMES 100
14 #define NUM_BENCHMARK_FRAMES 1000
15 #define NUM_CHANNELS 8
16 #define NUM_SAMPLES 1024
17
18 using namespace std;
19 using namespace std::chrono;
20
21 // 16-bit samples, white noise at full volume.
22 uint8_t samples16[(NUM_SAMPLES * NUM_CHANNELS + 1024) * sizeof(uint16_t)];
23
24 // 24-bit samples, white noise at low volume (-48 dB).
25 uint8_t samples24[(NUM_SAMPLES * NUM_CHANNELS + 1024) * 3];
26
27 void callback(float level_lufs, float peak_db,
28               std::vector<AudioMixer::BusLevel> bus_levels,
29               float global_level_lufs, float range_low_lufs, float range_high_lufs,
30               float final_makeup_gain_db,
31               float correlation)
32 {
33         // Empty.
34 }
35
36 int main(void)
37 {
38         for (unsigned i = 0; i < NUM_SAMPLES * NUM_CHANNELS + 1024; ++i) {
39                 samples16[i * 2] = rand() & 0xff;
40                 samples16[i * 2 + 1] = rand() & 0xff;
41
42                 samples24[i * 3] = rand() & 0xff;
43                 samples24[i * 3 + 1] = rand() & 0xff;
44                 samples24[i * 3 + 2] = 0;
45         }
46         AudioMixer mixer(NUM_BENCHMARK_CARDS);
47         mixer.set_audio_level_callback(callback);
48
49         InputMapping mapping;
50
51         InputMapping::Bus bus1;
52         bus1.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 0};
53         bus1.source_channel[0] = 0;
54         bus1.source_channel[1] = 1;
55         mapping.buses.push_back(bus1);
56
57         InputMapping::Bus bus2;
58         bus2.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 3};
59         bus2.source_channel[0] = 6;
60         bus2.source_channel[1] = 4;
61         mapping.buses.push_back(bus2);
62
63         mixer.set_input_mapping(mapping);
64
65         size_t out_samples = 0;
66
67         steady_clock::time_point start, end;
68         for (unsigned i = 0; i < NUM_WARMUP_FRAMES + NUM_BENCHMARK_FRAMES; ++i) {
69                 if (i == NUM_WARMUP_FRAMES) {
70                         start = steady_clock::now();
71                 }
72                 // Feed the inputs.
73                 for (unsigned card_index = 0; card_index < NUM_BENCHMARK_CARDS; ++card_index) {
74                         bmusb::AudioFormat audio_format;
75                         audio_format.bits_per_sample = card_index == 3 ? 24 : 16;
76                         audio_format.num_channels = NUM_CHANNELS;
77                         
78                         unsigned num_samples = NUM_SAMPLES + (rand() % 9) - 5;
79                         bool ok = mixer.add_audio(DeviceSpec{InputSourceType::CAPTURE_CARD, card_index},
80                                 card_index == 3 ? samples24 : samples16, num_samples, audio_format,
81                                 NUM_SAMPLES * TIMEBASE / OUTPUT_FREQUENCY);
82                         assert(ok);
83                 }
84
85                 double pts = double(i) * NUM_SAMPLES / OUTPUT_FREQUENCY;
86                 vector<float> output = mixer.get_output(pts, NUM_SAMPLES, ResamplingQueue::ADJUST_RATE);
87                 if (i >= NUM_WARMUP_FRAMES) {
88                         out_samples += output.size();
89                 }
90         }
91         end = steady_clock::now();
92
93         double elapsed = duration<double>(end - start).count();
94         double simulated = double(out_samples) / (OUTPUT_FREQUENCY * 2);
95         printf("%ld samples produced in %.1f ms (%.1f%% CPU, %.1fx realtime).\n",
96                 out_samples, elapsed * 1e3, 100.0 * elapsed / simulated, simulated / elapsed);
97 }