Debian Codegraph @

BambooTracker/io/ff_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 "ff_io.hpp"
27 #include <vector>
28 #include <algorithm>
29 #include "instrument.hpp"
30 #include "file_io_error.hpp"
31
32 namespace io
33 {
34 FfIO::FfIO() : AbstractBankIO("ff", "PMD FF", true, false) {}
35
36 AbstractBank* FfIO::load(const BinaryContainer& ctr) const
37 {
38 size_t csr = 0;
39 // File size check
40 size_t ctrSize = ctr.size();
41 if (!ctrSize || ctrSize & 0x1f || ctrSize > 0x2000)
42 throw FileCorruptionError(FileType::Bank, csr);
43
44 std::vector<int> ids;
45 std::vector<std::string> names;
46 std::vector<BinaryContainer> ctrs;
47 int max = static_cast<int>(ctrSize / 0x20);
48 for (int i = 0; i < max; ++i) {
49 BinaryContainer block = ctr.getSubcontainer(csr, 25);
50 csr += 25;
51 std::string name = "";
52 for (size_t j = 0; j < 7; ++j) {
53 if (char c = ctr.readChar(csr + j)) name += c;
54 else break;
55 }
56 csr += 7;
57
58 // Empty
59 if (std::all_of(block.begin(), block.end(),
60 [](const char c) { return c == 0; }) && name.empty())
61 continue;
62
63 ids.push_back(i);
64 names.push_back(name);
65 ctrs.push_back(block);
66 }
67
68 return new FfBank(ids, names, ctrs);
69 }
70
71 AbstractInstrument* FfIO::loadInstrument(const BinaryContainer& instCtr,
72 const std::string& name,
73 std::weak_ptr<InstrumentsManager> instMan,
74 int instNum)
75 {
76 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
77 int envIdx = instManLocked->findFirstAssignableEnvelopeFM();
78 if (envIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
79
80 InstrumentFM* fm = new InstrumentFM(instNum, name, instManLocked.get());
81 fm->setEnvelopeNumber(envIdx);
82
83 uint8_t tmp;
84 size_t initCsr[] = { 0, 2, 1, 3 };
85 for (int op = 0; op < 4; ++op) {
86 size_t csr = initCsr[op];
87 auto& params = FM_OP_PARAMS[op];
88 tmp = instCtr.readUint8(csr);
89 uint8_t ssgeg = (tmp & 0x80) ? 8 : 0;
90 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DT), (tmp & 0x70) >> 4);
91 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::ML), tmp & 0x0f);
92 csr += 4;
93 tmp = instCtr.readUint8(csr);
94 if (tmp & 0x80) ssgeg |= 4;
95 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::TL), tmp & 0x7f);
96 csr += 4;
97 tmp = instCtr.readUint8(csr);
98 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::KS), tmp >> 6);
99 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::AR), tmp & 0x1f);
100 csr += 4;
101 tmp = instCtr.readUint8(csr);
102 ssgeg |= ((tmp & 0x60) >> 5);
103 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SSGEG),
104 (ssgeg & 8) ? ssgeg & 7 : -1);
105 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DR), tmp & 0x1f);
106 csr += 4;
107 tmp = instCtr.readUint8(csr);
108 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SR), tmp);
109 csr += 4;
110 tmp = instCtr.readUint8(csr);
111 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SL), tmp >> 4);
112 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::RR), tmp & 0x0f);
113 }
114 tmp = instCtr.readUint8(24);
115 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::FB, tmp >> 3);
116 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::AL, tmp & 0x07);
117
118 return fm;
119 }
120 }