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->reset_base();
51 ui->peak_display_2->reset_base();
52 ui->peak_display_1->audio_clip_updated();
53 ui->peak_display_2->audio_clip_updated();
56 void DelayAnalyzer::card_selected(QComboBox *card_combo, int selected_index)
58 assert(card_combo == ui->card_combo_1 || card_combo == ui->card_combo_2);
59 QComboBox *channel_combo = (card_combo == ui->card_combo_1 ? ui->channel_combo_1 : ui->channel_combo_2);
60 int current_channel = channel_combo->currentIndex();
62 channel_combo->clear();
64 uint64_t key = card_combo->itemData(selected_index).toULongLong();
65 auto device_it = devices.find(key_to_DeviceSpec(key));
66 assert(device_it != devices.end());
67 unsigned num_device_channels = device_it->second.num_channels;
68 for (unsigned source = 0; source < num_device_channels; ++source) {
70 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
71 channel_combo->addItem(QString(buf));
74 if (current_channel != -1 && current_channel < channel_combo->count()) {
75 channel_combo->setCurrentIndex(current_channel);
77 channel_combo->setCurrentIndex(0);
80 if (card_combo == ui->card_combo_1) {
82 ui->peak_display_1->audio_clip_updated();
85 ui->peak_display_2->audio_clip_updated();
89 void DelayAnalyzer::channel_selected(QComboBox *channel_combo)
91 if (channel_combo == ui->channel_combo_1) {
93 ui->peak_display_1->audio_clip_updated();
96 ui->peak_display_2->audio_clip_updated();
100 DeviceSpec DelayAnalyzer::get_selected_device(QComboBox *card_combo)
102 return key_to_DeviceSpec(card_combo->currentData().toULongLong());
105 void DelayAnalyzer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, steady_clock::time_point frame_time)
107 unique_ptr<float[]> tmp;
109 if (device_spec == get_selected_device(ui->card_combo_1)) {
110 tmp.reset(new float[num_samples]);
112 convert_audio_to_fp32(tmp.get(), /*out_channel=*/0, /*out_num_channels=*/1, data, ui->channel_combo_1->currentIndex(), audio_format, num_samples);
113 clip1.add_audio(tmp.get(), num_samples, audio_format.sample_rate, frame_time);
114 ui->peak_display_1->audio_clip_updated();
116 if (device_spec == get_selected_device(ui->card_combo_2)) {
117 if (tmp == nullptr) {
118 tmp.reset(new float[num_samples]);
121 convert_audio_to_fp32(tmp.get(), /*out_channel=*/0, /*out_num_channels=*/1, data, ui->channel_combo_2->currentIndex(), audio_format, num_samples);
122 clip2.add_audio(tmp.get(), num_samples, audio_format.sample_rate, frame_time);
123 ui->peak_display_2->audio_clip_updated();
126 if (clip1.empty() && clip2.empty()) {
127 // No interesting data yet.
131 steady_clock::time_point base;
132 if (!clip1.empty() && !clip2.empty()) {
133 base = max(clip1.get_first_sample(), clip2.get_first_sample());
134 } else if (!clip1.empty()) {
135 base = clip1.get_first_sample();
137 assert(!clip2.empty());
138 base = clip2.get_first_sample();
140 ui->peak_display_1->set_base(base);
141 ui->peak_display_2->set_base(base);
143 if (clip1.get_length_seconds_after_base(base) >= 1.0 &&
144 clip2.get_length_seconds_after_base(base) >= 1.0) {