]> git.sesse.net Git - nageru/blob - nageru/delay_analyzer.cpp
Begin working on a delay analyzer.
[nageru] / nageru / delay_analyzer.cpp
1 #include "delay_analyzer.h"
2
3 #include "audio_mixer.h"
4 #include "ui_delay_analyzer.h"
5
6 #include <bmusb/bmusb.h>
7
8 #include <memory>
9
10 using namespace bmusb;
11 using namespace std;
12 using namespace std::chrono;
13 using namespace std::placeholders;
14
15 DelayAnalyzer::DelayAnalyzer()
16         : ui(new Ui::DelayAnalyzer),
17           devices(global_audio_mixer->get_devices(AudioMixer::HOLD_NO_DEVICES))
18 {
19         ui->setupUi(this);
20         connect(ui->grab_btn, &QPushButton::clicked, this, &DelayAnalyzer::grab_clicked);
21
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));
30
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)));
35         }
36
37         ui->peak_display_1->set_audio_clip(&clip1);
38         ui->peak_display_2->set_audio_clip(&clip2);
39 }
40
41 DelayAnalyzer::~DelayAnalyzer()
42 {
43 }
44
45 void DelayAnalyzer::grab_clicked()
46 {
47         grabbing = true;
48         clip1.clear();
49         clip2.clear();
50         ui->peak_display_1->audio_clip_updated();
51         ui->peak_display_2->audio_clip_updated();
52 }
53
54 void DelayAnalyzer::card_selected(QComboBox *card_combo, int selected_index)
55 {
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();
59
60         channel_combo->clear();
61
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) {
67                 char buf[256];
68                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
69                 channel_combo->addItem(QString(buf));
70         }
71
72         if (current_channel != -1 && current_channel < channel_combo->count()) {
73                 channel_combo->setCurrentIndex(current_channel);
74         } else {
75                 channel_combo->setCurrentIndex(0);
76         }
77
78         if (card_combo == ui->card_combo_1) {
79                 clip1.clear();
80                 ui->peak_display_1->audio_clip_updated();
81         } else {
82                 clip2.clear();
83                 ui->peak_display_2->audio_clip_updated();
84         }
85 }
86
87 void DelayAnalyzer::channel_selected(QComboBox *channel_combo)
88 {
89         if (channel_combo == ui->channel_combo_1) {
90                 clip1.clear();
91                 ui->peak_display_1->audio_clip_updated();
92         } else {
93                 clip2.clear();
94                 ui->peak_display_2->audio_clip_updated();
95         }
96 }
97
98 DeviceSpec DelayAnalyzer::get_selected_device(QComboBox *card_combo)
99 {
100         return key_to_DeviceSpec(card_combo->currentData().toULongLong());
101 }
102
103 void DelayAnalyzer::add_audio(DeviceSpec device_spec, const uint8_t *data, unsigned num_samples, AudioFormat audio_format, steady_clock::time_point frame_time)
104 {
105         unique_ptr<float[]> tmp;
106
107         if (device_spec == get_selected_device(ui->card_combo_1)) {
108                 tmp.reset(new float[num_samples]);
109
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();
113         }
114         if (device_spec == get_selected_device(ui->card_combo_2)) {
115                 if (tmp == nullptr) {
116                         tmp.reset(new float[num_samples]);
117                 }
118
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();
122         }
123
124         if (clip1.get_length_seconds() >= 1.0 && clip2.get_length_seconds() >= 1.0) {
125                 grabbing = false;
126         }
127 }