Debian Codegraph @

BambooTracker/io/pvi_io.cpp

raw
1 /*
2 * Copyright (C) 2020-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 "pvi_io.hpp"
27 #include <vector>
28 #include "instrument.hpp"
29 #include "file_io_error.hpp"
30
31 namespace io
32 {
33 PviIO::PviIO() : AbstractBankIO("pvi", "FMP PVI", true, false) {}
34
35 AbstractBank* PviIO::load(const BinaryContainer& ctr) const
36 {
37 std::string ident = ctr.readString(0, 4);
38 if (ident != "PVI1" && ident != "PVI2")
39 throw FileCorruptionError(FileType::Bank, 0);
40 uint16_t deltaN = ctr.readUint16(8);
41 uint8_t cnt = ctr.readUint8(11);
42
43 constexpr size_t sampOffs = 0x10 + 128 * 4;
44 if (ctr.size() < sampOffs) throw FileCorruptionError(FileType::Bank, 0x10);
45
46 std::vector<int> ids;
47 std::vector<std::vector<uint8_t>> samples;
48 size_t offs = 0;
49 size_t addrPos = 0x10;
50 for (size_t i = 0; i < cnt; ++i) {
51 uint16_t start = ctr.readUint16(addrPos);
52 addrPos += 2;
53 uint16_t stop = ctr.readUint16(addrPos);
54 addrPos += 2;
55
56 if (start < stop) {
57 if (ids.empty()) offs = start;
58 ids.push_back(static_cast<int>(i));
59 size_t st = sampOffs + static_cast<size_t>((start - offs) << 5);
60 size_t sampSize = std::min<size_t>((stop + 1u - start) << 5, ctr.size() - st);
61 samples.push_back(ctr.getSubcontainer(st, sampSize).toVector());
62 }
63 }
64 /* if (ids.size() != cnt) throw FileCorruptionError(FileType::Bank, 11); */
65
66 return new PviBank(ids, deltaN, samples);
67 }
68
69 AbstractInstrument* PviIO::loadInstrument(const std::vector<uint8_t>& sample, uint16_t deltaN,
70 std::weak_ptr<InstrumentsManager> instMan, int instNum)
71 {
72 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
73 int sampIdx = instManLocked->findFirstAssignableSampleADPCM();
74 if (sampIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
75
76 InstrumentADPCM* adpcm = new InstrumentADPCM(instNum, "", instManLocked.get());
77 adpcm->setSampleNumber(sampIdx);
78
79 instManLocked->storeSampleADPCMRawSample(sampIdx, sample);
80 instManLocked->setSampleADPCMRootKeyNumber(sampIdx, 60); // o5c
81 instManLocked->setSampleADPCMRootDeltaN(sampIdx, deltaN);
82 instManLocked->setSampleADPCMRepeatEnabled(sampIdx, false);
83 instManLocked->setSampleADPCMRepeatrange(sampIdx, SampleRepeatRange(0, (sample.size() - 1) >> 5));
84
85 return adpcm;
86 }
87 }