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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
#include <QtTest>
#include <QFile>
#include <QTemporaryFile>
#include <QtMultimedia/QAudioFormat>
#include "Audio/BWFFile.hpp"
#include "Audio/WavFile.hpp"
namespace
{
QByteArray le16 (quint16 value)
{
QByteArray bytes (2, '\0');
bytes[0] = static_cast<char> (value & 0xffu);
bytes[1] = static_cast<char> ((value >> 8) & 0xffu);
return bytes;
}
QByteArray le32 (quint32 value)
{
QByteArray bytes (4, '\0');
bytes[0] = static_cast<char> (value & 0xffu);
bytes[1] = static_cast<char> ((value >> 8) & 0xffu);
bytes[2] = static_cast<char> ((value >> 16) & 0xffu);
bytes[3] = static_cast<char> ((value >> 24) & 0xffu);
return bytes;
}
QByteArray chunk (char const * id, QByteArray const& payload)
{
QByteArray out;
out.append (id, 4);
out.append (le32 (static_cast<quint32> (payload.size ())));
out.append (payload);
if (payload.size () & 1)
{
out.append ('\0');
}
return out;
}
QByteArray wave_file (QList<QByteArray> const& chunks)
{
QByteArray payload {"WAVE", 4};
for (auto const& current : chunks)
{
payload.append (current);
}
QByteArray out;
out.append ("RIFF", 4);
out.append (le32 (static_cast<quint32> (payload.size ())));
out.append (payload);
return out;
}
QByteArray fmt_chunk (quint16 channels = 1, quint32 sample_rate = 12000,
quint16 bits_per_sample = 16, quint16 block_align = 0,
quint32 byte_rate = 0)
{
if (!block_align)
{
block_align = static_cast<quint16> (channels * (bits_per_sample / 8u));
}
if (!byte_rate)
{
byte_rate = sample_rate * block_align;
}
QByteArray payload;
payload.append (le16 (1));
payload.append (le16 (channels));
payload.append (le32 (sample_rate));
payload.append (le32 (byte_rate));
payload.append (le16 (block_align));
payload.append (le16 (bits_per_sample));
return chunk ("fmt ", payload);
}
QByteArray data_chunk (QByteArray const& data = {})
{
return chunk ("data", data);
}
QByteArray info_list_chunk ()
{
QByteArray payload {"INFO", 4};
payload.append (chunk ("INAM", QByteArray {"wsjtx", 5}));
return chunk ("LIST", payload);
}
QByteArray info_list_chunk_with_terminal_pad_outside_size ()
{
QByteArray payload {"INFO", 4};
payload.append ("ISRC", 4);
payload.append (le32 (5));
payload.append ("K1JT", 5);
QByteArray out;
out.append ("LIST", 4);
out.append (le32 (static_cast<quint32> (payload.size ())));
out.append (payload);
out.append ('\0');
return out;
}
QByteArray bare_bext_payload ()
{
return QByteArray (604, '\0');
}
QAudioFormat default_format ()
{
QAudioFormat format;
format.setByteOrder (QAudioFormat::LittleEndian);
format.setChannelCount (1);
format.setCodec ("audio/pcm");
format.setSampleRate (12000);
format.setSampleSize (16);
format.setSampleType (QAudioFormat::SignedInt);
return format;
}
QString write_temp_file (QByteArray const& data)
{
QTemporaryFile file;
file.setAutoRemove (false);
if (!file.open ())
{
return {};
}
if (file.write (data) != data.size ())
{
return {};
}
auto const name = file.fileName ();
file.close ();
return name;
}
}
class TestBWFFile
: public QObject
{
Q_OBJECT
private:
Q_SLOT void opens_valid_pcm_wave ()
{
auto const name = write_temp_file (wave_file ({fmt_chunk (), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QCOMPARE (file.format ().channelCount (), 1);
QCOMPARE (file.format ().sampleRate (), 12000);
file.close ();
QFile::remove (name);
}
Q_SLOT void loads_supported_decode_wave ()
{
QByteArray samples;
samples.append (le16 (0x1234));
samples.append (le16 (0x5678));
samples.append (le16 (0x9abc));
auto const name = write_temp_file (wave_file ({fmt_chunk (), data_chunk (samples)}));
QVERIFY (!name.isEmpty ());
auto const result = Radio::WavFile::load (name, 2);
QVERIFY2 (result.isValid (), qPrintable (result.error));
QCOMPARE (result.frames, 2);
QCOMPARE (result.samples, samples.left (4));
QFile::remove (name);
}
Q_SLOT void loads_legacy_decode_waves ()
{
auto const eight_bit_name = write_temp_file (
wave_file ({fmt_chunk (1, 11025, 8), data_chunk (QByteArray::fromHex ("7f80"))}));
QVERIFY (!eight_bit_name.isEmpty ());
auto const eight_bit = Radio::WavFile::load (eight_bit_name, 2);
QVERIFY2 (eight_bit.isValid (), qPrintable (eight_bit.error));
QCOMPARE (eight_bit.frames, 2);
QCOMPARE (eight_bit.samples, QByteArray::fromHex ("7f80"));
QFile::remove (eight_bit_name);
auto const sixteen_bit_name = write_temp_file (
wave_file ({fmt_chunk (1, 11025, 16), data_chunk (QByteArray::fromHex ("01000200"))}));
QVERIFY (!sixteen_bit_name.isEmpty ());
auto const sixteen_bit = Radio::WavFile::load (sixteen_bit_name, 2);
QVERIFY2 (sixteen_bit.isValid (), qPrintable (sixteen_bit.error));
QCOMPARE (sixteen_bit.frames, 2);
QCOMPARE (sixteen_bit.samples, QByteArray::fromHex ("01000200"));
QFile::remove (sixteen_bit_name);
}
Q_SLOT void rejects_multichannel_decode_wave ()
{
auto const name = write_temp_file (
wave_file ({fmt_chunk (2000, 12000, 16), data_chunk (QByteArray::fromHex ("0000"))}));
QVERIFY (!name.isEmpty ());
BWFFile parsed {QAudioFormat {}, name};
QVERIFY (parsed.open (QIODevice::ReadOnly));
QCOMPARE (parsed.format ().channelCount (), 2000);
parsed.close ();
auto const result = Radio::WavFile::load (name, 36000);
QVERIFY (!result.isValid ());
QVERIFY (result.samples.isEmpty ());
QCOMPARE (result.frames, 0);
QFile::remove (name);
}
Q_SLOT void discards_partial_decode_frame ()
{
auto const name = write_temp_file (
wave_file ({fmt_chunk (), data_chunk (QByteArray::fromHex ("010002"))}));
QVERIFY (!name.isEmpty ());
auto const result = Radio::WavFile::load (name, 2);
QVERIFY2 (result.isValid (), qPrintable (result.error));
QCOMPARE (result.frames, 1);
QCOMPARE (result.samples, QByteArray::fromHex ("0100"));
QFile::remove (name);
}
Q_SLOT void opens_valid_list_info_chunk ()
{
auto const name = write_temp_file (wave_file ({fmt_chunk (), info_list_chunk (), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile::InfoDictionary::key_type const inam {{'I', 'N', 'A', 'M'}};
QByteArray const expected_name {"wsjtx", 5};
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QVERIFY (file.list_info ().contains (inam));
QCOMPARE (file.list_info ().value (inam), expected_name);
file.close ();
QFile::remove (name);
}
Q_SLOT void opens_list_info_with_terminal_subchunk_pad ()
{
auto const name = write_temp_file (wave_file ({fmt_chunk (), data_chunk (),
info_list_chunk_with_terminal_pad_outside_size ()}));
QVERIFY (!name.isEmpty ());
BWFFile::InfoDictionary::key_type const isrc {{'I', 'S', 'R', 'C'}};
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QVERIFY (file.list_info ().contains (isrc));
QCOMPARE (file.list_info ().value (isrc), QByteArray ("K1JT", 5));
file.close ();
QFile::remove (name);
}
Q_SLOT void bext_version_without_chunk_defaults_to_v0 ()
{
auto const name = write_temp_file (wave_file ({fmt_chunk (), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
BWFFile const& const_file = file;
QVERIFY (file.open (QIODevice::ReadOnly));
QCOMPARE (static_cast<int> (const_file.bext_version ()),
static_cast<int> (BWFFile::BextVersion::v_0));
file.close ();
QFile::remove (name);
}
Q_SLOT void bext_fixed_field_getter_stays_bounded ()
{
QByteArray bext = bare_bext_payload ();
bext.replace (0, 256, QByteArray (256, 'B'));
auto const name = write_temp_file (wave_file ({fmt_chunk (), chunk ("bext", bext), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QCOMPARE (file.bext_description (), QByteArray (256, 'B'));
file.close ();
QFile::remove (name);
}
Q_SLOT void bext_origination_date_time_stays_bounded ()
{
QByteArray bext = bare_bext_payload ();
bext.replace (320, 10, QByteArray {"2026-04-06", 10});
bext.replace (330, 10, QByteArray {"12-34-56ZZ", 10});
auto const name = write_temp_file (wave_file ({fmt_chunk (), chunk ("bext", bext), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QCOMPARE (file.bext_origination_date_time ().date (), QDate (2026, 4, 6));
QCOMPARE (file.bext_origination_date_time ().time (), QTime (12, 34, 56));
file.close ();
QFile::remove (name);
}
Q_SLOT void bext_origination_date_time_setter_handles_invalid_value ()
{
QTemporaryFile temp;
temp.setAutoRemove (false);
QVERIFY (temp.open ());
auto const name = temp.fileName ();
temp.close ();
{
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::WriteOnly));
file.bext_origination_date_time ({});
file.close ();
}
{
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QVERIFY (!file.bext_origination_date_time ().isValid ());
file.close ();
}
QFile::remove (name);
}
Q_SLOT void bext_setter_truncates_without_overread ()
{
QTemporaryFile temp;
temp.setAutoRemove (false);
QVERIFY (temp.open ());
auto const name = temp.fileName ();
temp.close ();
{
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::WriteOnly));
file.bext_description (QByteArray (300, 'A'));
file.close ();
}
{
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QCOMPARE (file.bext_description (), QByteArray (256, 'A'));
file.close ();
}
QFile::remove (name);
}
Q_SLOT void rejects_truncated_bext_chunk ()
{
QByteArray bad_bext;
bad_bext.append ("bext", 4);
bad_bext.append (le32 (700));
bad_bext.append (QByteArray (16, '\0'));
auto const name = write_temp_file (wave_file ({fmt_chunk (), bad_bext, data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (!file.open (QIODevice::ReadOnly));
QFile::remove (name);
}
Q_SLOT void rejects_list_info_subchunk_overrun ()
{
QByteArray payload {"INFO", 4};
payload.append ("INAM", 4);
payload.append (le32 (8));
payload.append ("bad", 3);
auto const name = write_temp_file (wave_file ({fmt_chunk (), chunk ("LIST", payload), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (!file.open (QIODevice::ReadOnly));
QFile::remove (name);
}
Q_SLOT void rejects_short_fmt_chunk ()
{
QByteArray payload;
payload.append (le16 (1));
payload.append (le16 (1));
auto const name = write_temp_file (wave_file ({chunk ("fmt ", payload), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (!file.open (QIODevice::ReadOnly));
QFile::remove (name);
}
Q_SLOT void rejects_zero_channel_fmt ()
{
auto const name = write_temp_file (wave_file ({fmt_chunk (0), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (!file.open (QIODevice::ReadOnly));
QFile::remove (name);
}
Q_SLOT void tolerates_inconsistent_fmt_rates ()
{
auto const name = write_temp_file (wave_file ({fmt_chunk (1, 12000, 16, 4, 48000), data_chunk ()}));
QVERIFY (!name.isEmpty ());
BWFFile file {default_format (), name};
QVERIFY (file.open (QIODevice::ReadOnly));
QCOMPARE (file.format ().sampleRate (), 12000);
QCOMPARE (file.format ().sampleSize (), 16);
file.close ();
QFile::remove (name);
}
};
QTEST_MAIN (TestBWFFile)
#include "test_bwf_file.moc"
|