-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathExportSamplesTask.cpp
More file actions
341 lines (278 loc) · 7.37 KB
/
Copy pathExportSamplesTask.cpp
File metadata and controls
341 lines (278 loc) · 7.37 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
//
// Misc/ExportSamplesTask.cpp: Description
// Copyright (C) 2020 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include <ExportSamplesTask.h>
#include <QCoreApplication>
using namespace SigDigger;
#define SIGDIGGER_EXPORT_SAMPLES_BREATHE_INTERVAL_MS 100
#define SIGDIGGER_EXPORT_SAMPLES_BREATHE_BLOCK_SIZE 0x10000
void
ExportSamplesTask::breathe(quint64 i)
{
size_t size = this->data.size();
if (this->timer.elapsed() > SIGDIGGER_EXPORT_SAMPLES_BREATHE_INTERVAL_MS) {
this->timer.restart();
emit progress(
static_cast<qreal>(i) / static_cast<qreal>(size - 1),
"Saving data");
QCoreApplication::processEvents();
}
}
bool
ExportSamplesTask::exportToMatlab(void)
{
size_t size = this->data.size();
of << "%\n";
of << "% Time domain capture file generated by SigDigger\n";
of << "%\n\n";
of << "sampleRate = " << this->fs << ";\n";
of << "deltaT = " << 1 / this->fs << ";\n";
of << "X = [ ";
of << std::setprecision(std::numeric_limits<float>::digits10);
for (size_t i = 0; !this->cancelFlag && i < size; ++i) {
of
<< SU_C_REAL(this->data[i]) << " + "
<< SU_C_IMAG(this->data[i]) << "i, ";
if (i % SIGDIGGER_EXPORT_SAMPLES_BREATHE_BLOCK_SIZE == 0)
this->breathe(i);
}
of << "];\n";
return true;
}
bool
ExportSamplesTask::exportToMat5(void)
{
size_t size = this->data.size();
bool ok = false;
for (size_t i = 0; !this->cancelFlag && i < size; ++i) {
SU_TRYCATCH(
su_mat_file_stream_col(
this->mf,
SU_C_REAL(this->data[i]),
SU_C_IMAG(this->data[i])),
goto done);
if (i % SIGDIGGER_EXPORT_SAMPLES_BREATHE_BLOCK_SIZE == 0) {
SU_TRYCATCH(su_mat_file_flush(mf), goto done);
this->breathe(i);
}
}
SU_TRYCATCH(su_mat_file_flush(mf), goto done);
ok = true;
done:
if (!ok)
emit error(
"Cannot save data to Mat5 file "
+ this->path
+ ". See error log for details.");
return ok;
}
bool
ExportSamplesTask::exportToWav(void)
{
size_t size = this->data.size();
bool ok = false;
size_t i = 0;
size_t amount;
for (
i = 0;
!this->cancelFlag && i < size;
i += SIGDIGGER_EXPORT_SAMPLES_BREATHE_BLOCK_SIZE) {
amount = size - i;
if (amount > SIGDIGGER_EXPORT_SAMPLES_BREATHE_BLOCK_SIZE)
amount = SIGDIGGER_EXPORT_SAMPLES_BREATHE_BLOCK_SIZE;
if (sf_write_float(
this->sfp,
reinterpret_cast<const SUFLOAT *>(this->data.data() + i),
2 * amount)
!= 2 * static_cast<sf_count_t>(amount))
goto done;
this->breathe(i);
}
if (i < size)
if (sf_write_float(
this->sfp,
reinterpret_cast<const SUFLOAT *>(this->data.data() + i),
2 * static_cast<sf_count_t>(size - i)) !=
2 * static_cast<sf_count_t>(size - i))
goto done;
ok = true;
done:
if (!ok)
emit error(
"Cannot save data to WAV/RAW file "
+ this->path
+ ": "
+ QString(sf_strerror(this->sfp)));
return ok;
}
bool
ExportSamplesTask::work(void)
{
bool ok = false;
this->timer.start();
if (this->format == "mat")
ok = this->exportToMat5();
else if (this->format == "m")
ok = this->exportToMatlab();
else if (this->format == "wav" || this->format == "raw")
ok = this->exportToWav();
else
emit error("Unsupported data format " + this->format);
if (ok) {
if (this->cancelFlag)
emit cancelled();
else
emit done();
}
return false;
}
void
ExportSamplesTask::cancel(void)
{
this->cancelFlag = true;
}
bool
ExportSamplesTask::openMat5(void)
{
su_mat_matrix_t *mtx = nullptr;
bool ok = false;
SU_TRYCATCH(this->mf = su_mat_file_new(), goto done);
SU_TRYCATCH(
mtx = su_mat_file_make_matrix(this->mf, "sampleRate", 1, 1),
goto done);
SU_TRYCATCH(su_mat_matrix_write_col(mtx, fs), goto done);
SU_TRYCATCH(
mtx = su_mat_file_make_matrix(this->mf, "deltaT", 1, 1),
goto done);
SU_TRYCATCH(su_mat_matrix_write_col(mtx, 1 / fs), goto done);
SU_TRYCATCH(
mtx = su_mat_file_make_streaming_matrix(this->mf, "X", 2, 0),
goto done);
SU_TRYCATCH(su_mat_file_dump(mf, path.toStdString().c_str()), goto done);
ok = true;
done:
if (!ok)
this->lastError =
"Cannot create Mat5 file "
+ this->path
+ ". See error log for details.";
return ok;
}
QString
ExportSamplesTask::getLastError(void) const
{
return this->lastError;
}
bool
ExportSamplesTask::openMatlab(void)
{
this->of = std::ofstream(path.toStdString().c_str(), std::ofstream::binary);
if (!of.is_open()) {
this->lastError =
"Cannot open "
+ this->path
+ ": "
+ QString(strerror(errno));
return false;
}
return true;
}
bool
ExportSamplesTask::openWav(void)
{
SF_INFO sfinfo;
sfinfo.channels = 2;
sfinfo.samplerate = static_cast<int>(fs);
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
if ((this->sfp = sf_open(
path.toStdString().c_str(),
SFM_WRITE,
&sfinfo)) == nullptr) {
this->lastError =
"Cannot open "
+ this->path
+ ": "
+ QString(strerror(errno));
return false;
}
return true;
}
bool
ExportSamplesTask::openRaw(void)
{
SF_INFO sfinfo;
sfinfo.channels = 2;
sfinfo.samplerate = static_cast<int>(fs);
sfinfo.format = SF_FORMAT_RAW | SF_FORMAT_FLOAT;
if ((this->sfp = sf_open(
path.toStdString().c_str(),
SFM_WRITE,
&sfinfo)) == nullptr) {
this->lastError =
"Cannot open "
+ this->path
+ ": "
+ QString(sf_strerror(nullptr));
return false;
}
return true;
}
bool
ExportSamplesTask::attemptOpen(void)
{
if (this->format == "mat")
return this->openMat5();
else if (this->format == "m")
return this->openMatlab();
else if (this->format == "wav")
return this->openWav();
else if (this->format == "raw")
return this->openRaw();
else
this->lastError = "Unsupported format \"" + this->lastError + "\"";
return false;
}
ExportSamplesTask::~ExportSamplesTask(void)
{
if (this->sfp != nullptr)
sf_close(this->sfp);
if (this->mf != nullptr)
su_mat_file_destroy(this->mf);
}
ExportSamplesTask::ExportSamplesTask(
QString const &path,
QString const &format,
const SUCOMPLEX *data,
size_t length,
qreal fs,
int start,
int end)
{
if (start < 0)
start = 0;
if (end > static_cast<int>(length))
end = static_cast<int>(length);
this->start = start;
this->end = end;
this->fs = fs;
this->path = path;
this->format = format;
this->data.resize(static_cast<unsigned>(end - start));
this->data.assign(data + start, data + end);
this->setDataSize(this->data.size());
}