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.
9 #include "audio_mixer.h"
13 #define NUM_BENCHMARK_CARDS 4
14 #define NUM_WARMUP_FRAMES 100
15 #define NUM_BENCHMARK_FRAMES 1000
16 #define NUM_TEST_FRAMES 10
17 #define NUM_CHANNELS 8
18 #define NUM_SAMPLES 1024
21 using namespace std::chrono;
23 // 16-bit samples, white noise at full volume.
24 uint8_t samples16[(NUM_SAMPLES * NUM_CHANNELS + 1024) * sizeof(uint16_t)];
26 // 24-bit samples, white noise at low volume (-48 dB).
27 uint8_t samples24[(NUM_SAMPLES * NUM_CHANNELS + 1024) * 3];
29 static uint32_t seed = 1234;
31 // We use our own instead of rand() to get deterministic behavior.
32 // Quality doesn't really matter much.
35 seed = seed * 1103515245u + 12345u;
44 void callback(float level_lufs, float peak_db,
45 std::vector<AudioMixer::BusLevel> bus_levels,
46 float global_level_lufs, float range_low_lufs, float range_high_lufs,
47 float final_makeup_gain_db,
53 vector<float> process_frame(unsigned frame_num, AudioMixer *mixer)
56 for (unsigned card_index = 0; card_index < NUM_BENCHMARK_CARDS; ++card_index) {
57 bmusb::AudioFormat audio_format;
58 audio_format.bits_per_sample = card_index == 3 ? 24 : 16;
59 audio_format.num_channels = NUM_CHANNELS;
61 unsigned num_samples = NUM_SAMPLES + (lcgrand() % 9) - 5;
62 bool ok = mixer->add_audio(DeviceSpec{InputSourceType::CAPTURE_CARD, card_index},
63 card_index == 3 ? samples24 : samples16, num_samples, audio_format,
64 NUM_SAMPLES * TIMEBASE / OUTPUT_FREQUENCY);
68 double pts = double(frame_num) * NUM_SAMPLES / OUTPUT_FREQUENCY;
69 return mixer->get_output(pts, NUM_SAMPLES, ResamplingQueue::ADJUST_RATE);
72 void init_mapping(AudioMixer *mixer)
76 InputMapping::Bus bus1;
77 bus1.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 0};
78 bus1.source_channel[0] = 0;
79 bus1.source_channel[1] = 1;
80 mapping.buses.push_back(bus1);
82 InputMapping::Bus bus2;
83 bus2.device = DeviceSpec{InputSourceType::CAPTURE_CARD, 3};
84 bus2.source_channel[0] = 6;
85 bus2.source_channel[1] = 4;
86 mapping.buses.push_back(bus2);
88 mixer->set_input_mapping(mapping);
91 void do_test(const char *filename)
93 AudioMixer mixer(NUM_BENCHMARK_CARDS);
94 mixer.set_audio_level_callback(callback);
100 for (unsigned i = 0; i < NUM_TEST_FRAMES; ++i) {
101 vector<float> frame_output = process_frame(i, &mixer);
102 output.insert(output.end(), frame_output.begin(), frame_output.end());
105 FILE *fp = fopen(filename, "rb");
107 fprintf(stderr, "%s not found, writing new reference.\n", filename);
108 fp = fopen(filename, "wb");
109 fwrite(&output[0], output.size() * sizeof(float), 1, fp);
115 ref.resize(output.size());
116 fread(&ref[0], output.size() * sizeof(float), 1, fp);
119 float max_err = 0.0f, sum_sq_err = 0.0f;
120 for (unsigned i = 0; i < output.size(); ++i) {
121 float err = output[i] - ref[i];
122 max_err = max(max_err, fabs(err));
123 sum_sq_err += err * err;
126 printf("Largest error: %.6f (%+.1f dB)\n", max_err, to_db(max_err));
127 printf("RMS error: %+.1f dB\n", to_db(sqrt(sum_sq_err) / output.size()));
132 AudioMixer mixer(NUM_BENCHMARK_CARDS);
133 mixer.set_audio_level_callback(callback);
134 init_mapping(&mixer);
136 size_t out_samples = 0;
140 steady_clock::time_point start, end;
141 for (unsigned i = 0; i < NUM_WARMUP_FRAMES + NUM_BENCHMARK_FRAMES; ++i) {
142 if (i == NUM_WARMUP_FRAMES) {
143 start = steady_clock::now();
145 vector<float> output = process_frame(i, &mixer);
146 if (i >= NUM_WARMUP_FRAMES) {
147 out_samples += output.size();
150 end = steady_clock::now();
152 double elapsed = duration<double>(end - start).count();
153 double simulated = double(out_samples) / (OUTPUT_FREQUENCY * 2);
154 printf("%ld samples produced in %.1f ms (%.1f%% CPU, %.1fx realtime).\n",
155 out_samples, elapsed * 1e3, 100.0 * elapsed / simulated, simulated / elapsed);
158 int main(int argc, char **argv)
160 for (unsigned i = 0; i < NUM_SAMPLES * NUM_CHANNELS + 1024; ++i) {
161 samples16[i * 2] = lcgrand() & 0xff;
162 samples16[i * 2 + 1] = lcgrand() & 0xff;
164 samples24[i * 3] = lcgrand() & 0xff;
165 samples24[i * 3 + 1] = lcgrand() & 0xff;
166 samples24[i * 3 + 2] = 0;