| 1 | /* 31-sample MOD loader (Amiga, PC, etc.) |
| 2 | ** |
| 3 | ** Note: Data sanitation is done in the last stage |
| 4 | ** of module loading, so you don't need to do that here. |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <ctype.h> // isdigit() |
| 11 | #include <stdint.h> |
| 12 | #include <stdbool.h> |
| 13 | #include "../pt2_header.h"cxx . . $ `<file>/src/pt2_header.h`/ |
| 14 | #include "../pt2_paula.h"cxx . . $ `<file>/src/pt2_paula.h`/ // PAULA_VOICES |
| 15 | #include "../pt2_config.h"cxx . . $ `<file>/src/pt2_config.h`/ |
| 16 | #include "../pt2_structs.h"cxx . . $ `<file>/src/pt2_structs.h`/ |
| 17 | #include "../pt2_replayer.h"cxx . . $ `<file>/src/pt2_replayer.h`/ |
| 18 | #include "../pt2_textout.h"cxx . . $ `<file>/src/pt2_textout.h`/ |
| 19 | #include "../pt2_visuals.h"cxx . . $ `<file>/src/pt2_visuals.h`/ |
| 20 | |
| 21 | enumcxx . . $ $anonymous_type_27a5271f077165f4_0# // 31-sample .MOD types |
| 22 | { |
| 23 | FORMAT_UNKNOWNcxx . . $ FORMAT_UNKNOWN., |
| 24 | |
| 25 | FORMAT_PTcxx . . $ FORMAT_PT., // ProTracker or compatible |
| 26 | FORMAT_FLTcxx . . $ FORMAT_FLT., // Startrekker (4 channels) |
| 27 | FORMAT_FT2cxx . . $ FORMAT_FT2., // FT2 (or other trackers, multichannel) |
| 28 | FORMAT_NTcxx . . $ FORMAT_NT., // NoiseTracker |
| 29 | FORMAT_HMNTcxx . . $ FORMAT_HMNT. // His Master's NoiseTracker (special one) |
| 30 | }; |
| 31 | |
| 32 | static int32_tcxx . . $ int32_t# realSampleLengthscxx . . $ realSampleLengths.[MOD_SAMPLEScxx . . $ `src/pt2_header.h:74:9`!]; |
| 33 | |
| 34 | static uint8_tcxx . . $ uint8_t# getMod31Typecxx . . $ getMod31Type(f3b311040e9d3abb).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# filesize, uint8_tcxx . . $ uint8_t# *numChannels); // 0 = not detected |
| 35 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! detectMod31cxx . . $ detectMod31(f3e7299796ab6c6f).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# filesize); |
| 36 | |
| 37 | module_tcxx . . $ module_t# *loadMod31cxx . . $ loadMod31(abcad8371f3628e2).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# filesize) |
| 38 | { |
| 39 | module_tcxx . . $ module_t# *m = createEmptyModcxx . . $ createEmptyMod(51965c0e12ed79c4).(); |
| 40 | if (m == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 41 | { |
| 42 | statusOutOfMemorycxx . . $ statusOutOfMemory(49f6e7a06ebc5aa8).(); |
| 43 | goto loadError; |
| 44 | } |
| 45 | |
| 46 | uint8_tcxx . . $ uint8_t# numChannels; |
| 47 | uint8_tcxx . . $ uint8_t# modFormat = getMod31Typecxx . . $ getMod31Type(f3b311040e9d3abb).(buffer, filesize, &numChannels); |
| 48 | if (modFormat == FORMAT_UNKNOWNcxx . . $ FORMAT_UNKNOWN. || numChannels > PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!) |
| 49 | { |
| 50 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("UNSUPPORTED MOD !"); |
| 51 | goto loadError; |
| 52 | } |
| 53 | |
| 54 | uint8_tcxx . . $ uint8_t# *p = buffer; |
| 55 | |
| 56 | memcpycxx . . $ memcpy(cc817cb175f282d).(m->headercxx . . $ module_t#header..namecxx . . $ moduleHeader_t#name., p, 20); p += 20; |
| 57 | |
| 58 | // read sample headers |
| 59 | moduleSample_tcxx . . $ moduleSample_t# *s = m->samplescxx . . $ module_t#samples.; |
| 60 | for (int32_tcxx . . $ int32_t# i = 0; i < MOD_SAMPLEScxx . . $ `src/pt2_header.h:74:9`!; i++, s++) |
| 61 | { |
| 62 | memcpycxx . . $ memcpy(cc817cb175f282d).(s->textcxx . . $ moduleSample_t#text., p, 22); p += 22; |
| 63 | |
| 64 | realSampleLengthscxx . . $ realSampleLengths.[i] = ((p[0] << 8) | p[1]) * 2; p += 2; |
| 65 | s->lengthcxx . . $ moduleSample_t#length. = (realSampleLengthscxx . . $ realSampleLengths.[i] > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) ? configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength. : realSampleLengthscxx . . $ realSampleLengths.[i]; |
| 66 | |
| 67 | s->fineTunecxx . . $ moduleSample_t#fineTune. = *p++ & 0xF; |
| 68 | s->volumecxx . . $ moduleSample_t#volume. = *p++; |
| 69 | |
| 70 | s->loopStartcxx . . $ moduleSample_t#loopStart. = ((p[0] << 8) | p[1]) * 2; p += 2; |
| 71 | s->loopLengthcxx . . $ moduleSample_t#loopLength. = ((p[0] << 8) | p[1]) * 2; p += 2; |
| 72 | |
| 73 | // fix for poorly converted STK (< v2.5) -> PT/NT modules |
| 74 | if (s->loopLengthcxx . . $ moduleSample_t#loopLength. > 2 && s->loopStartcxx . . $ moduleSample_t#loopStart.+s->loopLengthcxx . . $ moduleSample_t#loopLength. > s->lengthcxx . . $ moduleSample_t#length.) |
| 75 | { |
| 76 | if ((s->loopStartcxx . . $ moduleSample_t#loopStart./2) + s->loopLengthcxx . . $ moduleSample_t#loopLength. <= s->lengthcxx . . $ moduleSample_t#length.) |
| 77 | s->loopStartcxx . . $ moduleSample_t#loopStart. /= 2; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. = *p++; |
| 82 | p++; // skip uninteresting field (127 in PT MOds, "restart pos" in PC MODs) |
| 83 | |
| 84 | if (modFormat == FORMAT_PTcxx . . $ FORMAT_PT. && m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. == 129) |
| 85 | m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. = 127; // fixes a specific copy of beatwave.mod (XXX: hackish...) |
| 86 | |
| 87 | if (m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. == 0 || m->headercxx . . $ module_t#header..songLengthcxx . . $ moduleHeader_t#songLength. > 129) |
| 88 | { |
| 89 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("NOT A MOD FILE !"); |
| 90 | goto loadError; |
| 91 | } |
| 92 | |
| 93 | // read orders and count number of patterns |
| 94 | int32_tcxx . . $ int32_t# numPatterns = 0; |
| 95 | for (int32_tcxx . . $ int32_t# i = 0; i < 128; i++) |
| 96 | { |
| 97 | m->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i] = *p++; |
| 98 | if (m->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i] > numPatterns) |
| 99 | numPatterns = m->headercxx . . $ module_t#header..patternTablecxx . . $ moduleHeader_t#patternTable.[i]; |
| 100 | } |
| 101 | numPatterns++; |
| 102 | |
| 103 | if (numPatterns > MAX_PATTERNScxx . . $ `src/pt2_header.h:75:9`!) |
| 104 | { |
| 105 | displayErrorMsgcxx . . $ displayErrorMsg(85c52e162fed56f9).("UNSUPPORTED MOD !"); |
| 106 | goto loadError; |
| 107 | } |
| 108 | |
| 109 | p += 4; // skip magic ID (already handled) |
| 110 | |
| 111 | // load pattern data |
| 112 | for (int32_tcxx . . $ int32_t# i = 0; i < numPatterns; i++) |
| 113 | { |
| 114 | note_tcxx . . $ note_t# *note = m->patternscxx . . $ module_t#patterns.[i]; |
| 115 | for (int32_tcxx . . $ int32_t# j = 0; j < MOD_ROWScxx . . $ `src/pt2_header.h:73:9`!; j++) |
| 116 | { |
| 117 | for (int32_tcxx . . $ int32_t# k = 0; k < numChannels; k++, note++, p += 4) |
| 118 | { |
| 119 | note->periodcxx . . $ note_t#period. = ((p[0] & 0x0F) << 8) | p[1]; |
| 120 | note->samplecxx . . $ note_t#sample. = ((p[0] & 0xF0) | (p[2] >> 4)) & 31; |
| 121 | note->commandcxx . . $ note_t#command. = p[2] & 0x0F; |
| 122 | note->paramcxx . . $ note_t#param. = p[3]; |
| 123 | } |
| 124 | |
| 125 | if (numChannels < PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!) |
| 126 | note += PAULA_VOICEScxx . . $ `src/pt2_paula.h:13:9`!-numChannels; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (modFormat != FORMAT_PTcxx . . $ FORMAT_PT.) // pattern command conversion for non-PT formats |
| 131 | { |
| 132 | for (int32_tcxx . . $ int32_t# i = 0; i < numPatterns; i++) |
| 133 | { |
| 134 | note_tcxx . . $ note_t# *note = m->patternscxx . . $ module_t#patterns.[i]; |
| 135 | for (int32_tcxx . . $ int32_t# j = 0; j < MOD_ROWScxx . . $ `src/pt2_header.h:73:9`!*4; j++, note++) |
| 136 | { |
| 137 | if (modFormat == FORMAT_NTcxx . . $ FORMAT_NT. || modFormat == FORMAT_HMNTcxx . . $ FORMAT_HMNT.) |
| 138 | { |
| 139 | // any Dxx == D00 in NT/HMNT |
| 140 | if (note->commandcxx . . $ note_t#command. == 0xD) |
| 141 | note->paramcxx . . $ note_t#param. = 0; |
| 142 | |
| 143 | // effect F with param 0x00 does nothing in NT/HMNT |
| 144 | if (note->commandcxx . . $ note_t#command. == 0xF && note->paramcxx . . $ note_t#param. == 0) |
| 145 | note->commandcxx . . $ note_t#command. = 0; |
| 146 | } |
| 147 | else if (modFormat == FORMAT_FLTcxx . . $ FORMAT_FLT.) // Startrekker (4 channels) |
| 148 | { |
| 149 | if (note->commandcxx . . $ note_t#command. == 0xE) // remove unsupported "assembly macros" command |
| 150 | { |
| 151 | note->commandcxx . . $ note_t#command. = 0; |
| 152 | note->paramcxx . . $ note_t#param. = 0; |
| 153 | } |
| 154 | |
| 155 | // Startrekker is always in vblank mode, and limits speed to 0x1F |
| 156 | if (note->commandcxx . . $ note_t#command. == 0xF && note->paramcxx . . $ note_t#param. > 0x1F) |
| 157 | note->paramcxx . . $ note_t#param. = 0x1F; |
| 158 | } |
| 159 | |
| 160 | // remove E8x (Karplus-Strong is only supported for ProTracker .MODs) |
| 161 | if (note->commandcxx . . $ note_t#command. == 0xE && (note->paramcxx . . $ note_t#param. >> 4) == 0x8) |
| 162 | { |
| 163 | note->commandcxx . . $ note_t#command. = 0; |
| 164 | note->paramcxx . . $ note_t#param. = 0; |
| 165 | } |
| 166 | |
| 167 | // remove EFx (Invert Loop is only supported for ProTracker .MODs) |
| 168 | if (note->commandcxx . . $ note_t#command. == 0xE && (note->paramcxx . . $ note_t#param. >> 4) == 0xF) |
| 169 | { |
| 170 | note->commandcxx . . $ note_t#command. = 0; |
| 171 | note->paramcxx . . $ note_t#param. = 0; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // load sample data |
| 178 | s = m->samplescxx . . $ module_t#samples.; |
| 179 | for (int32_tcxx . . $ int32_t# i = 0; i < MOD_SAMPLEScxx . . $ `src/pt2_header.h:74:9`!; i++, s++) |
| 180 | { |
| 181 | int32_tcxx . . $ int32_t# bytesToSkip = 0; |
| 182 | if (realSampleLengthscxx . . $ realSampleLengths.[i] > configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.) |
| 183 | bytesToSkip = realSampleLengthscxx . . $ realSampleLengths.[i] - configcxx . . $ config..maxSampleLengthcxx . . $ config_t#maxSampleLength.; |
| 184 | |
| 185 | memcpycxx . . $ memcpy(cc817cb175f282d).(&m->sampleDatacxx . . $ module_t#sampleData.[s->offsetcxx . . $ moduleSample_t#offset.], p, s->lengthcxx . . $ moduleSample_t#length.); p += s->lengthcxx . . $ moduleSample_t#length.; |
| 186 | if (bytesToSkip > 0) |
| 187 | p += bytesToSkip; |
| 188 | } |
| 189 | |
| 190 | m->headercxx . . $ module_t#header..initialTempocxx . . $ moduleHeader_t#initialTempo. = 125; |
| 191 | return m; |
| 192 | |
| 193 | loadError: |
| 194 | if (m != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 195 | { |
| 196 | for (int32_tcxx . . $ int32_t# i = 0; i < MAX_PATTERNScxx . . $ `src/pt2_header.h:75:9`!; i++) |
| 197 | { |
| 198 | if (m->patternscxx . . $ module_t#patterns.[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 199 | freecxx . . $ free(5d22bdacc48458e8).(m->patternscxx . . $ module_t#patterns.[i]); |
| 200 | } |
| 201 | |
| 202 | if (m->sampleDatacxx . . $ module_t#sampleData. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 203 | freecxx . . $ free(5d22bdacc48458e8).(m->sampleDatacxx . . $ module_t#sampleData.); |
| 204 | |
| 205 | freecxx . . $ free(5d22bdacc48458e8).(m); |
| 206 | } |
| 207 | |
| 208 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 209 | } |
| 210 | |
| 211 | static uint8_tcxx . . $ uint8_t# getMod31Typecxx . . $ getMod31Type(f3b311040e9d3abb).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# filesize, uint8_tcxx . . $ uint8_t# *numChannels) // 0 = not detected |
| 212 | { |
| 213 | const uint8_tcxx . . $ uint8_t# *id = &buffer[1080]; |
| 214 | #define IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!(s) !memcmp(s, id, 4) |
| 215 | |
| 216 | *numChannels = 0; |
| 217 | if (buffer == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || filesize < 1084+1024) |
| 218 | return FORMAT_UNKNOWNcxx . . $ FORMAT_UNKNOWN.; |
| 219 | |
| 220 | *numChannels = 4; |
| 221 | if (IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("M.K.") || IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("M!K!") || IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("NSMS") || IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("LARD") || IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("PATT")) |
| 222 | { |
| 223 | return FORMAT_PTcxx . . $ FORMAT_PT.; // ProTracker (or compatible) |
| 224 | } |
| 225 | else if (IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("FLT4")) |
| 226 | { |
| 227 | return FORMAT_FLTcxx . . $ FORMAT_FLT.; // Startrekker (4 channels) |
| 228 | } |
| 229 | else if (IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("N.T.")) |
| 230 | { |
| 231 | return FORMAT_NTcxx . . $ FORMAT_NT.; // NoiseTracker |
| 232 | } |
| 233 | else if (IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("M&K!") || IDcxx . . $ `src/modloaders/pt2_load_mod31.c:214:9`!("FEST")) |
| 234 | { |
| 235 | return FORMAT_HMNTcxx . . $ FORMAT_HMNT.; // His Master's NoiseTracker |
| 236 | } |
| 237 | else if (isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(id[0]) && (id[1] == 'C' && id[2] == 'H' && id[3] == 'N')) |
| 238 | { |
| 239 | *numChannels = id[0] - '0'; |
| 240 | return FORMAT_FT2cxx . . $ FORMAT_FT2.; // Fasttracker II 1..9 channels (or other trackers) |
| 241 | } |
| 242 | else if (isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(id[0]) && isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(id[1]) && id[1] == 'C' && id[2] == 'H') |
| 243 | { |
| 244 | *numChannels = ((id[0] - '0') * 10) + (id[1] - '0'); |
| 245 | return FORMAT_FT2cxx . . $ FORMAT_FT2.; // Fasttracker II 10+ channels (or other trackers) |
| 246 | } |
| 247 | |
| 248 | return FORMAT_UNKNOWNcxx . . $ FORMAT_UNKNOWN.; |
| 249 | } |
| 250 | |
| 251 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! detectMod31cxx . . $ detectMod31(f3e7299796ab6c6f).(uint8_tcxx . . $ uint8_t# *buffer, uint32_tcxx . . $ uint32_t# filesize) |
| 252 | { |
| 253 | uint8_tcxx . . $ uint8_t# junk; |
| 254 | |
| 255 | if (buffer == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || filesize < 1084+1024) |
| 256 | return FORMAT_UNKNOWNcxx . . $ FORMAT_UNKNOWN.; |
| 257 | |
| 258 | return getMod31Typecxx . . $ getMod31Type(f3b311040e9d3abb).(buffer, filesize, &junk) != FORMAT_UNKNOWNcxx . . $ FORMAT_UNKNOWN.; |
| 259 | } |