Debian Codegraph @

BambooTracker/io/ins_io.cpp

raw
1 /*
2 * Copyright (C) 2020 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 "ins_io.hpp"
27 #include "file_io_error.hpp"
28
29 namespace io
30 {
31 InsIO::InsIO() : AbstractInstrumentIO("ins", "MVSTracker instrument", true, false) {}
32
33 AbstractInstrument* InsIO::load(const BinaryContainer& ctr, const std::string& fileName,
34 std::weak_ptr<InstrumentsManager> instMan, int instNum) const
35 {
36 (void)fileName;
37 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
38 size_t csr = 0;
39 if (ctr.readString(csr, 4).compare("MVSI") != 0) throw FileCorruptionError(FileType::Inst, csr);
40 csr += 4;
41 /*uint8_t fileVersion = */std::stoi(ctr.readString(csr++, 1));
42 size_t nameCsr = 0;
43 while (ctr.readChar(nameCsr++) != '\0')
44 ;
45 std::string name = ctr.readString(csr, nameCsr - csr);
46 csr = nameCsr;
47
48 if (ctr.size() - csr != 25) throw FileCorruptionError(FileType::Inst, csr);
49
50 int envIdx = instManLocked->findFirstAssignableEnvelopeFM();
51 if (envIdx < 0) throw FileCorruptionError(FileType::Inst, csr);
52
53 InstrumentFM* inst = new InstrumentFM(instNum, name, instManLocked.get());
54 inst->setEnvelopeNumber(envIdx);
55 uint8_t tmp;
56 size_t initCsr[] = { 0, 1, 2, 3 };
57 for (int op = 0; op < 4; ++op) {
58 size_t pcsr = csr + initCsr[op];
59 auto& params = FM_OP_PARAMS[op];
60 tmp = ctr.readUint8(pcsr);
61 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::ML), 0x0f & tmp);
62 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DT), tmp >> 4);
63 pcsr += 4;
64 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::TL), ctr.readUint8(pcsr));
65 pcsr += 4;
66 tmp = ctr.readUint8(pcsr);
67 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::AR), 0x3f & tmp);
68 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::KS), tmp >> 6);
69 pcsr += 4;
70 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DR), ctr.readUint8(pcsr));
71 pcsr += 4;
72 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SR), ctr.readUint8(pcsr));
73 pcsr += 4;
74 tmp = ctr.readUint8(pcsr);
75 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::RR), 0x0f & tmp);
76 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SL), tmp >> 4);
77 }
78 csr += 24;
79 tmp = ctr.readUint8(csr);
80 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::AL, 0x07 & tmp);
81 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::FB, tmp >> 3);
82
83 return inst;
84 }
85 }