Debian Codegraph @

BambooTracker/format/wopn_file.c

raw
1 /*
2 * Wohlstand's OPN2 Bank File - a bank format to store OPN2 timbre data and setup
3 *
4 * Copyright (c) 2018 Vitaly Novichkov <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * 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 NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "wopn_file.h"
26 #include <string.h>
27 #include <stdlib.h>
28
29 static const char *wopn2_magic1 = "WOPN2-BANK\0";
30 static const char *wopn2_magic2 = "WOPN2-B2NK\0";
31 static const char *opni_magic1 = "WOPN2-INST\0";
32 static const char *opni_magic2 = "WOPN2-IN2T\0";
33
34 static const uint16_t wopn_latest_version = 2;
35
36 enum
37 {
38 WOPN_INST_SIZE_V1 = 65,
39 WOPN_INST_SIZE_V2 = 69
40 };
41
42 static uint16_t toUint16LE(const uint8_t *arr)
43 {
44 uint16_t num = arr[0];
45 num |= ((arr[1] << 8) & 0xFF00);
46 return num;
47 }
48
49 static uint16_t toUint16BE(const uint8_t *arr)
50 {
51 uint16_t num = arr[1];
52 num |= ((arr[0] << 8) & 0xFF00);
53 return num;
54 }
55
56 static int16_t toSint16BE(const uint8_t *arr)
57 {
58 int16_t num = *(const int8_t *)(&arr[0]);
59 num *= 1 << 8;
60 num |= arr[1];
61 return num;
62 }
63
64 static void fromUint16LE(uint16_t in, uint8_t *arr)
65 {
66 arr[0] = in & 0x00FF;
67 arr[1] = (in >> 8) & 0x00FF;
68 }
69
70 static void fromUint16BE(uint16_t in, uint8_t *arr)
71 {
72 arr[1] = in & 0x00FF;
73 arr[0] = (in >> 8) & 0x00FF;
74 }
75
76 static void fromSint16BE(int16_t in, uint8_t *arr)
77 {
78 arr[1] = in & 0x00FF;
79 arr[0] = ((uint16_t)in >> 8) & 0x00FF;
80 }
81
82
83 WOPNFile *WOPN_Init(uint16_t melodic_banks, uint16_t percussive_banks)
84 {
85 WOPNFile *file = (WOPNFile*)calloc(1, sizeof(WOPNFile));
86 if(!file)
87 return NULL;
88
89 file->banks_count_melodic = (melodic_banks != 0) ? melodic_banks : 1;
90 file->banks_melodic = (WOPNBank*)calloc(file->banks_count_melodic, sizeof(WOPNBank));
91
92 if(melodic_banks == 0)
93 {
94 unsigned i;
95 for(i = 0; i < 128; ++i)
96 file->banks_melodic[0].ins[i].inst_flags = WOPN_Ins_IsBlank;
97 }
98
99 file->banks_count_percussion = (percussive_banks != 0) ? percussive_banks : 1;
100 file->banks_percussive = (WOPNBank*)calloc(file->banks_count_percussion, sizeof(WOPNBank));
101
102 if(percussive_banks == 0)
103 {
104 unsigned i;
105 for(i = 0; i < 128; ++i)
106 file->banks_percussive[0].ins[i].inst_flags = WOPN_Ins_IsBlank;
107 }
108
109 return file;
110 }
111
112 void WOPN_Free(WOPNFile *file)
113 {
114 if(file)
115 {
116 if(file->banks_melodic)
117 free(file->banks_melodic);
118 if(file->banks_percussive)
119 free(file->banks_percussive);
120 free(file);
121 }
122 }
123
124 int WOPN_BanksCmp(const WOPNFile *bank1, const WOPNFile *bank2)
125 {
126 int res = 1;
127
128 res &= (bank1->version == bank2->version);
129 res &= (bank1->lfo_freq == bank2->lfo_freq);
130 res &= (bank1->chip_type == bank2->chip_type);
131 res &= (bank1->volume_model == bank2->volume_model);
132 res &= (bank1->banks_count_melodic == bank2->banks_count_melodic);
133 res &= (bank1->banks_count_percussion == bank2->banks_count_percussion);
134
135 if(res)
136 {
137 int i;
138 for(i = 0; i < bank1->banks_count_melodic; i++)
139 res &= (memcmp(&bank1->banks_melodic[i], &bank2->banks_melodic[i], sizeof(WOPNBank)) == 0);
140 if(res)
141 {
142 for(i = 0; i < bank1->banks_count_percussion; i++)
143 res &= (memcmp(&bank1->banks_percussive[i], &bank2->banks_percussive[i], sizeof(WOPNBank)) == 0);
144 }
145 }
146
147 return res;
148 }
149
150 static void WOPN_parseInstrument(WOPNInstrument *ins, uint8_t *cursor, uint16_t version, uint8_t has_sounding_delays)
151 {
152 int l;
153 strncpy(ins->inst_name, (const char*)cursor, 32);
154 ins->inst_name[32] = '\0';
155 ins->note_offset = toSint16BE(cursor + 32);
156 ins->midi_velocity_offset = 0; /* TODO: for future version > 2 */
157 ins->percussion_key_number = cursor[34];
158 ins->inst_flags = 0; /* TODO: for future version > 2 */
159 ins->fbalg = cursor[35];
160 ins->lfosens = cursor[36];
161 for(l = 0; l < 4; l++)
162 {
163 size_t off = 37 + (size_t)(l) * 7;
164 ins->operators[l].dtfm_30 = cursor[off + 0];
165 ins->operators[l].level_40 = cursor[off + 1];
166 ins->operators[l].rsatk_50 = cursor[off + 2];
167 ins->operators[l].amdecay1_60 = cursor[off + 3];
168 ins->operators[l].decay2_70 = cursor[off + 4];
169 ins->operators[l].susrel_80 = cursor[off + 5];
170 ins->operators[l].ssgeg_90 = cursor[off + 6];
171 }
172 if((version >= 2) && has_sounding_delays)
173 {
174 ins->delay_on_ms = toUint16BE(cursor + 65);
175 ins->delay_off_ms = toUint16BE(cursor + 67);
176
177 /* Null delays indicate the blank instrument in version 2 */
178 if((version < 3) && ins->delay_on_ms == 0 && ins->delay_off_ms == 0)
179 ins->inst_flags |= WOPN_Ins_IsBlank;
180 }
181 }
182
183 static void WOPN_writeInstrument(WOPNInstrument *ins, uint8_t *cursor, uint16_t version, uint8_t has_sounding_delays)
184 {
185 int l;
186 unsigned int nameLen = strlen(ins->inst_name) + 1;
187 if (32 < nameLen) nameLen = 32;
188 memcpy(cursor, ins->inst_name, nameLen);
189 /*strncpy((char*)cursor, ins->inst_name, 32);*/
190 fromSint16BE(ins->note_offset, cursor + 32);
191 cursor[34] = ins->percussion_key_number;
192 cursor[35] = ins->fbalg;
193 cursor[36] = ins->lfosens;
194 for(l = 0; l < 4; l++)
195 {
196 size_t off = 37 + (size_t)(l) * 7;
197 cursor[off + 0] = ins->operators[l].dtfm_30;
198 cursor[off + 1] = ins->operators[l].level_40;
199 cursor[off + 2] = ins->operators[l].rsatk_50;
200 cursor[off + 3] = ins->operators[l].amdecay1_60;
201 cursor[off + 4] = ins->operators[l].decay2_70;
202 cursor[off + 5] = ins->operators[l].susrel_80;
203 cursor[off + 6] = ins->operators[l].ssgeg_90;
204 }
205 if((version >= 2) && has_sounding_delays)
206 {
207 if((version < 3) && (ins->inst_flags & WOPN_Ins_IsBlank) != 0)
208 {
209 /* Null delays indicate the blank instrument in version 2 */
210 fromUint16BE(0, cursor + 65);
211 fromUint16BE(0, cursor + 67);
212 }
213 else
214 {
215 fromUint16BE(ins->delay_on_ms, cursor + 65);
216 fromUint16BE(ins->delay_off_ms, cursor + 67);
217 }
218 }
219 }
220
221 WOPNFile *WOPN_LoadBankFromMem(void *mem, size_t length, int *error)
222 {
223 WOPNFile *outFile = NULL;
224 uint16_t i = 0, j = 0, k = 0;
225 uint16_t version = 0;
226 uint16_t count_melodic_banks = 1;
227 uint16_t count_percussive_banks = 1;
228 uint8_t *cursor = (uint8_t *)mem;
229
230 WOPNBank *bankslots[2];
231 uint16_t bankslots_sizes[2];
232
233 #define SET_ERROR(err) \
234 {\
235 WOPN_Free(outFile);\
236 if(error)\
237 {\
238 *error = err;\
239 }\
240 }
241
242 #define GO_FORWARD(bytes) { cursor += bytes; length -= bytes; }
243
244 if(!cursor)
245 {
246 SET_ERROR(WOPN_ERR_NULL_POINTER);
247 return NULL;
248 }
249
250 {/* Magic number */
251 if(length < 11)
252 {
253 SET_ERROR(WOPN_ERR_UNEXPECTED_ENDING);
254 return NULL;
255 }
256 if(memcmp(cursor, wopn2_magic1, 11) == 0)
257 {
258 version = 1;
259 }
260 else if(memcmp(cursor, wopn2_magic2, 11) != 0)
261 {
262 SET_ERROR(WOPN_ERR_BAD_MAGIC);
263 return NULL;
264 }
265 GO_FORWARD(11);
266 }
267
268 if (version == 0)
269 {/* Version code */
270 if(length < 2)
271 {
272 SET_ERROR(WOPN_ERR_UNEXPECTED_ENDING);
273 return NULL;
274 }
275 version = toUint16LE(cursor);
276 if(version > wopn_latest_version)
277 {
278 SET_ERROR(WOPN_ERR_NEWER_VERSION);
279 return NULL;
280 }
281 GO_FORWARD(2);
282 }
283
284 {/* Header of WOPN */
285 uint8_t head[5];
286 if(length < 5)
287 {
288 SET_ERROR(WOPN_ERR_UNEXPECTED_ENDING);
289 return NULL;
290 }
291 memcpy(head, cursor, 5);
292 count_melodic_banks = toUint16BE(head);
293 count_percussive_banks = toUint16BE(head + 2);
294 GO_FORWARD(5);
295
296 outFile = WOPN_Init(count_melodic_banks, count_percussive_banks);
297 if(!outFile)
298 {
299 SET_ERROR(WOPN_ERR_OUT_OF_MEMORY);
300 return NULL;
301 }
302
303 outFile->version = version;
304 outFile->lfo_freq = head[4] & 0xf;
305 if(version >= 2)
306 outFile->chip_type = (head[4] >> 4) & 1;
307 outFile->volume_model = 0;
308 }
309
310 bankslots_sizes[0] = count_melodic_banks;
311 bankslots[0] = outFile->banks_melodic;
312 bankslots_sizes[1] = count_percussive_banks;
313 bankslots[1] = outFile->banks_percussive;
314
315 if(version >= 2) /* Bank names and LSB/MSB titles */
316 {
317 for(i = 0; i < 2; i++)
318 {
319 for(j = 0; j < bankslots_sizes[i]; j++)
320 {
321 if(length < 34)
322 {
323 SET_ERROR(WOPN_ERR_UNEXPECTED_ENDING);
324 return NULL;
325 }
326 strncpy(bankslots[i][j].bank_name, (const char*)cursor, 32);
327 bankslots[i][j].bank_name[32] = '\0';
328 bankslots[i][j].bank_midi_lsb = cursor[32];
329 bankslots[i][j].bank_midi_msb = cursor[33];
330 GO_FORWARD(34);
331 }
332 }
333 }
334
335 {/* Read instruments data */
336 uint16_t insSize = 0;
337 if(version > 1)
338 insSize = WOPN_INST_SIZE_V2;
339 else
340 insSize = WOPN_INST_SIZE_V1;
341 for(i = 0; i < 2; i++)
342 {
343 if(length < (insSize * 128) * (size_t)bankslots_sizes[i])
344 {
345 SET_ERROR(WOPN_ERR_UNEXPECTED_ENDING);
346 return NULL;
347 }
348
349 for(j = 0; j < bankslots_sizes[i]; j++)
350 {
351 for(k = 0; k < 128; k++)
352 {
353 WOPNInstrument *ins = &bankslots[i][j].ins[k];
354 WOPN_parseInstrument(ins, cursor, version, 1);
355 GO_FORWARD(insSize);
356 }
357 }
358 }
359 }
360
361 #undef GO_FORWARD
362 #undef SET_ERROR
363
364 return outFile;
365 }
366
367 int WOPN_LoadInstFromMem(OPNIFile *file, void *mem, size_t length)
368 {
369 uint16_t version = 0;
370 uint8_t *cursor = (uint8_t *)mem;
371 uint16_t ins_size;
372
373 if(!cursor)
374 return WOPN_ERR_NULL_POINTER;
375
376 #define GO_FORWARD(bytes) { cursor += bytes; length -= bytes; }
377
378 {/* Magic number */
379 if(length < 11)
380 return WOPN_ERR_UNEXPECTED_ENDING;
381 if(memcmp(cursor, opni_magic1, 11) == 0)
382 version = 1;
383 else if(memcmp(cursor, opni_magic2, 11) != 0)
384 return WOPN_ERR_BAD_MAGIC;
385 GO_FORWARD(11);
386 }
387
388 if (version == 0) {/* Version code */
389 if(length < 2)
390 return WOPN_ERR_UNEXPECTED_ENDING;
391 version = toUint16LE(cursor);
392 if(version > wopn_latest_version)
393 return WOPN_ERR_NEWER_VERSION;
394 GO_FORWARD(2);
395 }
396
397 file->version = version;
398
399 {/* is drum flag */
400 if(length < 1)
401 return WOPN_ERR_UNEXPECTED_ENDING;
402 file->is_drum = *cursor;
403 GO_FORWARD(1);
404 }
405
406 if(version > 1)
407 /* Skip sounding delays are not part of single-instrument file
408 * two sizes of uint16_t will be subtracted */
409 ins_size = WOPN_INST_SIZE_V2 - (sizeof(uint16_t) * 2);
410 else
411 ins_size = WOPN_INST_SIZE_V1;
412
413 if(length < ins_size)
414 return WOPN_ERR_UNEXPECTED_ENDING;
415
416 WOPN_parseInstrument(&file->inst, cursor, version, 0);
417 GO_FORWARD(ins_size);
418
419 return WOPN_ERR_OK;
420 #undef GO_FORWARD
421 }
422
423 size_t WOPN_CalculateBankFileSize(WOPNFile *file, uint16_t version)
424 {
425 size_t final_size = 0;
426 size_t ins_size = 0;
427
428 if(version == 0)
429 version = wopn_latest_version;
430
431 if(!file)
432 return 0;
433 final_size += 11 + 2 + 2 + 2 + 1;
434 /*
435 * Magic number,
436 * Version,
437 * Count of melodic banks,
438 * Count of percussive banks,
439 * Chip specific flags
440 */
441
442 if(version >= 2)
443 {
444 /* Melodic banks meta-data */
445 final_size += (32 + 1 + 1) * file->banks_count_melodic;
446 /* Percussive banks meta-data */
447 final_size += (32 + 1 + 1) * file->banks_count_percussion;
448 }
449
450 if(version >= 2)
451 ins_size = WOPN_INST_SIZE_V2;
452 else
453 ins_size = WOPN_INST_SIZE_V1;
454 /* Melodic instruments */
455 final_size += (ins_size * 128) * file->banks_count_melodic;
456 /* Percussive instruments */
457 final_size += (ins_size * 128) * file->banks_count_percussion;
458
459 return final_size;
460 }
461
462 size_t WOPN_CalculateInstFileSize(OPNIFile *file, uint16_t version)
463 {
464 size_t final_size = 0;
465 size_t ins_size = 0;
466
467 if(version == 0)
468 version = wopn_latest_version;
469
470 if(!file)
471 return 0;
472 final_size += 11 + 1;
473 /*
474 * Magic number,
475 * is percussive instrument
476 */
477
478 /* Version */
479 if (version > 1)
480 final_size += 2;
481
482 if(version > 1)
483 /* Skip sounding delays are not part of single-instrument file
484 * two sizes of uint16_t will be subtracted */
485 ins_size = WOPN_INST_SIZE_V2 - (sizeof(uint16_t) * 2);
486 else
487 ins_size = WOPN_INST_SIZE_V1;
488 final_size += ins_size;
489
490 return final_size;
491 }
492
493 int WOPN_SaveBankToMem(WOPNFile *file, void *dest_mem, size_t length, uint16_t version, uint16_t force_gm)
494 {
495 uint8_t *cursor = (uint8_t *)dest_mem;
496 uint16_t ins_size = 0;
497 uint16_t i, j, k;
498 uint16_t banks_melodic = force_gm ? 1 : file->banks_count_melodic;
499 uint16_t banks_percussive = force_gm ? 1 : file->banks_count_percussion;
500
501 WOPNBank *bankslots[2];
502 uint16_t bankslots_sizes[2];
503
504 if(version == 0)
505 version = wopn_latest_version;
506
507 #define GO_FORWARD(bytes) { cursor += bytes; length -= bytes; }
508
509 if(length < 11)
510 return WOPN_ERR_UNEXPECTED_ENDING;
511 if(version > 1)
512 memcpy(cursor, wopn2_magic2, 11);
513 else
514 memcpy(cursor, wopn2_magic1, 11);
515 GO_FORWARD(11);
516
517 if(version > 1)
518 {
519 if(length < 2)
520 return WOPN_ERR_UNEXPECTED_ENDING;
521 fromUint16LE(version, cursor);
522 GO_FORWARD(2);
523 }
524
525 if(length < 2)
526 return WOPN_ERR_UNEXPECTED_ENDING;
527 fromUint16BE(banks_melodic, cursor);
528 GO_FORWARD(2);
529
530 if(length < 2)
531 return WOPN_ERR_UNEXPECTED_ENDING;
532 fromUint16BE(banks_percussive, cursor);
533 GO_FORWARD(2);
534
535 if(length < 1)
536 return WOPN_ERR_UNEXPECTED_ENDING;
537 cursor[0] = file->lfo_freq & 0xf;
538 if (version >= 2)
539 cursor[0] |= (file->chip_type & 1) << 4;
540 GO_FORWARD(1);
541
542 bankslots[0] = file->banks_melodic;
543 bankslots_sizes[0] = banks_melodic;
544 bankslots[1] = file->banks_percussive;
545 bankslots_sizes[1] = banks_percussive;
546
547 if(version >= 2)
548 {
549 for(i = 0; i < 2; i++)
550 {
551 for(j = 0; j < bankslots_sizes[i]; j++)
552 {
553 if(length < 34)
554 return WOPN_ERR_UNEXPECTED_ENDING;
555 memcpy(cursor, bankslots[i][j].bank_name, 32);
556 cursor[32] = bankslots[i][j].bank_midi_lsb;
557 cursor[33] = bankslots[i][j].bank_midi_msb;
558 GO_FORWARD(34);
559 }
560 }
561 }
562
563 {/* Write instruments data */
564 if(version >= 2)
565 ins_size = WOPN_INST_SIZE_V2;
566 else
567 ins_size = WOPN_INST_SIZE_V1;
568 for(i = 0; i < 2; i++)
569 {
570 if(length < (ins_size * 128) * (size_t)bankslots_sizes[i])
571 return WOPN_ERR_UNEXPECTED_ENDING;
572
573 for(j = 0; j < bankslots_sizes[i]; j++)
574 {
575 for(k = 0; k < 128; k++)
576 {
577 WOPNInstrument *ins = &bankslots[i][j].ins[k];
578 WOPN_writeInstrument(ins, cursor, version, 1);
579 GO_FORWARD(ins_size);
580 }
581 }
582 }
583 }
584
585 return WOPN_ERR_OK;
586 #undef GO_FORWARD
587 }
588
589 int WOPN_SaveInstToMem(OPNIFile *file, void *dest_mem, size_t length, uint16_t version)
590 {
591 uint8_t *cursor = (uint8_t *)dest_mem;
592 uint16_t ins_size;
593
594 if(!cursor)
595 return WOPN_ERR_NULL_POINTER;
596
597 if(version == 0)
598 version = wopn_latest_version;
599
600 #define GO_FORWARD(bytes) { cursor += bytes; length -= bytes; }
601
602 {/* Magic number */
603 if(length < 11)
604 return WOPN_ERR_UNEXPECTED_ENDING;
605 if(version > 1)
606 memcpy(cursor, opni_magic2, 11);
607 else
608 memcpy(cursor, opni_magic1, 11);
609 GO_FORWARD(11);
610 }
611
612 if (version > 1)
613 {/* Version code */
614 if(length < 2)
615 return WOPN_ERR_UNEXPECTED_ENDING;
616 fromUint16LE(version, cursor);
617 GO_FORWARD(2);
618 }
619
620 {/* is drum flag */
621 if(length < 1)
622 return WOPN_ERR_UNEXPECTED_ENDING;
623 *cursor = file->is_drum;
624 GO_FORWARD(1);
625 }
626
627 if(version > 1)
628 /* Skip sounding delays are not part of single-instrument file
629 * two sizes of uint16_t will be subtracted */
630 ins_size = WOPN_INST_SIZE_V2 - (sizeof(uint16_t) * 2);
631 else
632 ins_size = WOPN_INST_SIZE_V1;
633
634 if(length < ins_size)
635 return WOPN_ERR_UNEXPECTED_ENDING;
636
637 WOPN_writeInstrument(&file->inst, cursor, version, 0);
638 GO_FORWARD(ins_size);
639
640 return WOPN_ERR_OK;
641 #undef GO_FORWARD
642 }