-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebuggerWindow.cpp
More file actions
201 lines (157 loc) · 5.9 KB
/
Copy pathDebuggerWindow.cpp
File metadata and controls
201 lines (157 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// This file is part of the Open Audio Live System project, a live audio environment
// Copyright (c) 2026 - Mathis DELGADO
//
// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0
#include <iostream>
#include "DebuggerWindow.h"
#include "ui_DebuggerWindow.h"
DebuggerWindow::DebuggerWindow(QWidget *parent) : QWidget(parent), ui(new Ui::DebuggerWindow) {
ui->setupUi(this);
m_rendered_packets_count = 0;
m_is_recording = false;
init_scope_scene();
init_stats();
init_network();
}
DebuggerWindow::~DebuggerWindow() {
delete ui;
}
void DebuggerWindow::reset_min_max() {
m_packet_min_delta = 0xFFFFFFFFFFFFFFFF;
m_packet_max_delta = 0;
}
void DebuggerWindow::init_stats() {
reset_min_max();
m_last_stamp = 0;
m_first_pck_received = false;
}
void DebuggerWindow::init_network() {
const char dev_name[32] = "DEBUGGER";
m_pconf.iface = QApplication::arguments()[1].toStdString();
m_pconf.sample_rate = SamplingRate::SAMPLING_96K;
m_pconf.dev_type = DeviceType::MONITORING;
m_pconf.uid = ui->self_id->value();
m_pconf.topo.phy_in_count = 1;
m_pconf.topo.phy_out_count = 1;
m_pconf.topo.pipes_count = 1;
m_pconf.ck_type = CKTYPE_SLAVE;
memcpy(&m_pconf.dev_name, dev_name, 32);
m_nmapper = std::make_shared<NetworkMapper>(m_pconf);
std::cout << "Initializing on " << m_pconf.iface << std::endl;
if (m_nmapper->init_mapper(m_pconf.iface)) {
m_nmapper->launch_mapping_process();
} else {
QMessageBox::critical(this, "ERROR", "Failed to init network mapper");
quick_exit(-1);
}
m_audio_socket = std::make_shared<LowLatSocket>(m_pconf.uid, m_nmapper);
m_audio_socket->init_socket(m_pconf.iface, EthProtocol::ETH_PROTO_OANAUDIO);
std::thread audio_thread = std::thread([this]() {
LowLatPacket<AudioPacket> rx_packet;
int counter = 0;
while (true) {
if (m_audio_socket->receive_data(&rx_packet) > 0) {
update_stats();
emit stats_changed();
if (m_is_recording) {
m_audio_packets.push_back(rx_packet.payload.packet_data);
emit audio_received(rx_packet.payload.packet_data);
}
}
}
});
audio_thread.detach();
connect(this, &DebuggerWindow::stats_changed, this, [this]() {
ui->jitter->setValue((m_packet_max_delta - m_packet_min_delta) / 1000.0f);
ui->max_delta->setValue(m_packet_max_delta / 1000.0f);
ui->min_delta->setValue(m_packet_min_delta / 1000.0f);
ui->mean_delta->setValue(m_mean_delta / 1000.0f);
});
connect(this, &DebuggerWindow::audio_received, this, [this](AudioData data) {
scope_render_audio(data);
});
connect(ui->min_max_rst, &QPushButton::clicked, this, [this]() {
reset_min_max();
});
connect(ui->rec_start, &QPushButton::clicked, this, [this]() {
m_is_recording = true;
m_stream_scope_scene->clear();
m_audio_packets.clear();
ui->rec_start->setEnabled(false);
ui->rec_stop->setEnabled(true);
});
connect(ui->rec_stop, &QPushButton::clicked, this, [this]() {
m_is_recording = false;
ui->rec_start->setEnabled(true);
ui->rec_stop->setEnabled(false);
QString file_path = QFileDialog::getSaveFileName(
this,
"Save rec to...",
"",
"*.wav"
);
if (!file_path.isEmpty()) {
std::cout << "Saving rec to " << file_path.toStdString() << std::endl;
SF_INFO info{};
info.channels = 1;
info.format = SF_FORMAT_FLOAT | SF_FORMAT_WAV;
info.samplerate = 96000;
SNDFILE* wav_out = sf_open(file_path.toStdString().c_str(), SFM_WRITE, &info);
if (wav_out) {
for (auto& pck : m_audio_packets) {
sf_write_float(wav_out, pck.samples, 64);
}
sf_close(wav_out);
} else {
QMessageBox::critical(this, "ERROR", "Failed to save to wav file");
}
}
});
}
void DebuggerWindow::update_stats() {
auto now = m_nmapper->local_now_us();
if (m_first_pck_received) {
auto delta = now - m_last_stamp;
m_packet_deltas.push_back(delta);
if (delta > m_packet_max_delta) {
m_packet_max_delta = delta;
}
if (delta < m_packet_min_delta) {
m_packet_min_delta = delta;
}
// Mean measurement over 500 packets
// Reset min max measure too
if (m_packet_deltas.size() > 500) {
m_packet_deltas.pop_front();
}
float delta_sum = 0;
for (auto& d : m_packet_deltas) {
delta_sum += d;
}
delta_sum /= m_packet_deltas.size();
m_mean_delta = delta_sum;
}
m_last_stamp = now;
m_first_pck_received = true;
}
void DebuggerWindow::init_scope_scene() {
m_stream_scope_scene = new QGraphicsScene{};
ui->stream_view->setScene(m_stream_scope_scene);
}
void DebuggerWindow::scope_render_audio(const AudioData& data) {
const Qt::GlobalColor colors[3] = {Qt::red, Qt::cyan, Qt::green};
static int color_counter = 0;
QGraphicsScene* scene = ui->stream_view->scene();
QPen pen{colors[color_counter]};
pen.setWidth(1);
float x_start = m_rendered_packets_count * 5.0f * AUDIO_DATA_SAMPLES_PER_PACKETS;
float x_end = (m_rendered_packets_count + 1) * 5.0f * AUDIO_DATA_SAMPLES_PER_PACKETS;
scene->addLine(x_start, 0, x_end, 0, pen);
for (int i = 0; i < AUDIO_DATA_SAMPLES_PER_PACKETS; i++) {
float sample_y = data.samples[i] * 500.0f;
float sample_x = x_start + i * 5.0f;
scene->addLine(sample_x, 0, sample_x, sample_y, pen);
}
m_rendered_packets_count++;
color_counter = (color_counter + 1) % 3;
}