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
|
#include "nw4r/snd/snd_BankFile.h"
/* Original source:
* kiwi515/ogws
* src/nw4r/snd/snd_BankFile.cpp
*/
/*******************************************************************************
* headers
*/
#include "common.h"
#include "nw4r/snd/snd_Util.h"
#include "nw4r/snd/snd_WaveArchive.h" // WaveArchiveReader
#include "nw4r/snd/snd_WaveFile.h"
#include "nw4r/ut/ut_binaryFileFormat.h" // ut::BinaryFileHeader
#include "nw4r/ut/ut_algorithm.h"
#include "nw4r/NW4RAssert.hpp"
/*******************************************************************************
* local function declarations
*/
namespace nw4r { namespace snd { namespace detail
{
inline byte_t ReadByte(void const *address)
{
return *static_cast<byte_t const *>(address);
}
}}} // namespace nw4r::snd::detail
/*******************************************************************************
* functions
*/
namespace nw4r { namespace snd { namespace detail {
bool BankFileReader::IsValidFileHeader(void const *bankData)
{
ut::BinaryFileHeader const *fileHeader =
static_cast<ut::BinaryFileHeader const *>(bankData);
NW4RAssertMessage_Line(
59, fileHeader->signature == BankFile::SIGNATURE_FILE,
"invalid file signature. bank data is not available.");
if (fileHeader->signature != BankFile::SIGNATURE_FILE)
return false;
NW4RAssertMessage_Line(67, fileHeader->version >= NW4R_FILE_VERSION(1, 0),
"bank file is not supported version.\n please "
"reconvert file using new version tools.\n");
if (fileHeader->version < NW4R_FILE_VERSION(1, 0))
return false;
NW4RAssertMessage_Line(73, fileHeader->version <= SUPPORTED_FILE_VERSION,
"bank file is not supported version.\n please "
"reconvert file using new version tools.\n");
if (fileHeader->version > SUPPORTED_FILE_VERSION)
return false;
return true;
}
BankFileReader::BankFileReader(void const *bankData) :
mHeader (nullptr),
mDataBlock (nullptr),
mWaveBlock (nullptr)
{
NW4RAssertPointerNonnull_Line(93, bankData);
if (!IsValidFileHeader(bankData))
return;
mHeader = static_cast<BankFile::Header const *>(bankData);
if (mHeader->dataBlockOffset)
{
mDataBlock = static_cast<BankFile::DataBlock const *>(
ut::AddOffsetToPtr(mHeader, mHeader->dataBlockOffset));
NW4RAssert_Line(105, mDataBlock->blockHeader.kind
== BankFile::SIGNATURE_DATA_BLOCK);
}
if (mHeader->waveBlockOffset)
{
mWaveBlock = static_cast<BankFile::WaveBlock const *>(
ut::AddOffsetToPtr(mHeader, mHeader->waveBlockOffset));
NW4RAssert_Line(113, mWaveBlock->blockHeader.kind
== BankFile::SIGNATURE_WAVE_BLOCK);
}
}
BankFile::InstParam const *BankFileReader::GetInstParam(int prgNo, int key,
int velocity) const
{
NW4RAssertPointerNonnull_Line(135, mHeader);
if (!mHeader)
return nullptr;
if (prgNo < 0 || prgNo >= static_cast<int>(mDataBlock->instTable.count))
return nullptr;
BankFile::DataRegion const *ref = &mDataBlock->instTable.item[prgNo];
if (ref->dataType == 4)
return nullptr;
if (ref->dataType != 1)
{
ref = GetReferenceToSubRegion(ref, key);
if (!ref)
return nullptr;
}
if (ref->dataType == 4)
return nullptr;
if (ref->dataType != 1)
{
ref = GetReferenceToSubRegion(ref, velocity);
if (!ref)
return nullptr;
}
if (ref->dataType != 1)
return nullptr;
BankFile::InstParam const *instParam =
Util::GetDataRefAddress1(*ref, &mDataBlock->instTable);
return instParam;
}
bool BankFileReader::ReadInstInfo(InstInfo *instInfo, int prgNo, int key,
int velocity) const
{
NW4RAssertPointerNonnull_Line(188, instInfo);
BankFile::InstParam const *instParam = GetInstParam(prgNo, key, velocity);
if (!instParam)
return false;
if (instParam->waveDataLocationType
== InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_INDEX)
{
if (instParam->waveIndex < 0)
return false;
instInfo->waveDataLocation.type =
InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_INDEX;
instInfo->waveDataLocation.index = instParam->waveIndex;
}
else if (instParam->waveDataLocationType
== InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_ADDRESS)
{
if (!instParam->waveInfoAddress)
return false;
instInfo->waveDataLocation.type =
InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_ADDRESS;
instInfo->waveDataLocation.address = instParam->waveInfoAddress;
}
else if (instParam->waveDataLocationType
== InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_CALLBACK)
{
if (!instParam->waveDataLocationCallback)
return false;
instInfo->waveDataLocation.type =
InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_CALLBACK;
instInfo->waveDataLocation.callback =
instParam->waveDataLocationCallback;
}
else
{
NW4RPanicMessage_Line(210, "Invalid waveDataLocationType %d",
instParam->waveDataLocationType);
return false;
}
instInfo->attack = instParam->attack;
instInfo->hold = instParam->hold;
instInfo->decay = instParam->decay;
instInfo->sustain = instParam->sustain;
instInfo->release = instParam->release;
instInfo->originalKey = instParam->originalKey;
instInfo->pan = instParam->pan;
if (mHeader->fileHeader.version >= NW4R_FILE_VERSION(1, 1))
{
instInfo->volume = instParam->volume;
instInfo->tune = instParam->tune;
}
else
{
instInfo->volume = 127;
instInfo->tune = 1.0f;
}
switch (instParam->noteOffType)
{
case 0:
instInfo->noteOffType = InstInfo::NOTE_OFF_TYPE_RELEASE;
break;
case 1:
instInfo->noteOffType = InstInfo::NOTE_OFF_TYPE_IGNORE;
break;
default:
NW4RPanicMessage_Line(240, "Invalid noteOffType %d",
instParam->noteOffType);
return false;
}
instInfo->alternateAssign = instParam->alternateAssign;
return true;
}
BankFile::DataRegion const *BankFileReader::GetReferenceToSubRegion(
BankFile::DataRegion const *ref, int splitKey) const
{
BankFile::DataRegion const *subRef = nullptr;
switch (ref->dataType)
{
case 0:
break;
case 1:
subRef = ref;
break;
case 2:
{
BankFile::RangeTable const *table =
Util::GetDataRefAddress2(*ref, &mDataBlock->instTable);
if (!table)
return nullptr;
int index = 0;
while (splitKey > ReadByte(table->key + index))
{
if (++index >= table->tableSize)
return nullptr;
}
u32 refOffset = sizeof(BankFile::DataRegion) * index
+ ut::RoundUp(table->tableSize + 1, 4);
/* TODO: fake: how to properly match this call? (the arguments to
* AddOffsetToPtr are supposed to be the other way around)
*/
subRef = static_cast<BankFile::DataRegion const *>(
ut::AddOffsetToPtr(reinterpret_cast<void const *>(refOffset),
reinterpret_cast<u32>(table)));
}
break;
case 3:
{
BankFile::IndexTable const *table =
Util::GetDataRefAddress3(*ref, &mDataBlock->instTable);
if (!table)
return nullptr;
if (splitKey < table->min || splitKey > table->max)
return nullptr;
subRef = reinterpret_cast<BankFile::DataRegion const *>(
table->ref
+ (splitKey - table->min) * sizeof(BankFile::DataRegion));
}
break;
}
return subRef;
}
bool BankFileReader::ReadWaveInfo(
WaveInfo *waveParam, InstInfo::WaveDataLocation const &waveDataLocation,
void const *waveDataAddress, WaveInfo const **waveInfoAddress) const
{
NW4RAssertPointerNonnull_Line(331, waveParam);
if (waveInfoAddress)
*waveInfoAddress = nullptr;
if (!mHeader)
return false;
if (waveDataLocation.type
== InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_INDEX)
{
int waveIndex = waveDataLocation.index;
if (!mWaveBlock)
{
WaveArchiveReader waveArchiveReader(waveDataAddress);
WaveFile::FileHeader const *fileHeader =
waveArchiveReader.GetWaveFile(waveIndex);
if (!fileHeader)
return false;
WaveFileReader waveFileReader(fileHeader);
return waveFileReader.ReadWaveInfo(waveParam);
}
else if (waveIndex >= mWaveBlock->waveInfoTable.count)
{
return false;
}
else
{
WaveFile::WaveInfo const *waveInfo = Util::GetDataRefAddress0(
mWaveBlock->waveInfoTable.item[waveIndex],
&mWaveBlock->waveInfoTable);
if (!waveInfo)
return false;
WaveFileReader waveFileReader(waveInfo);
return waveFileReader.ReadWaveInfo(waveParam, waveDataAddress);
}
}
else if (waveDataLocation.type
== InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_ADDRESS)
{
if (!waveDataLocation.address)
return false;
if (waveInfoAddress)
*waveInfoAddress = waveDataLocation.address;
*waveParam = *waveDataLocation.address;
return true;
}
else if (waveDataLocation.type
== InstInfo::WaveDataLocation::WAVE_DATA_LOCATION_CALLBACK)
{
if (!waveDataLocation.callback)
return false;
WaveInfo *waveInfo = waveDataLocation.callback->at_0x08();
if (!waveInfo)
return false;
if (waveInfoAddress)
*waveInfoAddress = waveInfo;
*waveParam = *waveInfo;
return true;
}
else
{
NW4RPanicMessage_Line(389, "Invalid waveDataLocation.type %d",
waveDataLocation.type);
return false;
}
}
}}} // namespace nw4r::snd::detail
|