Debian Codegraph @

BambooTracker/io/pzi_io.cpp

raw
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 "pzi_io.hpp"
27 #include <vector>
28 #include <limits>
29 #include "instrument.hpp"
30 #include "file_io_error.hpp"
31 #include "chip/codec/ymb_codec.hpp"
32
33 namespace io
34 {
35 PziIO::PziIO() : AbstractBankIO("pzi", "FMP PZI", true, false) {}
36
37 AbstractBank* PziIO::load(const BinaryContainer& ctr) const
38 {
39 std::string ident = ctr.readString(0, 4);
40 if (ident != "PZI0" && ident != "PZI1")
41 throw FileCorruptionError(FileType::Bank, 0);
42
43 constexpr size_t SAMP_OFFS = 0x920;
44 if (ctr.size() < SAMP_OFFS) throw FileCorruptionError(FileType::Bank, 0x20);
45
46 std::vector<int> ids;
47 std::vector<std::vector<uint8_t>> samples;
48 std::vector<bool> isRepeatedList;
49 std::vector<int> deltaNs;
50 size_t globCsr = 0x20;
51 constexpr int MAX_CNT = 128;
52 for (int i = 0; i < MAX_CNT; ++i) {
53 uint32_t start = ctr.readUint32(globCsr);
54 globCsr += 4;
55 uint32_t len = ctr.readUint32(globCsr);
56 globCsr += 4;
57 uint32_t loopStart = ctr.readUint32(globCsr);
58 globCsr += 4;
59 uint32_t loopEnd = ctr.readUint32(globCsr);
60 globCsr += 4;
61 uint16_t sr = ctr.readUint16(globCsr);
62 globCsr += 2;
63
64 // only support loop within entire region
65 bool isRepeated = (!loopStart && len == loopEnd);
66
67 if (len) {
68 ids.push_back(i);
69 isRepeatedList.push_back(isRepeated);
70 deltaNs.push_back(SampleADPCM::calculateADPCMDeltaN(sr));
71
72 std::vector<uint8_t>&& smp = ctr.getSubcontainer(SAMP_OFFS + start, len).toVector();
73 std::vector<int16_t> buf(smp.size());
74 std::transform(smp.begin(), smp.end(), buf.begin(), [](uint8_t v) {
75 // Centering
76 return (static_cast<int16_t>(v) - std::numeric_limits<int8_t>::max()) << 8;
77 });
78 smp.resize((smp.size() + 1) / 2);
79 smp.shrink_to_fit();
80 smp.back() = 0; // Clear last data
81 codec::ymb_encode(buf.data(), smp.data(), buf.size());
82 samples.push_back(std::move(smp));
83 }
84 }
85
86 return new PziBank(ids, deltaNs, isRepeatedList, samples);
87 }
88
89 AbstractInstrument* PziIO::loadInstrument(const std::vector<uint8_t>& sample,
90 int deltaN, bool isRepeated,
91 std::weak_ptr<InstrumentsManager> instMan,
92 int instNum)
93 {
94 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
95 int sampIdx = instManLocked->findFirstAssignableSampleADPCM();
96 if (sampIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
97
98 InstrumentADPCM* adpcm = new InstrumentADPCM(instNum, "", instManLocked.get());
99 adpcm->setSampleNumber(sampIdx);
100
101 instManLocked->storeSampleADPCMRawSample(sampIdx, sample);
102 instManLocked->setSampleADPCMRootKeyNumber(sampIdx, 67); // o5g
103 instManLocked->setSampleADPCMRootDeltaN(sampIdx, deltaN);
104 instManLocked->setSampleADPCMRepeatEnabled(sampIdx, isRepeated);
105 instManLocked->setSampleADPCMRepeatrange(sampIdx, SampleRepeatRange(0, (sample.size() - 1) >> 5));
106
107 return adpcm;
108 }
109 }