-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBnkParser.cpp
321 lines (246 loc) · 9.02 KB
/
BnkParser.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
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
#include "BnkParser.hpp"
#include "utils.hpp"
static constexpr uint32_t MARKER_ENVT = 0x454e5654;
static constexpr uint32_t MARKER_OSCT = 0x4f534354;
static constexpr uint32_t MARKER_RAND = 0x52414e44;
static constexpr uint32_t MARKER_SENS = 0x53454e53;
static constexpr uint32_t MARKER_INST = 0x494e5354;
static constexpr uint32_t MARKER_PMAP = 0x504d4150;
static constexpr uint32_t MARKER_PERC = 0x50455243;
static constexpr uint32_t MARKER_LIST = 0x4c495354;
static constexpr uint32_t MARKER_PERC_ENTRY = 0x50657263;
static constexpr uint32_t MARKER_INST_ENTRY = 0x496e7374;
static constexpr uint32_t MARKER_PMAP_ENTRY = 0x506d6170;
namespace z2sound
{
BnkParser::BnkParser(uint32_t wave_bank_id, std::istream &stream, std::streamoff base_offset, Poco::Logger &logger)
: wave_bank_id_{static_cast<uint8_t>(wave_bank_id)}, base_offset_{base_offset}, size_{0},
reader_{stream, Poco::BinaryReader::BIG_ENDIAN_BYTE_ORDER}, logger_{logger}
{
}
std::optional<InstrumentBank>
BnkParser::parse()
{
uint32_t id{};
uint32_t version{};
reader_ >> size_;
reader_ >> id;
reader_ >> version;
if (version != 1)
{
logger_.warning("Unsupported BNK version %u. Only version 1 is supported", version);
return std::nullopt;
}
logger_.debug("Start parsing BNK (ID %u, wave bank %u, %u bytes)", static_cast<uint32_t>(wave_bank_id_), id, size_);
reader_.stream().seekg(4 * sizeof(uint32_t), std::ios::cur);
enumerate_chunks();
logger_.debug("Found the following chunks:");
for (const auto &[marker, chunk_info] : chunk_table_)
{
logger_.debug("%s (position=%x, size=%u)", marker_to_string(marker),
static_cast<uint32_t>(chunk_info.position), chunk_info.size);
}
std::optional list_chunk = get_chunk_info(MARKER_LIST);
std::optional envt_chunk = get_chunk_info(MARKER_ENVT);
std::optional osct_chunk = get_chunk_info(MARKER_OSCT);
if (!list_chunk.has_value())
{
report_missing_chunk(MARKER_LIST);
return std::nullopt;
}
if (!envt_chunk.has_value())
{
report_missing_chunk(MARKER_ENVT);
return std::nullopt;
}
if (!osct_chunk.has_value())
{
report_missing_chunk(MARKER_OSCT);
return std::nullopt;
}
InstrumentBank bank{static_cast<uint8_t>(id), wave_bank_id_};
reader_.stream().seekg(envt_chunk.value().get().position);
const size_t num_envelopes = envt_chunk.value().get().size / 6;
bank.envelopes_.reserve(num_envelopes);
for (size_t envelope_index = 0; envelope_index < num_envelopes; envelope_index++)
{
Envelope envelope{};
reader_ >> envelope.curve_type;
reader_ >> envelope.time;
reader_ >> envelope.volume;
bank.envelopes_.emplace_back(envelope);
}
uint32_t num_oscillators{};
reader_.stream().seekg(osct_chunk.value().get().position);
reader_ >> num_oscillators;
for (size_t oscillator_index = 0; oscillator_index < num_oscillators; oscillator_index++)
{
Oscillator osci{};
reader_.stream().seekg(sizeof(uint32_t), std::ios::cur);
reader_ >> osci.b1;
reader_.stream().seekg(3, std::ios::cur);
reader_ >> osci.value_multiplier;
uint32_t attack_envt_offset{};
uint32_t release_envt_offset{};
reader_ >> attack_envt_offset;
reader_ >> release_envt_offset;
const size_t attack_envt_index = attack_envt_offset / 6;
const size_t release_envt_index = release_envt_offset / 6;
osci.pre_sustain = bank.envelopes_.cbegin() + attack_envt_index;
osci.post_sustain = bank.envelopes_.cbegin() + release_envt_index;
reader_ >> osci.time_scale;
reader_ >> osci.value_offset;
bank.oscillators_.emplace_back(osci);
}
uint32_t num_instruments{};
reader_.stream().seekg(list_chunk.value().get().position);
reader_ >> num_instruments;
std::streampos instrument_list_position = reader_.stream().tellg();
for (size_t instrument_index = 0; instrument_index < num_instruments; instrument_index++)
{
reader_.stream().seekg(
instrument_list_position + static_cast<std::streamoff>(instrument_index * sizeof(uint32_t)));
uint32_t offset{};
reader_ >> offset;
if (offset == 0)
{
bank.instruments_.emplace_back(std::unique_ptr<Instrument>{nullptr});
continue;
}
reader_.stream().seekg(base_offset_ + offset);
uint32_t marker{};
reader_ >> marker;
if (marker == MARKER_INST_ENTRY)
{
auto instrument = parse_inst(bank.oscillators_);
bank.instruments_.emplace_back(std::make_unique<BasicInstrument>(instrument));
}
else if (marker == MARKER_PERC_ENTRY)
{
auto percussion_set = parse_perc();
bank.instruments_.emplace_back(std::make_unique<PercussionSet>(percussion_set));
}
else
{
logger_.warning("Unknown instrument_index type %s (entry %i)", marker_to_string(marker),
instrument_index);
}
}
logger_.debug("Found %u instruments", num_instruments);
return bank;
}
void
BnkParser::enumerate_chunks()
{
while (reader_.stream().tellg() < base_offset_ + size_)
{
uint32_t marker{};
reader_ >> marker;
if (marker == 0)
{
break;
}
uint32_t size{};
reader_ >> size;
chunk_table_.emplace(marker, ChunkInfo{
.position = reader_.stream().tellg(),
.size = size});
uint32_t aligned_chunk_size = (size + sizeof(uint32_t) - 1) & ~(sizeof(uint32_t) - 1);
reader_.stream().seekg(aligned_chunk_size, std::ios::cur);
}
}
std::optional<std::reference_wrapper<const BnkParser::ChunkInfo>>
BnkParser::get_chunk_info(uint32_t marker) const
{
if (chunk_table_.count(marker) == 1)
{
return std::cref(chunk_table_.at(marker));
}
return std::nullopt;
}
void
BnkParser::report_missing_chunk(uint32_t marker)
{
logger_.error("BNK is missing %s chunk, which is mandatory", marker_to_string(marker));
}
BasicInstrument
BnkParser::parse_inst(const std::vector<Oscillator> &oscillators)
{
BasicInstrument instrument;
uint32_t num_osc_entries{};
reader_ >> num_osc_entries;
for (size_t i = 0; i < num_osc_entries; i++)
{
uint32_t osc_index{};
reader_ >> osc_index;
instrument.oscillator_indices_.push_back(osc_index);
}
instrument.oscillator_ = oscillators.at(instrument.oscillator_indices_[0]);
uint32_t unknown_count{};
reader_ >> unknown_count;
reader_.stream().seekg(unknown_count * sizeof(uint32_t), std::ios::cur);
uint32_t num_key_regions{};
reader_ >> num_key_regions;
for (size_t i = 0; i < num_key_regions; i++)
{
BasicInstrument::KeyRegion key_region{};
reader_ >> key_region.upper_key_limit;
reader_.stream().seekg(3, std::ios::cur);
uint32_t num_velocity_regions{};
reader_ >> num_velocity_regions;
// Yes, this case exists in TP's SE banks
if (num_velocity_regions == 0)
{
continue;
}
reader_.stream().seekg(6, std::ios::cur);
reader_ >> key_region.sample_id;
reader_ >> key_region.volume_multiplier;
reader_ >> key_region.pitch_multiplier;
// Skip any additional velocity region
reader_.stream().seekg((num_velocity_regions - 1) * 12, std::ios::cur);
instrument.key_regions_.emplace_back(key_region);
}
reader_ >> instrument.volume_multiplier_;
reader_ >> instrument.pitch_multiplier_;
return instrument;
}
PercussionSet
BnkParser::parse_perc()
{
PercussionSet percussion_set;
uint32_t num_entries{};
reader_ >> num_entries;
std::streampos list_position = reader_.stream().tellg();
for (size_t entry = 0; entry < num_entries; entry++)
{
reader_.stream().seekg(list_position + static_cast<std::streamoff>(entry * sizeof(uint32_t)));
uint32_t offset{};
reader_ >> offset;
if (offset == 0)
{
continue;
}
reader_.stream().seekg(base_offset_ + offset + sizeof(uint32_t));
PercussionSet::PercussionEntry percussion_entry{};
reader_ >> percussion_entry.volume_multiplier_2;
reader_ >> percussion_entry.pitch_multiplier_2;
reader_ >> percussion_entry.pan;
reader_.stream().seekg(1, std::ios::cur);
reader_ >> percussion_entry.release;
reader_.stream().seekg(4, std::ios::cur);
uint32_t num_velocity_regions{};
reader_ >> num_velocity_regions;
if (num_velocity_regions == 0)
{
continue;
}
reader_.stream().seekg(6, std::ios::cur);
reader_ >> percussion_entry.sample_id;
reader_ >> percussion_entry.volume_multiplier;
reader_ >> percussion_entry.pitch_multiplier;
percussion_set.set_entry(static_cast<Key>(entry), percussion_entry);
}
return percussion_set;
}
}