Debian Codegraph @

src/engine/fileOps/p.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 //P VOX ADPCM sample bank
25
26 /* =======================================
27 Header
28 =======================================
29
30 0x0000 - 0x03FF 256 * {
31 Sample start (uint32_t)
32 - 0x00000000 = unused
33 }
34
35
36 =======================================
37 Body
38 =======================================
39
40 Stream of Sample Data {
41
42 MSM6258 ADPCM encoding
43 nibble-swapped VOX / Dialogic ADPCM
44 Mono
45 Sample rate?
46 16000Hz seems fine
47
48 } */
49
50 #define P_BANK_SIZE 256
51 #define P_SAMPLE_RATE 16000
52
53 typedef struct
54 {
55 uint32_t start_pointer;
56 } P_HEADER;
57
58
59 void DivEngine::loadP(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
60 {
61 try
62 {
63 reader.seek(0, SEEK_SET);
64
65 P_HEADER headers[P_BANK_SIZE];
66
67 for(int i = 0; i < P_BANK_SIZE; i++)
68 {
69 headers[i].start_pointer = (unsigned int)reader.readI_BE();
70 }
71
72 for(int i = 0; i < P_BANK_SIZE; i++)
73 {
74 if(headers[i].start_pointer != 0)
75 {
76 DivSample* s = new DivSample;
77
78 s->rate = P_SAMPLE_RATE;
79 s->centerRate = P_SAMPLE_RATE;
80 s->depth = DIV_SAMPLE_DEPTH_VOX;
81
82 reader.seek((int)headers[i].start_pointer, SEEK_SET);
83
84 int sample_pos = 0;
85 int sample_len = 0;
86
87 if(i < P_BANK_SIZE - 1)
88 {
89 sample_len = headers[i + 1].start_pointer - headers[i].start_pointer;
90 }
91 else
92 {
93 sample_len = (int)reader.size() - headers[i].start_pointer;
94 }
95
96 if(sample_len > 0)
97 {
98 s->init(sample_len * 2);
99
100 for(int j = 0; j < sample_len; j++)
101 {
102 unsigned char curr_byte = (unsigned char)reader.readC();
103 curr_byte = (curr_byte << 4) | (curr_byte >> 4);
104
105 s->dataVOX[sample_pos] = curr_byte;
106 sample_pos++;
107 }
108
109 ret.push_back(s);
110 logI("p: start %d len %d", headers[i].start_pointer, sample_len);
111 }
112 else
113 {
114 delete s;
115 }
116 }
117 }
118 }
119 catch (EndOfFileException& e)
120 {
121 lastError=_("premature end of file");
122 logE("premature end of file");
123 }
124 }