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
|
// unit-test-riff.cpp
#include "../../source/core/slang-random-generator.h"
#include "../../source/core/slang-riff.h"
#include "unit-test/slang-unit-test.h"
using namespace Slang;
static void _writeRandom(
RandomGenerator* rand,
size_t maxSize,
RIFF::BuildCursor& cursor,
List<uint8_t>& ioData)
{
while (true)
{
const Index oldCount = ioData.getCount();
const size_t allocSize = size_t(rand->nextInt32InRange(1, 50));
if (allocSize + oldCount > maxSize)
{
break;
}
ioData.setCount(oldCount + Index(allocSize));
rand->nextData(ioData.getBuffer() + oldCount, allocSize);
// Write
cursor.addData(ioData.getBuffer() + oldCount, allocSize);
}
// Should be a single block with same data as the List
auto dataChunk = as<RIFF::DataChunkBuilder>(cursor.getCurrentChunk());
SLANG_ASSERT(dataChunk);
}
namespace
{
struct DumpContext
{
private:
WriterHelper _writer;
Count _indent = 0;
Count _hexByteCount = 0;
bool _isRoot = true;
public:
DumpContext(ISlangWriter* writer)
: _writer(writer)
{
}
void beginListChunk(RIFF::Chunk::Type type)
{
_dumpIndent();
// If it's the root it's 'riff'
_dumpRiffType(_isRoot ? RIFF::RootChunk::kTag : RIFF::ListChunk::kTag);
_writer.put(" ");
_dumpRiffType(type);
_writer.put("\n");
_indent++;
}
void endListChunk() { _indent--; }
void beginDataChunk(RIFF::Chunk::Type type)
{
_dumpIndent();
// Write out the name
_dumpRiffType(type);
_writer.put("\n");
_indent++;
_hexByteCount = 0;
}
void endDataChunk() { _indent--; }
void handleData(void const* data, Size size)
{
auto cursor = static_cast<Byte const*>(data);
auto remainingSize = size;
while (remainingSize--)
{
auto byte = *cursor++;
static const Count kBytesPerLine = 32;
static const Count kBytesPerCluster = 4;
if (_hexByteCount % kBytesPerLine == 0)
{
_writer.put("\n");
_dumpIndent();
}
else if (_hexByteCount % kBytesPerCluster == 0)
{
_writer.put(" ");
}
_hexByteCount++;
char text[4] = {0, 0, ' ', 0};
char const* hexDigits = "0123456789abcdef";
text[0] = hexDigits[(byte >> 4) & 0xF];
text[1] = hexDigits[(byte >> 0) & 0xF];
_writer.put(text);
}
}
void _dumpIndent()
{
for (int i = 0; i < _indent; ++i)
{
_writer.put(" ");
}
}
void _dumpRiffType(FourCC fourCC)
{
auto rawValue = FourCC::RawValue(fourCC);
char text[5];
for (int i = 0; i < 4; ++i)
{
text[i] = char(rawValue & 0xFF);
rawValue >>= 8;
}
text[4] = 0;
_writer.put(text);
}
};
} // namespace
static void _dump(RIFF::Chunk const* chunk, DumpContext context)
{
if (auto listChunk = as<RIFF::ListChunk>(chunk))
{
context.beginListChunk(listChunk->getType());
for (auto child : listChunk->getChildren())
_dump(child, context);
context.endListChunk();
}
else if (auto dataChunk = as<RIFF::DataChunk>(chunk))
{
context.beginDataChunk(dataChunk->getType());
context.handleData(dataChunk->getPayload(), dataChunk->getPayloadSize());
context.endDataChunk();
}
}
static void _dump(RIFF::ChunkBuilder* chunk, DumpContext context)
{
if (auto listChunk = as<RIFF::ListChunkBuilder>(chunk))
{
context.beginListChunk(listChunk->getType());
for (auto child : listChunk->getChildren())
_dump(child, context);
context.endListChunk();
}
else if (auto dataChunk = as<RIFF::DataChunkBuilder>(chunk))
{
context.beginDataChunk(dataChunk->getType());
for (auto shard : dataChunk->getShards())
context.handleData(shard->getPayload(), shard->getPayloadSize());
context.endDataChunk();
}
}
static bool _isSingleShard(RIFF::DataChunkBuilder* chunk)
{
Count count = 0;
for (auto shard : chunk->getShards())
{
count++;
if (count > 1)
break;
}
return count == 1;
}
static bool _isEqual(RIFF::DataChunkBuilder* chunk, void const* data, Size size)
{
auto remainingData = static_cast<Byte const*>(data);
auto remainingSize = size;
for (auto shard : chunk->getShards())
{
// If there is more content in the chunk than remains
// to compare against, then there is no chance of a match.
//
auto shardSize = shard->getPayloadSize();
if (shard->getPayloadSize() > remainingSize)
{
return false;
}
// Contents must match, byte-for-byte.
//
if (::memcmp(remainingData, shard->getPayload(), shardSize) != 0)
{
return false;
}
remainingData += shardSize;
remainingSize -= shardSize;
}
// If we reach the end of the chunk, then we have
// a match if there is no data remaining to
// compare against.
//
return remainingSize == 0;
}
SLANG_UNIT_TEST(riff)
{
const FourCC markThings = SLANG_FOUR_CC('T', 'H', 'I', 'N');
const FourCC markData = SLANG_FOUR_CC('D', 'A', 'T', 'A');
{
RIFF::Builder riffBuilder;
RIFF::BuildCursor cursor(riffBuilder);
{
SLANG_SCOPED_RIFF_BUILDER_LIST_CHUNK(cursor, markThings);
{
SLANG_SCOPED_RIFF_BUILDER_DATA_CHUNK(cursor, markData);
const char hello[] = "Hello ";
const char world[] = "World!";
cursor.addData(hello, sizeof(hello));
cursor.addData(world, sizeof(world));
}
{
SLANG_SCOPED_RIFF_BUILDER_DATA_CHUNK(cursor, markData);
const char test0[] = "Testing... ";
const char test1[] = "Testing!";
cursor.addData(test0, sizeof(test0));
cursor.addData(test1, sizeof(test1));
}
{
SLANG_SCOPED_RIFF_BUILDER_LIST_CHUNK(cursor, markThings);
{
SLANG_SCOPED_RIFF_BUILDER_DATA_CHUNK(cursor, markData);
const char another[] = "Another?";
cursor.addData(another, sizeof(another));
}
}
}
SLANG_CHECK(cursor.getCurrentChunk() == nullptr);
SLANG_CHECK(riffBuilder.getRootChunk() != nullptr);
{
StringBuilder builder;
{
StringWriter writer(&builder);
_dump(riffBuilder.getRootChunk(), &writer);
}
{
ComPtr<ISlangBlob> blob;
SLANG_CHECK(SLANG_SUCCEEDED(riffBuilder.writeToBlob(blob.writeRef())));
auto rootChunk = RIFF::RootChunk::getFromBlob(blob);
SLANG_CHECK(rootChunk != nullptr);
// Dump the read contents
StringBuilder readBuilder;
{
StringWriter writer(&readBuilder, 0);
_dump(rootChunk, &writer);
}
// They should be the same
SLANG_CHECK(readBuilder == builder);
}
}
}
// Test writing as a stream only allocates a single data block (as long as there is enough
// space).
{
RIFF::Builder builder;
RIFF::BuildCursor cursor(builder);
SLANG_SCOPED_RIFF_BUILDER_LIST_CHUNK(cursor, markThings);
{
SLANG_SCOPED_RIFF_BUILDER_DATA_CHUNK(cursor, markData);
RefPtr<RandomGenerator> rand = RandomGenerator::create(0x345234);
List<uint8_t> data;
_writeRandom(rand, builder._getMemoryArena().getBlockPayloadSize() / 2, cursor, data);
// Should be a single block with same data as the List
RIFF::DataChunkBuilder* dataChunk =
as<RIFF::DataChunkBuilder>(cursor.getCurrentChunk());
SLANG_ASSERT(dataChunk);
// It should be a single shard
SLANG_CHECK(_isSingleShard(dataChunk));
SLANG_CHECK(_isEqual(dataChunk, data.getBuffer(), data.getCount()));
}
}
// Test writing across multiple data blocks
{
RefPtr<RandomGenerator> rand = RandomGenerator::create(0x345234);
for (Int i = 0; i < 100; ++i)
{
RIFF::Builder builder;
RIFF::BuildCursor cursor(builder);
const size_t maxSize = rand->nextInt32InRange(
1,
int32_t(builder._getMemoryArena().getBlockPayloadSize() * 3));
SLANG_SCOPED_RIFF_BUILDER_LIST_CHUNK(cursor, markThings);
{
SLANG_SCOPED_RIFF_BUILDER_DATA_CHUNK(cursor, markData);
List<uint8_t> data;
_writeRandom(rand, maxSize, cursor, data);
// Should be a single block with same data as the List
RIFF::DataChunkBuilder* dataChunk =
as<RIFF::DataChunkBuilder>(cursor.getCurrentChunk());
SLANG_CHECK(dataChunk && _isEqual(dataChunk, data.getBuffer(), data.getCount()));
}
}
}
#if 0
{
RiffContainer container;
{
FileStream readStream("ambient-drop.wav", FileMode::Open, FileAccess::Read, FileShare::ReadWrite);
SLANG_CHECK(SLANG_SUCCEEDED(RiffUtil::read(&readStream, container)));
RiffUtil::dump(container.getRoot(), StdWriters::getOut());
}
// Write it
{
FileStream writeStream("check.wav", FileMode::Create, FileAccess::Write, FileShare::ReadWrite);
SLANG_CHECK(SLANG_SUCCEEDED(RiffUtil::write(container.getRoot(), true, &writeStream)));
}
}
#endif
}
|