| 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 "y12_io.hpp" |
| 27 |
#include "file_io_error.hpp" |
| 28 |
|
| 29 |
namespace io |
| 30 |
{ |
| 31 |
Y12IO::Y12IO() : AbstractInstrumentIO("y12", "Gens KMod dump", true, false) {} |
| 32 |
|
| 33 |
AbstractInstrument* Y12IO::load(const BinaryContainer& ctr, const std::string& fileName, |
| 34 |
std::weak_ptr<InstrumentsManager> instMan, |
| 35 |
int instNum) const |
| 36 |
{ |
| 37 |
std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock(); |
| 38 |
if (ctr.size() != 128) throw FileCorruptionError(FileType::Inst, 0); |
| 39 |
int envIdx = instManLocked->findFirstAssignableEnvelopeFM(); |
| 40 |
if (envIdx < 0) throw FileCorruptionError(FileType::Inst, 0); |
| 41 |
|
| 42 |
InstrumentFM* inst = new InstrumentFM(instNum, fileName, instManLocked.get()); |
| 43 |
inst->setEnvelopeNumber(envIdx); |
| 44 |
size_t csr = 0; |
| 45 |
for (const int op : { 0, 2, 1, 3 }) { |
| 46 |
const auto& params = FM_OP_PARAMS[op]; |
| 47 |
uint8_t tmp = ctr.readUint8(csr++); |
| 48 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::ML), 0x0f & tmp); |
| 49 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DT), 0x07 & (tmp >> 4)); |
| 50 |
tmp = ctr.readUint8(csr++); |
| 51 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::TL), 0x7f & tmp); |
| 52 |
tmp = ctr.readUint8(csr++); |
| 53 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::AR), 0x1f & tmp); |
| 54 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::KS), tmp >> 6); |
| 55 |
tmp = ctr.readUint8(csr++); |
| 56 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::DR), 0x1f & tmp); |
| 57 |
tmp = ctr.readUint8(csr++); |
| 58 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SR), 0x1f & tmp); |
| 59 |
tmp = ctr.readUint8(csr++); |
| 60 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::RR), 0x0f & tmp); |
| 61 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SL), tmp >> 4); |
| 62 |
int ssgeg = ctr.readUint8(csr++); |
| 63 |
ssgeg = ssgeg & 8 ? ssgeg & 7 : -1; |
| 64 |
instManLocked->setEnvelopeFMParameter(envIdx, params.at(FMOperatorParameter::SSGEG), ssgeg); |
| 65 |
csr += 9; |
| 66 |
} |
| 67 |
|
| 68 |
instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::AL, ctr.readUint8(0x40)); |
| 69 |
instManLocked->setEnvelopeFMParameter(envIdx, FMEnvelopeParameter::FB, ctr.readUint8(0x41)); |
| 70 |
|
| 71 |
return inst; |
| 72 |
} |
| 73 |
} |