Debian Codegraph @

src/engine/fileOps/pvi.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 //PVI YM2608 ADPCM-B sample bank
25
26 /* =======================================
27 General
28 =======================================
29
30 ADPCM RAM addresses: see docs/common.txt {
31
32 address_start = 0x0000
33 file_header_size = 0x0210
34
35 }
36
37 =======================================
38 Header
39 =======================================
40
41 0x0000 Identifier (4b)
42 "PVI2"
43 0x0004 - 0x0007 Unknown Settings (4b)
44 Unknown mappings PCME switches <-> Values
45 "0x10 0x00 0x10 0x02" in all example files
46 First 1h may be Start Address in ADPCM RAM?
47 0x0008 - 0x0009 "Delta-N" playback frequency (1h)
48 Default 0x49BA "== 16kHz"??
49 0x000A Unknown (1b)
50 RAM type? (Docs indicate values 0 or 8, all example files have value 0x02?)
51 0x000B Amount of defined Samples (1b)
52 0x000C - 0x000F Unknown (4b)
53 Padding?
54 "0x00 0x00 0x00 0x00" in all example files
55 0x0010 - 0x020F 128 * {
56
57 Start of Sample (32b blocks) in ADPCM RAM (1h)
58 End of Sample (32b blocks) in ADPCM RAM (1h)
59
60 (0x0000 0x0000 -> no sample for this instrument ID)
61
62 }
63
64
65 =======================================
66 Body
67 =======================================
68
69 Stream of Sample Data {
70
71 Yamaha ADPCM-B encoding (4-Bit Signed ADPCM)
72 Mono
73 Sample rate as specified earlier
74 (examples i have seems Stereo or 32kHz despite "16kHz" playback frequency setting?)
75
76 } */
77
78 #define PVIV2_FILE_SIG "PVI2"
79 #define PVIV1_FILE_SIG "PVI1"
80
81 #define PVI_BANK_SIZE 128
82 #define PVI_SAMPLE_RATE 16000
83
84 typedef struct
85 {
86 uint16_t start_pointer;
87 uint16_t end_pointer;
88 } PVI_HEADER;
89
90 #define UNUSED(x) (void)(x)
91
92 #define ADPCM_DATA_START 0x0210
93
94 void DivEngine::loadPVI(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath)
95 {
96 try
97 {
98 reader.seek(0, SEEK_SET);
99
100 String file_sig = reader.readString(4);
101 if(file_sig != PVIV1_FILE_SIG && file_sig != PVIV2_FILE_SIG) return;
102
103 unsigned int unknown_settings = (unsigned int)reader.readI();
104 UNUSED(unknown_settings);
105 unsigned short delta_n = (unsigned short)reader.readS();
106 UNUSED(delta_n);
107 unsigned char one_byte = (unsigned char)reader.readC();
108 UNUSED(one_byte);
109 unsigned char amount_of_samples = (unsigned char)reader.readC();
110 UNUSED(amount_of_samples);
111 unsigned int padding = (unsigned int)reader.readI();
112 UNUSED(padding);
113
114 PVI_HEADER headers[PVI_BANK_SIZE];
115
116 for(int i = 0; i < PVI_BANK_SIZE; i++)
117 {
118 headers[i].start_pointer = (unsigned short)reader.readS();
119 headers[i].end_pointer = (unsigned short)reader.readS();
120 }
121
122 for(int i = 0; i < PVI_BANK_SIZE; i++)
123 {
124 if((headers[i].start_pointer != 0 || headers[i].end_pointer != 0) && headers[i].start_pointer < headers[i].end_pointer)
125 {
126 DivSample* s = new DivSample;
127
128 s->rate = PVI_SAMPLE_RATE;
129 s->centerRate = PVI_SAMPLE_RATE;
130 s->depth = DIV_SAMPLE_DEPTH_ADPCM_B;
131 s->init((headers[i].end_pointer - headers[i].start_pointer) * 32 * 2);
132
133 int sample_pos = 0;
134 int sample_length = (headers[i].end_pointer - headers[i].start_pointer) * 32;
135
136 reader.seek(ADPCM_DATA_START + headers[i].start_pointer * 32, SEEK_SET);
137
138 for(int j = 0; j < sample_length; j++)
139 {
140 unsigned char curr_byte = (unsigned char)reader.readC();
141 //curr_byte=(curr_byte<<4)|(curr_byte>>4);
142
143 s->dataB[sample_pos] = curr_byte;
144 sample_pos++;
145 }
146
147 logI("pvi: start %d end %d len in bytes %d", headers[i].start_pointer, headers[i].end_pointer, (headers[i].end_pointer - headers[i].start_pointer) * 32);
148
149 ret.push_back(s);
150 }
151 }
152 }
153 catch (EndOfFileException& e)
154 {
155 lastError=_("premature end of file");
156 logE("premature end of file");
157 }
158 }