-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wave.hpp
495 lines (404 loc) · 12.5 KB
/
Wave.hpp
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#pragma once
//---------------------------------------------------------------------------//
//
// Wave.hpp
// Audio data class
// Copyright (C) 2013-2018 tapetums
//
//---------------------------------------------------------------------------//
#include <cstdint>
#include <windows.h>
#include <mmreg.h>
#include "RIFF.hpp"
#include "File.hpp"
#include "GenerateUUIDString.hpp"
#pragma warning(push)
#pragma warning(disable: 4815)
//---------------------------------------------------------------------------//
// Forward Declarations
//---------------------------------------------------------------------------//
namespace tapetums
{
class Wave;
inline uint32_t MaskChannelMask(uint16_t channels);
}
//---------------------------------------------------------------------------//
// Classes
//---------------------------------------------------------------------------//
class tapetums::Wave
{
private:
File file;
WAVEFORMATEXTENSIBLE wfex { };
uint8_t* data_offset { nullptr };
int64_t data_size { 0 };
uint32_t table_length { 0 };
ChunkSize64* table_ds64 { nullptr };
public:
Wave() = default;
~Wave() { Dispose(); }
Wave(const Wave&) = delete;
Wave& operator =(const Wave&) = delete;
Wave(Wave&& rhs) noexcept { swap(std::move(rhs)); }
Wave& operator =(Wave&& rhs) noexcept { swap(std::move(rhs)); return *this; }
public:
void swap(Wave&& rhs) noexcept;
public:
auto& format() const noexcept { return wfex; }
auto size() const noexcept { return data_size; }
auto data() const noexcept { return (const uint8_t*)data_offset; }
auto data() noexcept { return data_offset; }
public:
bool Create (LPCWSTR path, const WAVEFORMATEXTENSIBLE& format, int64_t data_size);
void Dispose();
bool Load (LPCWSTR path);
bool Save (LPCWSTR path);
private:
bool ReadAllChunks ();
bool ReadHeader (uint8_t* p);
bool ReadFormatChunk (uint8_t* p);
void ReadDataSize64Chunk(uint8_t* p, uint32_t chunkSize);
uint8_t* ForwardPointer (uint8_t* p, const char chunkId[4], uint32_t chunkSize);
int64_t LookUpSizeTable(const char chunkId[4]);
};
//---------------------------------------------------------------------------//
// Wave Move Constructor
//---------------------------------------------------------------------------//
inline void tapetums::Wave::swap(Wave&& rhs) noexcept
{
if ( this == &rhs ) { return; }
std::swap(file, rhs.file);
std::swap(wfex, rhs.wfex);
std::swap(data_offset, rhs.data_offset);
std::swap(data_size, rhs.data_size);
std::swap(table_length, rhs.table_length);
std::swap(table_ds64, rhs.table_ds64);
}
//---------------------------------------------------------------------------//
// Wave Methods
//---------------------------------------------------------------------------//
inline bool tapetums::Wave::Create
(
LPCWSTR path, const WAVEFORMATEXTENSIBLE& format, int64_t size
)
{
if ( file.is_mapped() ) { Dispose(); }
// メンバ変数に保存
wfex = format;
data_size = size;
// ファイルサイズの計算
const auto riffSize = sizeof(RiffChunk) +
sizeof(DataSize64Chunk) +
sizeof(FormatExtensibleChunk) +
sizeof(DataChunk) +
data_size;
// メモリマップトファイルの生成
if ( path == nullptr || path[0] == L'\0' )
{
// メモリ上に生成
wchar_t uuid [40];
GenerateUUIDStringW(uuid, 40); // ランダムな名前を生成
file.Map(riffSize, uuid, File::ACCESS::WRITE);
}
else
{
// ディスク上に生成
file.Open(path, File::ACCESS::WRITE, File::SHARE::WRITE, File::OPEN::NEW);
file.Map(riffSize, path, File::ACCESS::WRITE);
}
if ( ! file.is_mapped() )
{
return false;
}
// ヘッダ部の生成
RiffChunk riff;
if ( riffSize > UINT32_MAX )
{
::memcpy(riff.chunkId, chunkId_RF64, 4);
}
else
{
::memcpy(riff.chunkId, chunkId_RIFF, 4);
}
riff.chunkSize = riffSize > UINT32_MAX ? uint32_t(-1) : uint32_t(riffSize - 8);
::memcpy(riff.riffType, riffType_WAVE, 4);
file.Write(riff);
DataSize64Chunk ds64;
if ( riffSize > UINT32_MAX )
{
::memcpy(ds64.chunkId, chunkId_ds64, 4);
}
else
{
::memcpy(ds64.chunkId, chunkId_JUNK, 4);
}
ds64.chunkSize = sizeof(ds64) - 8;
ds64.riffSize = riffSize - 8;
ds64.dataSize = data_size;
ds64.sampleCount = 0;
ds64.tableLength = 0;
file.Write(ds64);
if ( wfex.dwChannelMask == 0 )
{
wfex.dwChannelMask = MaskChannelMask(wfex.Format.nChannels);
}
FormatExtensibleChunk fmt;
::memcpy(fmt.chunkId, chunkId_fmt, 4);
fmt.chunkSize = sizeof(fmt) - 8;
::memcpy(&fmt.formatType, &wfex.Format.wFormatTag, sizeof(wfex));
file.Write(fmt);
DataChunk data;
::memcpy(data.chunkId, chunkId_data, 4);
data.chunkSize = data_size > UINT32_MAX ? uint32_t(-1) : uint32_t(data_size);
data_offset = file.pointer() + 8;
file.Write(data);
return true;
}
//---------------------------------------------------------------------------//
inline void tapetums::Wave::Dispose()
{
if ( table_ds64 )
{
delete[] table_ds64;
table_ds64 = nullptr;
}
data_size = table_length = 0;
data_offset = nullptr;
::memset(&wfex, 0, sizeof(wfex));
file.Close();
return;
}
//---------------------------------------------------------------------------//
inline bool tapetums::Wave::Load(LPCWSTR path)
{
if ( file.is_mapped() ) { return false; }
file.Open(path, File::ACCESS::WRITE, File::SHARE::WRITE, File::OPEN::EXISTING);
if ( ! file.is_open() )
{
return false;
}
file.Map(File::ACCESS::WRITE);
if ( ! file.is_mapped() )
{
return false;
}
file.Seek(0);
return ReadAllChunks();
}
//---------------------------------------------------------------------------//
inline bool tapetums::Wave::Save(LPCWSTR path)
{
if ( ! file.is_mapped() ) { return false; }
file.Seek(0);
File tmp;
tmp.Open
(
path, File::ACCESS::WRITE, File::SHARE::WRITE, File::OPEN::OR_TRUNCATE
);
tmp.Write(file.pointer(), size_t(file.size()));
return true;
}
//---------------------------------------------------------------------------//
// Wave Internal Methods
//---------------------------------------------------------------------------//
inline bool tapetums::Wave::ReadAllChunks()
{
auto p = file.pointer();
// RIFFチャンクの読み込み
if ( ! ReadHeader(p) )
{
return false;
}
// データサイズを取得
const auto end = p + *(uint32_t*)(p + 4) + 8;
// ポインタを進める
p += sizeof(RiffChunk);
char chunkId[4];
uint32_t chunkSize;
// RIFFサブチャンクの読み込み
while ( p < end )
{
::memcpy(chunkId, p, sizeof(chunkId));
::memcpy(&chunkSize, p + 4, sizeof(chunkSize));
// チャンクデータの読み込み
if ( 0 == ::memcmp(chunkId, chunkId_ds64, sizeof(chunkId)) )
{
// 'ds64' chunk
ReadDataSize64Chunk(p + 8, chunkSize);
}
else if ( 0 == ::memcmp(chunkId, chunkId_fmt, sizeof(chunkId)) )
{
// 'fmt ' chunk
if ( ! ReadFormatChunk(p + 8) )
{
return false;
}
}
else if ( 0 == memcmp(chunkId, chunkId_data, sizeof(chunkId)) )
{
// 'data' chunk
if ( chunkSize < UINT32_MAX )
{
data_size = chunkSize;
}
data_offset = p + 8;
}
// ポインタを次のチャンクまで進める
p = ForwardPointer(p, chunkId, chunkSize);
if ( nullptr == p )
{
return false;
}
}
return true;
}
//---------------------------------------------------------------------------//
inline bool tapetums::Wave::ReadHeader(uint8_t* p)
{
char chunkId[4];
uint32_t chunkSize;
char riffType[4];
memcpy(chunkId, p, sizeof(chunkId));
memcpy(&chunkSize, p + 4, sizeof(chunkSize));
memcpy(riffType, p + 8, sizeof(riffType));
if ( 0 == ::memcmp(chunkId, chunkId_RF64, sizeof(chunkId)) )
{
if ( chunkSize != UINT32_MAX )
{
return false;
}
}
else if ( 0 != ::memcmp(chunkId, chunkId_RIFF, sizeof(chunkId)) )
{
return false;
}
if ( 0 != ::memcmp(riffType, riffType_WAVE, sizeof(riffType)) )
{
return false;
}
return true;
}
//---------------------------------------------------------------------------//
inline bool tapetums::Wave::ReadFormatChunk(uint8_t* p)
{
WORD tag = WAVE_FORMAT_UNKNOWN;
memcpy(&tag, p, sizeof(tag));
if ( tag == WAVE_FORMAT_PCM || tag == WAVE_FORMAT_IEEE_FLOAT )
{
memcpy(&wfex, p, sizeof(PCMWAVEFORMAT));
}
else if ( tag == WAVE_FORMAT_EXTENSIBLE )
{
memcpy(&wfex, p, sizeof(WAVEFORMATEXTENSIBLE));
}
else
{
return false;
}
return true;
}
//---------------------------------------------------------------------------//
inline void tapetums::Wave::ReadDataSize64Chunk
(
uint8_t* p, uint32_t chunkSize
)
{
if ( chunkSize <= sizeof(DataSize64Chunk) )
{
// 最小限の情報しか格納されていないとき
DataSize64ChunkLight chunk;
::memcpy(&chunk, p, sizeof(chunk));
data_size = chunk.dataSize;
}
else
{
// チャンクサイズ情報が格納されているとき
DataSize64Chunk chunk;
::memcpy(&chunk, p, sizeof(chunk));
data_size = chunk.dataSize;
table_length = chunk.tableLength;
table_ds64 = new ChunkSize64[table_length];
::memcpy(table_ds64, p + sizeof(DataSize64Chunk), table_length * sizeof(ChunkSize64));
}
}
//---------------------------------------------------------------------------//
inline uint8_t* tapetums::Wave::ForwardPointer
(
uint8_t* p, const char chunkId[4], uint32_t chunkSize
)
{
if ( 4 <= chunkSize && chunkSize < UINT32_MAX )
{
return p + 4 * sizeof(char) + sizeof(chunkSize) + chunkSize;
}
else
{
if ( 0 == memcmp(chunkId, chunkId_data, sizeof(char) * 4) )
{
return p + 4 * sizeof(char) + sizeof(chunkSize) + data_size;
}
else if ( table_ds64 )
{
return p + 4 * sizeof(char) + sizeof(chunkSize) + LookUpSizeTable(chunkId);
}
else
{
return nullptr;
}
}
}
//---------------------------------------------------------------------------//
inline int64_t tapetums::Wave::LookUpSizeTable(const char chunkId[4])
{
for ( size_t index = 0; index < table_length; ++index )
{
const auto chunk = table_ds64[index];
if ( 0 == memcmp(chunkId, chunk.chunkId, sizeof(char) * 4) )
{
return chunk.chunkSize;
}
}
return 0;
}
//---------------------------------------------------------------------------//
// Utility Functions
//---------------------------------------------------------------------------//
inline uint32_t tapetums::MaskChannelMask(uint16_t channels)
{
switch ( channels )
{
case 1: // monaural
{
return SPEAKER_FRONT_CENTER;
}
case 2: // stereo
{
return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
}
case 4: // 4 way
{
return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
}
case 6: // 5.1 ch
{
return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
}
case 8: // 7.1 ch
{
return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT |
SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT;
}
default:
{
return 0;
}
}
}
//---------------------------------------------------------------------------//
#pragma warning(pop)
// Wave.hpp