| 1 |
/* |
| 2 |
* Copyright (C) 2021-2023 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 "pps_io.hpp" |
| 27 |
#include <vector> |
| 28 |
#include "instrument.hpp" |
| 29 |
#include "file_io_error.hpp" |
| 30 |
#include "chip/codec/ymb_codec.hpp" |
| 31 |
|
| 32 |
namespace io |
| 33 |
{ |
| 34 |
PpsIO::PpsIO() : AbstractBankIO("pps", "PMD PPS", true, false) {} |
| 35 |
|
| 36 |
AbstractBank* PpsIO::load(const BinaryContainer& ctr) const |
| 37 |
{ |
| 38 |
constexpr size_t SAMP_OFFS = 0x54; |
| 39 |
if (ctr.size() < SAMP_OFFS) throw FileCorruptionError(FileType::Bank, 0); |
| 40 |
|
| 41 |
std::vector<int> ids; |
| 42 |
std::vector<std::vector<uint8_t>> samples; |
| 43 |
size_t globCsr = 0; |
| 44 |
constexpr int MAX_CNT = 14; |
| 45 |
for (int i = 0; i < MAX_CNT; ++i) { |
| 46 |
uint16_t start = ctr.readUint16(globCsr); |
| 47 |
globCsr += 2; |
| 48 |
uint16_t len = ctr.readUint16(globCsr); |
| 49 |
globCsr += 4; // Skip 2 bytes |
| 50 |
|
| 51 |
if (len) { |
| 52 |
ids.push_back(i); |
| 53 |
|
| 54 |
std::vector<uint8_t>&& smp = ctr.getSubcontainer(start, len).toVector(); |
| 55 |
std::vector<int16_t> buf(smp.size() * 2); |
| 56 |
for (size_t i = 0; i < smp.size(); ++i) { |
| 57 |
uint8_t sample = smp[i]; |
| 58 |
buf[i] = (static_cast<int16_t>(sample >> 4) - 8) << 12; |
| 59 |
buf[i + 1] = (static_cast<int16_t>(sample & 0x0f) - 8) << 12; |
| 60 |
} |
| 61 |
codec::ymb_encode(buf.data(), smp.data(), buf.size()); |
| 62 |
samples.push_back(std::move(smp)); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return new PpsBank(ids, samples); |
| 67 |
} |
| 68 |
|
| 69 |
AbstractInstrument* PpsIO::loadInstrument(const std::vector<uint8_t>& sample, |
| 70 |
std::weak_ptr<InstrumentsManager> instMan, |
| 71 |
int instNum) |
| 72 |
{ |
| 73 |
std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock(); |
| 74 |
int sampIdx = instManLocked->findFirstAssignableSampleADPCM(); |
| 75 |
if (sampIdx < 0) throw FileCorruptionError(FileType::Bank, 0); |
| 76 |
|
| 77 |
InstrumentADPCM* adpcm = new InstrumentADPCM(instNum, "", instManLocked.get()); |
| 78 |
adpcm->setSampleNumber(sampIdx); |
| 79 |
|
| 80 |
instManLocked->storeSampleADPCMRawSample(sampIdx, sample); |
| 81 |
instManLocked->setSampleADPCMRootKeyNumber(sampIdx, 67); // o5g |
| 82 |
instManLocked->setSampleADPCMRootDeltaN(sampIdx, 0x49cd); // 16000Hz |
| 83 |
instManLocked->setSampleADPCMRepeatEnabled(sampIdx, false); |
| 84 |
instManLocked->setSampleADPCMRepeatrange(sampIdx, SampleRepeatRange(0, (sample.size() - 1) >> 5)); |
| 85 |
|
| 86 |
return adpcm; |
| 87 |
} |
| 88 |
} |