Debian Codegraph @

BambooTracker/io/pmb_io.cpp

raw
1 /*
2 * Copyright (C) 2022-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 "pmb_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 PmbIO::PmbIO() : AbstractBankIO("pmb", "FM Towns PMB", true, false) {}
36
37 AbstractBank* PmbIO::load(const BinaryContainer& ctr) const
38 {
39 /* no identifier & weird header inconsistencies across samples, just don't bother with the header */
40 constexpr size_t HEADER_SIZE = 0x1008;
41 if (ctr.size() < HEADER_SIZE) throw FileCorruptionError(FileType::Bank, 0);
42
43 std::vector<int> ids;
44 std::vector<std::string> names;
45 std::vector<std::vector<uint8_t>> samples;
46 size_t globCsr = HEADER_SIZE;
47 constexpr int MAX_CNT = 32;
48 for (int i = 0; i < MAX_CNT; ++i) {
49 // cannot determine how many samples there will be, just stop when we've reached EOF
50 if (globCsr == ctr.size()) break;
51 std::string name = ctr.readString(globCsr, 8);
52 globCsr += 8;
53 // ignore sample ID, they may override each other or disagree with the ones in the header
54 // uint32_t id = ctr.readUint32(globCsr);
55 globCsr += 4;
56 uint32_t len = ctr.readUint32(globCsr);
57 globCsr += 4;
58
59 // unknown data
60 globCsr += 16;
61
62 ids.push_back(i);
63 names.push_back(name);
64
65 std::vector<uint8_t>&& smp = ctr.getSubcontainer(globCsr, len).toVector();
66
67 std::vector<int16_t> buf(smp.size());
68 std::transform(smp.begin(), smp.end(), buf.begin(), [globCsr](uint8_t v) {
69 // first convert from RF5C68 encoding to more regular unsigned 8-bit
70 // summarised from the datasheet:
71 // 0x00 -> 0x80 (not mentioned in datasheet but seems to work like this in banks)
72 // 0x01 - 0x7F -> 0x7F - 0x01
73 // 0x80 - 0xFE -> 0x80 - 0xFE (unchanged)
74 // 0xFF -> trigger for jumping into loop. unsure if occurs in PMB banks, loops not handled by us anyway. error if encountered
75 if (v == 0xFF) throw FileCorruptionError (FileType::Bank, globCsr);
76 uint8_t regular = (v < 0x80) ? (0x80 - v) : v;
77
78 // now convert this into the format ADPCM conversion requires
79 uint16_t grown = static_cast<uint16_t>(regular + 0x80) << 8;
80 return *(reinterpret_cast<int16_t *> (&grown));
81 });
82 smp.resize((smp.size() + 1) / 2);
83 smp.shrink_to_fit();
84 smp.back() = 0; // Clear last data
85 codec::ymb_encode(buf.data(), smp.data(), buf.size());
86 samples.push_back(std::move(smp));
87 globCsr += len;
88 }
89
90 return new PmbBank(ids, names, samples);
91 }
92
93 AbstractInstrument* PmbIO::loadInstrument(const std::vector<uint8_t>& sample,
94 std::string name,
95 std::weak_ptr<InstrumentsManager> instMan,
96 int instNum)
97 {
98 std::shared_ptr<InstrumentsManager> instManLocked = instMan.lock();
99 int sampIdx = instManLocked->findFirstAssignableSampleADPCM();
100 if (sampIdx < 0) throw FileCorruptionError(FileType::Bank, 0);
101
102 InstrumentADPCM* adpcm = new InstrumentADPCM(instNum, name, instManLocked.get());
103 adpcm->setSampleNumber(sampIdx);
104
105 instManLocked->storeSampleADPCMRawSample(sampIdx, sample);
106 instManLocked->setSampleADPCMRootKeyNumber(sampIdx, 67); // o5g
107 instManLocked->setSampleADPCMRootDeltaN(sampIdx, 0x49cd); // 16000Hz
108 instManLocked->setSampleADPCMRepeatEnabled(sampIdx, false);
109 instManLocked->setSampleADPCMRepeatrange(sampIdx, SampleRepeatRange(0, (sample.size() - 1) >> 5));
110
111 return adpcm;
112 }
113 }