Debian Codegraph @

src/engine/fileOps/pdx.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "fileOpsCommon.h"
21
22 class DivEngine;
23
24 //PDX 8-bit OKI ADPCM sample bank
25
26 /* File format
27 The file starts with a header with 96 8-byte pairs, with each pair containing offset and length of ADPCM data chunks for each note value, like so:
28
29 [[byte pointer to sample in file -- 32-bit unsigned integer (4 bytes)] [empty: $0000 -- 16-bit integer] [length of sample, amount in bytes -- 16-bit unsigned integer] ×96] [ADPCM data] EOF
30
31 The first sample (1) is mapped to 0x80 (= the lowest note within MXDRV) and the last sample (96) is mapped to 0xDF (= the highest note within MXDRV).
32
33
34 Samples are encoded in 4-bit OKI ADPCM encoded nibbles, where each byte contains 2 nibbles: [nibble 2 << 4 || nibble 1]
35
36
37 Unfortunately, sample rates for each samples are not defined within the .PDX file and have to be set manually with the appropriate command for that at play-time */
38
39 #define PDX_BANK_SIZE 96
40 #define PDX_SAMPLE_RATE 16000
41
42 typedef struct
43 {
44 unsigned int start_pointer;
45 unsigned short sample_length;
46 } PDX_HEADER;
47
48 #define UNUSED(x) (void)(x)
49
50 void DivEngine::loadPDX(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
51 {
52 try
53 {
54 reader.seek(0, SEEK_SET);
55
56 PDX_HEADER headers[PDX_BANK_SIZE];
57
58 for(int i = 0; i < PDX_BANK_SIZE; i++)
59 {
60 headers[i].start_pointer = (unsigned int)reader.readI_BE();
61 unsigned short empty = (unsigned short)reader.readS_BE(); //skip 1st 2 bytes
62 UNUSED(empty);
63 headers[i].sample_length = (unsigned short)reader.readS_BE();
64 }
65
66 for(int i = 0; i < PDX_BANK_SIZE; i++)
67 {
68 if(headers[i].start_pointer != 0 && headers[i].sample_length != 0)
69 {
70 DivSample* s = new DivSample;
71
72 s->rate = PDX_SAMPLE_RATE;
73 s->centerRate = PDX_SAMPLE_RATE;
74 s->depth = DIV_SAMPLE_DEPTH_VOX;
75 s->init(headers[i].sample_length * 2);
76
77 reader.seek((int)headers[i].start_pointer, SEEK_SET);
78
79 int sample_pos = 0;
80
81 for(unsigned short j = 0; j < headers[i].sample_length; j++)
82 {
83 unsigned char curr_byte = (unsigned char)reader.readC();
84 curr_byte = (curr_byte << 4) | (curr_byte >> 4);
85
86 s->dataVOX[sample_pos] = curr_byte;
87 sample_pos++;
88 }
89
90 ret.push_back(s);
91
92 logI("pdx: start %d len %d", headers[i].start_pointer, headers[i].sample_length);
93 }
94 }
95 }
96 catch (EndOfFileException& e)
97 {
98 lastError=_("premature end of file");
99 logE("premature end of file");
100 }
101 }