Debian Codegraph @

BambooTracker/io/ppc_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 "ppc_io.hpp"
27 #include <vector>
28 #include "instrument.hpp"
29 #include "file_io_error.hpp"
30
31 namespace io
32 {
33 PpcIO::PpcIO() : AbstractBankIO("ppc", "PMD PPC", true, false) {}
34
35 AbstractBank* PpcIO::load(const BinaryContainer& ctr) const
36 {
37 size_t globCsr = 0;
38 if (ctr.readString(globCsr, 30) != "ADPCM DATA for PMD ver.4.4- ")
39 throw FileCorruptionError(FileType::Bank, globCsr);
40 globCsr += 30;
41 uint16_t nextAddr = ctr.readUint16(globCsr);
42 if ((nextAddr - 0x26u) * 0x20u + 0x420u != ctr.size()) // File size check
43 throw FileCorruptionError(FileType::Bank, globCsr);
44 globCsr += 2;
45
46 size_t sampOffs = globCsr + 256 * 4;
47 if (ctr.size() < sampOffs) throw FileCorruptionError(FileType::Bank, globCsr);
48
49 std::vector<int> ids;
50 std::vector<std::vector<uint8_t>> samples;
51 size_t offs = 0;
52 constexpr int MAX_CNT = 256;
53 for (int i = 0; i < MAX_CNT; ++i) {
54 uint16_t start = ctr.readUint16(globCsr);
55 globCsr += 2;
56 uint16_t stop = ctr.readUint16(globCsr);
57 globCsr += 2;
58
59 if (start < stop) {
60 if (ids.empty()) offs = start;
61 ids.push_back(i);
62 size_t st = sampOffs + static_cast<size_t>((start - offs) << 5);
63 size_t sampSize = std::min<size_t>((stop + 1u - start) << 5, ctr.size() - st);
64 samples.push_back(ctr.getSubcontainer(st, sampSize).toVector());
65 }
66 }
67
68 return new PpcBank(ids, samples);
69 }
70
71 AbstractInstrument* PpcIO::loadInstrument(const std::vector<uint8_t>& sample,
72 std::weak_ptr<InstrumentsManager> instMan,
73 int instNum)
74 {
75 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
76 int sampIdx = instManLocked->findFirstAssignableSampleADPCM();
77 if (sampIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
78
79 InstrumentADPCM* adpcm = new InstrumentADPCM(instNum, "", instManLocked.get());
80 adpcm->setSampleNumber(sampIdx);
81
82 instManLocked->storeSampleADPCMRawSample(sampIdx, sample);
83 instManLocked->setSampleADPCMRootKeyNumber(sampIdx, 67); // o5g
84 instManLocked->setSampleADPCMRootDeltaN(sampIdx, 0x49cd); // 16000Hz
85 instManLocked->setSampleADPCMRepeatEnabled(sampIdx, false);
86 instManLocked->setSampleADPCMRepeatrange(sampIdx, SampleRepeatRange(0, (sample.size() - 1) >> 5));
87
88 return adpcm;
89 }
90 }