Debian Codegraph @

BambooTracker/io/opni_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 "opni_io.hpp"
27 #include "format/wopn_file.h"
28 #include "file_io_error.hpp"
29 #include "note.hpp"
30
31 namespace io
32 {
33 OpniIO::OpniIO() : AbstractInstrumentIO("opni", "WOPN instrument", true, false) {}
34
35 AbstractInstrument* OpniIO::load(const BinaryContainer& ctr, const std::string& fileName,
36 std::weak_ptr<InstrumentsManager> instMan, int instNum) const
37 {
38 (void)fileName;
39 OPNIFile opni;
40 auto&& mem = ctr.toVector();
41 if (WOPN_LoadInstFromMem(&opni, mem.data(), mem.size()) != 0)
42 throw FileCorruptionError(FileType::Inst, 0);
43
44 return loadWOPNInstrument(opni.inst, instMan, instNum);
45 }
46
47 AbstractInstrument* OpniIO::loadWOPNInstrument(const WOPNInstrument &srcInst,
48 std::weak_ptr<InstrumentsManager> instMan,
49 int instNum)
50 {
51 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
52 int envIdx = instManLocked->findFirstAssignableEnvelopeFM();
53 if (envIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
54 const char *name = srcInst.inst_name;
55
56 InstrumentFM* inst = new InstrumentFM(instNum, name, instManLocked.get());
57 inst->setEnvelopeNumber(envIdx);
58 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::AL, srcInst.fbalg & 7);
59 instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::FB, (srcInst.fbalg >> 3) & 7);
60
61 const WOPNOperator *op[4] = {
62 &srcInst.operators[0],
63 &srcInst.operators[2],
64 &srcInst.operators[1],
65 &srcInst.operators[3],
66 };
67
68 int am[4];
69 for (int n = 0; n < 4; ++n) {
70 auto& params = FM_OP_PARAMS[n];
71 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::ML), op[n]->dtfm_30 & 15);
72 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DT), (op[n]->dtfm_30 >> 4) & 7);
73 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::TL), op[n]->level_40);
74 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::KS), op[n]->rsatk_50 >> 6);
75 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::AR), op[n]->rsatk_50 & 31);
76 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DR), op[n]->amdecay1_60 & 31);
77 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SR), op[n]->decay2_70 & 31);
78 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::RR), op[n]->susrel_80 & 15);
79 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SL), op[n]->susrel_80 >> 4);
80 int ssgeg = op[n]->ssgeg_90;
81 ssgeg = ssgeg & 8 ? ssgeg & 7 : -1;
82 instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SSGEG), ssgeg);
83 am[n] = op[n]->amdecay1_60 >> 7;
84 }
85
86 if (srcInst.lfosens != 0) {
87 int lfoIdx = instManLocked->findFirstAssignableLFOFM();
88 if (lfoIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
89 inst->setLFOEnabled(true);
90 inst->setLFONumber(lfoIdx);
91 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::PMS, srcInst.lfosens & 7);
92 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AMS, (srcInst.lfosens >> 4) & 3);
93 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM1, am[0]);
94 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM2, am[1]);
95 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM3, am[2]);
96 instManLocked->setLFOFMParameter(lfoIdx, FMLFOParameter::AM4, am[3]);
97 }
98
99 if (srcInst.note_offset != 0) {
100 int arpIdx = instManLocked->findFirstAssignableArpeggioFM();
101 if (arpIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
102 inst->setArpeggioEnabled(FMOperatorType::All, true);
103 inst->setArpeggioNumber(FMOperatorType::All, arpIdx);
104 instManLocked->setArpeggioFMSequenceData(arpIdx, 0, srcInst.note_offset + Note::DEFAULT_NOTE_NUM);
105 instManLocked->setArpeggioFMType(arpIdx, SequenceType::AbsoluteSequence);
106 }
107
108 return inst;
109 }
110 }