Debian Codegraph @

BambooTracker/io/btb_io.cpp

raw
1 /*
2 * Copyright (C) 2020-2023 Rerrah
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "btb_io.hpp"
27 #include <vector>
28 #include <set>
29 #include <iterator>
30 #include <algorithm>
31 #include <unordered_map>
32 #include <limits>
33 #include "enum_hash.hpp"
34 #include "version.hpp"
35 #include "instrument.hpp"
36 #include "file_io_error.hpp"
37 #include "io_utils.hpp"
38
39 namespace io
40 {
41 namespace
42 {
43 bool judgeRequiredProperty(const std::multiset<int>& usedInstIdcs, const std::vector<int>& reqInstIdcs)
44 {
45 std::vector<int> intrsct;
46 std::set_intersection(usedInstIdcs.begin(), usedInstIdcs.end(), reqInstIdcs.begin(), reqInstIdcs.end(), std::back_inserter(intrsct));
47 return !intrsct.empty();
48 }
49 }
50
51 BtbIO::BtbIO() : AbstractBankIO("btb", "BambooTracker bank", true, true) {}
52
53 AbstractBank* BtbIO::load(const BinaryContainer& ctr) const
54 {
55 size_t globCsr = 0;
56 if (ctr.readString(globCsr, 16) != "BambooTrackerBnk")
57 throw FileCorruptionError(FileType::Bank, globCsr);
58 globCsr += 16;
59 /*size_t eofOfs = */ctr.readUint32(globCsr);
60 globCsr += 4;
61 size_t fileVersion = ctr.readUint32(globCsr);
62 if (fileVersion > Version::ofBankFileInBCD())
63 throw FileVersionError(FileType::Bank);
64 globCsr += 4;
65
66
67 /***** Instrument section *****/
68 std::vector<int> ids;
69 std::vector<std::string> names;
70 std::vector<BinaryContainer> instCtrs;
71 if (ctr.readString(globCsr, 8) != "INSTRMNT")
72 throw FileCorruptionError(FileType::Bank, globCsr);
73 globCsr += 8;
74 size_t instOfs = ctr.readUint32(globCsr);
75 size_t instCsr = globCsr + 4;
76 uint8_t instCnt = ctr.readUint8(instCsr);
77 instCsr += 1;
78 for (uint8_t i = 0; i < instCnt; ++i) {
79 size_t pos = instCsr;
80 uint8_t idx = ctr.readUint8(instCsr);
81 ids.push_back(idx);
82 instCsr += 1;
83 size_t iOfs = ctr.readUint32(instCsr);
84 size_t iCsr = instCsr + 4;
85 size_t nameLen = ctr.readUint32(iCsr);
86 iCsr += 4;
87 std::string name = u8"";
88 if (nameLen > 0) {
89 name = ctr.readString(iCsr, nameLen);
90 /* iCsr += nameLen; */
91 }
92 names.push_back(name);
93 instCsr += iOfs; // Jump to next
94 instCtrs.push_back(ctr.getSubcontainer(pos, 1 + iOfs));
95 }
96 globCsr += instOfs;
97
98
99 /***** Instrument property section *****/
100 if (ctr.readString(globCsr, 8) != "INSTPROP")
101 throw FileCorruptionError(FileType::Inst, globCsr);
102 globCsr += 8;
103 size_t instPropOfs = ctr.readUint32(globCsr);
104 BinaryContainer propCtr = ctr.getSubcontainer(globCsr + 4, instPropOfs - 4);
105
106 return new BtBank(ids, names, instCtrs, propCtr, fileVersion);
107 }
108
109 void BtbIO::save(BinaryContainer& ctr, const std::weak_ptr<InstrumentsManager> instMan,
110 const std::vector<int>& instNums) const
111 {
112 ctr.appendString("BambooTrackerBnk");
113 size_t eofOfs = ctr.size();
114 ctr.appendUint32(0); // Dummy EOF offset
115 uint32_t fileVersion = Version::ofBankFileInBCD();
116 ctr.appendUint32(fileVersion);
117
118
119 /***** Instrument section *****/
120 ctr.appendString("INSTRMNT");
121 size_t instOfs = ctr.size();
122 ctr.appendUint32(0); // Dummy instrument section offset
123 ctr.appendUint8(static_cast<uint8_t>(instNums.size()));
124 for (auto& idx : instNums) {
125 if (std::shared_ptr<AbstractInstrument> inst = instMan.lock()->getInstrumentSharedPtr(static_cast<int>(idx))) {
126 ctr.appendUint8(static_cast<uint8_t>(idx));
127 size_t iOfs = ctr.size();
128 ctr.appendUint32(0); // Dummy instrument block offset
129 std::string name = inst->getName();
130 ctr.appendUint32(name.length());
131 if (!name.empty()) ctr.appendString(name);
132 switch (inst->getType()) {
133 case InstrumentType::FM:
134 {
135 ctr.appendUint8(0x00);
136 auto instFM = std::dynamic_pointer_cast<InstrumentFM>(inst);
137 ctr.appendUint8(static_cast<uint8_t>(instFM->getEnvelopeNumber()));
138 uint8_t tmp = static_cast<uint8_t>(instFM->getLFONumber());
139 ctr.appendUint8(instFM->getLFOEnabled() ? tmp : (0x80 | tmp));
140 for (auto& param : FM_OPSEQ_PARAMS) {
141 tmp = static_cast<uint8_t>(instFM->getOperatorSequenceNumber(param));
142 ctr.appendUint8(instFM->getOperatorSequenceEnabled(param) ? tmp : (0x80 | tmp));
143 }
144 tmp = static_cast<uint8_t>(instFM->getArpeggioNumber(FMOperatorType::All));
145 ctr.appendUint8(instFM->getArpeggioEnabled(FMOperatorType::All) ? tmp : (0x80 | tmp));
146 tmp = static_cast<uint8_t>(instFM->getPitchNumber(FMOperatorType::All));
147 ctr.appendUint8(instFM->getPitchEnabled(FMOperatorType::All) ? tmp : (0x80 | tmp));
148 tmp = static_cast<uint8_t>(instFM->getEnvelopeResetEnabled(FMOperatorType::All))
149 | static_cast<uint8_t>(instFM->getEnvelopeResetEnabled(FMOperatorType::Op1) << 1)
150 | static_cast<uint8_t>(instFM->getEnvelopeResetEnabled(FMOperatorType::Op2) << 2)
151 | static_cast<uint8_t>(instFM->getEnvelopeResetEnabled(FMOperatorType::Op3) << 3)
152 | static_cast<uint8_t>(instFM->getEnvelopeResetEnabled(FMOperatorType::Op4) << 4);
153 ctr.appendUint8(tmp);
154 for (auto& type : FM_OP_TYPES) {
155 tmp = static_cast<uint8_t>(instFM->getArpeggioNumber(type));
156 ctr.appendUint8(instFM->getArpeggioEnabled(type) ? tmp : (0x80 | tmp));
157 }
158 for (auto& type : FM_OP_TYPES) {
159 tmp = static_cast<uint8_t>(instFM->getPitchNumber(type));
160 ctr.appendUint8(instFM->getPitchEnabled(type) ? tmp : (0x80 | tmp));
161 }
162 tmp = static_cast<uint8_t>(instFM->getPanNumber());
163 ctr.appendUint8(instFM->getPanEnabled() ? tmp : (0x80 | tmp));
164 break;
165 }
166 case InstrumentType::SSG:
167 {
168 ctr.appendUint8(0x01);
169 auto instSSG = std::dynamic_pointer_cast<InstrumentSSG>(inst);
170 uint8_t tmp = static_cast<uint8_t>(instSSG->getWaveformNumber());
171 ctr.appendUint8(instSSG->getWaveformEnabled() ? tmp : (0x80 | tmp));
172 tmp = static_cast<uint8_t>(instSSG->getToneNoiseNumber());
173 ctr.appendUint8(instSSG->getToneNoiseEnabled() ? tmp : (0x80 | tmp));
174 tmp = static_cast<uint8_t>(instSSG->getEnvelopeNumber());
175 ctr.appendUint8(instSSG->getEnvelopeEnabled() ? tmp : (0x80 | tmp));
176 tmp = static_cast<uint8_t>(instSSG->getArpeggioNumber());
177 ctr.appendUint8(instSSG->getArpeggioEnabled() ? tmp : (0x80 | tmp));
178 tmp = static_cast<uint8_t>(instSSG->getPitchNumber());
179 ctr.appendUint8(instSSG->getPitchEnabled() ? tmp : (0x80 | tmp));
180 break;
181 }
182 case InstrumentType::ADPCM:
183 {
184 ctr.appendUint8(0x02);
185 auto instADPCM = std::dynamic_pointer_cast<InstrumentADPCM>(inst);
186 ctr.appendUint8(static_cast<uint8_t>(instADPCM->getSampleNumber()));
187 uint8_t tmp = static_cast<uint8_t>(instADPCM->getEnvelopeNumber());
188 ctr.appendUint8(instADPCM->getEnvelopeEnabled() ? tmp : (0x80 | tmp));
189 tmp = static_cast<uint8_t>(instADPCM->getArpeggioNumber());
190 ctr.appendUint8(instADPCM->getArpeggioEnabled() ? tmp : (0x80 | tmp));
191 tmp = static_cast<uint8_t>(instADPCM->getPitchNumber());
192 ctr.appendUint8(instADPCM->getPitchEnabled() ? tmp : (0x80 | tmp));
193 tmp = static_cast<uint8_t>(instADPCM->getPanNumber());
194 ctr.appendUint8(instADPCM->getPanEnabled() ? tmp : (0x80 | tmp));
195 break;
196 }
197 case InstrumentType::Drumkit:
198 {
199 ctr.appendUint8(0x03);
200 auto instKit = std::dynamic_pointer_cast<InstrumentDrumkit>(inst);
201 std::vector<int> keys = instKit->getAssignedKeys();
202 ctr.appendUint8(static_cast<uint8_t>(keys.size()));
203 for (const int& key : keys) {
204 ctr.appendUint8(static_cast<uint8_t>(key));
205 ctr.appendUint8(static_cast<uint8_t>(instKit->getSampleNumber(key)));
206 ctr.appendInt8(static_cast<int8_t>(instKit->getPitch(key)));
207 ctr.appendUint8(static_cast<uint8_t>(instKit->getPan(key)));
208 }
209 break;
210 }
211 }
212 ctr.writeUint32(iOfs, ctr.size() - iOfs);
213 }
214 }
215 ctr.writeUint32(instOfs, ctr.size() - instOfs);
216
217
218 /***** Instrument property section *****/
219 ctr.appendString("INSTPROP");
220 size_t instPropOfs = ctr.size();
221 ctr.appendUint32(0); // Dummy instrument property section offset
222
223 // FM envelope
224 std::vector<int> envFMIdcs;
225 for (auto& idx : instMan.lock()->getEnvelopeFMEntriedIndices()) {
226 if (judgeRequiredProperty(instMan.lock()->getEnvelopeFMUsers(idx), instNums))
227 envFMIdcs.push_back(idx);
228 }
229 if (!envFMIdcs.empty()) {
230 ctr.appendUint8(0x00);
231 ctr.appendUint8(static_cast<uint8_t>(envFMIdcs.size()));
232 for (auto& idx : envFMIdcs) {
233 ctr.appendUint8(static_cast<uint8_t>(idx));
234 size_t ofs = ctr.size();
235 ctr.appendUint8(0); // Dummy offset
236 uint8_t tmp = static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, FMEnvelopeParameter::AL) << 4)
237 | static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, FMEnvelopeParameter::FB));
238 ctr.appendUint8(tmp);
239 for (int op = 0; op < 4; ++op) { // Operator
240 auto& params = FM_OP_PARAMS[op];
241 tmp = instMan.lock()->getEnvelopeFMOperatorEnabled(idx, op);
242 tmp = static_cast<uint8_t>((tmp << 5))
243 | static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::AR)));
244 ctr.appendUint8(tmp);
245 tmp = static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::KS)) << 5)
246 | static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::DR)));
247 ctr.appendUint8(tmp);
248 tmp = static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::DT)) << 5)
249 | static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::SR)));
250 ctr.appendUint8(tmp);
251 tmp = static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::SL)) << 4)
252 | static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::RR)));
253 ctr.appendUint8(tmp);
254 tmp = static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::TL)));
255 ctr.appendUint8(tmp);
256 int tmp2 = instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::SSGEG));
257 tmp = ((tmp2 == -1) ? 0x80 : static_cast<uint8_t>(tmp2 << 4))
258 | static_cast<uint8_t>(instMan.lock()->getEnvelopeFMParameter(idx, params.at(FMOperatorParameter::ML)));
259 ctr.appendUint8(tmp);
260 }
261 ctr.writeUint8(ofs, static_cast<uint8_t>(ctr.size() - ofs));
262 }
263 }
264
265 // FM LFO
266 std::vector<int> lfoFMIdcs;
267 for (auto& idx : instMan.lock()->getLFOFMEntriedIndices()) {
268 if (judgeRequiredProperty(instMan.lock()->getLFOFMUsers(idx), instNums))
269 lfoFMIdcs.push_back(idx);
270 }
271 if (!lfoFMIdcs.empty()) {
272 ctr.appendUint8(0x01);
273 ctr.appendUint8(static_cast<uint8_t>(lfoFMIdcs.size()));
274 for (auto& idx : lfoFMIdcs) {
275 ctr.appendUint8(static_cast<uint8_t>(idx));
276 size_t ofs = ctr.size();
277 ctr.appendUint8(0); // Dummy offset
278 uint8_t tmp = static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::FREQ) << 4)
279 | static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::PMS));
280 ctr.appendUint8(tmp);
281 tmp = static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::AM4) << 7)
282 | static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::AM3) << 6)
283 | static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::AM2) << 5)
284 | static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::AM1) << 4)
285 | static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::AMS));
286 ctr.appendUint8(tmp);
287 tmp = static_cast<uint8_t>(instMan.lock()->getLFOFMparameter(idx, FMLFOParameter::Count));
288 ctr.appendUint8(tmp);
289 ctr.writeUint8(ofs, static_cast<uint8_t>(ctr.size() - ofs));
290 }
291 }
292
293 // FM envelope parameter
294 for (size_t i = 0; i < 38; ++i) {
295 std::vector<int> idcs;
296 for (auto& idx : instMan.lock()->getOperatorSequenceFMEntriedIndices(FM_OPSEQ_PARAMS[i])) {
297 if (judgeRequiredProperty(instMan.lock()->getOperatorSequenceFMUsers(FM_OPSEQ_PARAMS[i], idx), instNums))
298 idcs.push_back(idx);
299 }
300 if (!idcs.empty()) {
301 ctr.appendUint8(0x02 + static_cast<uint8_t>(i));
302 ctr.appendUint8(static_cast<uint8_t>(idcs.size()));
303 for (auto& idx : idcs) {
304 ctr.appendUint8(static_cast<uint8_t>(idx));
305 size_t ofs = ctr.size();
306 ctr.appendUint16(0); // Dummy offset
307 auto seq = instMan.lock()->getOperatorSequenceFMSequence(FM_OPSEQ_PARAMS[i], idx);
308 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
309 for (auto& unit : seq) {
310 ctr.appendUint16(static_cast<uint16_t>(unit.data));
311 }
312 auto loops = instMan.lock()->getOperatorSequenceFMLoopRoot(FM_OPSEQ_PARAMS[i], idx).getAllLoops();
313 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
314 for (auto& loop : loops) {
315 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
316 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
317 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
318 }
319 auto release = instMan.lock()->getOperatorSequenceFMRelease(FM_OPSEQ_PARAMS[i], idx);
320 switch (release.getType()) {
321 case InstrumentSequenceRelease::NoRelease:
322 ctr.appendUint8(0x00);
323 break;
324 case InstrumentSequenceRelease::FixedRelease:
325 ctr.appendUint8(0x01);
326 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
327 break;
328 case InstrumentSequenceRelease::AbsoluteRelease:
329 ctr.appendUint8(0x02);
330 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
331 break;
332 case InstrumentSequenceRelease::RelativeRelease:
333 ctr.appendUint8(0x03);
334 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
335 break;
336 }
337 ctr.appendUint8(0); // Skip sequence type
338 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
339 }
340 }
341 }
342
343 // FM arpeggio
344 std::vector<int> arpFMIdcs;
345 for (auto& idx : instMan.lock()->getArpeggioFMEntriedIndices()) {
346 if (judgeRequiredProperty(instMan.lock()->getArpeggioFMUsers(idx), instNums))
347 arpFMIdcs.push_back(idx);
348 }
349 if (!arpFMIdcs.empty()) {
350 ctr.appendUint8(0x28);
351 ctr.appendUint8(static_cast<uint8_t>(arpFMIdcs.size()));
352 for (auto& idx : arpFMIdcs) {
353 ctr.appendUint8(static_cast<uint8_t>(idx));
354 size_t ofs = ctr.size();
355 ctr.appendUint16(0); // Dummy offset
356 auto seq = instMan.lock()->getArpeggioFMSequence(idx);
357 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
358 for (auto& unit : seq) {
359 ctr.appendUint16(static_cast<uint16_t>(unit.data));
360 }
361 auto loops = instMan.lock()->getArpeggioFMLoopRoot(idx).getAllLoops();
362 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
363 for (auto& loop : loops) {
364 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
365 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
366 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
367 }
368 auto release = instMan.lock()->getArpeggioFMRelease(idx);
369 switch (release.getType()) {
370 case InstrumentSequenceRelease::NoRelease:
371 ctr.appendUint8(0x00);
372 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
373 break;
374 case InstrumentSequenceRelease::FixedRelease:
375 ctr.appendUint8(0x01);
376 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
377 break;
378 case InstrumentSequenceRelease::AbsoluteRelease:
379 ctr.appendUint8(0x02);
380 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
381 break;
382 case InstrumentSequenceRelease::RelativeRelease:
383 ctr.appendUint8(0x03);
384 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
385 break;
386 }
387 switch (instMan.lock()->getArpeggioFMType(idx)) {
388 case SequenceType::AbsoluteSequence: ctr.appendUint8(0x00); break;
389 case SequenceType::FixedSequence: ctr.appendUint8(0x01); break;
390 case SequenceType::RelativeSequence: ctr.appendUint8(0x02); break;
391 default: break;
392 }
393 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
394 }
395 }
396
397 // FM pitch
398 std::vector<int> ptFMIdcs;
399 for (auto& idx : instMan.lock()->getPitchFMEntriedIndices()) {
400 if (judgeRequiredProperty(instMan.lock()->getPitchFMUsers(idx), instNums))
401 ptFMIdcs.push_back(idx);
402 }
403 if (!ptFMIdcs.empty()) {
404 ctr.appendUint8(0x29);
405 ctr.appendUint8(static_cast<uint8_t>(ptFMIdcs.size()));
406 for (auto& idx : ptFMIdcs) {
407 ctr.appendUint8(static_cast<uint8_t>(idx));
408 size_t ofs = ctr.size();
409 ctr.appendUint16(0); // Dummy offset
410 auto seq = instMan.lock()->getPitchFMSequence(idx);
411 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
412 for (auto& unit : seq) {
413 ctr.appendUint16(static_cast<uint16_t>(unit.data));
414 }
415 auto loops = instMan.lock()->getPitchFMLoopRoot(idx).getAllLoops();
416 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
417 for (auto& loop : loops) {
418 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
419 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
420 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
421 }
422 auto release = instMan.lock()->getPitchFMRelease(idx);
423 switch (release.getType()) {
424 case InstrumentSequenceRelease::NoRelease:
425 ctr.appendUint8(0x00);
426 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
427 break;
428 case InstrumentSequenceRelease::FixedRelease:
429 ctr.appendUint8(0x01);
430 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
431 break;
432 case InstrumentSequenceRelease::AbsoluteRelease:
433 ctr.appendUint8(0x02);
434 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
435 break;
436 case InstrumentSequenceRelease::RelativeRelease:
437 ctr.appendUint8(0x03);
438 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
439 break;
440 }
441 switch (instMan.lock()->getPitchFMType(idx)) {
442 case SequenceType::AbsoluteSequence: ctr.appendUint8(0x00); break;
443 case SequenceType::RelativeSequence: ctr.appendUint8(0x02); break;
444 default: break;
445 }
446 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
447 }
448 }
449
450 // FM pan
451 std::vector<int> panFMIdcs;
452 for (auto& idx : instMan.lock()->getPanFMEntriedIndices()) {
453 if (judgeRequiredProperty(instMan.lock()->getPanFMUsers(idx), instNums))
454 panFMIdcs.push_back(idx);
455 }
456 if (!panFMIdcs.empty()) {
457 ctr.appendUint8(0x2a);
458 ctr.appendUint8(static_cast<uint8_t>(panFMIdcs.size()));
459 for (auto& idx : panFMIdcs) {
460 ctr.appendUint8(static_cast<uint8_t>(idx));
461 size_t ofs = ctr.size();
462 ctr.appendUint16(0); // Dummy offset
463 auto seq = instMan.lock()->getPanFMSequence(idx);
464 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
465 for (auto& unit : seq) {
466 ctr.appendUint16(static_cast<uint16_t>(unit.data));
467 }
468 auto loops = instMan.lock()->getPanFMLoopRoot(idx).getAllLoops();
469 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
470 for (auto& loop : loops) {
471 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
472 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
473 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
474 }
475 auto release = instMan.lock()->getPanFMRelease(idx);
476 switch (release.getType()) {
477 case InstrumentSequenceRelease::NoRelease:
478 ctr.appendUint8(0x00);
479 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
480 break;
481 case InstrumentSequenceRelease::FixedRelease:
482 ctr.appendUint8(0x01);
483 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
484 break;
485 case InstrumentSequenceRelease::AbsoluteRelease:
486 ctr.appendUint8(0x02);
487 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
488 break;
489 case InstrumentSequenceRelease::RelativeRelease:
490 ctr.appendUint8(0x03);
491 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
492 break;
493 }
494 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
495 }
496 }
497
498 // SSG waveform
499 std::vector<int> wfSSGIdcs;
500 for (auto& idx : instMan.lock()->getWaveformSSGEntriedIndices()) {
501 if (judgeRequiredProperty(instMan.lock()->getWaveformSSGUsers(idx), instNums))
502 wfSSGIdcs.push_back(idx);
503 }
504 if (!wfSSGIdcs.empty()) {
505 ctr.appendUint8(0x30);
506 ctr.appendUint8(static_cast<uint8_t>(wfSSGIdcs.size()));
507 for (auto& idx : wfSSGIdcs) {
508 ctr.appendUint8(static_cast<uint8_t>(idx));
509 size_t ofs = ctr.size();
510 ctr.appendUint16(0); // Dummy offset
511 auto seq = instMan.lock()->getWaveformSSGSequence(idx);
512 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
513 for (auto& unit : seq) {
514 ctr.appendUint16(static_cast<uint16_t>(unit.data));
515 ctr.appendInt32(static_cast<int32_t>(unit.subdata));
516 }
517 auto loops = instMan.lock()->getWaveformSSGLoopRoot(idx).getAllLoops();
518 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
519 for (auto& loop : loops) {
520 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
521 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
522 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
523 }
524 auto release = instMan.lock()->getWaveformSSGRelease(idx);
525 switch (release.getType()) {
526 case InstrumentSequenceRelease::NoRelease:
527 ctr.appendUint8(0x00);
528 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
529 break;
530 case InstrumentSequenceRelease::FixedRelease:
531 ctr.appendUint8(0x01);
532 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
533 break;
534 case InstrumentSequenceRelease::AbsoluteRelease:
535 ctr.appendUint8(0x02);
536 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
537 break;
538 case InstrumentSequenceRelease::RelativeRelease:
539 ctr.appendUint8(0x03);
540 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
541 break;
542 }
543 ctr.appendUint8(0); // Skip sequence type
544 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
545 }
546 }
547
548 // SSG tone/noise
549 std::vector<int> tnSSGIdcs;
550 for (auto& idx : instMan.lock()->getToneNoiseSSGEntriedIndices()) {
551 if (judgeRequiredProperty(instMan.lock()->getToneNoiseSSGUsers(idx), instNums))
552 tnSSGIdcs.push_back(idx);
553 }
554 if (!tnSSGIdcs.empty()) {
555 ctr.appendUint8(0x31);
556 ctr.appendUint8(static_cast<uint8_t>(tnSSGIdcs.size()));
557 for (auto& idx : tnSSGIdcs) {
558 ctr.appendUint8(static_cast<uint8_t>(idx));
559 size_t ofs = ctr.size();
560 ctr.appendUint16(0); // Dummy offset
561 auto seq = instMan.lock()->getToneNoiseSSGSequence(idx);
562 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
563 for (auto& unit : seq) {
564 ctr.appendUint16(static_cast<uint16_t>(unit.data));
565 }
566 auto loops = instMan.lock()->getToneNoiseSSGLoopRoot(idx).getAllLoops();
567 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
568 for (auto& loop : loops) {
569 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
570 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
571 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
572 }
573 auto release = instMan.lock()->getToneNoiseSSGRelease(idx);
574 switch (release.getType()) {
575 case InstrumentSequenceRelease::NoRelease:
576 ctr.appendUint8(0x00);
577 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
578 break;
579 case InstrumentSequenceRelease::FixedRelease:
580 ctr.appendUint8(0x01);
581 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
582 break;
583 case InstrumentSequenceRelease::AbsoluteRelease:
584 ctr.appendUint8(0x02);
585 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
586 break;
587 case InstrumentSequenceRelease::RelativeRelease:
588 ctr.appendUint8(0x03);
589 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
590 break;
591 }
592 ctr.appendUint8(0); // Skip sequence type
593 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
594 }
595 }
596
597 // SSG envelope
598 std::vector<int> envSSGIdcs;
599 for (auto& idx : instMan.lock()->getEnvelopeSSGEntriedIndices()) {
600 if (judgeRequiredProperty(instMan.lock()->getEnvelopeSSGUsers(idx), instNums))
601 envSSGIdcs.push_back(idx);
602 }
603 if (!envSSGIdcs.empty()) {
604 ctr.appendUint8(0x32);
605 ctr.appendUint8(static_cast<uint8_t>(envSSGIdcs.size()));
606 for (auto& idx : envSSGIdcs) {
607 ctr.appendUint8(static_cast<uint8_t>(idx));
608 size_t ofs = ctr.size();
609 ctr.appendUint16(0); // Dummy offset
610 auto seq = instMan.lock()->getEnvelopeSSGSequence(idx);
611 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
612 for (auto& unit : seq) {
613 ctr.appendUint16(static_cast<uint16_t>(unit.data));
614 ctr.appendInt32(static_cast<int32_t>(unit.subdata));
615 }
616 auto loops = instMan.lock()->getEnvelopeSSGLoopRoot(idx).getAllLoops();
617 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
618 for (auto& loop : loops) {
619 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
620 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
621 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
622 }
623 auto release = instMan.lock()->getEnvelopeSSGRelease(idx);
624
625 switch (release.getType()) {
626 case InstrumentSequenceRelease::NoRelease:
627 ctr.appendUint8(0x00);
628 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
629 break;
630 case InstrumentSequenceRelease::FixedRelease:
631 ctr.appendUint8(0x01);
632 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
633 break;
634 case InstrumentSequenceRelease::AbsoluteRelease:
635 ctr.appendUint8(0x02);
636 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
637 break;
638 case InstrumentSequenceRelease::RelativeRelease:
639 ctr.appendUint8(0x03);
640 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
641 break;
642 }
643 ctr.appendUint8(0); // Skip sequence type
644 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
645 }
646 }
647
648 // SSG arpeggio
649 std::vector<int> arpSSGIdcs;
650 for (auto& idx : instMan.lock()->getArpeggioSSGEntriedIndices()) {
651 if (judgeRequiredProperty(instMan.lock()->getArpeggioSSGUsers(idx), instNums))
652 arpSSGIdcs.push_back(idx);
653 }
654 if (!arpSSGIdcs.empty()) {
655 ctr.appendUint8(0x33);
656 ctr.appendUint8(static_cast<uint8_t>(arpSSGIdcs.size()));
657 for (auto& idx : arpSSGIdcs) {
658 ctr.appendUint8(static_cast<uint8_t>(idx));
659 size_t ofs = ctr.size();
660 ctr.appendUint16(0); // Dummy offset
661 auto seq = instMan.lock()->getArpeggioSSGSequence(idx);
662 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
663 for (auto& unit : seq) {
664 ctr.appendUint16(static_cast<uint16_t>(unit.data));
665 }
666 auto loops = instMan.lock()->getArpeggioSSGLoopRoot(idx).getAllLoops();
667 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
668 for (auto& loop : loops) {
669 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
670 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
671 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
672 }
673 auto release = instMan.lock()->getArpeggioSSGRelease(idx);
674 switch (release.getType()) {
675 case InstrumentSequenceRelease::NoRelease:
676 ctr.appendUint8(0x00);
677 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
678 break;
679 case InstrumentSequenceRelease::FixedRelease:
680 ctr.appendUint8(0x01);
681 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
682 break;
683 case InstrumentSequenceRelease::AbsoluteRelease:
684 ctr.appendUint8(0x02);
685 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
686 break;
687 case InstrumentSequenceRelease::RelativeRelease:
688 ctr.appendUint8(0x03);
689 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
690 break;
691 }
692 switch (instMan.lock()->getArpeggioSSGType(idx)) {
693 case SequenceType::AbsoluteSequence: ctr.appendUint8(0x00); break;
694 case SequenceType::FixedSequence: ctr.appendUint8(0x01); break;
695 case SequenceType::RelativeSequence: ctr.appendUint8(0x02); break;
696 default: break;
697 }
698 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
699 }
700 }
701
702 // SSG pitch
703 std::vector<int> ptSSGIdcs;
704 for (auto& idx : instMan.lock()->getPitchSSGEntriedIndices()) {
705 if (judgeRequiredProperty(instMan.lock()->getPitchSSGUsers(idx), instNums))
706 ptSSGIdcs.push_back(idx);
707 }
708 if (!ptSSGIdcs.empty()) {
709 ctr.appendUint8(0x34);
710 ctr.appendUint8(static_cast<uint8_t>(ptSSGIdcs.size()));
711 for (auto& idx : ptSSGIdcs) {
712 ctr.appendUint8(static_cast<uint8_t>(idx));
713 size_t ofs = ctr.size();
714 ctr.appendUint16(0); // Dummy offset
715 auto seq = instMan.lock()->getPitchSSGSequence(idx);
716 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
717 for (auto& unit : seq) {
718 ctr.appendUint16(static_cast<uint16_t>(unit.data));
719 }
720 auto loops = instMan.lock()->getPitchSSGLoopRoot(idx).getAllLoops();
721 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
722 for (auto& loop : loops) {
723 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
724 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
725 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
726 }
727 auto release = instMan.lock()->getPitchSSGRelease(idx);
728 switch (release.getType()) {
729 case InstrumentSequenceRelease::NoRelease:
730 ctr.appendUint8(0x00);
731 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
732 break;
733 case InstrumentSequenceRelease::FixedRelease:
734 ctr.appendUint8(0x01);
735 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
736 break;
737 case InstrumentSequenceRelease::AbsoluteRelease:
738 ctr.appendUint8(0x02);
739 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
740 break;
741 case InstrumentSequenceRelease::RelativeRelease:
742 ctr.appendUint8(0x03);
743 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
744 break;
745 }
746 switch (instMan.lock()->getPitchSSGType(idx)) {
747 case SequenceType::AbsoluteSequence: ctr.appendUint8(0x00); break;
748 case SequenceType::RelativeSequence: ctr.appendUint8(0x02); break;
749 default: break;
750 }
751 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
752 }
753 }
754
755 // ADPCM sample
756 std::vector<int> sampADPCMIdcs;
757 for (auto& idx : instMan.lock()->getSampleADPCMEntriedIndices()) {
758 if (judgeRequiredProperty(instMan.lock()->getSampleADPCMUsers(idx), instNums))
759 sampADPCMIdcs.push_back(idx);
760 }
761 if (!sampADPCMIdcs.empty()) {
762 ctr.appendUint8(0x40);
763 ctr.appendUint8(static_cast<uint8_t>(sampADPCMIdcs.size()));
764 for (auto& idx : sampADPCMIdcs) {
765 ctr.appendUint8(static_cast<uint8_t>(idx));
766 size_t ofs = ctr.size();
767 ctr.appendUint32(0); // Dummy offset
768 ctr.appendUint8(static_cast<uint8_t>(instMan.lock()->getSampleADPCMRootKeyNumber(idx)));
769 ctr.appendUint16(static_cast<uint16_t>(instMan.lock()->getSampleADPCMRootDeltaN(idx)));
770 ctr.appendUint8(static_cast<uint8_t>(instMan.lock()->isSampleADPCMRepeatable(idx)));
771 std::vector<uint8_t> samples = instMan.lock()->getSampleADPCMRawSample(idx);
772 ctr.appendUint32(samples.size());
773 ctr.appendVector(samples);
774 SampleRepeatRange range = instMan.lock()->getSampleADPCMRepeatRange(idx);
775 ctr.appendUint16(range.first());
776 ctr.appendUint16(range.last());
777 ctr.writeUint32(ofs, ctr.size() - ofs);
778 }
779 }
780
781 // ADPCM envelope
782 std::vector<int> envADPCMIdcs;
783 for (auto& idx : instMan.lock()->getEnvelopeADPCMEntriedIndices()) {
784 if (judgeRequiredProperty(instMan.lock()->getEnvelopeADPCMUsers(idx), instNums))
785 envADPCMIdcs.push_back(idx);
786 }
787 if (!envADPCMIdcs.empty()) {
788 ctr.appendUint8(0x41);
789 ctr.appendUint8(static_cast<uint8_t>(envADPCMIdcs.size()));
790 for (auto& idx : envADPCMIdcs) {
791 ctr.appendUint8(static_cast<uint8_t>(idx));
792 size_t ofs = ctr.size();
793 ctr.appendUint16(0); // Dummy offset
794 auto seq = instMan.lock()->getEnvelopeADPCMSequence(idx);
795 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
796 for (auto& unit : seq) {
797 ctr.appendUint16(static_cast<uint16_t>(unit.data));
798 }
799 auto loops = instMan.lock()->getEnvelopeADPCMLoopRoot(idx).getAllLoops();
800 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
801 for (auto& loop : loops) {
802 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
803 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
804 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
805 }
806 auto release = instMan.lock()->getEnvelopeADPCMRelease(idx);
807
808 switch (release.getType()) {
809 case InstrumentSequenceRelease::NoRelease:
810 ctr.appendUint8(0x00);
811 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
812 break;
813 case InstrumentSequenceRelease::FixedRelease:
814 ctr.appendUint8(0x01);
815 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
816 break;
817 case InstrumentSequenceRelease::AbsoluteRelease:
818 ctr.appendUint8(0x02);
819 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
820 break;
821 case InstrumentSequenceRelease::RelativeRelease:
822 ctr.appendUint8(0x03);
823 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
824 break;
825 }
826 ctr.appendUint8(0); // Skip sequence type
827 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
828 }
829 }
830
831 // ADPCM arpeggio
832 std::vector<int> arpADPCMIdcs;
833 for (auto& idx : instMan.lock()->getArpeggioADPCMEntriedIndices()) {
834 if (judgeRequiredProperty(instMan.lock()->getArpeggioADPCMUsers(idx), instNums))
835 arpADPCMIdcs.push_back(idx);
836 }
837 if (!arpADPCMIdcs.empty()) {
838 ctr.appendUint8(0x42);
839 ctr.appendUint8(static_cast<uint8_t>(arpADPCMIdcs.size()));
840 for (auto& idx : arpADPCMIdcs) {
841 ctr.appendUint8(static_cast<uint8_t>(idx));
842 size_t ofs = ctr.size();
843 ctr.appendUint16(0); // Dummy offset
844 auto seq = instMan.lock()->getArpeggioADPCMSequence(idx);
845 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
846 for (auto& unit : seq) {
847 ctr.appendUint16(static_cast<uint16_t>(unit.data));
848 }
849 auto loops = instMan.lock()->getArpeggioADPCMLoopRoot(idx).getAllLoops();
850 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
851 for (auto& loop : loops) {
852 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
853 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
854 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
855 }
856 auto release = instMan.lock()->getArpeggioADPCMRelease(idx);
857 switch (release.getType()) {
858 case InstrumentSequenceRelease::NoRelease:
859 ctr.appendUint8(0x00);
860 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
861 break;
862 case InstrumentSequenceRelease::FixedRelease:
863 ctr.appendUint8(0x01);
864 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
865 break;
866 case InstrumentSequenceRelease::AbsoluteRelease:
867 ctr.appendUint8(0x02);
868 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
869 break;
870 case InstrumentSequenceRelease::RelativeRelease:
871 ctr.appendUint8(0x03);
872 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
873 break;
874 }
875 switch (instMan.lock()->getArpeggioADPCMType(idx)) {
876 case SequenceType::AbsoluteSequence: ctr.appendUint8(0x00); break;
877 case SequenceType::FixedSequence: ctr.appendUint8(0x01); break;
878 case SequenceType::RelativeSequence: ctr.appendUint8(0x02); break;
879 default: break;
880 }
881 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
882 }
883 }
884
885 // ADPCM pitch
886 std::vector<int> ptADPCMIdcs;
887 for (auto& idx : instMan.lock()->getPitchADPCMEntriedIndices()) {
888 if (judgeRequiredProperty(instMan.lock()->getPitchADPCMUsers(idx), instNums))
889 ptADPCMIdcs.push_back(idx);
890 }
891 if (!ptADPCMIdcs.empty()) {
892 ctr.appendUint8(0x43);
893 ctr.appendUint8(static_cast<uint8_t>(ptADPCMIdcs.size()));
894 for (auto& idx : ptADPCMIdcs) {
895 ctr.appendUint8(static_cast<uint8_t>(idx));
896 size_t ofs = ctr.size();
897 ctr.appendUint16(0); // Dummy offset
898 auto seq = instMan.lock()->getPitchADPCMSequence(idx);
899 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
900 for (auto& unit : seq) {
901 ctr.appendUint16(static_cast<uint16_t>(unit.data));
902 }
903 auto loops = instMan.lock()->getPitchADPCMLoopRoot(idx).getAllLoops();
904 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
905 for (auto& loop : loops) {
906 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
907 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
908 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
909 }
910 auto release = instMan.lock()->getPitchADPCMRelease(idx);
911 switch (release.getType()) {
912 case InstrumentSequenceRelease::NoRelease:
913 ctr.appendUint8(0x00);
914 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
915 break;
916 case InstrumentSequenceRelease::FixedRelease:
917 ctr.appendUint8(0x01);
918 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
919 break;
920 case InstrumentSequenceRelease::AbsoluteRelease:
921 ctr.appendUint8(0x02);
922 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
923 break;
924 case InstrumentSequenceRelease::RelativeRelease:
925 ctr.appendUint8(0x03);
926 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
927 break;
928 }
929 switch (instMan.lock()->getPitchADPCMType(idx)) {
930 case SequenceType::AbsoluteSequence: ctr.appendUint8(0x00); break;
931 case SequenceType::RelativeSequence: ctr.appendUint8(0x02); break;
932 default: break;
933 }
934 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
935 }
936 }
937
938 // ADPCM pan
939 std::vector<int> panADPCMIdcs;
940 for (auto& idx : instMan.lock()->getPanADPCMEntriedIndices()) {
941 if (judgeRequiredProperty(instMan.lock()->getPanADPCMUsers(idx), instNums))
942 panADPCMIdcs.push_back(idx);
943 }
944 if (!panADPCMIdcs.empty()) {
945 ctr.appendUint8(0x34);
946 ctr.appendUint8(static_cast<uint8_t>(panADPCMIdcs.size()));
947 for (auto& idx : panADPCMIdcs) {
948 ctr.appendUint8(static_cast<uint8_t>(idx));
949 size_t ofs = ctr.size();
950 ctr.appendUint16(0); // Dummy offset
951 auto seq = instMan.lock()->getPanADPCMSequence(idx);
952 ctr.appendUint16(static_cast<uint16_t>(seq.size()));
953 for (auto& unit : seq) {
954 ctr.appendUint16(static_cast<uint16_t>(unit.data));
955 }
956 auto loops = instMan.lock()->getPanADPCMLoopRoot(idx).getAllLoops();
957 ctr.appendUint16(static_cast<uint16_t>(loops.size()));
958 for (auto& loop : loops) {
959 ctr.appendUint16(static_cast<uint16_t>(loop.getBeginPos()));
960 ctr.appendUint16(static_cast<uint16_t>(loop.getEndPos()));
961 ctr.appendUint8(static_cast<uint8_t>(loop.getTimes()));
962 }
963 auto release = instMan.lock()->getPanADPCMRelease(idx);
964 switch (release.getType()) {
965 case InstrumentSequenceRelease::NoRelease:
966 ctr.appendUint8(0x00);
967 // If release.type is NO_RELEASE, then release.begin == -1 so omit to save it.
968 break;
969 case InstrumentSequenceRelease::FixedRelease:
970 ctr.appendUint8(0x01);
971 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
972 break;
973 case InstrumentSequenceRelease::AbsoluteRelease:
974 ctr.appendUint8(0x02);
975 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
976 break;
977 case InstrumentSequenceRelease::RelativeRelease:
978 ctr.appendUint8(0x03);
979 ctr.appendUint16(static_cast<uint16_t>(release.getBeginPos()));
980 break;
981 }
982 ctr.writeUint16(ofs, static_cast<uint16_t>(ctr.size() - ofs));
983 }
984 }
985
986 ctr.writeUint32(instPropOfs, ctr.size() - instPropOfs);
987
988 ctr.writeUint32(eofOfs, ctr.size() - eofOfs);
989 }
990
991 namespace
992 {
993 size_t getPropertyPosition(const BinaryContainer& propCtr, uint8_t subsecType, uint8_t index)
994 {
995 size_t csr = 0;
996
997 while (csr < propCtr.size()) {
998 uint8_t type = propCtr.readUint8(csr++);
999 bool isSection = (type == subsecType);
1000 size_t bcnt = propCtr.readUint8(csr++);
1001 for (size_t i = 0; i < bcnt; ++i) {
1002 if (isSection) {
1003 if (propCtr.readUint8(csr++) == index) {
1004 switch (type) {
1005 case 0x00: // FM envelope
1006 case 0x01: // FM LFO
1007 csr += 1;
1008 break;
1009 case 0x40: // ADPCM sample
1010 csr += 4;
1011 break;
1012 default: // Sequence
1013 csr += 2;
1014 break;
1015 }
1016 return csr;
1017 }
1018 else {
1019 switch (type) {
1020 case 0x00: // FM envelope
1021 case 0x01: // FM LFO
1022 csr += propCtr.readUint8(csr);
1023 break;
1024 case 0x40: // ADPCM sample
1025 csr += propCtr.readUint32(csr);
1026 break;
1027 default: // Sequence
1028 csr += propCtr.readUint16(csr);
1029 break;
1030 }
1031 }
1032 }
1033 else {
1034 ++csr; // Skip index
1035 switch (type) {
1036 case 0x00: // FM envelope
1037 case 0x01: // FM LFO
1038 csr += propCtr.readUint8(csr);
1039 break;
1040 case 0x40: // ADPCM sample
1041 csr += propCtr.readUint32(csr);
1042 break;
1043 default: // Sequence
1044 csr += propCtr.readUint16(csr);
1045 break;
1046 }
1047 }
1048 }
1049 }
1050
1051 return std::numeric_limits<size_t>::max();
1052 }
1053 }
1054
1055 AbstractInstrument* BtbIO::loadInstrument(const BinaryContainer& instCtr,
1056 const BinaryContainer& propCtr,
1057 std::weak_ptr<InstrumentsManager> instMan,
1058 int instNum, uint32_t bankVersion)
1059 {
1060 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
1061 size_t instCsr = 5; // Skip instrument id and offset
1062 size_t nameLen = instCtr.readUint32(instCsr);
1063 instCsr += 4;
1064 std::string name = u8"";
1065 if (nameLen > 0) {
1066 name = instCtr.readString(instCsr, nameLen);
1067 instCsr += nameLen;
1068 }
1069
1070 switch (instCtr.readUint8(instCsr++)) {
1071 case 0x00: // FM
1072 {
1073 auto fm = new InstrumentFM(instNum, name, instManLocked.get());
1074
1075 /* Envelope */
1076 {
1077 auto orgEnvNum = instCtr.readUint8(instCsr++);
1078 int envNum = instManLocked->findFirstAssignableEnvelopeFM();
1079 if (envNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1080 fm->setEnvelopeNumber(envNum);
1081 size_t envCsr = getPropertyPosition(propCtr, 0x00, orgEnvNum);
1082 if (envCsr != std::numeric_limits<size_t>::max()) {
1083 uint8_t tmp = propCtr.readUint8(envCsr++);
1084 instManLocked->setEnvelopeFMParameter(envNum, FMEnvelopeParameter::AL, tmp >> 4);
1085 instManLocked->setEnvelopeFMParameter(envNum, FMEnvelopeParameter::FB, tmp & 0x0f);
1086 for (int op = 0; op < 4; ++op) {
1087 auto& params = FM_OP_PARAMS[op];
1088 tmp = propCtr.readUint8(envCsr++);
1089 instManLocked->setEnvelopeFMOperatorEnabled(envNum, op, (0x20 & tmp) ? true : false);
1090 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::AR), tmp & 0x1f);
1091 tmp = propCtr.readUint8(envCsr++);
1092 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::KS), tmp >> 5);
1093 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::DR), tmp & 0x1f);
1094 tmp = propCtr.readUint8(envCsr++);
1095 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::DT), tmp >> 5);
1096 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::SR), tmp & 0x1f);
1097 tmp = propCtr.readUint8(envCsr++);
1098 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::SL), tmp >> 4);
1099 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::RR), tmp & 0x0f);
1100 tmp = propCtr.readUint8(envCsr++);
1101 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::TL), tmp);
1102 tmp = propCtr.readUint8(envCsr++);
1103 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::ML), tmp & 0x0f);
1104 instManLocked->setEnvelopeFMParameter(envNum, params.at(FMOperatorParameter::SSGEG),
1105 (tmp & 0x80) ? -1 : ((tmp >> 4) & 0x07));
1106 }
1107 }
1108 }
1109
1110 /* LFO */
1111 {
1112 uint8_t tmp = instCtr.readUint8(instCsr++);
1113 if (0x80 & tmp) {
1114 fm->setLFOEnabled(false);
1115 fm->setLFONumber(0x7f & tmp);
1116 }
1117 else {
1118 fm->setLFOEnabled(true);
1119 uint8_t orgLFONum = 0x7f & tmp;
1120 int lfoNum = instManLocked->findFirstAssignableLFOFM();
1121 if (lfoNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1122 fm->setLFONumber(lfoNum);
1123 size_t lfoCsr = getPropertyPosition(propCtr, 0x01, orgLFONum);
1124 if (lfoCsr != std::numeric_limits<size_t>::max()) {
1125 tmp = propCtr.readUint8(lfoCsr++);
1126 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::FREQ, tmp >> 4);
1127 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::PMS, tmp & 0x0f);
1128 tmp = propCtr.readUint8(lfoCsr++);
1129 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::AMS, tmp & 0x0f);
1130 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::AM1, (tmp & 0x10) ? true : false);
1131 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::AM2, (tmp & 0x20) ? true : false);
1132 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::AM3, (tmp & 0x40) ? true : false);
1133 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::AM4, (tmp & 0x80) ? true : false);
1134 tmp = propCtr.readUint8(lfoCsr++);
1135 instManLocked->setLFOFMParameter(lfoNum, FMLFOParameter::Count, tmp);
1136 }
1137 }
1138 }
1139
1140 /* Operator sequence */
1141 uint8_t tmpCnt = 0;
1142 for (auto& param : FM_OPSEQ_PARAMS) {
1143 ++tmpCnt;
1144 uint8_t tmp = instCtr.readUint8(instCsr++);
1145 if (0x80 & tmp) {
1146 fm->setOperatorSequenceEnabled(param, false);
1147 fm->setOperatorSequenceNumber(param, 0x7f & tmp);
1148 }
1149 else {
1150 fm->setOperatorSequenceEnabled(param, true);
1151 uint8_t orgOpSeqNum = 0x7f & tmp;
1152 int opSeqNum = instManLocked->findFirstAssignableOperatorSequenceFM(param);
1153 if (opSeqNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1154 fm->setOperatorSequenceNumber(param, opSeqNum);
1155 size_t opSeqCsr = getPropertyPosition(propCtr, 0x02 + tmpCnt, orgOpSeqNum);
1156
1157 if (opSeqCsr != std::numeric_limits<size_t>::max()) {
1158 uint16_t seqLen = propCtr.readUint16(opSeqCsr);
1159 opSeqCsr += 2;
1160 for (uint16_t l = 0; l < seqLen; ++l) {
1161 uint16_t data = propCtr.readUint16(opSeqCsr);
1162 opSeqCsr += 2;
1163 if (l == 0)
1164 instManLocked->setOperatorSequenceFMSequenceData(param, opSeqNum, 0, data);
1165 else
1166 instManLocked->addOperatorSequenceFMSequenceData(param, opSeqNum, data);
1167 }
1168
1169 uint16_t loopCnt = propCtr.readUint16(opSeqCsr);
1170 opSeqCsr += 2;
1171 for (uint16_t l = 0; l < loopCnt; ++l) {
1172 int begin = propCtr.readUint16(opSeqCsr);
1173 opSeqCsr += 2;
1174 int end = propCtr.readUint16(opSeqCsr);
1175 opSeqCsr += 2;
1176 int times = propCtr.readUint8(opSeqCsr++);
1177 instManLocked->addOperatorSequenceFMLoop(param, opSeqNum, InstrumentSequenceLoop(begin, end, times));
1178 }
1179
1180 switch (propCtr.readUint8(opSeqCsr++)) {
1181 case 0x00: // No release
1182 instManLocked->setOperatorSequenceFMRelease(param, opSeqNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1183 break;
1184 case 0x01: // Fixed
1185 {
1186 uint16_t pos = propCtr.readUint16(opSeqCsr);
1187 opSeqCsr += 2;
1188 // Release point check (prevents a bug)
1189 // https://github.com/BambooTracker/BambooTracker/issues/11
1190 if (pos < seqLen) instManLocked->setOperatorSequenceFMRelease(param, opSeqNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1191 else instManLocked->setOperatorSequenceFMRelease(param, opSeqNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1192 break;
1193 }
1194 default:
1195 throw FileCorruptionError(FileType::Bank, instCsr);
1196 }
1197 }
1198 }
1199 }
1200
1201 /* Arpeggio */
1202 {
1203 std::unordered_map<FMOperatorType, int> tmpMap;
1204 std::unordered_map<int, FMOperatorType> orgNumMap;
1205 tmpMap.emplace(FMOperatorType::All, instCtr.readUint8(instCsr));
1206 instCsr += 3;
1207 tmpMap.emplace(FMOperatorType::Op1, instCtr.readUint8(instCsr++));
1208 tmpMap.emplace(FMOperatorType::Op2, instCtr.readUint8(instCsr++));
1209 tmpMap.emplace(FMOperatorType::Op3, instCtr.readUint8(instCsr++));
1210 tmpMap.emplace(FMOperatorType::Op4, instCtr.readUint8(instCsr));
1211 instCsr -= 5;
1212 for (auto& pair : tmpMap) {
1213 if (0x80 & pair.second) {
1214 fm->setArpeggioEnabled(pair.first, false);
1215 fm->setArpeggioNumber(pair.first, 0x7f & pair.second);
1216 }
1217 else {
1218 fm->setArpeggioEnabled(pair.first, true);
1219 uint8_t orgArpNum = 0x7f & pair.second;
1220 auto it = orgNumMap.find(orgArpNum);
1221 if (it == orgNumMap.end()) { // Make new property
1222 orgNumMap.emplace(orgArpNum, pair.first);
1223 int arpNum = instManLocked->findFirstAssignableArpeggioFM();
1224 if (arpNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1225 fm->setArpeggioNumber(pair.first, arpNum);
1226 size_t arpCsr = getPropertyPosition(propCtr, 0x28, orgArpNum);
1227
1228 if (arpCsr != std::numeric_limits<size_t>::max()) {
1229 uint16_t seqLen = propCtr.readUint16(arpCsr);
1230 arpCsr += 2;
1231 for (uint16_t l = 0; l < seqLen; ++l) {
1232 uint16_t data = propCtr.readUint16(arpCsr);
1233 arpCsr += 2;
1234 if (l == 0)
1235 instManLocked->setArpeggioFMSequenceData(arpNum, 0, data);
1236 else
1237 instManLocked->addArpeggioFMSequenceData(arpNum, data);
1238 }
1239
1240 uint16_t loopCnt = propCtr.readUint16(arpCsr);
1241 arpCsr += 2;
1242 for (uint16_t l = 0; l < loopCnt; ++l) {
1243 int begin = propCtr.readUint16(arpCsr);
1244 arpCsr += 2;
1245 int end = propCtr.readUint16(arpCsr);
1246 arpCsr += 2;
1247 int times = propCtr.readUint8(arpCsr++);
1248 instManLocked->addArpeggioFMLoop(arpNum, InstrumentSequenceLoop(begin, end, times));
1249 }
1250
1251 switch (propCtr.readUint8(arpCsr++)) {
1252 case 0x00: // No release
1253 instManLocked->setArpeggioFMRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1254 break;
1255 case 0x01: // Fixed
1256 {
1257 uint16_t pos = propCtr.readUint16(arpCsr);
1258 arpCsr += 2;
1259 // Release point check (prevents a bug)
1260 // https://github.com/BambooTracker/BambooTracker/issues/11
1261 if (pos < seqLen) instManLocked->setArpeggioFMRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1262 else instManLocked->setArpeggioFMRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1263 break;
1264 }
1265 default:
1266 throw FileCorruptionError(FileType::Bank, arpCsr);
1267 }
1268
1269 switch (propCtr.readUint8(arpCsr++)) {
1270 case 0x00: // Absolute
1271 instManLocked->setArpeggioFMType(arpNum, SequenceType::AbsoluteSequence);
1272 break;
1273 case 0x01: // Fixed
1274 instManLocked->setArpeggioFMType(arpNum, SequenceType::FixedSequence);
1275 if (bankVersion < Version::toBCD(1, 3, 1)) {
1276 // Add infinity loop to the last data, to keep compatibility
1277 auto loopRt = instManLocked->getArpeggioFMLoopRoot(arpNum).getAllLoops();
1278 if (std::none_of(loopRt.begin(), loopRt.end(), [](InstrumentSequenceLoop& loop) { return loop.isInfinite(); })) {
1279 auto seq = instManLocked->getArpeggioFMSequence(arpNum);
1280 if (!seq.empty()) {
1281 size_t pos = seq.size();
1282 instManLocked->addArpeggioFMSequenceData(arpNum, seq.back().data);
1283 instManLocked->addArpeggioFMLoop(arpNum, InstrumentSequenceLoop(pos, pos, InstrumentSequenceLoop::INFINITE_LOOP));
1284 }
1285 }
1286 }
1287 break;
1288 case 0x02: // Relative
1289 instManLocked->setArpeggioFMType(arpNum, SequenceType::RelativeSequence);
1290 break;
1291 default:
1292 if (bankVersion < Version::toBCD(1, 0, 2)) {
1293 // Recover deep clone bug
1294 // https://github.com/BambooTracker/BambooTracker/issues/170
1295 instManLocked->setArpeggioFMType(arpNum, SequenceType::AbsoluteSequence);
1296 break;
1297 }
1298 else {
1299 throw FileCorruptionError(FileType::Bank, arpCsr);
1300 }
1301 }
1302 }
1303 }
1304 else { // Use registered property
1305 fm->setArpeggioNumber(pair.first, fm->getArpeggioNumber(it->second));
1306 }
1307 }
1308 }
1309 }
1310
1311 /* Pitch */
1312 {
1313 std::unordered_map<FMOperatorType, int> tmpMap;
1314 std::unordered_map<int, FMOperatorType> orgNumMap;
1315 tmpMap.emplace(FMOperatorType::All, instCtr.readUint8(instCsr));
1316 instCsr += 6;
1317 tmpMap.emplace(FMOperatorType::Op1, instCtr.readUint8(instCsr++));
1318 tmpMap.emplace(FMOperatorType::Op2, instCtr.readUint8(instCsr++));
1319 tmpMap.emplace(FMOperatorType::Op3, instCtr.readUint8(instCsr++));
1320 tmpMap.emplace(FMOperatorType::Op4, instCtr.readUint8(instCsr));
1321 instCsr -= 8;
1322 for (auto& pair : tmpMap) {
1323 if (0x80 & pair.second) {
1324 fm->setPitchEnabled(pair.first, false);
1325 fm->setPitchNumber(pair.first, 0x7f & pair.second);
1326 }
1327 else {
1328 fm->setPitchEnabled(pair.first, true);
1329 uint8_t orgPtNum = 0x7f & pair.second;
1330 auto it = orgNumMap.find(orgPtNum);
1331 if (it == orgNumMap.end()) { // Make new property
1332 orgNumMap.emplace(orgPtNum, pair.first);
1333 int ptNum = instManLocked->findFirstAssignablePitchFM();
1334 if (ptNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1335 fm->setPitchNumber(pair.first, ptNum);
1336 size_t ptCsr = getPropertyPosition(propCtr, 0x29, orgPtNum);
1337
1338 if (ptCsr != std::numeric_limits<size_t>::max()) {
1339 uint16_t seqLen = propCtr.readUint16(ptCsr);
1340 ptCsr += 2;
1341 for (uint16_t l = 0; l < seqLen; ++l) {
1342 uint16_t data = propCtr.readUint16(ptCsr);
1343 ptCsr += 2;
1344 if (l == 0)
1345 instManLocked->setPitchFMSequenceData(ptNum, 0, data);
1346 else
1347 instManLocked->addPitchFMSequenceData(ptNum, data);
1348 }
1349
1350 uint16_t loopCnt = propCtr.readUint16(ptCsr);
1351 ptCsr += 2;
1352 for (uint16_t l = 0; l < loopCnt; ++l) {
1353 int begin = propCtr.readUint16(ptCsr);
1354 ptCsr += 2;
1355 int end = propCtr.readUint16(ptCsr);
1356 ptCsr += 2;
1357 int times = propCtr.readUint8(ptCsr++);
1358 instManLocked->addPitchFMLoop(ptNum, InstrumentSequenceLoop(begin, end, times));
1359 }
1360
1361 switch (propCtr.readUint8(ptCsr++)) {
1362 case 0x00: // No release
1363 instManLocked->setPitchFMRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1364 break;
1365 case 0x01: // Fixed
1366 {
1367 uint16_t pos = propCtr.readUint16(ptCsr);
1368 ptCsr += 2;
1369 // Release point check (prevents a bug)
1370 // https://github.com/BambooTracker/BambooTracker/issues/11
1371 if (pos < seqLen) instManLocked->setPitchFMRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1372 else instManLocked->setPitchFMRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1373 break;
1374 }
1375 default:
1376 throw FileCorruptionError(FileType::Bank, ptCsr);
1377 }
1378
1379 switch (propCtr.readUint8(ptCsr++)) {
1380 case 0x00: // Absolute
1381 instManLocked->setPitchFMType(ptNum, SequenceType::AbsoluteSequence);
1382 break;
1383 case 0x02: // Relative
1384 instManLocked->setPitchFMType(ptNum, SequenceType::RelativeSequence);
1385 break;
1386 default:
1387 if (bankVersion < Version::toBCD(1, 0, 2)) {
1388 // Recover deep clone bug
1389 // https://github.com/BambooTracker/BambooTracker/issues/170
1390 instManLocked->setPitchFMType(ptNum, SequenceType::AbsoluteSequence);
1391 break;
1392 }
1393 else {
1394 throw FileCorruptionError(FileType::Bank, ptCsr);
1395 }
1396 }
1397 }
1398 }
1399 else { // Use registered property
1400 fm->setPitchNumber(pair.first, fm->getPitchNumber(it->second));
1401 }
1402 }
1403 }
1404 }
1405
1406 /* Envelope reset */
1407 {
1408 uint8_t tmp = instCtr.readUint8(instCsr);
1409 fm->setEnvelopeResetEnabled(FMOperatorType::All, (tmp & 0x01));
1410 fm->setEnvelopeResetEnabled(FMOperatorType::Op1, (tmp & 0x02));
1411 fm->setEnvelopeResetEnabled(FMOperatorType::Op2, (tmp & 0x04));
1412 fm->setEnvelopeResetEnabled(FMOperatorType::Op3, (tmp & 0x08));
1413 fm->setEnvelopeResetEnabled(FMOperatorType::Op4, (tmp & 0x10));
1414 }
1415
1416 /* Pan */
1417 if (bankVersion >= Version::toBCD(1, 3, 0)) {
1418 instCsr += 9;
1419 uint8_t tmp = instCtr.readUint8(instCsr);
1420 if (0x80 & tmp) {
1421 fm->setPanEnabled(false);
1422 fm->setPanNumber(0x7f & tmp);
1423 }
1424 else {
1425 fm->setPanEnabled(true);
1426 uint8_t orgPanNum = 0x7f & tmp;
1427 int panNum = instManLocked->findFirstAssignablePanFM();
1428 if (panNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1429 fm->setPanNumber(panNum);
1430 size_t panCsr = getPropertyPosition(propCtr, 0x34, orgPanNum);
1431
1432 if (panCsr != std::numeric_limits<size_t>::max()) {
1433 uint16_t seqLen = propCtr.readUint16(panCsr);
1434 panCsr += 2;
1435 for (uint16_t l = 0; l < seqLen; ++l) {
1436 uint16_t data = propCtr.readUint16(panCsr);
1437 panCsr += 2;
1438 if (l == 0)
1439 instManLocked->setPanFMSequenceData(panNum, 0, data);
1440 else
1441 instManLocked->addPanFMSequenceData(panNum, data);
1442 }
1443
1444 uint16_t loopCnt = propCtr.readUint16(panCsr);
1445 panCsr += 2;
1446 for (uint16_t l = 0; l < loopCnt; ++l) {
1447 int begin = propCtr.readUint16(panCsr);
1448 panCsr += 2;
1449 int end = propCtr.readUint16(panCsr);
1450 panCsr += 2;
1451 int times = propCtr.readUint8(panCsr++);
1452 instManLocked->addPanFMLoop(panNum, InstrumentSequenceLoop(begin, end, times));
1453 }
1454
1455 switch (propCtr.readUint8(panCsr++)) {
1456 case 0x00: // No release
1457 instManLocked->setPanFMRelease(panNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1458 break;
1459 case 0x01: // Fixed
1460 {
1461 uint16_t pos = propCtr.readUint16(panCsr);
1462 panCsr += 2;
1463 // Release point check (prevents a bug)
1464 // https://github.com/BambooTracker/BambooTracker/issues/11
1465 if (pos < seqLen) instManLocked->setPanFMRelease(panNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1466 else instManLocked->setPanFMRelease(panNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1467 break;
1468 }
1469 default:
1470 throw FileCorruptionError(FileType::Bank, panCsr);
1471 }
1472 }
1473 }
1474 }
1475
1476 return fm;
1477 }
1478 case 0x01: // SSG
1479 {
1480 auto ssg = new InstrumentSSG(instNum, name, instManLocked.get());
1481
1482 /* Waveform */
1483 {
1484 uint8_t tmp = instCtr.readUint8(instCsr++);
1485 if (0x80 & tmp) {
1486 ssg->setWaveformEnabled(false);
1487 ssg->setWaveformNumber(0x7f & tmp);
1488 }
1489 else {
1490 ssg->setWaveformEnabled(true);
1491 uint8_t orgWfNum = 0x7f & tmp;
1492 int wfNum = instManLocked->findFirstAssignableWaveformSSG();
1493 if (wfNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1494 ssg->setWaveformNumber(wfNum);
1495 size_t wfCsr = getPropertyPosition(propCtr, 0x30, orgWfNum);
1496
1497 if (wfCsr != std::numeric_limits<size_t>::max()) {
1498 uint16_t seqLen = propCtr.readUint16(wfCsr);
1499 wfCsr += 2;
1500 for (uint16_t l = 0; l < seqLen; ++l) {
1501 uint16_t data = propCtr.readUint16(wfCsr);
1502 wfCsr += 2;
1503 int32_t subdata;
1504 subdata = propCtr.readInt32(wfCsr);
1505 wfCsr += 4;
1506 SSGWaveformUnit unit;
1507 switch (data) {
1508 case SSGWaveformType::SQM_TRIANGLE:
1509 case SSGWaveformType::SQM_SAW:
1510 case SSGWaveformType::SQM_INVSAW:
1511 unit = SSGWaveformUnit::makeUnitWithDecode(data, subdata);
1512 break;
1513 default:
1514 unit = SSGWaveformUnit::makeOnlyDataUnit(data);
1515 break;
1516 }
1517 if (l == 0)
1518 instManLocked->setWaveformSSGSequenceData(wfNum, 0, unit);
1519 else
1520 instManLocked->addWaveformSSGSequenceData(wfNum, unit);
1521 }
1522
1523 uint16_t loopCnt = propCtr.readUint16(wfCsr);
1524 wfCsr += 2;
1525 for (uint16_t l = 0; l < loopCnt; ++l) {
1526 int begin = propCtr.readUint16(wfCsr);
1527 wfCsr += 2;
1528 int end = propCtr.readUint16(wfCsr);
1529 wfCsr += 2;
1530 int times = propCtr.readUint8(wfCsr++);
1531 instManLocked->addWaveformSSGLoop(wfNum, InstrumentSequenceLoop(begin, end, times));
1532 }
1533
1534 switch (propCtr.readUint8(wfCsr++)) {
1535 case 0x00: // No release
1536 instManLocked->setWaveformSSGRelease(wfNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1537 break;
1538 case 0x01: // Fixed
1539 {
1540 uint16_t pos = propCtr.readUint16(wfCsr);
1541 wfCsr += 2;
1542 // Release point check (prevents a bug)
1543 // https://github.com/BambooTracker/BambooTracker/issues/11
1544 if (pos < seqLen) instManLocked->setWaveformSSGRelease(wfNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1545 else instManLocked->setWaveformSSGRelease(wfNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1546 break;
1547 }
1548 default:
1549 throw FileCorruptionError(FileType::Bank, wfCsr);
1550 }
1551 }
1552 }
1553 }
1554
1555 /* Tone/Noise */
1556 {
1557 uint8_t tmp = instCtr.readUint8(instCsr++);
1558 if (0x80 & tmp) {
1559 ssg->setToneNoiseEnabled(false);
1560 ssg->setToneNoiseNumber(0x7f & tmp);
1561 }
1562 else {
1563 ssg->setToneNoiseEnabled(true);
1564 uint8_t orgTnNum = 0x7f & tmp;
1565 int tnNum = instManLocked->findFirstAssignableToneNoiseSSG();
1566 if (tnNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1567 ssg->setToneNoiseNumber(tnNum);
1568 size_t tnCsr = getPropertyPosition(propCtr, 0x31, orgTnNum);
1569
1570 if (tnCsr != std::numeric_limits<size_t>::max()) {
1571 uint16_t seqLen = propCtr.readUint16(tnCsr);
1572 tnCsr += 2;
1573 for (uint16_t l = 0; l < seqLen; ++l) {
1574 uint16_t data = propCtr.readUint16(tnCsr);
1575 tnCsr += 2;
1576 if (bankVersion < Version::toBCD(1, 0, 1)) {
1577 if (data > 0) {
1578 uint16_t tmp = data - 1;
1579 data = tmp / 32 * 32 + (31 - tmp % 32) + 1;
1580 }
1581 }
1582 if (l == 0)
1583 instManLocked->setToneNoiseSSGSequenceData(tnNum, 0, data);
1584 else
1585 instManLocked->addToneNoiseSSGSequenceData(tnNum, data);
1586 }
1587
1588 uint16_t loopCnt = propCtr.readUint16(tnCsr);
1589 tnCsr += 2;
1590 for (uint16_t l = 0; l < loopCnt; ++l) {
1591 int begin = propCtr.readUint16(tnCsr);
1592 tnCsr += 2;
1593 int end = propCtr.readUint16(tnCsr);
1594 tnCsr += 2;
1595 int times = propCtr.readUint8(tnCsr++);
1596 instManLocked->addToneNoiseSSGLoop(tnNum, InstrumentSequenceLoop(begin, end, times));
1597 }
1598
1599 switch (propCtr.readUint8(tnCsr++)) {
1600 case 0x00: // No release
1601 instManLocked->setToneNoiseSSGRelease(tnNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1602 break;
1603 case 0x01: // Fixed
1604 {
1605 uint16_t pos = propCtr.readUint16(tnCsr);
1606 tnCsr += 2;
1607 // Release point check (prevents a bug)
1608 // https://github.com/BambooTracker/BambooTracker/issues/11
1609 if (pos < seqLen) instManLocked->setToneNoiseSSGRelease(tnNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1610 else instManLocked->setToneNoiseSSGRelease(tnNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1611 break;
1612 }
1613 default:
1614 throw FileCorruptionError(FileType::Bank, tnCsr);
1615 }
1616 }
1617 }
1618 }
1619
1620 /* Envelope */
1621 {
1622 uint8_t tmp = instCtr.readUint8(instCsr++);
1623 if (0x80 & tmp) {
1624 ssg->setEnvelopeEnabled(false);
1625 ssg->setEnvelopeNumber(0x7f & tmp);
1626 }
1627 else {
1628 ssg->setEnvelopeEnabled(true);
1629 uint8_t orgEnvNum = 0x7f & tmp;
1630 int envNum = instManLocked->findFirstAssignableEnvelopeSSG();
1631 if (envNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1632 ssg->setEnvelopeNumber(envNum);
1633 size_t envCsr = getPropertyPosition(propCtr, 0x32, orgEnvNum);
1634
1635 if (envCsr != std::numeric_limits<size_t>::max()) {
1636 uint16_t seqLen = propCtr.readUint16(envCsr);
1637 envCsr += 2;
1638 for (uint16_t l = 0; l < seqLen; ++l) {
1639 uint16_t data = propCtr.readUint16(envCsr);
1640 envCsr += 2;
1641 int32_t subdata;
1642 subdata = propCtr.readInt32(envCsr);
1643 envCsr += 4;
1644 SSGEnvelopeUnit unit = (data < 16) ? SSGEnvelopeUnit::makeOnlyDataUnit(data)
1645 : SSGEnvelopeUnit::makeUnitWithDecode(data, subdata);
1646 if (l == 0)
1647 instManLocked->setEnvelopeSSGSequenceData(envNum, 0, unit);
1648 else
1649 instManLocked->addEnvelopeSSGSequenceData(envNum, unit);
1650 }
1651
1652 uint16_t loopCnt = propCtr.readUint16(envCsr);
1653 envCsr += 2;
1654 for (uint16_t l = 0; l < loopCnt; ++l) {
1655 int begin = propCtr.readUint16(envCsr);
1656 envCsr += 2;
1657 int end = propCtr.readUint16(envCsr);
1658 envCsr += 2;
1659 int times = propCtr.readUint8(envCsr++);
1660 instManLocked->addEnvelopeSSGLoop(envNum, InstrumentSequenceLoop(begin, end, times));
1661 }
1662
1663 switch (propCtr.readUint8(envCsr++)) {
1664 case 0x00: // No release
1665 instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1666 break;
1667 // Release point check (prevents a bug)
1668 // https://github.com/BambooTracker/BambooTracker/issues/11
1669 case 0x01: // Fixed
1670 {
1671 uint16_t pos = propCtr.readUint16(envCsr);
1672 envCsr += 2;
1673 if (pos < seqLen) instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1674 else instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1675 break;
1676 }
1677 case 0x02: // Absolute
1678 {
1679 uint16_t pos = propCtr.readUint16(envCsr);
1680 envCsr += 2;
1681 if (pos < seqLen) instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::AbsoluteRelease, pos));
1682 else instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1683 break;
1684 }
1685 case 0x03: // Relative
1686 {
1687 uint16_t pos = propCtr.readUint16(envCsr);
1688 envCsr += 2;
1689 if (pos < seqLen) instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::RelativeRelease, pos));
1690 else instManLocked->setEnvelopeSSGRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1691 break;
1692 }
1693 default:
1694 throw FileCorruptionError(FileType::Bank, envCsr);
1695 }
1696 }
1697 }
1698 }
1699
1700 /* Arpeggio */
1701 {
1702 uint8_t tmp = instCtr.readUint8(instCsr++);
1703 if (0x80 & tmp) {
1704 ssg->setArpeggioEnabled(false);
1705 ssg->setArpeggioNumber(0x7f & tmp);
1706 }
1707 else {
1708 ssg->setArpeggioEnabled(true);
1709 uint8_t orgArpNum = 0x7f & tmp;
1710 int arpNum = instManLocked->findFirstAssignableArpeggioSSG();
1711 if (arpNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1712 ssg->setArpeggioNumber(arpNum);
1713 size_t arpCsr = getPropertyPosition(propCtr, 0x33, orgArpNum);
1714
1715 if (arpCsr != std::numeric_limits<size_t>::max()) {
1716 uint16_t seqLen = propCtr.readUint16(arpCsr);
1717 arpCsr += 2;
1718 for (uint16_t l = 0; l < seqLen; ++l) {
1719 uint16_t data = propCtr.readUint16(arpCsr);
1720 arpCsr += 2;
1721 if (l == 0)
1722 instManLocked->setArpeggioSSGSequenceData(arpNum, 0, data);
1723 else
1724 instManLocked->addArpeggioSSGSequenceData(arpNum, data);
1725 }
1726
1727 uint16_t loopCnt = propCtr.readUint16(arpCsr);
1728 arpCsr += 2;
1729 for (uint16_t l = 0; l < loopCnt; ++l) {
1730 int begin = propCtr.readUint16(arpCsr);
1731 arpCsr += 2;
1732 int end = propCtr.readUint16(arpCsr);
1733 arpCsr += 2;
1734 int times = propCtr.readUint8(arpCsr++);
1735 instManLocked->addArpeggioSSGLoop(arpNum, InstrumentSequenceLoop(begin, end, times));
1736 }
1737
1738 switch (propCtr.readUint8(arpCsr++)) {
1739 case 0x00: // No release
1740 instManLocked->setArpeggioSSGRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1741 break;
1742 case 0x01: // Fixed
1743 {
1744 uint16_t pos = propCtr.readUint16(arpCsr);
1745 arpCsr += 2;
1746 // Release point check (prevents a bug)
1747 // https://github.com/BambooTracker/BambooTracker/issues/11
1748 if (pos < seqLen) instManLocked->setArpeggioSSGRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1749 else instManLocked->setArpeggioSSGRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1750 break;
1751 }
1752 default:
1753 throw FileCorruptionError(FileType::Bank, arpCsr);
1754 }
1755
1756 switch (propCtr.readUint8(arpCsr++)) {
1757 case 0x00: // Absolute
1758 instManLocked->setArpeggioSSGType(arpNum, SequenceType::AbsoluteSequence);
1759 break;
1760 case 0x01: // Fixed
1761 instManLocked->setArpeggioSSGType(arpNum, SequenceType::FixedSequence);
1762 if (bankVersion < Version::toBCD(1, 3, 1)) {
1763 // Add infinity loop to the last data, to keep compatibility
1764 auto loopRt = instManLocked->getArpeggioSSGLoopRoot(arpNum).getAllLoops();
1765 if (std::none_of(loopRt.begin(), loopRt.end(), [](InstrumentSequenceLoop& loop) { return loop.isInfinite(); })) {
1766 auto seq = instManLocked->getArpeggioSSGSequence(arpNum);
1767 if (!seq.empty()) {
1768 size_t pos = seq.size();
1769 instManLocked->addArpeggioSSGSequenceData(arpNum, seq.back().data);
1770 instManLocked->addArpeggioSSGLoop(arpNum, InstrumentSequenceLoop(pos, pos, InstrumentSequenceLoop::INFINITE_LOOP));
1771 }
1772 }
1773 }
1774 break;
1775 case 0x02: // Relative
1776 instManLocked->setArpeggioSSGType(arpNum, SequenceType::RelativeSequence);
1777 break;
1778 default:
1779 if (bankVersion < Version::toBCD(1, 0, 2)) {
1780 // Recover deep clone bug
1781 // https://github.com/BambooTracker/BambooTracker/issues/170
1782 instManLocked->setArpeggioSSGType(arpNum, SequenceType::AbsoluteSequence);
1783 break;
1784 }
1785 else {
1786 throw FileCorruptionError(FileType::Bank, arpCsr);
1787 }
1788 }
1789 }
1790 }
1791 }
1792
1793 /* Pitch */
1794 {
1795 uint8_t tmp = instCtr.readUint8(instCsr++);
1796 if (0x80 & tmp) {
1797 ssg->setPitchEnabled(false);
1798 ssg->setPitchNumber(0x7f & tmp);
1799 }
1800 else {
1801 ssg->setPitchEnabled(true);
1802 uint8_t orgPtNum = 0x7f & tmp;
1803 int ptNum = instManLocked->findFirstAssignablePitchSSG();
1804 if (ptNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1805 ssg->setPitchNumber(ptNum);
1806 size_t ptCsr = getPropertyPosition(propCtr, 0x34, orgPtNum);
1807
1808 if (ptCsr != std::numeric_limits<size_t>::max()) {
1809 uint16_t seqLen = propCtr.readUint16(ptCsr);
1810 ptCsr += 2;
1811 for (uint16_t l = 0; l < seqLen; ++l) {
1812 uint16_t data = propCtr.readUint16(ptCsr);
1813 ptCsr += 2;
1814 if (l == 0)
1815 instManLocked->setPitchSSGSequenceData(ptNum, 0, data);
1816 else
1817 instManLocked->addPitchSSGSequenceData(ptNum, data);
1818 }
1819
1820 uint16_t loopCnt = propCtr.readUint16(ptCsr);
1821 ptCsr += 2;
1822 for (uint16_t l = 0; l < loopCnt; ++l) {
1823 int begin = propCtr.readUint16(ptCsr);
1824 ptCsr += 2;
1825 int end = propCtr.readUint16(ptCsr);
1826 ptCsr += 2;
1827 int times = propCtr.readUint8(ptCsr++);
1828 instManLocked->addPitchSSGLoop(ptNum, InstrumentSequenceLoop(begin, end, times));
1829 }
1830
1831 switch (propCtr.readUint8(ptCsr++)) {
1832 case 0x00: // No release
1833 instManLocked->setPitchSSGRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1834 break;
1835 case 0x01: // Fixed
1836 {
1837 uint16_t pos = propCtr.readUint16(ptCsr);
1838 ptCsr += 2;
1839 // Release point check (prevents a bug)
1840 // https://github.com/BambooTracker/BambooTracker/issues/11
1841 if (pos < seqLen) instManLocked->setPitchSSGRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1842 else instManLocked->setPitchSSGRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1843 break;
1844 }
1845 default:
1846 throw FileCorruptionError(FileType::Bank, ptCsr);
1847 }
1848
1849 switch (propCtr.readUint8(ptCsr++)) {
1850 case 0x00: // Absolute
1851 instManLocked->setPitchSSGType(ptNum, SequenceType::AbsoluteSequence);
1852 break;
1853 case 0x02: // Relative
1854 instManLocked->setPitchSSGType(ptNum, SequenceType::RelativeSequence);
1855 break;
1856 default:
1857 if (bankVersion < Version::toBCD(1, 0, 2)) {
1858 // Recover deep clone bug
1859 // https://github.com/BambooTracker/BambooTracker/issues/170
1860 instManLocked->setPitchSSGType(ptNum, SequenceType::AbsoluteSequence);
1861 break;
1862 }
1863 else {
1864 throw FileCorruptionError(FileType::Bank, ptCsr);
1865 }
1866 }
1867 }
1868 }
1869 }
1870
1871 return ssg;
1872 }
1873 case 0x02: // ADPCM
1874 {
1875 auto adpcm = new InstrumentADPCM(instNum, name, instManLocked.get());
1876
1877 /* Sample */
1878 {
1879 uint8_t orgSampNum = instCtr.readUint8(instCsr++);
1880 int sampNum = instManLocked->findFirstAssignableSampleADPCM();
1881 if (sampNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1882 adpcm->setSampleNumber(sampNum);
1883 size_t sampCsr = getPropertyPosition(propCtr, 0x40, orgSampNum);
1884 if (sampCsr != std::numeric_limits<size_t>::max()) {
1885 instManLocked->setSampleADPCMRootKeyNumber(sampNum, propCtr.readUint8(sampCsr++));
1886 instManLocked->setSampleADPCMRootDeltaN(sampNum, propCtr.readUint16(sampCsr));
1887 sampCsr += 2;
1888 instManLocked->setSampleADPCMRepeatEnabled(sampNum, (propCtr.readUint8(sampCsr++) & 0x01) != 0);
1889 uint32_t len = propCtr.readUint32(sampCsr);
1890 sampCsr += 4;
1891 std::vector<uint8_t> samples = propCtr.getSubcontainer(sampCsr, len).toVector();
1892 sampCsr += len;
1893 instManLocked->storeSampleADPCMRawSample(sampNum, samples);
1894 if (bankVersion >= Version::toBCD(1, 3, 1)) {
1895 uint16_t repeatBegin = propCtr.readUint16(sampCsr);
1896 sampCsr += 2;
1897 uint16_t repeatEnd = propCtr.readUint16(sampCsr);
1898 sampCsr += 2;
1899 instManLocked->setSampleADPCMRepeatrange(sampNum, SampleRepeatRange(repeatBegin, repeatEnd));
1900 }
1901 else {
1902 instManLocked->setSampleADPCMRepeatrange(sampNum, SampleRepeatRange(0, (samples.size() - 1) >> 5));
1903 }
1904 }
1905 }
1906
1907 /* Envelope */
1908 {
1909 uint8_t tmp = instCtr.readUint8(instCsr++);
1910 if (0x80 & tmp) {
1911 adpcm->setEnvelopeEnabled(false);
1912 adpcm->setEnvelopeNumber(0x7f & tmp);
1913 }
1914 else {
1915 adpcm->setEnvelopeEnabled(true);
1916 uint8_t orgEnvNum = 0x7f & tmp;
1917 int envNum = instManLocked->findFirstAssignableEnvelopeADPCM();
1918 if (envNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1919 adpcm->setEnvelopeNumber(envNum);
1920 size_t envCsr = getPropertyPosition(propCtr, 0x41, orgEnvNum);
1921
1922 if (envCsr != std::numeric_limits<size_t>::max()) {
1923 uint16_t seqLen = propCtr.readUint16(envCsr);
1924 envCsr += 2;
1925 for (uint16_t l = 0; l < seqLen; ++l) {
1926 uint16_t data = propCtr.readUint16(envCsr);
1927 envCsr += 2;
1928 if (l == 0)
1929 instManLocked->setEnvelopeADPCMSequenceData(envNum, 0, data);
1930 else
1931 instManLocked->addEnvelopeADPCMSequenceData(envNum, data);
1932 if (bankVersion < Version::toBCD(1, 3, 0)) envCsr += 4;
1933 }
1934
1935 uint16_t loopCnt = propCtr.readUint16(envCsr);
1936 envCsr += 2;
1937 for (uint16_t l = 0; l < loopCnt; ++l) {
1938 int begin = propCtr.readUint16(envCsr);
1939 envCsr += 2;
1940 int end = propCtr.readUint16(envCsr);
1941 envCsr += 2;
1942 int times = propCtr.readUint8(envCsr++);
1943 instManLocked->addEnvelopeADPCMLoop(envNum, InstrumentSequenceLoop(begin, end, times));
1944 }
1945
1946 switch (propCtr.readUint8(envCsr++)) {
1947 case 0x00: // No release
1948 instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1949 break;
1950 // Release point check (prevents a bug)
1951 // https://github.com/BambooTracker/BambooTracker/issues/11
1952 case 0x01: // Fixed
1953 {
1954 uint16_t pos = propCtr.readUint16(envCsr);
1955 envCsr += 2;
1956 if (pos < seqLen) instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
1957 else instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1958 break;
1959 }
1960 case 0x02: // Absolute
1961 {
1962 uint16_t pos = propCtr.readUint16(envCsr);
1963 envCsr += 2;
1964 if (pos < seqLen) instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::AbsoluteRelease, pos));
1965 else instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1966 break;
1967 }
1968 case 0x03: // Relative
1969 {
1970 uint16_t pos = propCtr.readUint16(envCsr);
1971 envCsr += 2;
1972 if (pos < seqLen) instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::RelativeRelease, pos));
1973 else instManLocked->setEnvelopeADPCMRelease(envNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
1974 break;
1975 }
1976 default:
1977 throw FileCorruptionError(FileType::Bank, envCsr);
1978 }
1979 }
1980 }
1981 }
1982
1983 /* Arpeggio */
1984 {
1985 uint8_t tmp = instCtr.readUint8(instCsr++);
1986 if (0x80 & tmp) {
1987 adpcm->setArpeggioEnabled(false);
1988 adpcm->setArpeggioNumber(0x7f & tmp);
1989 }
1990 else {
1991 adpcm->setArpeggioEnabled(true);
1992 uint8_t orgArpNum = 0x7f & tmp;
1993 int arpNum = instManLocked->findFirstAssignableArpeggioADPCM();
1994 if (arpNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
1995 adpcm->setArpeggioNumber(arpNum);
1996 size_t arpCsr = getPropertyPosition(propCtr, 0x42, orgArpNum);
1997
1998 if (arpCsr != std::numeric_limits<size_t>::max()) {
1999 uint16_t seqLen = propCtr.readUint16(arpCsr);
2000 arpCsr += 2;
2001 for (uint16_t l = 0; l < seqLen; ++l) {
2002 uint16_t data = propCtr.readUint16(arpCsr);
2003 arpCsr += 2;
2004 if (l == 0)
2005 instManLocked->setArpeggioADPCMSequenceData(arpNum, 0, data);
2006 else
2007 instManLocked->addArpeggioADPCMSequenceData(arpNum, data);
2008 }
2009
2010 uint16_t loopCnt = propCtr.readUint16(arpCsr);
2011 arpCsr += 2;
2012 for (uint16_t l = 0; l < loopCnt; ++l) {
2013 int begin = propCtr.readUint16(arpCsr);
2014 arpCsr += 2;
2015 int end = propCtr.readUint16(arpCsr);
2016 arpCsr += 2;
2017 int times = propCtr.readUint8(arpCsr++);
2018 instManLocked->addArpeggioADPCMLoop(arpNum, InstrumentSequenceLoop(begin, end, times));
2019 }
2020
2021 switch (propCtr.readUint8(arpCsr++)) {
2022 case 0x00: // No release
2023 instManLocked->setArpeggioADPCMRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
2024 break;
2025 case 0x01: // Fixed
2026 {
2027 uint16_t pos = propCtr.readUint16(arpCsr);
2028 arpCsr += 2;
2029 // Release point check (prevents a bug)
2030 // https://github.com/BambooTracker/BambooTracker/issues/11
2031 if (pos < seqLen) instManLocked->setArpeggioADPCMRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
2032 else instManLocked->setArpeggioADPCMRelease(arpNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
2033 break;
2034 }
2035 default:
2036 throw FileCorruptionError(FileType::Bank, arpCsr);
2037 }
2038
2039 switch (propCtr.readUint8(arpCsr++)) {
2040 case 0x00: // Absolute
2041 instManLocked->setArpeggioADPCMType(arpNum, SequenceType::AbsoluteSequence);
2042 break;
2043 case 0x01: // Fixed
2044 instManLocked->setArpeggioADPCMType(arpNum, SequenceType::FixedSequence);
2045 if (bankVersion < Version::toBCD(1, 3, 1)) {
2046 // Add infinity loop to the last data, to keep compatibility
2047 auto loopRt = instManLocked->getArpeggioADPCMLoopRoot(arpNum).getAllLoops();
2048 if (std::none_of(loopRt.begin(), loopRt.end(), [](InstrumentSequenceLoop& loop) { return loop.isInfinite(); })) {
2049 auto seq = instManLocked->getArpeggioADPCMSequence(arpNum);
2050 if (!seq.empty()) {
2051 size_t pos = seq.size();
2052 instManLocked->addArpeggioADPCMSequenceData(arpNum, seq.back().data);
2053 instManLocked->addArpeggioADPCMLoop(arpNum, InstrumentSequenceLoop(pos, pos, InstrumentSequenceLoop::INFINITE_LOOP));
2054 }
2055 }
2056 }
2057 break;
2058 case 0x02: // Relative
2059 instManLocked->setArpeggioADPCMType(arpNum, SequenceType::RelativeSequence);
2060 break;
2061 default:
2062 throw FileCorruptionError(FileType::Bank, arpCsr);
2063 }
2064 }
2065 }
2066 }
2067
2068 /* Pitch */
2069 {
2070 uint8_t tmp = instCtr.readUint8(instCsr++);
2071 if (0x80 & tmp) {
2072 adpcm->setPitchEnabled(false);
2073 adpcm->setPitchNumber(0x7f & tmp);
2074 }
2075 else {
2076 adpcm->setPitchEnabled(true);
2077 uint8_t orgPtNum = 0x7f & tmp;
2078 int ptNum = instManLocked->findFirstAssignablePitchADPCM();
2079 if (ptNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
2080 adpcm->setPitchNumber(ptNum);
2081 size_t ptCsr = getPropertyPosition(propCtr, 0x43, orgPtNum);
2082
2083 if (ptCsr != std::numeric_limits<size_t>::max()) {
2084 uint16_t seqLen = propCtr.readUint16(ptCsr);
2085 ptCsr += 2;
2086 for (uint16_t l = 0; l < seqLen; ++l) {
2087 uint16_t data = propCtr.readUint16(ptCsr);
2088 ptCsr += 2;
2089 if (l == 0)
2090 instManLocked->setPitchADPCMSequenceData(ptNum, 0, data);
2091 else
2092 instManLocked->addPitchADPCMSequenceData(ptNum, data);
2093 }
2094
2095 uint16_t loopCnt = propCtr.readUint16(ptCsr);
2096 ptCsr += 2;
2097 for (uint16_t l = 0; l < loopCnt; ++l) {
2098 int begin = propCtr.readUint16(ptCsr);
2099 ptCsr += 2;
2100 int end = propCtr.readUint16(ptCsr);
2101 ptCsr += 2;
2102 int times = propCtr.readUint8(ptCsr++);
2103 instManLocked->addPitchADPCMLoop(ptNum, InstrumentSequenceLoop(begin, end, times));
2104 }
2105
2106 switch (propCtr.readUint8(ptCsr++)) {
2107 case 0x00: // No release
2108 instManLocked->setPitchADPCMRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
2109 break;
2110 case 0x01: // Fixed
2111 {
2112 uint16_t pos = propCtr.readUint16(ptCsr);
2113 ptCsr += 2;
2114 // Release point check (prevents a bug)
2115 // https://github.com/BambooTracker/BambooTracker/issues/11
2116 if (pos < seqLen) instManLocked->setPitchADPCMRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
2117 else instManLocked->setPitchADPCMRelease(ptNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
2118 break;
2119 }
2120 default:
2121 throw FileCorruptionError(FileType::Bank, ptCsr);
2122 }
2123
2124 switch (propCtr.readUint8(ptCsr++)) {
2125 case 0x00: // Absolute
2126 instManLocked->setPitchADPCMType(ptNum, SequenceType::AbsoluteSequence);
2127 break;
2128 case 0x02: // Relative
2129 instManLocked->setPitchADPCMType(ptNum, SequenceType::RelativeSequence);
2130 break;
2131 default:
2132 throw FileCorruptionError(FileType::Bank, ptCsr);
2133 }
2134 }
2135 }
2136 }
2137
2138 /* Pan */
2139 if (bankVersion >= Version::toBCD(1, 3, 0)) {
2140 uint8_t tmp = instCtr.readUint8(instCsr++);
2141 if (0x80 & tmp) {
2142 adpcm->setPanEnabled(false);
2143 adpcm->setPanNumber(0x7f & tmp);
2144 }
2145 else {
2146 adpcm->setPanEnabled(true);
2147 uint8_t orgPanNum = 0x7f & tmp;
2148 int panNum = instManLocked->findFirstAssignablePanADPCM();
2149 if (panNum == -1) throw FileCorruptionError(FileType::Bank, instCsr);
2150 adpcm->setPanNumber(panNum);
2151 size_t panCsr = getPropertyPosition(propCtr, 0x34, orgPanNum);
2152
2153 if (panCsr != std::numeric_limits<size_t>::max()) {
2154 uint16_t seqLen = propCtr.readUint16(panCsr);
2155 panCsr += 2;
2156 for (uint16_t l = 0; l < seqLen; ++l) {
2157 uint16_t data = propCtr.readUint16(panCsr);
2158 panCsr += 2;
2159 if (l == 0)
2160 instManLocked->setPanADPCMSequenceData(panNum, 0, data);
2161 else
2162 instManLocked->addPanADPCMSequenceData(panNum, data);
2163 }
2164
2165 uint16_t loopCnt = propCtr.readUint16(panCsr);
2166 panCsr += 2;
2167 for (uint16_t l = 0; l < loopCnt; ++l) {
2168 int begin = propCtr.readUint16(panCsr);
2169 panCsr += 2;
2170 int end = propCtr.readUint16(panCsr);
2171 panCsr += 2;
2172 int times = propCtr.readUint8(panCsr++);
2173 instManLocked->addPanADPCMLoop(panNum, InstrumentSequenceLoop(begin, end, times));
2174 }
2175
2176 switch (propCtr.readUint8(panCsr++)) {
2177 case 0x00: // No release
2178 instManLocked->setPanADPCMRelease(panNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
2179 break;
2180 case 0x01: // Fixed
2181 {
2182 uint16_t pos = propCtr.readUint16(panCsr);
2183 panCsr += 2;
2184 // Release point check (prevents a bug)
2185 // https://github.com/BambooTracker/BambooTracker/issues/11
2186 if (pos < seqLen) instManLocked->setPanADPCMRelease(panNum, InstrumentSequenceRelease(InstrumentSequenceRelease::FixedRelease, pos));
2187 else instManLocked->setPanADPCMRelease(panNum, InstrumentSequenceRelease(InstrumentSequenceRelease::NoRelease));
2188 break;
2189 }
2190 default:
2191 throw FileCorruptionError(FileType::Bank, panCsr);
2192 }
2193 }
2194 }
2195 }
2196
2197 return adpcm;
2198 }
2199 case 0x03: // Drumkit
2200 {
2201 auto kit = new InstrumentDrumkit(instNum, name, instManLocked.get());
2202
2203 uint8_t keyCnt = instCtr.readUint8(instCsr++);
2204 std::unordered_map<int, int> sampMap;
2205 int newSamp = 0;
2206 for (uint8_t i = 0; i < keyCnt; ++i) {
2207 int key = instCtr.readUint8(instCsr++);
2208 kit->setSampleEnabled(key, true);
2209
2210 /* Sample */
2211 {
2212 uint8_t orgSamp = instCtr.readUint8(instCsr++);
2213 if (sampMap.count(orgSamp)) { // Use registered property
2214 kit->setSampleNumber(key, sampMap.at(orgSamp));
2215 }
2216 else {
2217 newSamp = instManLocked->findFirstAssignableSampleADPCM(newSamp);
2218 if (newSamp == -1) throw FileCorruptionError(FileType::Bank, instCsr);
2219 kit->setSampleNumber(key, newSamp);
2220 sampMap[orgSamp] = newSamp;
2221 size_t sampCsr = getPropertyPosition(propCtr, 0x40, orgSamp);
2222 if (sampCsr != std::numeric_limits<size_t>::max()) {
2223 instManLocked->setSampleADPCMRootKeyNumber(newSamp, propCtr.readUint8(sampCsr++));
2224 instManLocked->setSampleADPCMRootDeltaN(newSamp, propCtr.readUint16(sampCsr));
2225 sampCsr += 2;
2226 instManLocked->setSampleADPCMRepeatEnabled(newSamp, (propCtr.readUint8(sampCsr++) & 0x01) != 0);
2227 uint32_t len = propCtr.readUint32(sampCsr);
2228 sampCsr += 4;
2229 std::vector<uint8_t> samples = propCtr.getSubcontainer(sampCsr, len).toVector();
2230 sampCsr += len;
2231 instManLocked->storeSampleADPCMRawSample(newSamp, samples);
2232 if (bankVersion >= Version::toBCD(1, 3, 1)) {
2233 uint16_t repeatBegin = propCtr.readUint16(sampCsr);
2234 sampCsr += 2;
2235 uint16_t repeatEnd = propCtr.readUint16(sampCsr);
2236 sampCsr += 2;
2237 instManLocked->setSampleADPCMRepeatrange(newSamp, SampleRepeatRange(repeatBegin, repeatEnd));
2238 }
2239 else {
2240 instManLocked->setSampleADPCMRepeatrange(newSamp, SampleRepeatRange(0, (samples.size() - 1) >> 5));
2241 }
2242 ++newSamp; // Increment for search
2243 }
2244 }
2245 }
2246
2247 /* Pitch */
2248 kit->setPitch(key, instCtr.readInt8(instCsr++));
2249
2250 /* Pan */
2251 if (bankVersion >= Version::toBCD(1, 3, 0)) {
2252 kit->setPan(key, instCtr.readUint8(instCsr++));
2253 }
2254 }
2255
2256 return kit;
2257 }
2258 default:
2259 throw FileCorruptionError(FileType::Bank, instCsr);
2260 }
2261 }
2262 }