1 #include "delay_analyzer.h"
3 #include "audio_mixer.h"
4 #include "ui_delay_analyzer.h"
6 #include <bmusb/bmusb.h>
10 using namespace bmusb;
12 using namespace std::chrono;
13 using namespace std::placeholders;
15 DelayAnalyzer::DelayAnalyzer()
16 : ui(new Ui::DelayAnalyzer),
17 devices(global_audio_mixer->get_devices(AudioMixer::HOLD_NO_DEVICES))
20 connect(ui->grab_btn, &QPushButton::clicked, this, &DelayAnalyzer::grab_clicked);
22 connect(ui->card_combo_1, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
23 bind(&DelayAnalyzer::card_selected, this, ui->card_combo_1, _1));
24 connect(ui->card_combo_2, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
25 bind(&DelayAnalyzer::card_selected, this, ui->card_combo_2, _1));
26 connect(ui->channel_combo_1, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
27 bind(&DelayAnalyzer::channel_selected, this, ui->channel_combo_1));
28 connect(ui->channel_combo_2, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
29 bind(&DelayAnalyzer::channel_selected, this, ui->channel_combo_2));
31 for (const auto &spec_and_info : devices) {
32 QString label(QString::fromStdString(spec_and_info.second.display_name));
33 ui->card_combo_1->addItem(label + " ", qulonglong(DeviceSpec_to_key(spec_and_info.first)));
34 ui->card_combo_2->addItem(label + " ", qulonglong(DeviceSpec_to_key(spec_and_info.first)));
37 ui->peak_display_1->set_audio_clip(&clip1);
38 ui->peak_display_2->set_audio_clip(&clip2);
41 DelayAnalyzer::~DelayAnalyzer()
45 void DelayAnalyzer::grab_clicked()
50 ui->peak_display_1->audio_clip_updated();
51 ui->peak_display_2->audio_clip_updated();
54 void DelayAnalyzer::card_selected(QComboBox *card_combo, int selected_index)
56 assert(card_combo == ui->card_combo_1 || card_combo == ui->card_combo_2);
57 QComboBox *channel_combo = (card_combo == ui->card_combo_1 ? ui->channel_combo_1 : ui->channel_combo_2);
58 int current_channel = channel_combo->currentIndex();
60 channel_combo->clear();
62 uint64_t key = card_combo->itemData(selected_index).toULongLong();
63 auto device_it = devices.find(key_to_DeviceSpec(key));
64 assert(device_it != devices.end());
65 unsigned num_device_channels = device_it->second.num_channels;
66 for (unsigned source = 0; source < num_device_channels; ++source) {
68 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
69 channel_combo->addItem(QString(buf));
72 if (current_channel != -1 && current_channel < channel_combo->count()) {
73 channel_combo->setCurrentIndex(current_channel);
75 channel_combo->setCurrentIndex(0);
78 if (card_combo == ui->card_combo_1) {
80 ui->peak_display_1->audio_clip_updated();
83 ui->peak_display_2->audio_clip_updated();
87 void DelayAnalyzer::channel_selected(QComboBox *channel_combo)
89 if (channel_combo == ui->channel_combo_1) {
91 ui->peak_display_1->audio_clip_updated();
94 ui->peak_display_2->audio_clip_updated();
98 DeviceSpec DelayAnalyzer::get_selected_device(QComboBox *card_combo)
100 return key_to_DeviceSpec(card_combo->currentData().toULongLong());
103 void DelayAnalyzer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, steady_clock::time_point frame_time)
105 unique_ptr<float[]> tmp;
107 if (device_spec == get_selected_device(ui->card_combo_1)) {
108 tmp.reset(new float[num_samples]);
110 convert_audio_to_fp32(tmp.get(), /*out_channel=*/0, /*out_num_channels=*/1, data, ui->channel_combo_1->currentIndex(), audio_format, num_samples);
111 clip1.add_audio(tmp.get(), num_samples, audio_format.sample_rate, frame_time);
112 ui->peak_display_1->audio_clip_updated();
114 if (device_spec == get_selected_device(ui->card_combo_2)) {
115 if (tmp == nullptr) {
116 tmp.reset(new float[num_samples]);
119 convert_audio_to_fp32(tmp.get(), /*out_channel=*/0, /*out_num_channels=*/1, data, ui->channel_combo_2->currentIndex(), audio_format, num_samples);
120 clip2.add_audio(tmp.get(), num_samples, audio_format.sample_rate, frame_time);
121 ui->peak_display_2->audio_clip_updated();
124 if (clip1.get_length_seconds() >= 1.0 && clip2.get_length_seconds() >= 1.0) {