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
|
#include "nw4r/snd/snd_WsdFile.h"
/* Original source:
* kiwi515/ogws
* src/nw4r/snd/snd_WsdFile.cpp
*/
/*******************************************************************************
* headers
*/
#include "common.h" // nullptr
#include "nw4r/snd/snd_Util.h" // Util::GetDataRefAddress0
#include "nw4r/snd/snd_WaveFile.h"
#include "nw4r/snd/snd_WaveArchive.h" // WaveArchiveReader
#include "nw4r/ut/ut_binaryFileFormat.h" // ut::BinaryFileHeader
#include "nw4r/ut/ut_algorithm.h" // ut::AddOffsetToPtr
#include "nw4r/NW4RAssert.hpp"
/*******************************************************************************
* functions
*/
namespace nw4r { namespace snd { namespace detail {
bool WsdFileReader::IsValidFileHeader(void const *wsdData)
{
ut::BinaryFileHeader const *fileHeader =
static_cast<ut::BinaryFileHeader const *>(wsdData);
NW4RAssertMessage_Line(
59, fileHeader->signature == WsdFile::SIGNATURE_FILE,
"invalid file signature. wsd data is not available.");
if (fileHeader->signature != WsdFile::SIGNATURE_FILE)
return false;
NW4RAssertMessage_Line(67, fileHeader->version >= NW4R_FILE_VERSION(1, 0),
"wsd 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,
"wsd file is not supported version.\n"
" please reconvert file using new version tools.\n");
if (fileHeader->version > SUPPORTED_FILE_VERSION)
return false;
return true;
}
WsdFileReader::WsdFileReader(void const *wsdData) :
mHeader (nullptr),
mDataBlock (nullptr),
mWaveBlock (nullptr)
{
NW4RAssertPointerNonnull_Line(93, wsdData);
if (!IsValidFileHeader(wsdData))
return;
mHeader = static_cast<WsdFile::Header const *>(wsdData);
if (mHeader->dataBlockOffset)
{
mDataBlock = static_cast<WsdFile::DataBlock const *>(
ut::AddOffsetToPtr(mHeader, mHeader->dataBlockOffset));
NW4RAssert_Line(105, mDataBlock->blockHeader.kind
== WsdFile::SIGNATURE_DATA_BLOCK);
}
if (mHeader->waveBlockOffset)
{
mWaveBlock = static_cast<WsdFile::WaveBlock const *>(
ut::AddOffsetToPtr(mHeader, mHeader->waveBlockOffset));
NW4RAssert_Line(113, mWaveBlock->blockHeader.kind
== WsdFile::SIGNATURE_WAVE_BLOCK);
}
}
bool WsdFileReader::ReadWaveSoundInfo(WaveSoundInfo *info, int index) const
{
WsdFile::Wsd const *wsd = Util::GetDataRefAddress0(
mDataBlock->refWsd[index], &mDataBlock->wsdCount);
WsdFile::WsdInfo const *src =
Util::GetDataRefAddress0(wsd->refWsdInfo, &mDataBlock->wsdCount);
if (mHeader->fileHeader.version >= NW4R_FILE_VERSION(1, 2))
{
info->pitch = src->pitch;
info->pan = src->pan;
info->surroundPan = src->surroundPan;
info->fxSendA = src->fxSendA;
info->fxSendB = src->fxSendB;
info->fxSendC = src->fxSendC;
info->mainSend = src->mainSend;
}
else if (mHeader->fileHeader.version >= NW4R_FILE_VERSION(1, 1))
{
info->pitch = src->pitch;
info->pan = src->pan;
info->surroundPan = src->surroundPan;
info->fxSendA = 0;
info->fxSendB = 0;
info->fxSendC = 0;
info->mainSend = 127;
}
else
{
info->pitch = 1.0f;
info->pan = 64;
info->surroundPan = 0;
info->fxSendA = 0;
info->fxSendB = 0;
info->fxSendC = 0;
info->mainSend = 127;
}
return true;
}
bool WsdFileReader::ReadWaveSoundNoteInfo(WaveSoundNoteInfo *noteInfo,
int index, int noteIndex) const
{
WsdFile::Wsd const *wsd = Util::GetDataRefAddress0(
mDataBlock->refWsd[index], &mDataBlock->wsdCount);
WsdFile::NoteInfoTable const *noteTable =
Util::GetDataRefAddress0(wsd->refNoteTable, &mDataBlock->wsdCount);
WsdFile::NoteInfo const *src = Util::GetDataRefAddress0(
noteTable->item[noteIndex], &mDataBlock->wsdCount);
noteInfo->waveIndex = src->waveIndex;
noteInfo->attack = src->attack;
noteInfo->hold = src->hold;
noteInfo->decay = src->decay;
noteInfo->sustain = src->sustain;
noteInfo->release = src->release;
noteInfo->originalKey = src->originalKey;
noteInfo->volume = src->volume;
if (mHeader->fileHeader.version >= NW4R_FILE_VERSION(1, 1))
{
noteInfo->pan = src->pan;
noteInfo->surroundPan = src->surroundPan;
noteInfo->pitch = src->pitch;
}
else
{
noteInfo->pan = 64;
noteInfo->surroundPan = 0;
noteInfo->pitch = 1.0f;
}
return true;
}
bool WsdFileReader::ReadWaveInfo(int waveIndex, WaveInfo *waveData,
void const *waveDataAddress) const
{
if (!mWaveBlock)
{
WaveArchiveReader waveArchiveReader(waveDataAddress);
WaveFile::FileHeader const *fileHeader =
waveArchiveReader.GetWaveFile(waveIndex);
if (!fileHeader)
return false;
WaveFileReader waveFileReader(fileHeader);
return waveFileReader.ReadWaveInfo(waveData);
}
else
{
WaveFile::WaveInfo const *waveInfo;
if (mHeader->fileHeader.version >= NW4R_FILE_VERSION(1, 1))
{
if (waveIndex >= mWaveBlock->waveCount)
return false;
waveInfo =
static_cast<WaveFile::WaveInfo const *>(ut::AddOffsetToPtr(
mWaveBlock, mWaveBlock->offsetTable[waveIndex]));
}
else
{
WsdFile::WaveBlockOld const *waveBlockOld =
reinterpret_cast<WsdFile::WaveBlockOld const *>(mWaveBlock);
waveInfo =
static_cast<WaveFile::WaveInfo const *>(ut::AddOffsetToPtr(
waveBlockOld, waveBlockOld->offsetTable[waveIndex]));
}
WaveFileReader waveFileReader(waveInfo);
return waveFileReader.ReadWaveInfo(waveData, waveDataAddress);
}
}
}}} // namespace nw4r::snd::detail
|