-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathxactwavebank_binary.cpp
215 lines (168 loc) · 7.04 KB
/
xactwavebank_binary.cpp
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
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file
* A binary XACT WaveBank, found in the Xbox version of Jade Empire as XWB files.
*/
/* Based heavily on Luigi Auriemma's unxwb tool
* (<http://aluigi.altervista.org/papers.htm#xbox>), which is licensed
* under the terms of the GPLv2.
*
* The original copyright note in unxwb reads as follows:
*
* Copyright 2005-2016 Luigi Auriemma
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* people who supplied xwb files to analyze [to Luigi]: john deo, antti
*/
#include <cassert>
#include "src/common/util.h"
#include "src/common/strutil.h"
#include "src/common/error.h"
#include "src/common/encoding.h"
#include "src/common/memreadstream.h"
#include "src/sound/xactwavebank_binary.h"
#include "src/sound/decoders/pcm.h"
#include "src/sound/decoders/adpcm.h"
#include "src/sound/decoders/asf.h"
namespace Sound {
static constexpr uint32_t kXWBFlagsStreaming = 0x00000001;
static constexpr uint32_t kXWBFlagsEntryNames = 0x00010000;
static constexpr uint32_t kXWBFlagsCompact = 0x00020000;
static constexpr uint32_t kXWBFlagsSyncDisabled = 0x00040000;
static constexpr uint32_t kXWBFlagsSeekTables = 0x00080000;
static constexpr uint32_t kWaveFlagsReadAhead = 0x00000001; ///< Performance hint: read ahead while streaming.
static constexpr uint32_t kWaveFlagsLoopCache = 0x00000002; ///< Audio file is used by at least one looping sound.
static constexpr uint32_t kWaveFlagsRemoveLoopTail = 0x00000004; ///< Ignore the data after the looping section.
static constexpr uint32_t kWaveFlagsIgnoreLoop = 0x00000008; ///< Don't loop this sound.
XACTWaveBank_Binary::XACTWaveBank_Binary(Common::SeekableReadStream *xwb) : _xwb(xwb) {
assert(_xwb);
load(*_xwb);
}
bool XACTWaveBank_Binary::isStreaming() const {
return (_flags & kXWBFlagsStreaming) != 0;
}
size_t XACTWaveBank_Binary::getWaveCount() const {
return _waves.size();
}
RewindableAudioStream *XACTWaveBank_Binary::getWave(size_t index) const {
if (index >= _waves.size())
throw Common::Exception("XACTWaveBank_Binary::getWave(): Index out of range (%s >= %s)",
Common::composeString(index).c_str(),
Common::composeString(_waves.size()).c_str());
const Wave &wave = _waves[index];
_xwb->seek(wave.offset);
std::unique_ptr<Common::SeekableReadStream> dataStream(_xwb->readStream(wave.size));
switch (wave.codec) {
case Codec::PCM:
return makePCMStream(dataStream.release(), wave.samplingRate,
(wave.bitRate == 16) ? (FLAG_16BITS | FLAG_LITTLE_ENDIAN) : FLAG_UNSIGNED,
wave.channels);
case Codec::ADPCM:
return makeADPCMStream(dataStream.release(), true, dataStream->size(),
kADPCMXbox, wave.samplingRate, wave.channels);
case Codec::WMA:
return makeASFStream(dataStream.release());
default:
throw Common::Exception("XACTWaveBank_Binary::getWave(): Unknown encoding %u",
static_cast<uint>(wave.codec));
}
}
enum Segments {
kSegmentBankData = 0,
kSegmentEntryMetaData ,
kSegmentEntryNames ,
kSegmentWaveData ,
kSegmentMAX
};
struct Segment {
size_t offset;
size_t size;
};
void XACTWaveBank_Binary::load(Common::SeekableReadStream &xwb) {
static constexpr uint32_t kXWBID = MKTAG('W', 'B', 'N', 'D');
const uint32_t id = xwb.readUint32BE();
if (id != kXWBID)
throw Common::Exception("Not a XWB file (%s)", Common::debugTag(id).c_str());
const uint32_t version = xwb.readUint32LE();
if (version != 3)
throw Common::Exception("Unsupported XWB file version %u", version);
Segment segments[kSegmentMAX];
for (size_t i = 0; i < kSegmentMAX; i++) {
segments[i].offset = xwb.readUint32LE();
segments[i].size = xwb.readUint32LE();
}
xwb.seek(segments[kSegmentBankData].offset);
_flags = xwb.readUint32LE();
if (_flags & kXWBFlagsCompact)
throw Common::Exception("XACTWaveBank_Binary::load(): TODO: Compact format");
const size_t waveCount = xwb.readUint32LE();
_name = Common::readStringFixed(xwb, Common::kEncodingASCII, 16);
const size_t waveMetaSize = xwb.readUint32LE();
if (waveMetaSize < 24)
throw Common::Exception("XACTWaveBank_Binary::load(): Wave meta data size too small (%s)",
Common::composeString(waveMetaSize).c_str());
xwb.skip(4); // Size of a wave name
xwb.skip(4); // Alignment
uint32_t indexOffset = segments[kSegmentEntryMetaData].offset;
uint32_t dataOffset = segments[kSegmentWaveData].offset;
if (dataOffset == 0)
dataOffset = indexOffset + waveCount * waveMetaSize;
_waves.resize(waveCount);
for (auto &wave : _waves) {
xwb.seek(indexOffset);
wave.flags = xwb.readUint32LE();
const uint32_t formatCode = xwb.readUint32LE();
wave.offset = xwb.readUint32LE() + dataOffset;
wave.size = xwb.readUint32LE();
wave.loopOffset = xwb.readUint32LE();
wave.loopLength = xwb.readUint32LE();
wave.codec = static_cast<Codec>(((formatCode ) & ((1 << 2) - 1)));
wave.channels = (formatCode >> 2) & ((1 << 3) - 1);
wave.samplingRate = (formatCode >> 5) & ((1 << 18) - 1);
wave.blockAlign = (formatCode >> 23) & ((1 << 8) - 1);
wave.bitRate = ((formatCode >> 31) & ((1 << 1) - 1)) ? 16 : 8;
switch (wave.codec) {
case Codec::PCM:
case Codec::ADPCM:
case Codec::WMA:
break;
default:
throw Common::Exception("XACTWaveBank_Binary::load(): Unknown encoding %u",
static_cast<uint>(wave.codec));
}
indexOffset += waveMetaSize;
}
}
} // End of namespace Sound