-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffline_worker.cpp
More file actions
347 lines (309 loc) · 10.5 KB
/
Copy pathoffline_worker.cpp
File metadata and controls
347 lines (309 loc) · 10.5 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* @file offline_worker.cpp
* @brief File reader that fills the IQ circular buffer for GUI-side FFT.
*/
#include "offline_worker.h"
#include <QFileInfo>
#include <QMutexLocker>
#include <QThread>
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdint>
#include <fstream>
#include <limits>
#include <string>
#include <vector>
namespace {
/**
* @brief Bytes of one complex IQ sample for the selected file type.
*/
size_t iqSampleBytes(const QString &fileType)
{
if (fileType.compare(QStringLiteral("double64"), Qt::CaseInsensitive) == 0) {
return 2 * sizeof(double);
}
if (fileType.compare(QStringLiteral("uint16"), Qt::CaseInsensitive) == 0) {
return 2 * sizeof(int16_t);
}
return 2 * sizeof(float);
}
/**
* @brief Skips a RIFF/WAVE header so the following reads see PCM/IQ payload.
* @param in Open binary stream positioned at byte 0.
*/
void skipWavHeaderIfPresent(std::ifstream &in)
{
char riff[4]{};
in.read(riff, 4);
if (in.gcount() != 4 || std::string(riff, 4) != "RIFF") {
in.clear();
in.seekg(0, std::ios::beg);
return;
}
in.seekg(4, std::ios::cur); // file size
char wave[4]{};
in.read(wave, 4);
if (in.gcount() != 4 || std::string(wave, 4) != "WAVE") {
in.clear();
in.seekg(0, std::ios::beg);
return;
}
while (in.good()) {
char id[4]{};
in.read(id, 4);
uint32_t chunkSize = 0;
in.read(reinterpret_cast<char *>(&chunkSize), 4);
if (!in.good()) {
break;
}
if (std::string(id, 4) == "data") {
return;
}
in.seekg(static_cast<std::streamoff>(chunkSize), std::ios::cur);
}
in.clear();
in.seekg(0, std::ios::beg);
}
/**
* @brief Reads @p count interleaved IQ pairs and converts them to complex float.
* @param in Input stream at the current sample offset.
* @param fileType One of float32 / double64 / uint16.
* @param count Number of complex samples requested.
* @return Samples actually obtained (may be shorter at EOF).
*/
std::vector<std::complex<float>> readIqChunk(std::ifstream &in,
const QString &fileType,
size_t count)
{
std::vector<std::complex<float>> out;
out.reserve(count);
if (fileType.compare(QStringLiteral("double64"), Qt::CaseInsensitive) == 0) {
std::vector<double> raw(count * 2);
in.read(reinterpret_cast<char *>(raw.data()),
static_cast<std::streamsize>(raw.size() * sizeof(double)));
const size_t n = static_cast<size_t>(in.gcount()) / (2 * sizeof(double));
for (size_t i = 0; i < n; ++i) {
out.emplace_back(static_cast<float>(raw[2 * i]),
static_cast<float>(raw[2 * i + 1]));
}
return out;
}
if (fileType.compare(QStringLiteral("uint16"), Qt::CaseInsensitive) == 0) {
std::vector<int16_t> raw(count * 2);
in.read(reinterpret_cast<char *>(raw.data()),
static_cast<std::streamsize>(raw.size() * sizeof(int16_t)));
const size_t n = static_cast<size_t>(in.gcount()) / (2 * sizeof(int16_t));
const float norm = static_cast<float>(std::numeric_limits<int16_t>::max());
for (size_t i = 0; i < n; ++i) {
out.emplace_back(static_cast<float>(raw[2 * i]) / norm,
static_cast<float>(raw[2 * i + 1]) / norm);
}
return out;
}
std::vector<float> raw(count * 2);
in.read(reinterpret_cast<char *>(raw.data()),
static_cast<std::streamsize>(raw.size() * sizeof(float)));
const size_t n = static_cast<size_t>(in.gcount()) / (2 * sizeof(float));
for (size_t i = 0; i < n; ++i) {
out.emplace_back(raw[2 * i], raw[2 * i + 1]);
}
return out;
}
} // namespace
OfflineWorker::OfflineWorker(QObject *parent)
: QObject(parent)
{
}
void OfflineWorker::requestStop()
{
m_stop.store(true);
m_paused.store(false);
}
void OfflineWorker::setPaused(bool paused)
{
m_paused.store(paused);
}
bool OfflineWorker::isRunning() const
{
return m_running.load();
}
qint64 OfflineWorker::payloadBytes() const
{
return m_payloadBytes.load();
}
qint64 OfflineWorker::positionBytes() const
{
return m_positionBytes.load();
}
void OfflineWorker::requestSeek(qint64 payloadOffset)
{
const qint64 offset = std::max<qint64>(0, payloadOffset);
m_positionBytes.store(offset);
m_forceFrame.store(true);
m_seekBytes.store(offset);
}
void OfflineWorker::setAutoRepeat(bool enabled)
{
m_autoRepeat.store(enabled);
}
void OfflineWorker::runOffline(OfflineParams *params,
ThreadSafeBuffer<std::complex<float>> *timeBuf)
{
m_stop.store(false);
m_payloadBytes.store(0);
m_positionBytes.store(0);
m_running.store(true);
if (!params || !timeBuf) {
m_running.store(false);
emit errorOccurred(tr("Offline worker received a null pointer."));
emit finished();
return;
}
QString fileName;
size_t fftSize = 1024;
{
QMutexLocker lock(¶ms->mutex);
fileName = params->fileName;
fftSize = params->fftSize == 0 ? 1024 : params->fftSize;
m_autoRepeat.store(params->autoRepeat);
}
std::ifstream in(fileName.toStdString(), std::ios::binary);
if (!in.is_open()) {
m_running.store(false);
emit errorOccurred(tr("Cannot open offline file:\n%1").arg(fileName));
emit finished();
return;
}
skipWavHeaderIfPresent(in);
const auto payloadStart = in.tellg();
in.seekg(0, std::ios::end);
const auto fileEnd = in.tellg();
in.seekg(payloadStart, std::ios::beg);
if (fileEnd >= payloadStart) {
m_payloadBytes.store(static_cast<qint64>(fileEnd - payloadStart));
}
if (m_seekBytes.load() < 0) {
m_positionBytes.store(0);
}
qint64 readOffset = std::max<qint64>(0, m_seekBytes.load());
auto applyPendingSeek = [&]() {
const qint64 want = m_seekBytes.exchange(-1);
if (want < 0) {
return;
}
QString fileType;
{
QMutexLocker lock(¶ms->mutex);
fileType = params->fileType;
}
const qint64 sampleBytes = static_cast<qint64>(iqSampleBytes(fileType));
const qint64 payload = m_payloadBytes.load();
if (sampleBytes <= 0 || payload <= 0) {
return;
}
qint64 aligned = (want / sampleBytes) * sampleBytes;
aligned = std::clamp(aligned, qint64{0},
std::max(qint64{0}, payload - sampleBytes));
in.clear();
in.seekg(payloadStart + static_cast<std::streamoff>(aligned), std::ios::beg);
readOffset = aligned;
m_positionBytes.store(aligned);
timeBuf->clear();
m_forceFrame.store(true);
};
try {
while (!m_stop.load()) {
applyPendingSeek();
while (!m_stop.load() && m_paused.load() && !m_forceFrame.load()) {
applyPendingSeek();
QThread::msleep(10);
}
if (m_stop.load()) {
break;
}
QString fileType;
size_t wantedFft = fftSize;
size_t hop = fftSize;
bool autoRepeat = m_autoRepeat.load();
{
QMutexLocker lock(¶ms->mutex);
fileType = params->fileType;
wantedFft = params->fftSize == 0 ? 1024 : params->fftSize;
hop = params->hopSamples == 0 ? wantedFft : params->hopSamples;
autoRepeat = params->autoRepeat;
}
m_autoRepeat.store(autoRepeat);
hop = std::max<size_t>(1, std::min(hop, wantedFft));
if (wantedFft != fftSize) {
fftSize = wantedFft;
timeBuf->clear();
}
while (!m_stop.load() && !m_paused.load() && !m_forceFrame.load()
&& timeBuf->size() >= wantedFft * 4) {
applyPendingSeek();
if (m_forceFrame.load()) {
break;
}
QThread::msleep(2);
}
if (m_stop.load()) {
break;
}
if (m_paused.load() && !m_forceFrame.load()) {
continue;
}
m_forceFrame.store(false);
const qint64 sampleBytes = static_cast<qint64>(iqSampleBytes(fileType));
const qint64 needBytes = static_cast<qint64>(fftSize) * sampleBytes;
const qint64 payload = m_payloadBytes.load();
if (payload > 0 && sampleBytes > 0 && needBytes > 0) {
const qint64 remaining = payload - readOffset;
if (remaining < needBytes) {
if (autoRepeat) {
in.clear();
in.seekg(payloadStart, std::ios::beg);
readOffset = 0;
m_positionBytes.store(0);
continue;
}
readOffset = payload;
m_positionBytes.store(payload);
break;
}
}
auto samples = readIqChunk(in, fileType, fftSize);
if (samples.size() < fftSize) {
if (autoRepeat) {
in.clear();
in.seekg(payloadStart, std::ios::beg);
readOffset = 0;
m_positionBytes.store(0);
continue;
}
readOffset = m_payloadBytes.load();
m_positionBytes.store(readOffset);
break;
}
timeBuf->push_vec(samples);
// Overlap: rewind so the next frame starts `hop` samples later.
if (hop < fftSize) {
in.clear();
const auto back = static_cast<std::streamoff>(
(fftSize - hop) * iqSampleBytes(fileType));
in.seekg(-back, std::ios::cur);
}
readOffset += static_cast<qint64>(hop) * sampleBytes;
if (payload > 0) {
readOffset = std::min(readOffset, payload);
}
m_positionBytes.store(std::max<qint64>(0, readOffset));
}
} catch (const std::exception &ex) {
emit errorOccurred(tr("Offline worker exception: %1").arg(ex.what()));
} catch (...) {
emit errorOccurred(tr("Unknown exception in the offline worker."));
}
m_running.store(false);
emit finished();
}