Debian Codegraph @

BambooTracker/io/p86_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 "p86_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 namespace
35 {
36 inline uint32_t readUint24(const BinaryContainer& ctr, size_t offset)
37 {
38 return ctr.readUint8(offset) | (ctr.readUint8(offset + 1) << 8u) | (ctr.readUint8(offset + 2) << 16u);
39 }
40 }
41
42 P86IO::P86IO() : AbstractBankIO("p86", "PMD P86", true, false) {}
43
44 AbstractBank* P86IO::load(const BinaryContainer& ctr) const
45 {
46 using namespace std::literals::string_literals;
47
48 std::string id = ctr.readString(0, 12);
49 if (id != "PCM86 DATA\n\0"s)
50 throw FileCorruptionError(FileType::Bank, 0);
51 uint32_t size = readUint24(ctr, 13);
52 if (size != ctr.size())
53 throw FileCorruptionError(FileType::Bank, 13);
54
55 constexpr size_t SAMP_OFFS = 0x610;
56 if (ctr.size() < SAMP_OFFS) throw FileCorruptionError(FileType::Bank, 0x10);
57
58 std::vector<int> ids;
59 std::vector<std::vector<uint8_t>> samples;
60 size_t globCsr = 0x10;
61 size_t offs = 0;
62 constexpr int MAX_CNT = 256;
63 for (int i = 0; i < MAX_CNT; ++i) {
64 uint32_t start = readUint24(ctr, globCsr);
65 globCsr += 3;
66 uint32_t len = readUint24(ctr, globCsr);
67 globCsr += 3;
68
69 if (len) {
70 if (ids.empty()) offs = start;
71 ids.push_back(i);
72
73 std::vector<uint8_t>&& smp = ctr.getSubcontainer(SAMP_OFFS + start - offs, len).toVector();
74 std::vector<int16_t> buf(smp.size());
75 std::transform(smp.begin(), smp.end(), buf.begin(), [](uint8_t v) {
76 return static_cast<int16_t>(static_cast<int8_t>(v)) << 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 P86Bank(ids, samples);
87 }
88
89 AbstractInstrument* P86IO::loadInstrument(const std::vector<uint8_t>& sample,
90 std::weak_ptr<InstrumentsManager> instMan,
91 int instNum)
92 {
93 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
94 int sampIdx = instManLocked->findFirstAssignableSampleADPCM();
95 if (sampIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
96
97 InstrumentADPCM* adpcm = new InstrumentADPCM(instNum, "", instManLocked.get());
98 adpcm->setSampleNumber(sampIdx);
99
100 instManLocked->storeSampleADPCMRawSample(sampIdx, sample);
101 instManLocked->setSampleADPCMRootKeyNumber(sampIdx, 67); // o5g
102 instManLocked->setSampleADPCMRootDeltaN(sampIdx, 0x4a0d); // 16540Hz
103 instManLocked->setSampleADPCMRepeatEnabled(sampIdx, false);
104 instManLocked->setSampleADPCMRepeatrange(sampIdx, SampleRepeatRange(0, (sample.size() - 1) >> 5));
105
106 return adpcm;
107 }
108 }