Debian Codegraph @

BambooTracker/instrument/bank.cpp

raw
1 /*
2 * Copyright (C) 2018-2021 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 "bank.hpp"
27 #include <cstdio>
28 #include "io/instrument_io.hpp"
29 #include "io/opni_io.hpp"
30 #include "io/btb_io.hpp"
31 #include "io/ff_io.hpp"
32 #include "io/ppc_io.hpp"
33 #include "io/p86_io.hpp"
34 #include "io/pps_io.hpp"
35 #include "io/pvi_io.hpp"
36 #include "io/pzi_io.hpp"
37 #include "io/dat_io.hpp"
38 #include "io/pmb_io.hpp"
39 #include "format/wopn_file.h"
40
41 BtBank::BtBank(const std::vector<int>& ids, const std::vector<std::string>& names)
42 : ids_(ids),
43 names_(names)
44 {
45 }
46
47 BtBank::BtBank(const std::vector<int>& ids, const std::vector<std::string>& names,
48 const std::vector<io::BinaryContainer>& instSecs, const io::BinaryContainer& propSec, uint32_t version)
49 : instCtrs_(instSecs),
50 propCtr_(propSec),
51 ids_(ids),
52 names_(names),
53 version_(version)
54 {
55 }
56
57 size_t BtBank::getNumInstruments() const
58 {
59 return ids_.size();
60 }
61
62 std::string BtBank::getInstrumentIdentifier(size_t index) const
63 {
64 return std::to_string(ids_.at(index));
65 }
66
67 std::string BtBank::getInstrumentName(size_t index) const
68 {
69 return names_.at(index);
70 }
71
72 AbstractInstrument* BtBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
73 {
74 return io::BtbIO::loadInstrument(instCtrs_.at(static_cast<size_t>(index)), propCtr_, instMan, instNum, version_);
75 }
76
77 /******************************/
78 void WopnBank::WOPNDeleter::operator()(WOPNFile *x)
79 {
80 WOPN_Free(x);
81 }
82
83 struct WopnBank::InstEntry
84 {
85 WOPNInstrument *inst;
86 struct ValuesType
87 {
88 bool percussive : 1;
89 unsigned msb : 7;
90 unsigned lsb : 7;
91 unsigned nth : 7;
92 } vals;
93 };
94
95 WopnBank::WopnBank(WOPNFile* wopn)
96 : wopn_(wopn)
97 {
98 unsigned numM = wopn->banks_count_melodic;
99 unsigned numP = wopn->banks_count_percussion;
100
101 size_t instMax = 128 * (numP + numM);
102 entries_.reserve(instMax);
103
104 for (size_t i = 0; i < instMax; ++i) {
105 InstEntry ent;
106 ent.vals.percussive = (i / 128) >= numM;
107 WOPNBank& bank = ent.vals.percussive
108 ? wopn->banks_percussive[(i / 128) - numM]
109 : wopn->banks_melodic[i / 128];
110 ent.vals.msb = bank.bank_midi_msb;
111 ent.vals.lsb = bank.bank_midi_lsb;
112 ent.vals.nth = i % 128;
113 ent.inst = &bank.ins[ent.vals.nth];
114 if ((ent.inst->inst_flags & WOPN_Ins_IsBlank) == 0)
115 entries_.push_back(ent);
116 }
117
118 entries_.shrink_to_fit();
119 }
120
121 size_t WopnBank::getNumInstruments() const
122 {
123 return entries_.size();
124 }
125
126 std::string WopnBank::getInstrumentIdentifier(size_t index) const
127 {
128 const InstEntry& ent = entries_.at(index);
129 char identifier[64];
130 std::sprintf(identifier, "%c%03d:%03d:%03d", "MP"[ent.vals.percussive],
131 ent.vals.msb, ent.vals.lsb, ent.vals.nth);
132 return identifier;
133 }
134
135 std::string WopnBank::getInstrumentName(size_t index) const
136 {
137 const InstEntry& ent = entries_.at(index);
138 return ent.inst->inst_name;
139 }
140
141 AbstractInstrument* WopnBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
142 {
143 const InstEntry& ent = entries_.at(index);
144 return io::OpniIO::loadWOPNInstrument(*ent.inst, instMan, instNum);
145 }
146
147 /******************************/
148 FfBank::FfBank(const std::vector<int>& ids, const std::vector<std::string>& names, const std::vector<io::BinaryContainer>& ctrs)
149 : ids_(ids), names_(names), instCtrs_(ctrs)
150 {
151 }
152
153 size_t FfBank::getNumInstruments() const
154 {
155 return ids_.size();
156 }
157
158 std::string FfBank::getInstrumentIdentifier(size_t index) const
159 {
160 return std::to_string(ids_.at(index));
161 }
162
163 std::string FfBank::getInstrumentName(size_t index) const
164 {
165 return names_.at(index);
166 }
167
168 AbstractInstrument* FfBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
169 {
170 return io::FfIO::loadInstrument(instCtrs_.at(index), names_.at(index), instMan, instNum);
171 }
172
173 void FfBank::setInstrumentName(size_t index, const std::string& name)
174 {
175 names_.at(index) = name;
176 }
177
178 /******************************/
179 PpcBank::PpcBank(const std::vector<int>& ids, const std::vector<std::vector<uint8_t>>& samples)
180 : ids_(ids), samples_(samples)
181 {
182 }
183
184 size_t PpcBank::getNumInstruments() const
185 {
186 return samples_.size();
187 }
188
189 std::string PpcBank::getInstrumentIdentifier(size_t index) const
190 {
191 return std::to_string(ids_.at(index));
192 }
193
194 std::string PpcBank::getInstrumentName(size_t) const
195 {
196 return "";
197 }
198
199 AbstractInstrument* PpcBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
200 {
201 return io::PpcIO::loadInstrument(samples_.at(index), instMan, instNum);
202 }
203
204 /******************************/
205 P86Bank::P86Bank(const std::vector<int>& ids, const std::vector<std::vector<uint8_t>>& samples)
206 : ids_(ids), samples_(samples)
207 {
208 }
209
210 size_t P86Bank::getNumInstruments() const
211 {
212 return samples_.size();
213 }
214
215 std::string P86Bank::getInstrumentIdentifier(size_t index) const
216 {
217 return std::to_string(ids_.at(index));
218 }
219
220 std::string P86Bank::getInstrumentName(size_t) const
221 {
222 return "";
223 }
224
225 AbstractInstrument* P86Bank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
226 {
227 return io::P86IO::loadInstrument(samples_.at(index), instMan, instNum);
228 }
229
230 /******************************/
231 PpsBank::PpsBank(const std::vector<int>& ids, const std::vector<std::vector<uint8_t>>& samples)
232 : ids_(ids), samples_(samples)
233 {
234 }
235
236 size_t PpsBank::getNumInstruments() const
237 {
238 return samples_.size();
239 }
240
241 std::string PpsBank::getInstrumentIdentifier(size_t index) const
242 {
243 return std::to_string(ids_.at(index));
244 }
245
246 std::string PpsBank::getInstrumentName(size_t) const
247 {
248 return "";
249 }
250
251 AbstractInstrument* PpsBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
252 {
253 return io::PpsIO::loadInstrument(samples_.at(index), instMan, instNum);
254 }
255
256 /******************************/
257 PviBank::PviBank(const std::vector<int>& ids, uint16_t deltaN, const std::vector<std::vector<uint8_t>>& samples)
258 : ids_(ids), deltaN_(deltaN), samples_(samples)
259 {
260 }
261
262 size_t PviBank::getNumInstruments() const
263 {
264 return samples_.size();
265 }
266
267 std::string PviBank::getInstrumentIdentifier(size_t index) const
268 {
269 return std::to_string(ids_.at(index));
270 }
271
272 std::string PviBank::getInstrumentName(size_t) const
273 {
274 return "";
275 }
276
277 AbstractInstrument* PviBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
278 {
279 return io::PviIO::loadInstrument(samples_.at(index), deltaN_, instMan, instNum);
280 }
281
282 /******************************/
283 PziBank::PziBank(const std::vector<int>& ids, const std::vector<int>& deltaNs,
284 const std::vector<bool>& isRepeatedList, const std::vector<std::vector<uint8_t>>& samples)
285 : ids_(ids), deltaNs_(deltaNs), isRepeatedList_(isRepeatedList), samples_(samples)
286 {
287 }
288
289 size_t PziBank::getNumInstruments() const
290 {
291 return samples_.size();
292 }
293
294 std::string PziBank::getInstrumentIdentifier(size_t index) const
295 {
296 return std::to_string(ids_.at(index));
297 }
298
299 std::string PziBank::getInstrumentName(size_t) const
300 {
301 return "";
302 }
303
304 AbstractInstrument* PziBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
305 {
306 return io::PziIO::loadInstrument(samples_.at(index), deltaNs_.at(index), isRepeatedList_.at(index), instMan, instNum);
307 }
308
309 /******************************/
310 Mucom88Bank::Mucom88Bank(const std::vector<int>& ids, const std::vector<std::string>& names, const std::vector<io::BinaryContainer>& ctrs)
311 : ids_(ids), names_(names), instCtrs_(ctrs)
312 {
313 }
314
315 size_t Mucom88Bank::getNumInstruments() const
316 {
317 return ids_.size();
318 }
319
320 std::string Mucom88Bank::getInstrumentIdentifier(size_t index) const
321 {
322 return std::to_string(ids_.at(index));
323 }
324
325 std::string Mucom88Bank::getInstrumentName(size_t index) const
326 {
327 return names_.at(index);
328 }
329
330 AbstractInstrument* Mucom88Bank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
331 {
332 return io::DatIO::loadInstrument(instCtrs_.at(index), names_.at(index), instMan, instNum);
333 }
334
335 void Mucom88Bank::setInstrumentName(size_t index, const std::string& name)
336 {
337 names_.at(index) = name;
338 }
339
340 /******************************/
341 PmbBank::PmbBank(const std::vector<int>& ids, const std::vector<std::string>& names, const std::vector<std::vector<uint8_t>>& samples)
342 : ids_(ids), names_(names), samples_(samples)
343 {
344 }
345
346 size_t PmbBank::getNumInstruments() const
347 {
348 return ids_.size();
349 }
350
351 std::string PmbBank::getInstrumentIdentifier(size_t index) const
352 {
353 return std::to_string(ids_.at(index));
354 }
355
356 std::string PmbBank::getInstrumentName(size_t index) const
357 {
358 return names_.at(index);
359 }
360
361 AbstractInstrument* PmbBank::loadInstrument(size_t index, std::weak_ptr<InstrumentsManager> instMan, int instNum) const
362 {
363 return io::PmbIO::loadInstrument(samples_.at(index), names_.at(index), instMan, instNum);
364 }
365
366 void PmbBank::setInstrumentName(size_t index, const std::string& name)
367 {
368 names_.at(index) = name;
369 }