]> git.sesse.net Git - nageru/blob - benchmark_audio_mixer.cpp
Add a benchmark for a simple audio chain; easier than profiling all of Nageru.
[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 uint8_t samples[(NUM_SAMPLES * NUM_CHANNELS + 1024) * sizeof(uint16_t)];
22
23 void callback(float level_lufs, float peak_db,
24               std::vector<AudioMixer::BusLevel> bus_levels,
25               float global_level_lufs, float range_low_lufs, float range_high_lufs,
26               float final_makeup_gain_db,
27               float correlation)
28 {
29         // Empty.
30 }
31
32 int main(void)
33 {
34         for (unsigned i = 0; i < NUM_SAMPLES * NUM_CHANNELS + 1024; ++i) {
35                 samples[i] = rand() & 0xff;
36         }
37         AudioMixer mixer(NUM_BENCHMARK_CARDS);
38         mixer.set_audio_level_callback(callback);
39
40         InputMapping mapping;
41
42         InputMapping::Bus bus1;
43         bus1.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 0};
44         bus1.source_channel[0] = 0;
45         bus1.source_channel[1] = 1;
46         mapping.buses.push_back(bus1);
47
48         InputMapping::Bus bus2;
49         bus2.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 3};
50         bus2.source_channel[0] = 6;
51         bus2.source_channel[1] = 4;
52         mapping.buses.push_back(bus2);
53
54         mixer.set_input_mapping(mapping);
55
56         size_t out_samples = 0;
57
58         steady_clock::time_point start, end;
59         for (unsigned i = 0; i < NUM_WARMUP_FRAMES + NUM_BENCHMARK_FRAMES; ++i) {
60                 if (i == NUM_WARMUP_FRAMES) {
61                         start = steady_clock::now();
62                 }
63                 // Feed the inputs.
64                 for (unsigned card_index = 0; card_index < NUM_BENCHMARK_CARDS; ++card_index) {
65                         bmusb::AudioFormat audio_format;
66                         audio_format.bits_per_sample = 16;
67                         audio_format.num_channels = NUM_CHANNELS;
68                         
69                         unsigned num_samples = NUM_SAMPLES + (rand() % 9) - 5;
70                         bool ok = mixer.add_audio(DeviceSpec{InputSourceType::CAPTURE_CARD, card_index}, samples, num_samples, audio_format, NUM_SAMPLES * TIMEBASE / OUTPUT_FREQUENCY);
71                         assert(ok);
72                 }
73
74                 double pts = double(i) * NUM_SAMPLES / OUTPUT_FREQUENCY;
75                 vector<float> output = mixer.get_output(pts, NUM_SAMPLES, ResamplingQueue::ADJUST_RATE);
76                 if (i >= NUM_WARMUP_FRAMES) {
77                         out_samples += output.size();
78                 }
79         }
80         end = steady_clock::now();
81
82         double elapsed = duration<double>(end - start).count();
83         double simulated = double(out_samples) / (OUTPUT_FREQUENCY * 2);
84         printf("%ld samples produced in %.1f ms (%.1f%% CPU, %.1fx realtime).\n",
85                 out_samples, elapsed * 1e3, 100.0 * elapsed / simulated, simulated / elapsed);
86 }