| 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 |
//PZI 8-bit PCM sample bank |
| 25 |
|
| 26 |
/* ======================================= |
| 27 |
Header |
| 28 |
======================================= |
| 29 |
|
| 30 |
0x0000 Identifier (4b) |
| 31 |
"PZI1" |
| 32 |
0x0004 - 0x001F Unknown (28b) |
| 33 |
Part of identifier? Settings? |
| 34 |
All (\0)s in all example files |
| 35 |
0x0020 - 0x091F 128 * { |
| 36 |
|
| 37 |
Start of Sample after header (2h) |
| 38 |
Length of Sample (2h) |
| 39 |
Offset of loop start from sample start (2h) |
| 40 |
Offset of loop end from sample start (2h) |
| 41 |
Sample rate (1h) |
| 42 |
|
| 43 |
(0xFFFFFFFF 0xFFFFFFFF loop offsets -> no loop information) |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
======================================= |
| 49 |
Body |
| 50 |
======================================= |
| 51 |
|
| 52 |
Stream of Sample Data { |
| 53 |
|
| 54 |
Unsigned 8-Bit |
| 55 |
Mono |
| 56 |
Sample rate as specified in header |
| 57 |
|
| 58 |
} */ |
| 59 |
|
| 60 |
#define PZI_BANK_SIZE 128 |
| 61 |
|
| 62 |
#define PZI_FILE_SIG "PZI1" |
| 63 |
|
| 64 |
#define NO_LOOP (0xFFFFFFFFU) |
| 65 |
|
| 66 |
#define SAMPLE_DATA_OFFSET 0x0920 |
| 67 |
|
| 68 |
#define MAX_SANITY_CAP 9999999 |
| 69 |
|
| 70 |
#define HEADER_JUNK_SIZE 28 |
| 71 |
|
| 72 |
typedef struct |
| 73 |
{ |
| 74 |
uint32_t start_pointer; |
| 75 |
uint32_t sample_length; |
| 76 |
uint32_t loop_start; |
| 77 |
uint32_t loop_end; |
| 78 |
uint16_t sample_rate; |
| 79 |
} PZI_HEADER; |
| 80 |
|
| 81 |
#define UNUSED(x) (void)(x) |
| 82 |
|
| 83 |
void DivEngine::loadPZI(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath) |
| 84 |
{ |
| 85 |
try |
| 86 |
{ |
| 87 |
reader.seek(0, SEEK_SET); |
| 88 |
|
| 89 |
PZI_HEADER headers[PZI_BANK_SIZE]; |
| 90 |
|
| 91 |
String file_sig = reader.readString(4); |
| 92 |
if(file_sig != PZI_FILE_SIG) return; |
| 93 |
|
| 94 |
for (int i = 0; i < HEADER_JUNK_SIZE; i++) |
| 95 |
{ |
| 96 |
unsigned char curr_byte = (unsigned char)reader.readC(); |
| 97 |
UNUSED(curr_byte); |
| 98 |
} |
| 99 |
|
| 100 |
for(int i = 0; i < PZI_BANK_SIZE; i++) |
| 101 |
{ |
| 102 |
headers[i].start_pointer = (unsigned int)reader.readI(); |
| 103 |
headers[i].sample_length = (unsigned int)reader.readI(); |
| 104 |
headers[i].loop_start = (unsigned int)reader.readI(); |
| 105 |
headers[i].loop_end = (unsigned int)reader.readI(); |
| 106 |
headers[i].sample_rate = (unsigned short)reader.readS(); |
| 107 |
} |
| 108 |
|
| 109 |
for(int i = 0; i < PZI_BANK_SIZE; i++) |
| 110 |
{ |
| 111 |
if (headers[i].start_pointer < MAX_SANITY_CAP && headers[i].sample_length < MAX_SANITY_CAP && |
| 112 |
headers[i].sample_length > 0) |
| 113 |
{ |
| 114 |
DivSample* s = new DivSample; |
| 115 |
|
| 116 |
s->rate = headers[i].sample_rate; |
| 117 |
s->centerRate = headers[i].sample_rate; |
| 118 |
s->depth = DIV_SAMPLE_DEPTH_8BIT; |
| 119 |
s->init(headers[i].sample_length); //byte per sample |
| 120 |
|
| 121 |
reader.seek((int)headers[i].start_pointer + SAMPLE_DATA_OFFSET, SEEK_SET); |
| 122 |
|
| 123 |
int sample_pos = 0; |
| 124 |
|
| 125 |
for (uint32_t j = 0; j < headers[i].sample_length; j++) |
| 126 |
{ |
| 127 |
unsigned char curr_byte = (unsigned char)reader.readC(); |
| 128 |
curr_byte += 0x80; |
| 129 |
|
| 130 |
s->data8[sample_pos] = curr_byte; |
| 131 |
sample_pos++; |
| 132 |
} |
| 133 |
|
| 134 |
if (headers[i].loop_start != NO_LOOP && headers[i].loop_end != NO_LOOP) |
| 135 |
{ |
| 136 |
s->loop = true; |
| 137 |
s->loopMode = DIV_SAMPLE_LOOP_FORWARD; |
| 138 |
s->loopStart = headers[i].loop_start; |
| 139 |
s->loopEnd = headers[i].loop_end; |
| 140 |
} |
| 141 |
|
| 142 |
ret.push_back(s); |
| 143 |
|
| 144 |
logI("pzi: start %d len %d sample rate %d loop start %d loop end %d", headers[i].start_pointer, headers[i].sample_length, |
| 145 |
headers[i].sample_rate, headers[i].loop_start, headers[i].loop_end); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
catch (EndOfFileException& e) |
| 150 |
{ |
| 151 |
lastError=_("premature end of file"); |
| 152 |
logE("premature end of file"); |
| 153 |
} |
| 154 |
} |