Debian Codegraph @

src/engine/fileOps/p86.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 //P86 8-bit PCM sample bank
25
26 /* =======================================
27 Header
28 =======================================
29
30 0x0000 Identifier (12b)
31 "PCM86 DATA(\n)(\0)"
32 0x000C Targeted P86DRV version (1b)
33 version <high nibble>.<low nibble>
34 0x000D File Length (3b)
35 0x0010 - 0x060F 256 * {
36
37 Pointer to Sample Data Start (3b)
38 Length of Sample Data (3b)
39
40 (0x000000 0x000000 -> no sample for this instrument ID)
41
42 }
43
44
45 =======================================
46 Body
47 =======================================
48
49 Stream of Sample Data {
50
51 8-Bit Signed
52 Mono
53 16540Hz
54 (above sample rate according to KAJA's documentation
55 any sample rate possible, for different base note & octave)
56
57 } */
58
59 #define P86_BANK_SIZE 256
60 #define P86_SAMPLE_RATE 16540
61
62 #define P86_FILE_SIG "PCM86 DATA\n\0"
63
64 typedef struct
65 {
66 uint32_t start_pointer;
67 uint32_t sample_length;
68 } P86_HEADER;
69
70 #define UNUSED(x) (void)(x)
71
72 uint32_t read_3bytes(SafeReader& reader)
73 {
74 unsigned char arr[3];
75
76 for (int i = 0; i < 3; i++)
77 {
78 arr[i] = (unsigned char)reader.readC();
79 }
80
81 return (arr[0] | (arr[1] << 8) | (arr[2] << 16));
82 }
83
84 void DivEngine::loadP86(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
85 {
86 try
87 {
88 reader.seek(0, SEEK_SET);
89
90 P86_HEADER headers[P86_BANK_SIZE];
91
92 String file_sig = reader.readString(12);
93 if(file_sig != P86_FILE_SIG) return;
94
95 uint8_t version = reader.readC();
96 UNUSED(version);
97
98 uint32_t file_size = read_3bytes(reader);
99 UNUSED(file_size);
100
101 for(int i = 0; i < P86_BANK_SIZE; i++)
102 {
103 headers[i].start_pointer = read_3bytes(reader);
104 headers[i].sample_length = read_3bytes(reader);
105 }
106
107 for(int i = 0; i < P86_BANK_SIZE; i++)
108 {
109 if(headers[i].start_pointer != 0 && headers[i].sample_length != 0)
110 {
111 DivSample* s = new DivSample;
112
113 s->rate = P86_SAMPLE_RATE;
114 s->centerRate = P86_SAMPLE_RATE;
115 s->depth = DIV_SAMPLE_DEPTH_8BIT;
116 s->init(headers[i].sample_length); //byte per sample
117
118 reader.seek((int)headers[i].start_pointer, SEEK_SET);
119
120 int sample_pos = 0;
121
122 for(uint32_t j = 0; j < headers[i].sample_length; j++)
123 {
124 unsigned char curr_byte = (unsigned char)reader.readC();
125 //curr_byte += 0x80;
126
127 s->data8[sample_pos] = curr_byte;
128 sample_pos++;
129 }
130
131 ret.push_back(s);
132
133 logI("p86: start %06X len %06X", headers[i].start_pointer, headers[i].sample_length);
134 }
135 }
136 }
137 catch (EndOfFileException& e)
138 {
139 lastError=_("premature end of file");
140 logE("premature end of file");
141 }
142 }