Debian Codegraph @

BambooTracker/io/dat_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 "dat_io.hpp"
27 #include <vector>
28 #include <algorithm>
29 #include "instrument.hpp"
30 #include "file_io_error.hpp"
31
32 namespace io
33 {
34 DatIO::DatIO() : AbstractBankIO("dat", "MUCOM88 voice", true, false) {}
35
36 AbstractBank* DatIO::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 != 0x2000)
42 throw FileCorruptionError(FileType::Bank, 0);
43
44 std::vector<int> ids;
45 std::vector<std::string> names;
46 std::vector<BinaryContainer> ctrs;
47 for (int i = 0; i < 256; ++i) {
48 csr++; // Skip first byte (unknown byte)
49 BinaryContainer block = ctr.getSubcontainer(csr, 25);
50 csr += 25;
51 std::string name = "";
52 for (size_t j = 0; j < 6; ++j) {
53 if (char c = ctr.readChar(csr + j)) name += c;
54 else break;
55 }
56 csr += 6;
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 Mucom88Bank(ids, names, ctrs);
69 }
70
71 AbstractInstrument* DatIO::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 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DT), tmp >> 4);
90 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::ML), tmp & 0x0f);
91 csr += 4;
92 tmp = instCtr.readUint8(csr);
93 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::TL), tmp);
94 csr += 4;
95 tmp = instCtr.readUint8(csr);
96 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::KS), tmp >> 6);
97 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::AR), tmp & 0x1f);
98 csr += 4;
99 tmp = instCtr.readUint8(csr);
100 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DR), tmp & 0x1f);
101 csr += 4;
102 tmp = instCtr.readUint8(csr);
103 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SR), tmp);
104 csr += 4;
105 tmp = instCtr.readUint8(csr);
106 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SL), tmp >> 4);
107 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::RR), tmp & 0x0f);
108 }
109 tmp = instCtr.readUint8(24);
110 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::FB, tmp >> 3);
111 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::AL, tmp & 0x07);
112
113 return fm;
114 }
115 }