| 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" |
| 14 | #include "../pt2_paula.h" // PAULA_VOICES |
| 15 | #include "../pt2_config.h" |
| 16 | #include "../pt2_structs.h" |
| 17 | #include "../pt2_replayer.h" |
| 18 | #include "../pt2_textout.h" |
| 19 | #include "../pt2_visuals.h" |
| 20 | |
| 21 | enum // 31-sample .MOD types |
| 22 | { |
| 23 | FORMAT_UNKNOWN, |
| 24 | |
| 25 | FORMAT_PT, // ProTracker or compatible |
| 26 | FORMAT_FLT, // Startrekker (4 channels) |
| 27 | FORMAT_FT2, // FT2 (or other trackers, multichannel) |
| 28 | FORMAT_NT, // NoiseTracker |
| 29 | FORMAT_HMNT // His Master's NoiseTracker (special one) |
| 30 | }; |
| 31 | |
| 32 | static int32_t realSampleLengths[MOD_SAMPLES]; |
| 33 | |
| 34 | static uint8_t getMod31Type(uint8_t *buffer, uint32_t filesize, uint8_t *numChannels); // 0 = not detected |
| 35 | bool detectMod31(uint8_t *buffer, uint32_t filesize); |
| 36 | |
| 37 | module_t *loadMod31(uint8_t *buffer, uint32_t filesize) |
| 38 | { |
| 39 | module_t *m = createEmptyMod(); |
| 40 | if (m == NULL) |
| 41 | { |
| 42 | statusOutOfMemory(); |
| 43 | goto loadError; |
| 44 | } |
| 45 | |
| 46 | uint8_t numChannels; |
| 47 | uint8_t modFormat = getMod31Type(buffer, filesize, &numChannels); |
| 48 | if (modFormat == FORMAT_UNKNOWN || numChannels > PAULA_VOICES) |
| 49 | { |
| 50 | displayErrorMsg("UNSUPPORTED MOD !"); |
| 51 | goto loadError; |
| 52 | } |
| 53 | |
| 54 | uint8_t *p = buffer; |
| 55 | |
| 56 | memcpy(m->header.name, p, 20); p += 20; |
| 57 | |
| 58 | // read sample headers |
| 59 | moduleSample_t *s = m->samples; |
| 60 | for (int32_t i = 0; i < MOD_SAMPLES; i++, s++) |
| 61 | { |
| 62 | memcpy(s->text, p, 22); p += 22; |
| 63 | |
| 64 | realSampleLengths[i] = ((p[0] << 8) | p[1]) * 2; p += 2; |
| 65 | s->length = (realSampleLengths[i] > config.maxSampleLength) ? config.maxSampleLength : realSampleLengths[i]; |
| 66 | |
| 67 | s->fineTune = *p++ & 0xF; |
| 68 | s->volume = *p++; |
| 69 | |
| 70 | s->loopStart = ((p[0] << 8) | p[1]) * 2; p += 2; |
| 71 | s->loopLength = ((p[0] << 8) | p[1]) * 2; p += 2; |
| 72 | |
| 73 | // fix for poorly converted STK (< v2.5) -> PT/NT modules |
| 74 | if (s->loopLength > 2 && s->loopStart+s->loopLength > s->length) |
| 75 | { |
| 76 | if ((s->loopStart/2) + s->loopLength <= s->length) |
| 77 | s->loopStart /= 2; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | m->header.songLength = *p++; |
| 82 | p++; // skip uninteresting field (127 in PT MOds, "restart pos" in PC MODs) |
| 83 | |
| 84 | if (modFormat == FORMAT_PT && m->header.songLength == 129) |
| 85 | m->header.songLength = 127; // fixes a specific copy of beatwave.mod (XXX: hackish...) |
| 86 | |
| 87 | if (m->header.songLength == 0 || m->header.songLength > 129) |
| 88 | { |
| 89 | displayErrorMsg("NOT A MOD FILE !"); |
| 90 | goto loadError; |
| 91 | } |
| 92 | |
| 93 | // read orders and count number of patterns |
| 94 | int32_t numPatterns = 0; |
| 95 | for (int32_t i = 0; i < 128; i++) |
| 96 | { |
| 97 | m->header.patternTable[i] = *p++; |
| 98 | if (m->header.patternTable[i] > numPatterns) |
| 99 | numPatterns = m->header.patternTable[i]; |
| 100 | } |
| 101 | numPatterns++; |
| 102 | |
| 103 | if (numPatterns > MAX_PATTERNS) |
| 104 | { |
| 105 | displayErrorMsg("UNSUPPORTED MOD !"); |
| 106 | goto loadError; |
| 107 | } |
| 108 | |
| 109 | p += 4; // skip magic ID (already handled) |
| 110 | |
| 111 | // load pattern data |
| 112 | for (int32_t i = 0; i < numPatterns; i++) |
| 113 | { |
| 114 | note_t *note = m->patterns[i]; |
| 115 | for (int32_t j = 0; j < MOD_ROWS; j++) |
| 116 | { |
| 117 | for (int32_t k = 0; k < numChannels; k++, note++, p += 4) |
| 118 | { |
| 119 | note->period = ((p[0] & 0x0F) << 8) | p[1]; |
| 120 | note->sample = ((p[0] & 0xF0) | (p[2] >> 4)) & 31; |
| 121 | note->command = p[2] & 0x0F; |
| 122 | note->param = p[3]; |
| 123 | } |
| 124 | |
| 125 | if (numChannels < PAULA_VOICES) |
| 126 | note += PAULA_VOICES-numChannels; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (modFormat != FORMAT_PT) // pattern command conversion for non-PT formats |
| 131 | { |
| 132 | for (int32_t i = 0; i < numPatterns; i++) |
| 133 | { |
| 134 | note_t *note = m->patterns[i]; |
| 135 | for (int32_t j = 0; j < MOD_ROWS*4; j++, note++) |
| 136 | { |
| 137 | if (modFormat == FORMAT_NT || modFormat == FORMAT_HMNT) |
| 138 | { |
| 139 | // any Dxx == D00 in NT/HMNT |
| 140 | if (note->command == 0xD) |
| 141 | note->param = 0; |
| 142 | |
| 143 | // effect F with param 0x00 does nothing in NT/HMNT |
| 144 | if (note->command == 0xF && note->param == 0) |
| 145 | note->command = 0; |
| 146 | } |
| 147 | else if (modFormat == FORMAT_FLT) // Startrekker (4 channels) |
| 148 | { |
| 149 | if (note->command == 0xE) // remove unsupported "assembly macros" command |
| 150 | { |
| 151 | note->command = 0; |
| 152 | note->param = 0; |
| 153 | } |
| 154 | |
| 155 | // Startrekker is always in vblank mode, and limits speed to 0x1F |
| 156 | if (note->command == 0xF && note->param > 0x1F) |
| 157 | note->param = 0x1F; |
| 158 | } |
| 159 | |
| 160 | // remove E8x (Karplus-Strong is only supported for ProTracker .MODs) |
| 161 | if (note->command == 0xE && (note->param >> 4) == 0x8) |
| 162 | { |
| 163 | note->command = 0; |
| 164 | note->param = 0; |
| 165 | } |
| 166 | |
| 167 | // remove EFx (Invert Loop is only supported for ProTracker .MODs) |
| 168 | if (note->command == 0xE && (note->param >> 4) == 0xF) |
| 169 | { |
| 170 | note->command = 0; |
| 171 | note->param = 0; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // load sample data |
| 178 | s = m->samples; |
| 179 | for (int32_t i = 0; i < MOD_SAMPLES; i++, s++) |
| 180 | { |
| 181 | int32_t bytesToSkip = 0; |
| 182 | if (realSampleLengths[i] > config.maxSampleLength) |
| 183 | bytesToSkip = realSampleLengths[i] - config.maxSampleLength; |
| 184 | |
| 185 | memcpy(&m->sampleData[s->offset], p, s->length); p += s->length; |
| 186 | if (bytesToSkip > 0) |
| 187 | p += bytesToSkip; |
| 188 | } |
| 189 | |
| 190 | m->header.initialTempo = 125; |
| 191 | return m; |
| 192 | |
| 193 | loadError: |
| 194 | if (m != NULL) |
| 195 | { |
| 196 | for (int32_t i = 0; i < MAX_PATTERNS; i++) |
| 197 | { |
| 198 | if (m->patterns[i] != NULL) |
| 199 | free(m->patterns[i]); |
| 200 | } |
| 201 | |
| 202 | if (m->sampleData != NULL) |
| 203 | free(m->sampleData); |
| 204 | |
| 205 | free(m); |
| 206 | } |
| 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | static uint8_t getMod31Type(uint8_t *buffer, uint32_t filesize, uint8_t *numChannels) // 0 = not detected |
| 212 | { |
| 213 | const uint8_t *id = &buffer[1080]; |
| 214 | #define ID(s) !memcmp(s, id, 4) |
| 215 | |
| 216 | *numChannels = 0; |
| 217 | if (buffer == NULL || filesize < 1084+1024) |
| 218 | return FORMAT_UNKNOWN; |
| 219 | |
| 220 | *numChannels = 4; |
| 221 | if (ID("M.K.") || ID("M!K!") || ID("NSMS") || ID("LARD") || ID("PATT")) |
| 222 | { |
| 223 | return FORMAT_PT; // ProTracker (or compatible) |
| 224 | } |
| 225 | else if (ID("FLT4")) |
| 226 | { |
| 227 | return FORMAT_FLT; // Startrekker (4 channels) |
| 228 | } |
| 229 | else if (ID("N.T.")) |
| 230 | { |
| 231 | return FORMAT_NT; // NoiseTracker |
| 232 | } |
| 233 | else if (ID("M&K!") || ID("FEST")) |
| 234 | { |
| 235 | return FORMAT_HMNT; // His Master's NoiseTracker |
| 236 | } |
| 237 | else if (isdigit(id[0]) && (id[1] == 'C' && id[2] == 'H' && id[3] == 'N')) |
| 238 | { |
| 239 | *numChannels = id[0] - '0'; |
| 240 | return FORMAT_FT2; // Fasttracker II 1..9 channels (or other trackers) |
| 241 | } |
| 242 | else if (isdigit(id[0]) && isdigit(id[1]) && id[1] == 'C' && id[2] == 'H') |
| 243 | { |
| 244 | *numChannels = ((id[0] - '0') * 10) + (id[1] - '0'); |
| 245 | return FORMAT_FT2; // Fasttracker II 10+ channels (or other trackers) |
| 246 | } |
| 247 | |
| 248 | return FORMAT_UNKNOWN; |
| 249 | } |
| 250 | |
| 251 | bool detectMod31(uint8_t *buffer, uint32_t filesize) |
| 252 | { |
| 253 | uint8_t junk; |
| 254 | |
| 255 | if (buffer == NULL || filesize < 1084+1024) |
| 256 | return FORMAT_UNKNOWN; |
| 257 | |
| 258 | return getMod31Type(buffer, filesize, &junk) != FORMAT_UNKNOWN; |
| 259 | } |