| 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 |
//PPC PMD's YM2608 ADPCM-B sample bank |
| 25 |
|
| 26 |
/* ======================================== |
| 27 |
General |
| 28 |
======================================== |
| 29 |
|
| 30 |
ADPCM RAM addresses: see docs/common.txt { |
| 31 |
|
| 32 |
address_start = 0x0026 |
| 33 |
file_header_size = 0x0420 |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
======================================== |
| 39 |
Header |
| 40 |
======================================== |
| 41 |
|
| 42 |
0x0000 Identifier (30b) |
| 43 |
"ADPCM DATA for PMD ver.4.4- " |
| 44 |
0x001E Address of End of Data (32B blocks) in ADPCM RAM (1h) |
| 45 |
File Size == address -> file offset |
| 46 |
|
| 47 |
0x0020 - 0x041F 256 * { |
| 48 |
|
| 49 |
Start of Sample (32b blocks) in ADPCM RAM (1h) |
| 50 |
End of Sample (32b blocks) in ADPCM RAM (1h) |
| 51 |
|
| 52 |
(0x0000 0x0000 -> no sample for this instrument ID) |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
======================================== |
| 58 |
Body |
| 59 |
======================================== |
| 60 |
|
| 61 |
Stream of Sample Data { |
| 62 |
|
| 63 |
Yamaha ADPCM-B encoding (4-Bit Signed ADPCM) |
| 64 |
Mono |
| 65 |
16kHz |
| 66 |
(above sample rate according to KAJA's documentation |
| 67 |
any sample rate possible, for different base note & octave) |
| 68 |
|
| 69 |
} */ |
| 70 |
|
| 71 |
#define PPC_FILE_SIG "ADPCM DATA for PMD ver.4.4- " |
| 72 |
|
| 73 |
#define PPC_BANK_SIZE 256 |
| 74 |
#define PPC_SAMPLE_RATE 16000 |
| 75 |
|
| 76 |
typedef struct |
| 77 |
{ |
| 78 |
uint16_t start_pointer; |
| 79 |
uint16_t end_pointer; |
| 80 |
} PPC_HEADER; |
| 81 |
|
| 82 |
#define UNUSED(x) (void)(x) |
| 83 |
|
| 84 |
#define ADPCM_DATA_START 0x0420 |
| 85 |
|
| 86 |
void DivEngine::loadPPC(SafeReader& reader, std::vector<DivSample*>& ret, String& stripPath) |
| 87 |
{ |
| 88 |
try |
| 89 |
{ |
| 90 |
reader.seek(0, SEEK_SET); |
| 91 |
|
| 92 |
String file_sig = reader.readString(30); |
| 93 |
unsigned short end_of_data = (unsigned short)reader.readS(); |
| 94 |
UNUSED(end_of_data); |
| 95 |
|
| 96 |
if(file_sig != PPC_FILE_SIG) return; |
| 97 |
|
| 98 |
PPC_HEADER headers[PPC_BANK_SIZE]; |
| 99 |
|
| 100 |
for(int i = 0; i < PPC_BANK_SIZE; i++) |
| 101 |
{ |
| 102 |
headers[i].start_pointer = (unsigned short)reader.readS(); |
| 103 |
headers[i].end_pointer = (unsigned short)reader.readS(); |
| 104 |
} |
| 105 |
|
| 106 |
for(int i = 0; i < PPC_BANK_SIZE; i++) |
| 107 |
{ |
| 108 |
if((headers[i].start_pointer != 0 || headers[i].end_pointer != 0) && headers[i].start_pointer < headers[i].end_pointer) |
| 109 |
{ |
| 110 |
DivSample* s = new DivSample; |
| 111 |
|
| 112 |
s->rate = PPC_SAMPLE_RATE; |
| 113 |
s->centerRate = PPC_SAMPLE_RATE; |
| 114 |
s->depth = DIV_SAMPLE_DEPTH_ADPCM_B; |
| 115 |
s->init((headers[i].end_pointer - headers[i].start_pointer) * 32 * 2); |
| 116 |
|
| 117 |
int sample_pos = 0; |
| 118 |
int sample_length = (headers[i].end_pointer - headers[i].start_pointer) * 32; |
| 119 |
|
| 120 |
//reader.seek(ADPCM_DATA_START + headers[i].start_pointer * 32, SEEK_SET); |
| 121 |
|
| 122 |
for(int j = 0; j < sample_length; j++) |
| 123 |
{ |
| 124 |
unsigned char curr_byte = (unsigned char)reader.readC(); |
| 125 |
//curr_byte=(curr_byte<<4)|(curr_byte>>4); |
| 126 |
|
| 127 |
s->dataB[sample_pos] = curr_byte; |
| 128 |
sample_pos++; |
| 129 |
} |
| 130 |
|
| 131 |
logI("ppc: 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); |
| 132 |
|
| 133 |
ret.push_back(s); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
catch (EndOfFileException& e) |
| 138 |
{ |
| 139 |
lastError=_("premature end of file"); |
| 140 |
logE("premature end of file"); |
| 141 |
} |
| 142 |
} |