Debian Codegraph @

BambooTracker/io/dmp_io.cpp

raw
1 /*
2 * Copyright (C) 2020-2021 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 "dmp_io.hpp"
27 #include "file_io_error.hpp"
28 #include "io_utils.hpp"
29
30 namespace io
31 {
32 DmpIO::DmpIO() : AbstractInstrumentIO("dmp", "DefleMask preset", true, false) {}
33
34 AbstractInstrument* DmpIO::load(const BinaryContainer& ctr, const std::string& fileName,
35 std::weak_ptr<InstrumentsManager> instMan, int instNum) const
36 {
37 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
38 size_t csr = 0;
39
40 uint8_t insType = 1; // default to FM
41 uint8_t fileVersion = ctr.readUint8(csr++);
42 if (fileVersion == 0) { // older, unversioned dmp
43 if (ctr.size() != 49) throw FileCorruptionError(FileType::Inst, csr);
44 }
45 else {
46 if (fileVersion < 9) throw FileCorruptionError(FileType::Inst, csr);
47 if (fileVersion == 9 && ctr.size() != 51) { // make sure it's not for that discontinued chip
48 throw FileCorruptionError(FileType::Inst, csr);
49 }
50 uint8_t system = 2; // default to genesis
51 if (fileVersion >= 11) system = ctr.readUint8(csr++);
52 if (system != 2 && system != 3 && system != 8 && system != 9) { // genesis, sms, arcade and neo geo only
53 throw FileCorruptionError(FileType::Inst, csr);
54 }
55 insType = ctr.readUint8(csr++);
56 }
57 AbstractInstrument* inst = nullptr;
58 switch (insType) {
59 case 0x00: // SSG
60 {
61 inst = new InstrumentSSG(instNum, fileName, instManLocked.get());
62 auto ssg = dynamic_cast<InstrumentSSG*>(inst);
63 uint8_t envSize = ctr.readUint8(csr++);
64 if (envSize > 0) {
65 int idx = instManLocked->findFirstAssignableEnvelopeSSG();
66 if (idx < 0) throw FileCorruptionError(FileType::Inst, csr);
67 ssg->setEnvelopeEnabled(true);
68 ssg->setEnvelopeNumber(idx);
69 for (uint8_t l = 0; l < envSize; ++l) {
70 int data = ctr.readInt32(csr);
71 // compensate SN76489's envelope step of 2dB to SSG's 3dB
72 if (data > 0) data = 15 - (15 - data) * 2 / 3;
73 csr += 4;
74 if (l == 0) instManLocked->setEnvelopeSSGSequenceData(idx, 0, SSGEnvelopeUnit::makeOnlyDataUnit(data));
75 else instManLocked->addEnvelopeSSGSequenceData(idx, SSGEnvelopeUnit::makeOnlyDataUnit(data));
76 }
77 int8_t loop = ctr.readInt8(csr++);
78 if (loop >= 0) instManLocked->addEnvelopeSSGLoop(idx, InstrumentSequenceLoop(loop, envSize - 1));
79 }
80 uint8_t arpSize = ctr.readUint8(csr++);
81 if (arpSize > 0) {
82 int idx = instManLocked->findFirstAssignableArpeggioSSG();
83 if (idx < 0) throw FileCorruptionError(FileType::Inst, csr);
84 ssg->setArpeggioEnabled(true);
85 ssg->setArpeggioNumber(idx);
86 uint8_t arpType = ctr.readUint8(csr + arpSize * 4 + 1);
87 if (arpType == 1) instManLocked->setArpeggioSSGType(idx, SequenceType::FixedSequence);
88 for (uint8_t l = 0; l < arpSize; ++l) {
89 int data = ctr.readInt32(csr) + 36;
90 csr += 4;
91 if (arpType == 1) data -= 24;
92 if (l == 0) instManLocked->setArpeggioSSGSequenceData(idx, 0, data);
93 else instManLocked->addArpeggioSSGSequenceData(idx, data);
94 }
95 int8_t loop = ctr.readInt8(csr++);
96 if (loop >= 0) instManLocked->addArpeggioSSGLoop(idx, InstrumentSequenceLoop(loop, arpSize - 1));
97 }
98 break;
99 }
100 case 0x01: // FM
101 {
102 int envIdx = instManLocked->findFirstAssignableEnvelopeFM();
103 if (envIdx < 0) throw FileCorruptionError(FileType::Inst, csr);
104 inst = new InstrumentFM(instNum, fileName, instManLocked.get());
105 auto fm = dynamic_cast<InstrumentFM*>(inst);
106 fm->setEnvelopeNumber(envIdx);
107 if (fileVersion == 9) csr++; // skip version 9's total operators field
108 uint8_t pms = ctr.readUint8(csr++);
109 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::FB, ctr.readUint8(csr++));
110 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::AL, ctr.readUint8(csr++));
111 uint8_t ams = ctr.readUint8(csr++);
112
113 uint8_t am[4] = {};
114 for (const int op : { 0, 2, 1, 3 }) {
115 auto& params = FM_OP_PARAMS[op];
116 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::ML), ctr.readUint8(csr++));
117 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::TL), ctr.readUint8(csr++));
118 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::AR), ctr.readUint8(csr++));
119 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DR), ctr.readUint8(csr++));
120 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SL), ctr.readUint8(csr++));
121 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::RR), ctr.readUint8(csr++));
122 am[op] = ctr.readUint8(csr++);
123 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::KS), ctr.readUint8(csr++));
124 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DT),
125 convertDtFromDmpTfiVgi(ctr.readUint8(csr++) & 15)); // mask out OPM's DT2
126 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SR), ctr.readUint8(csr++));
127 int ssgeg1 = ctr.readUint8(csr++);
128 ssgeg1 = ssgeg1 & 8 ? ssgeg1 & 7 : -1;
129 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SSGEG), ssgeg1);
130 }
131
132 if (pms || ams) {
133 int lfoIdx = instManLocked->findFirstAssignableLFOFM();
134 if (lfoIdx < 0) throw FileCorruptionError(FileType::Inst, csr);
135 fm->setLFOEnabled(true);
136 fm->setLFONumber(lfoIdx);
137 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::PMS, pms);
138 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AMS, ams);
139 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM1, am[0]);
140 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM2, am[1]);
141 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM3, am[2]);
142 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM4, am[3]);
143 }
144 break;
145 }
146 }
147 return inst;
148 }
149 }