| 1 | /* |
| 2 | * This source code is public domain. |
| 3 | * |
| 4 | * Authors: Olivier Lapicque <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | /////////////////////////////////////////////////// |
| 9 | // |
| 10 | // PSM module loader |
| 11 | // |
| 12 | /////////////////////////////////////////////////// |
| 13 | #include "stdafx.h" |
| 14 | #include "sndfile.h" |
| 15 | |
| 16 | //#define PSM_LOG |
| 17 | |
| 18 | #define PSM_ID_NEW 0x204d5350 |
| 19 | #define PSM_ID_OLD 0xfe4d5350 |
| 20 | #define IFFID_FILE 0x454c4946 |
| 21 | #define IFFID_TITL 0x4c544954 |
| 22 | #define IFFID_SDFT 0x54464453 |
| 23 | #define IFFID_PBOD 0x444f4250 |
| 24 | #define IFFID_SONG 0x474e4f53 |
| 25 | #define IFFID_PATT 0x54544150 |
| 26 | #define IFFID_DSMP 0x504d5344 |
| 27 | #define IFFID_OPLH 0x484c504f |
| 28 | |
| 29 | #pragma pack(1) |
| 30 | |
| 31 | typedef struct _PSMCHUNK |
| 32 | { |
| 33 | DWORD id; |
| 34 | DWORD len; |
| 35 | DWORD listid; |
| 36 | } PSMCHUNK; |
| 37 | |
| 38 | typedef struct _PSMSONGHDR |
| 39 | { |
| 40 | CHAR songname[8]; // "MAINSONG" |
| 41 | BYTE reserved1; |
| 42 | BYTE reserved2; |
| 43 | BYTE channels; |
| 44 | } PSMSONGHDR; |
| 45 | |
| 46 | typedef struct _PSMPATTERN |
| 47 | { |
| 48 | DWORD size; |
| 49 | DWORD name; |
| 50 | WORD rows; |
| 51 | WORD reserved1; |
| 52 | BYTE data[4]; |
| 53 | } PSMPATTERN; |
| 54 | |
| 55 | typedef struct _PSMSAMPLE |
| 56 | { |
| 57 | BYTE flags; |
| 58 | CHAR songname[8]; |
| 59 | DWORD smpid; |
| 60 | CHAR samplename[34]; |
| 61 | DWORD reserved1; |
| 62 | BYTE reserved2; |
| 63 | BYTE insno; |
| 64 | BYTE reserved3; |
| 65 | DWORD length; |
| 66 | DWORD loopstart; |
| 67 | DWORD loopend; |
| 68 | WORD reserved4; |
| 69 | BYTE defvol; |
| 70 | DWORD reserved5; |
| 71 | DWORD samplerate; |
| 72 | BYTE reserved6[19]; |
| 73 | } PSMSAMPLE; |
| 74 | |
| 75 | #pragma pack() |
| 76 | |
| 77 | static void swap_PSMCHUNK(PSMCHUNK* p){ |
| 78 | p->id = bswapLE32(p->id); |
| 79 | p->len = bswapLE32(p->len); |
| 80 | p->listid = bswapLE32(p->listid); |
| 81 | } |
| 82 | |
| 83 | static void swap_PSMPATTERN(PSMPATTERN* p){ |
| 84 | p->size = bswapLE32(p->size); |
| 85 | p->name = bswapLE32(p->name); |
| 86 | p->rows = bswapLE16(p->rows); |
| 87 | } |
| 88 | |
| 89 | static void swap_PSMSAMPLE(PSMSAMPLE* p){ |
| 90 | p->smpid = bswapLE32(p->smpid); |
| 91 | p->length = bswapLE32(p->length); |
| 92 | p->loopstart = bswapLE32(p->loopstart); |
| 93 | p->loopend = bswapLE32(p->loopend); |
| 94 | p->samplerate = bswapLE32(p->samplerate); |
| 95 | } |
| 96 | |
| 97 | BOOL CSoundFile::ReadPSM(LPCBYTE lpStream, DWORD dwMemLength) |
| 98 | //----------------------------------------------------------- |
| 99 | { |
| 100 | PSMCHUNK *pfh = (PSMCHUNK *)lpStream; |
| 101 | DWORD dwMemPos, dwSongPos; |
| 102 | DWORD patptrs[MAX_PATTERNS]; |
| 103 | BYTE samplemap[MAX_SAMPLES]; |
| 104 | UINT nPatterns; |
| 105 | |
| 106 | if (dwMemLength < 256) return FALSE; |
| 107 | |
| 108 | // Swap chunk |
| 109 | swap_PSMCHUNK(pfh); |
| 110 | |
| 111 | // Chunk0: "PSM ",filesize,"FILE" |
| 112 | if (pfh->id == PSM_ID_OLD) |
| 113 | { |
| 114 | #ifdef PSM_LOG |
| 115 | Log("Old PSM format not supported\n"); |
| 116 | #endif |
| 117 | return FALSE; |
| 118 | } |
| 119 | if ((pfh->id != PSM_ID_NEW) || (pfh->len+12 > dwMemLength) || (pfh->listid != IFFID_FILE)) return FALSE; |
| 120 | m_nType = MOD_TYPE_PSM; |
| 121 | m_nChannels = 16; |
| 122 | m_nSamples = 0; |
| 123 | nPatterns = 0; |
| 124 | dwMemPos = 12; |
| 125 | dwSongPos = 0; |
| 126 | for (UINT iChPan=0; iChPan<16; iChPan++) |
| 127 | { |
| 128 | UINT pan = (((iChPan & 3) == 1) || ((iChPan&3)==2)) ? 0xC0 : 0x40; |
| 129 | ChnSettings[iChPan].nPan = pan; |
| 130 | } |
| 131 | while (dwMemPos+8 < dwMemLength) |
| 132 | { |
| 133 | PSMCHUNK *pchunk = (PSMCHUNK *)(lpStream+dwMemPos); |
| 134 | swap_PSMCHUNK(pchunk); |
| 135 | if ((pchunk->len >= dwMemLength - 8) || (dwMemPos + pchunk->len + 8 > dwMemLength)) break; |
| 136 | dwMemPos += 8; |
| 137 | PUCHAR pdata = (PUCHAR)(lpStream+dwMemPos); |
| 138 | ULONG len = pchunk->len; |
| 139 | if (len) switch(pchunk->id) |
| 140 | { |
| 141 | // "TITL": Song title |
| 142 | case IFFID_TITL: |
| 143 | if (!pdata[0]) { pdata++; len--; } |
| 144 | memcpy(m_szNames[0], pdata, (len>31) ? 31 : len); |
| 145 | m_szNames[0][31] = 0; |
| 146 | break; |
| 147 | // "PBOD": Pattern |
| 148 | case IFFID_PBOD: |
| 149 | if ((len >= 12) && (nPatterns < MAX_PATTERNS)) |
| 150 | { |
| 151 | patptrs[nPatterns++] = dwMemPos-8; |
| 152 | } |
| 153 | break; |
| 154 | // "SONG": Song description |
| 155 | case IFFID_SONG: |
| 156 | if ((len >= sizeof(PSMSONGHDR)+8) && (!dwSongPos)) |
| 157 | { |
| 158 | dwSongPos = dwMemPos - 8; |
| 159 | } |
| 160 | break; |
| 161 | // "DSMP": Sample Data |
| 162 | case IFFID_DSMP: |
| 163 | if ((len >= sizeof(PSMSAMPLE)) && (m_nSamples+1 < MAX_SAMPLES)) |
| 164 | { |
| 165 | m_nSamples++; |
| 166 | MODINSTRUMENT *pins = &Ins[m_nSamples]; |
| 167 | PSMSAMPLE *psmp = (PSMSAMPLE *)pdata; |
| 168 | swap_PSMSAMPLE(psmp); |
| 169 | memcpy(m_szNames[m_nSamples], psmp->samplename, 31); |
| 170 | m_szNames[m_nSamples][31] = 0; |
| 171 | samplemap[m_nSamples-1] = (BYTE)m_nSamples; |
| 172 | // Init sample |
| 173 | pins->nGlobalVol = 0x40; |
| 174 | pins->nC4Speed = psmp->samplerate; |
| 175 | pins->nLength = psmp->length; |
| 176 | pins->nLoopStart = psmp->loopstart; |
| 177 | pins->nLoopEnd = psmp->loopend; |
| 178 | pins->nPan = 128; |
| 179 | pins->nVolume = (psmp->defvol+1) * 2; |
| 180 | pins->uFlags = (psmp->flags & 0x80) ? CHN_LOOP : 0; |
| 181 | if (pins->nLoopStart > 0) pins->nLoopStart--; |
| 182 | // Point to sample data |
| 183 | pdata += 0x60; |
| 184 | len -= 0x60; |
| 185 | // Load sample data |
| 186 | if ((pins->nLength > 3) && (len > 3)) |
| 187 | { |
| 188 | ReadSample(pins, RS_PCM8D, (LPCSTR)pdata, len); |
| 189 | } else |
| 190 | { |
| 191 | pins->nLength = 0; |
| 192 | } |
| 193 | } |
| 194 | break; |
| 195 | #if 0 |
| 196 | default: |
| 197 | { |
| 198 | CHAR s[8], s2[64]; |
| 199 | *(DWORD *)s = pchunk->id; |
| 200 | s[4] = 0; |
| 201 | wsprintfA(s2, "%s: %4d bytes @ %4d\n", s, pchunk->len, dwMemPos); |
| 202 | OutputDebugString(s2); |
| 203 | } |
| 204 | #endif |
| 205 | } |
| 206 | dwMemPos += pchunk->len; |
| 207 | } |
| 208 | // Step #1: convert song structure |
| 209 | PSMSONGHDR *pSong = (PSMSONGHDR *)(lpStream+dwSongPos+8); |
| 210 | if ((!dwSongPos) || (pSong->channels < 2) || (pSong->channels > 32)) return TRUE; |
| 211 | m_nChannels = pSong->channels; |
| 212 | // Valid song header -> convert attached chunks |
| 213 | { |
| 214 | DWORD dwSongEnd = dwSongPos + 8 + READ_LE32(lpStream+dwSongPos+4); |
| 215 | dwMemPos = dwSongPos + 8 + 11; // sizeof(PSMCHUNK)+sizeof(PSMSONGHDR) |
| 216 | while (dwMemPos + 8 < dwSongEnd) |
| 217 | { |
| 218 | PSMCHUNK *pchunk = (PSMCHUNK *)(lpStream+dwMemPos); |
| 219 | swap_PSMCHUNK(pchunk); |
| 220 | dwMemPos += 8; |
| 221 | if ((pchunk->len > dwSongEnd) || (dwMemPos + pchunk->len > dwSongEnd)) break; |
| 222 | PUCHAR pdata = (PUCHAR)(lpStream+dwMemPos); |
| 223 | ULONG len = pchunk->len; |
| 224 | switch(pchunk->id) |
| 225 | { |
| 226 | case IFFID_OPLH: |
| 227 | if (len >= 0x20) |
| 228 | { |
| 229 | UINT pos = len - 3; |
| 230 | while (pos > 5) |
| 231 | { |
| 232 | BOOL bFound = FALSE; |
| 233 | pos -= 5; |
| 234 | DWORD dwName = READ_LE32(pdata+pos); |
| 235 | for (UINT i=0; i<nPatterns; i++) |
| 236 | { |
| 237 | DWORD dwPatName = READ_LE32(lpStream+patptrs[i]+12); |
| 238 | if (dwName == dwPatName) |
| 239 | { |
| 240 | bFound = TRUE; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | if ((!bFound) && (pdata[pos+1] > 0) && (pdata[pos+1] <= 0x10) |
| 245 | && (pdata[pos+3] > 0x40) && (pdata[pos+3] < 0xC0)) |
| 246 | { |
| 247 | m_nDefaultSpeed = pdata[pos+1]; |
| 248 | m_nDefaultTempo = pdata[pos+3]; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | UINT iOrd = 0; |
| 253 | while ((pos+5<len) && (iOrd < MAX_ORDERS)) |
| 254 | { |
| 255 | DWORD dwName = READ_LE32(pdata+pos); |
| 256 | for (UINT i=0; i<nPatterns; i++) |
| 257 | { |
| 258 | DWORD dwPatName = READ_LE32(lpStream+patptrs[i]+12); |
| 259 | if (dwName == dwPatName) |
| 260 | { |
| 261 | Order[iOrd++] = i; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | pos += 5; |
| 266 | } |
| 267 | } |
| 268 | break; |
| 269 | } |
| 270 | dwMemPos += pchunk->len; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Step #2: convert patterns |
| 275 | for (UINT nPat=0; nPat<nPatterns; nPat++) |
| 276 | { |
| 277 | PSMPATTERN *pPsmPat = (PSMPATTERN *)(lpStream+patptrs[nPat]+8); |
| 278 | swap_PSMPATTERN(pPsmPat); |
| 279 | ULONG len = *(DWORD *)(lpStream+patptrs[nPat]+4) - 12; |
| 280 | UINT nRows = pPsmPat->rows; |
| 281 | if (len > pPsmPat->size) len = pPsmPat->size; |
| 282 | if ((nRows < 64) || (nRows > 256)) nRows = 64; |
| 283 | PatternSize[nPat] = nRows; |
| 284 | if ((Patterns[nPat] = AllocatePattern(nRows, m_nChannels)) == NULL) break; |
| 285 | MODCOMMAND *m = Patterns[nPat]; |
| 286 | BYTE *p = pPsmPat->data; |
| 287 | MODCOMMAND *sp, dummy; |
| 288 | UINT pos = 0; |
| 289 | UINT row = 0; |
| 290 | UINT rowlim; |
| 291 | #ifdef PSM_LOG |
| 292 | Log("Pattern %d at offset 0x%04X\n", nPat, (DWORD)(p - (BYTE *)lpStream)); |
| 293 | #endif |
| 294 | UINT flags, ch; |
| 295 | rowlim = bswapLE16(pPsmPat->reserved1)-2; |
| 296 | while ((row < nRows) && (pos+3 < len)) |
| 297 | { |
| 298 | if ((pos+1) >= rowlim) { |
| 299 | pos = rowlim; |
| 300 | rowlim = (((int)p[pos+1])<<8) |
| 301 | | ((int)p[pos+0]); |
| 302 | m += m_nChannels; |
| 303 | row++; |
| 304 | rowlim += pos; |
| 305 | pos += 2; |
| 306 | } |
| 307 | if (row >= nRows) continue; |
| 308 | |
| 309 | flags = p[pos++]; |
| 310 | ch = p[pos++]; |
| 311 | if (ch >= m_nChannels) { |
| 312 | sp = &dummy; |
| 313 | } else { |
| 314 | sp = &m[ch]; |
| 315 | } |
| 316 | // Note + Instr |
| 317 | if ((flags & 0x80) && (pos+1 < len)) |
| 318 | { |
| 319 | UINT note = p[pos++]; |
| 320 | note = (note>>4)*12+(note&0x0f)+12+1; |
| 321 | if (note > 0x80) note = 0; |
| 322 | sp->note = note; |
| 323 | } |
| 324 | if ((flags & 0x40) && (pos+1 < len)) |
| 325 | { |
| 326 | UINT nins = p[pos++]; |
| 327 | #ifdef PSM_LOG |
| 328 | //if (!nPat) Log("note+ins: %02X.%02X\n", note, nins); |
| 329 | if ((!nPat) && (nins >= m_nSamples)) Log("WARNING: invalid instrument number (%d)\n", nins); |
| 330 | #endif |
| 331 | sp->instr = samplemap[nins]; |
| 332 | } |
| 333 | // Volume |
| 334 | if ((flags & 0x20) && (pos < len)) |
| 335 | { |
| 336 | sp->volcmd = VOLCMD_VOLUME; |
| 337 | sp->vol = p[pos++] / 2; |
| 338 | } |
| 339 | // Effect |
| 340 | if ((flags & 0x10) && (pos+1 < len)) |
| 341 | { |
| 342 | UINT command = p[pos++]; |
| 343 | UINT param = p[pos++]; |
| 344 | // Convert effects |
| 345 | switch(command) |
| 346 | { |
| 347 | // 01: fine volslide up |
| 348 | case 0x01: command = CMD_VOLUMESLIDE; param |= 0x0f; |
| 349 | if (param == 15) param=31; |
| 350 | break; |
| 351 | // 02: volslide up |
| 352 | case 0x02: command = CMD_VOLUMESLIDE; param>>=1; param<<=4; break; |
| 353 | // 03: fine volslide down |
| 354 | case 0x03: command = CMD_VOLUMESLIDE; param>>=4; param |= 0xf0; |
| 355 | if (param == 240) param=241; |
| 356 | break; |
| 357 | // 04: fine volslide down |
| 358 | case 0x04: command = CMD_VOLUMESLIDE; param>>=4; param |= 0xf0; break; |
| 359 | // 0C: portamento up |
| 360 | case 0x0C: command = CMD_PORTAMENTOUP; param = (param+1)/2; break; |
| 361 | // 0E: portamento down |
| 362 | case 0x0E: command = CMD_PORTAMENTODOWN; param = (param+1)/2; break; |
| 363 | // 0F: tone portamento |
| 364 | case 0x0F: command = CMD_TONEPORTAMENTO; param = param/4; break; |
| 365 | // 15: vibrato |
| 366 | case 0x15: command = CMD_VIBRATO; break; |
| 367 | // 29: sample offset |
| 368 | case 0x29: pos += 2; break; |
| 369 | // 2A: retrigger note |
| 370 | case 0x2A: command = CMD_RETRIG; break; |
| 371 | // 33: Position Jump |
| 372 | case 0x33: command = CMD_POSITIONJUMP; break; |
| 373 | // 34: Pattern break |
| 374 | case 0x34: command = CMD_PATTERNBREAK; break; |
| 375 | // 3D: speed |
| 376 | case 0x3D: command = CMD_SPEED; break; |
| 377 | // 3E: tempo |
| 378 | case 0x3E: command = CMD_TEMPO; break; |
| 379 | // Unknown |
| 380 | default: |
| 381 | #ifdef PSM_LOG |
| 382 | Log("Unknown PSM effect pat=%d row=%d ch=%d: %02X.%02X\n", nPat, row, ch, command, param); |
| 383 | #endif |
| 384 | command = param = 0; |
| 385 | } |
| 386 | sp->command = (BYTE)command; |
| 387 | sp->param = (BYTE)param; |
| 388 | } |
| 389 | } |
| 390 | #ifdef PSM_LOG |
| 391 | if (pos < len) |
| 392 | { |
| 393 | Log("Pattern %d: %d/%d[%d] rows (%d bytes) -> %d bytes left\n", nPat, row, nRows, pPsmPat->rows, pPsmPat->size, len-pos); |
| 394 | } |
| 395 | #endif |
| 396 | } |
| 397 | |
| 398 | // Done (finally!) |
| 399 | return TRUE; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | ////////////////////////////////////////////////////////////// |
| 404 | // |
| 405 | // PSM Old Format |
| 406 | // |
| 407 | |
| 408 | /* |
| 409 | |
| 410 | CONST |
| 411 | c_PSM_MaxOrder = $FF; |
| 412 | c_PSM_MaxSample = $FF; |
| 413 | c_PSM_MaxChannel = $0F; |
| 414 | |
| 415 | TYPE |
| 416 | PPSM_Header = ^TPSM_Header; |
| 417 | TPSM_Header = RECORD |
| 418 | PSM_Sign : ARRAY[01..04] OF CHAR; { PSM + #254 } |
| 419 | PSM_SongName : ARRAY[01..58] OF CHAR; |
| 420 | PSM_Byte00 : BYTE; |
| 421 | PSM_Byte1A : BYTE; |
| 422 | PSM_Unknown00 : BYTE; |
| 423 | PSM_Unknown01 : BYTE; |
| 424 | PSM_Unknown02 : BYTE; |
| 425 | PSM_Speed : BYTE; |
| 426 | PSM_Tempo : BYTE; |
| 427 | PSM_Unknown03 : BYTE; |
| 428 | PSM_Unknown04 : WORD; |
| 429 | PSM_OrderLength : WORD; |
| 430 | PSM_PatternNumber : WORD; |
| 431 | PSM_SampleNumber : WORD; |
| 432 | PSM_ChannelNumber : WORD; |
| 433 | PSM_ChannelUsed : WORD; |
| 434 | PSM_OrderPosition : LONGINT; |
| 435 | PSM_ChannelSettingPosition : LONGINT; |
| 436 | PSM_PatternPosition : LONGINT; |
| 437 | PSM_SamplePosition : LONGINT; |
| 438 | { *** perhaps there are some more infos in a larger header, |
| 439 | but i have not decoded it and so it apears here NOT } |
| 440 | END; |
| 441 | |
| 442 | PPSM_Sample = ^TPSM_Sample; |
| 443 | TPSM_Sample = RECORD |
| 444 | PSM_SampleFileName : ARRAY[01..12] OF CHAR; |
| 445 | PSM_SampleByte00 : BYTE; |
| 446 | PSM_SampleName : ARRAY[01..22] OF CHAR; |
| 447 | PSM_SampleUnknown00 : ARRAY[01..02] OF BYTE; |
| 448 | PSM_SamplePosition : LONGINT; |
| 449 | PSM_SampleUnknown01 : ARRAY[01..04] OF BYTE; |
| 450 | PSM_SampleNumber : BYTE; |
| 451 | PSM_SampleFlags : WORD; |
| 452 | PSM_SampleLength : LONGINT; |
| 453 | PSM_SampleLoopBegin : LONGINT; |
| 454 | PSM_SampleLoopEnd : LONGINT; |
| 455 | PSM_Unknown03 : BYTE; |
| 456 | PSM_SampleVolume : BYTE; |
| 457 | PSM_SampleC5Speed : WORD; |
| 458 | END; |
| 459 | |
| 460 | PPSM_SampleList = ^TPSM_SampleList; |
| 461 | TPSM_SampleList = ARRAY[01..c_PSM_MaxSample] OF TPSM_Sample; |
| 462 | |
| 463 | PPSM_Order = ^TPSM_Order; |
| 464 | TPSM_Order = ARRAY[00..c_PSM_MaxOrder] OF BYTE; |
| 465 | |
| 466 | PPSM_ChannelSettings = ^TPSM_ChannelSettings; |
| 467 | TPSM_ChannelSettings = ARRAY[00..c_PSM_MaxChannel] OF BYTE; |
| 468 | |
| 469 | CONST |
| 470 | PSM_NotesInPattern : BYTE = $00; |
| 471 | PSM_ChannelInPattern : BYTE = $00; |
| 472 | |
| 473 | CONST |
| 474 | c_PSM_SetSpeed = 60; |
| 475 | |
| 476 | FUNCTION PSM_Size(FileName : STRING;FilePosition : LONGINT) : LONGINT; |
| 477 | BEGIN |
| 478 | END; |
| 479 | |
| 480 | PROCEDURE PSM_UnpackPattern(VAR Source,Destination;PatternLength : WORD); |
| 481 | VAR |
| 482 | Witz : ARRAY[00..04] OF WORD; |
| 483 | I1,I2 : WORD; |
| 484 | I3,I4 : WORD; |
| 485 | TopicalByte : ^BYTE; |
| 486 | Pattern : PUnpackedPattern; |
| 487 | ChannelP : BYTE; |
| 488 | NoteP : BYTE; |
| 489 | InfoByte : BYTE; |
| 490 | CodeByte : BYTE; |
| 491 | InfoWord : WORD; |
| 492 | Effect : BYTE; |
| 493 | Opperand : BYTE; |
| 494 | Panning : BYTE; |
| 495 | Volume : BYTE; |
| 496 | PrevInfo : BYTE; |
| 497 | InfoIndex : BYTE; |
| 498 | BEGIN |
| 499 | Pattern := @Destination; |
| 500 | TopicalByte := @Source; |
| 501 | { *** Initialize patttern } |
| 502 | FOR I2 := 0 TO c_Maximum_NoteIndex DO |
| 503 | FOR I3 := 0 TO c_Maximum_ChannelIndex DO |
| 504 | BEGIN |
| 505 | Pattern^[I2,I3,c_Pattern_NoteIndex] := $FF; |
| 506 | Pattern^[I2,I3,c_Pattern_SampleIndex] := $00; |
| 507 | Pattern^[I2,I3,c_Pattern_VolumeIndex] := $FF; |
| 508 | Pattern^[I2,I3,c_Pattern_PanningIndex] := $FF; |
| 509 | Pattern^[I2,I3,c_Pattern_EffectIndex] := $00; |
| 510 | Pattern^[I2,I3,c_Pattern_OpperandIndex] := $00; |
| 511 | END; |
| 512 | { *** Byte-pointer on first pattern-entry } |
| 513 | ChannelP := $00; |
| 514 | NoteP := $00; |
| 515 | InfoByte := $00; |
| 516 | PrevInfo := $00; |
| 517 | InfoIndex := $02; |
| 518 | { *** read notes in pattern } |
| 519 | PSM_NotesInPattern := TopicalByte^; INC(TopicalByte); DEC(PatternLength); INC(InfoIndex); |
| 520 | PSM_ChannelInPattern := TopicalByte^; INC(TopicalByte); DEC(PatternLength); INC(InfoIndex); |
| 521 | { *** unpack pattern } |
| 522 | WHILE (INTEGER(PatternLength) > 0) AND (NoteP < c_Maximum_NoteIndex) DO |
| 523 | BEGIN |
| 524 | { *** Read info-byte } |
| 525 | InfoByte := TopicalByte^; INC(TopicalByte); DEC(PatternLength); INC(InfoIndex); |
| 526 | IF InfoByte <> $00 THEN |
| 527 | BEGIN |
| 528 | ChannelP := InfoByte AND $0F; |
| 529 | IF InfoByte AND 128 = 128 THEN { note and sample } |
| 530 | BEGIN |
| 531 | { *** read note } |
| 532 | CodeByte := TopicalByte^; INC(TopicalByte); DEC(PatternLength); |
| 533 | DEC(CodeByte); |
| 534 | CodeByte := CodeByte MOD 12 * 16 + CodeByte DIV 12 + 2; |
| 535 | Pattern^[NoteP,ChannelP,c_Pattern_NoteIndex] := CodeByte; |
| 536 | { *** read sample } |
| 537 | CodeByte := TopicalByte^; INC(TopicalByte); DEC(PatternLength); |
| 538 | Pattern^[NoteP,ChannelP,c_Pattern_SampleIndex] := CodeByte; |
| 539 | END; |
| 540 | IF InfoByte AND 64 = 64 THEN { Volume } |
| 541 | BEGIN |
| 542 | CodeByte := TopicalByte^; INC(TopicalByte); DEC(PatternLength); |
| 543 | Pattern^[NoteP,ChannelP,c_Pattern_VolumeIndex] := CodeByte; |
| 544 | END; |
| 545 | IF InfoByte AND 32 = 32 THEN { effect AND opperand } |
| 546 | BEGIN |
| 547 | Effect := TopicalByte^; INC(TopicalByte); DEC(PatternLength); |
| 548 | Opperand := TopicalByte^; INC(TopicalByte); DEC(PatternLength); |
| 549 | CASE Effect OF |
| 550 | c_PSM_SetSpeed: |
| 551 | BEGIN |
| 552 | Effect := c_I_Set_Speed; |
| 553 | END; |
| 554 | ELSE |
| 555 | BEGIN |
| 556 | Effect := c_I_NoEffect; |
| 557 | Opperand := $00; |
| 558 | END; |
| 559 | END; |
| 560 | Pattern^[NoteP,ChannelP,c_Pattern_EffectIndex] := Effect; |
| 561 | Pattern^[NoteP,ChannelP,c_Pattern_OpperandIndex] := Opperand; |
| 562 | END; |
| 563 | END ELSE INC(NoteP); |
| 564 | END; |
| 565 | END; |
| 566 | |
| 567 | PROCEDURE PSM_Load(FileName : STRING;FilePosition : LONGINT;VAR Module : PModule;VAR ErrorCode : WORD); |
| 568 | { *** caution : Module has to be inited before!!!! } |
| 569 | VAR |
| 570 | Header : PPSM_Header; |
| 571 | Sample : PPSM_SampleList; |
| 572 | Order : PPSM_Order; |
| 573 | ChannelSettings : PPSM_ChannelSettings; |
| 574 | MultiPurposeBuffer : PByteArray; |
| 575 | PatternBuffer : PUnpackedPattern; |
| 576 | TopicalParaPointer : WORD; |
| 577 | |
| 578 | InFile : FILE; |
| 579 | I1,I2 : WORD; |
| 580 | I3,I4 : WORD; |
| 581 | TempW : WORD; |
| 582 | TempB : BYTE; |
| 583 | TempP : PByteArray; |
| 584 | TempI : INTEGER; |
| 585 | { *** copy-vars for loop-extension } |
| 586 | CopySource : LONGINT; |
| 587 | CopyDestination : LONGINT; |
| 588 | CopyLength : LONGINT; |
| 589 | BEGIN |
| 590 | { *** try to open file } |
| 591 | ASSIGN(InFile,FileName); |
| 592 | {$I-} |
| 593 | RESET(InFile,1); |
| 594 | {$I+} |
| 595 | IF IORESULT <> $00 THEN |
| 596 | BEGIN |
| 597 | EXIT; |
| 598 | END; |
| 599 | {$I-} |
| 600 | { *** seek start of module } |
| 601 | IF FILESIZE(InFile) < FilePosition THEN |
| 602 | BEGIN |
| 603 | EXIT; |
| 604 | END; |
| 605 | SEEK(InFile,FilePosition); |
| 606 | { *** look for enough memory for temporary variables } |
| 607 | IF MEMAVAIL < SIZEOF(TPSM_Header) + SIZEOF(TPSM_SampleList) + |
| 608 | SIZEOF(TPSM_Order) + SIZEOF(TPSM_ChannelSettings) + |
| 609 | SIZEOF(TByteArray) + SIZEOF(TUnpackedPattern) |
| 610 | THEN |
| 611 | BEGIN |
| 612 | EXIT; |
| 613 | END; |
| 614 | { *** init dynamic variables } |
| 615 | NEW(Header); |
| 616 | NEW(Sample); |
| 617 | NEW(Order); |
| 618 | NEW(ChannelSettings); |
| 619 | NEW(MultiPurposeBuffer); |
| 620 | NEW(PatternBuffer); |
| 621 | { *** read header } |
| 622 | BLOCKREAD(InFile,Header^,SIZEOF(TPSM_Header)); |
| 623 | { *** test if this is a DSM-file } |
| 624 | IF NOT ((Header^.PSM_Sign[1] = 'P') AND (Header^.PSM_Sign[2] = 'S') AND |
| 625 | (Header^.PSM_Sign[3] = 'M') AND (Header^.PSM_Sign[4] = #254)) THEN |
| 626 | BEGIN |
| 627 | ErrorCode := c_NoValidFileFormat; |
| 628 | CLOSE(InFile); |
| 629 | EXIT; |
| 630 | END; |
| 631 | { *** read order } |
| 632 | SEEK(InFile,FilePosition + Header^.PSM_OrderPosition); |
| 633 | BLOCKREAD(InFile,Order^,Header^.PSM_OrderLength); |
| 634 | { *** read channelsettings } |
| 635 | SEEK(InFile,FilePosition + Header^.PSM_ChannelSettingPosition); |
| 636 | BLOCKREAD(InFile,ChannelSettings^,SIZEOF(TPSM_ChannelSettings)); |
| 637 | { *** read samplelist } |
| 638 | SEEK(InFile,FilePosition + Header^.PSM_SamplePosition); |
| 639 | BLOCKREAD(InFile,Sample^,Header^.PSM_SampleNumber * SIZEOF(TPSM_Sample)); |
| 640 | { *** copy header to intern NTMIK-structure } |
| 641 | Module^.Module_Sign := 'MF'; |
| 642 | Module^.Module_FileFormatVersion := $0100; |
| 643 | Module^.Module_SampleNumber := Header^.PSM_SampleNumber; |
| 644 | Module^.Module_PatternNumber := Header^.PSM_PatternNumber; |
| 645 | Module^.Module_OrderLength := Header^.PSM_OrderLength; |
| 646 | Module^.Module_ChannelNumber := Header^.PSM_ChannelNumber+1; |
| 647 | Module^.Module_Initial_GlobalVolume := 64; |
| 648 | Module^.Module_Initial_MasterVolume := $C0; |
| 649 | Module^.Module_Initial_Speed := Header^.PSM_Speed; |
| 650 | Module^.Module_Initial_Tempo := Header^.PSM_Tempo; |
| 651 | { *** paragraph 01 start } |
| 652 | Module^.Module_Flags := c_Module_Flags_ZeroVolume * BYTE(1) + |
| 653 | c_Module_Flags_Stereo * BYTE(1) + |
| 654 | c_Module_Flags_ForceAmigaLimits * BYTE(0) + |
| 655 | c_Module_Flags_Panning * BYTE(1) + |
| 656 | c_Module_Flags_Surround * BYTE(1) + |
| 657 | c_Module_Flags_QualityMixing * BYTE(1) + |
| 658 | c_Module_Flags_FastVolumeSlides * BYTE(0) + |
| 659 | c_Module_Flags_SpecialCustomData * BYTE(0) + |
| 660 | c_Module_Flags_SongName * BYTE(1); |
| 661 | I1 := $01; |
| 662 | WHILE (Header^.PSM_SongName[I1] > #00) AND (I1 < c_Module_SongNameLength) DO |
| 663 | BEGIN |
| 664 | Module^.Module_Name[I1] := Header^.PSM_SongName[I1]; |
| 665 | INC(I1); |
| 666 | END; |
| 667 | Module^.Module_Name[c_Module_SongNameLength] := #00; |
| 668 | { *** Init channelsettings } |
| 669 | FOR I1 := 0 TO c_Maximum_ChannelIndex DO |
| 670 | BEGIN |
| 671 | IF I1 < Header^.PSM_ChannelUsed THEN |
| 672 | BEGIN |
| 673 | { *** channel enabled } |
| 674 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_GlobalVolume := 64; |
| 675 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_Panning := (ChannelSettings^[I1]) * $08; |
| 676 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_Code := I1 + $10 * BYTE(ChannelSettings^[I1] > $08) + |
| 677 | c_ChannelSettings_Code_ChannelEnabled * BYTE(1) + |
| 678 | c_ChannelSettings_Code_ChannelDigital * BYTE(1); |
| 679 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_Controls := |
| 680 | c_ChannelSettings_Controls_EnhancedMode * BYTE(1) + |
| 681 | c_ChannelSettings_Controls_SurroundMode * BYTE(0); |
| 682 | END |
| 683 | ELSE |
| 684 | BEGIN |
| 685 | { *** channel disabled } |
| 686 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_GlobalVolume := $00; |
| 687 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_Panning := $00; |
| 688 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_Code := $00; |
| 689 | Module^.Module_ChannelSettingPointer^[I1].ChannelSettings_Controls := $00; |
| 690 | END; |
| 691 | END; |
| 692 | { *** init and copy order } |
| 693 | FILLCHAR(Module^.Module_OrderPointer^,c_Maximum_OrderIndex+1,$FF); |
| 694 | MOVE(Order^,Module^.Module_OrderPointer^,Header^.PSM_OrderLength); |
| 695 | { *** read pattern } |
| 696 | SEEK(InFile,FilePosition + Header^.PSM_PatternPosition); |
| 697 | NTMIK_LoaderPatternNumber := Header^.PSM_PatternNumber-1; |
| 698 | FOR I1 := 0 TO Header^.PSM_PatternNumber-1 DO |
| 699 | BEGIN |
| 700 | NTMIK_LoadPatternProcedure; |
| 701 | { *** read length } |
| 702 | BLOCKREAD(InFile,TempW,2); |
| 703 | { *** read pattern } |
| 704 | BLOCKREAD(InFile,MultiPurposeBuffer^,TempW-2); |
| 705 | { *** unpack pattern and set notes per channel to 64 } |
| 706 | PSM_UnpackPattern(MultiPurposeBuffer^,PatternBuffer^,TempW); |
| 707 | NTMIK_PackPattern(MultiPurposeBuffer^,PatternBuffer^,PSM_NotesInPattern); |
| 708 | TempW := WORD(256) * MultiPurposeBuffer^[01] + MultiPurposeBuffer^[00]; |
| 709 | GETMEM(Module^.Module_PatternPointer^[I1],TempW); |
| 710 | MOVE(MultiPurposeBuffer^,Module^.Module_PatternPointer^[I1]^,TempW); |
| 711 | { *** next pattern } |
| 712 | END; |
| 713 | { *** read samples } |
| 714 | NTMIK_LoaderSampleNumber := Header^.PSM_SampleNumber; |
| 715 | FOR I1 := 1 TO Header^.PSM_SampleNumber DO |
| 716 | BEGIN |
| 717 | NTMIK_LoadSampleProcedure; |
| 718 | { *** get index for sample } |
| 719 | I3 := Sample^[I1].PSM_SampleNumber; |
| 720 | { *** clip PSM-sample } |
| 721 | IF Sample^[I1].PSM_SampleLoopEnd > Sample^[I1].PSM_SampleLength |
| 722 | THEN Sample^[I1].PSM_SampleLoopEnd := Sample^[I1].PSM_SampleLength; |
| 723 | { *** init intern sample } |
| 724 | NEW(Module^.Module_SamplePointer^[I3]); |
| 725 | FILLCHAR(Module^.Module_SamplePointer^[I3]^,SIZEOF(TSample),$00); |
| 726 | FILLCHAR(Module^.Module_SamplePointer^[I3]^.Sample_SampleName,c_Sample_SampleNameLength,#32); |
| 727 | FILLCHAR(Module^.Module_SamplePointer^[I3]^.Sample_FileName,c_Sample_FileNameLength,#32); |
| 728 | { *** copy informations to intern sample } |
| 729 | I2 := $01; |
| 730 | WHILE (Sample^[I1].PSM_SampleName[I2] > #00) AND (I2 < c_Sample_SampleNameLength) DO |
| 731 | BEGIN |
| 732 | Module^.Module_SamplePointer^[I3]^.Sample_SampleName[I2] := Sample^[I1].PSM_SampleName[I2]; |
| 733 | INC(I2); |
| 734 | END; |
| 735 | Module^.Module_SamplePointer^[I3]^.Sample_Sign := 'DF'; |
| 736 | Module^.Module_SamplePointer^[I3]^.Sample_FileFormatVersion := $00100; |
| 737 | Module^.Module_SamplePointer^[I3]^.Sample_Position := $00000000; |
| 738 | Module^.Module_SamplePointer^[I3]^.Sample_Selector := $0000; |
| 739 | Module^.Module_SamplePointer^[I3]^.Sample_Volume := Sample^[I1].PSM_SampleVolume; |
| 740 | Module^.Module_SamplePointer^[I3]^.Sample_LoopCounter := $00; |
| 741 | Module^.Module_SamplePointer^[I3]^.Sample_C5Speed := Sample^[I1].PSM_SampleC5Speed; |
| 742 | Module^.Module_SamplePointer^[I3]^.Sample_Length := Sample^[I1].PSM_SampleLength; |
| 743 | Module^.Module_SamplePointer^[I3]^.Sample_LoopBegin := Sample^[I1].PSM_SampleLoopBegin; |
| 744 | Module^.Module_SamplePointer^[I3]^.Sample_LoopEnd := Sample^[I1].PSM_SampleLoopEnd; |
| 745 | { *** now it's time for the flags } |
| 746 | Module^.Module_SamplePointer^[I3]^.Sample_Flags := |
| 747 | c_Sample_Flags_DigitalSample * BYTE(1) + |
| 748 | c_Sample_Flags_8BitSample * BYTE(1) + |
| 749 | c_Sample_Flags_UnsignedSampleData * BYTE(1) + |
| 750 | c_Sample_Flags_Packed * BYTE(0) + |
| 751 | c_Sample_Flags_LoopCounter * BYTE(0) + |
| 752 | c_Sample_Flags_SampleName * BYTE(1) + |
| 753 | c_Sample_Flags_LoopActive * |
| 754 | BYTE(Sample^[I1].PSM_SampleFlags AND (LONGINT(1) SHL 15) = (LONGINT(1) SHL 15)); |
| 755 | { *** alloc memory for sample-data } |
| 756 | E_Getmem(Module^.Module_SamplePointer^[I3]^.Sample_Selector, |
| 757 | Module^.Module_SamplePointer^[I3]^.Sample_Position, |
| 758 | Module^.Module_SamplePointer^[I3]^.Sample_Length + c_LoopExtensionSize); |
| 759 | { *** read out data } |
| 760 | EPT(TempP).p_Selector := Module^.Module_SamplePointer^[I3]^.Sample_Selector; |
| 761 | EPT(TempP).p_Offset := $0000; |
| 762 | SEEK(InFile,Sample^[I1].PSM_SamplePosition); |
| 763 | E_BLOCKREAD(InFile,TempP^,Module^.Module_SamplePointer^[I3]^.Sample_Length); |
| 764 | { *** 'coz the samples are signed in a DSM-file -> PC-fy them } |
| 765 | IF Module^.Module_SamplePointer^[I3]^.Sample_Length > 4 THEN |
| 766 | BEGIN |
| 767 | CopyLength := Module^.Module_SamplePointer^[I3]^.Sample_Length; |
| 768 | { *** decode sample } |
| 769 | ASM |
| 770 | DB 066h; MOV CX,WORD PTR CopyLength |
| 771 | { *** load sample selector } |
| 772 | MOV ES,WORD PTR TempP[00002h] |
| 773 | DB 066h; XOR SI,SI |
| 774 | DB 066h; XOR DI,DI |
| 775 | XOR AH,AH |
| 776 | { *** conert all bytes } |
| 777 | @@MainLoop: |
| 778 | DB 026h; DB 067h; LODSB |
| 779 | ADD AL,AH |
| 780 | MOV AH,AL |
| 781 | DB 067h; STOSB |
| 782 | DB 066h; LOOP @@MainLoop |
| 783 | END; |
| 784 | { *** make samples unsigned } |
| 785 | ASM |
| 786 | DB 066h; MOV CX,WORD PTR CopyLength |
| 787 | { *** load sample selector } |
| 788 | MOV ES,WORD PTR TempP[00002h] |
| 789 | DB 066h; XOR SI,SI |
| 790 | DB 066h; XOR DI,DI |
| 791 | { *** conert all bytes } |
| 792 | @@MainLoop: |
| 793 | DB 026h; DB 067h; LODSB |
| 794 | SUB AL,080h |
| 795 | DB 067h; STOSB |
| 796 | DB 066h; LOOP @@MainLoop |
| 797 | END; |
| 798 | { *** Create Loop-Extension } |
| 799 | IF Module^.Module_SamplePointer^[I3]^.Sample_Flags AND c_Sample_Flags_LoopActive = c_Sample_Flags_LoopActive THEN |
| 800 | BEGIN |
| 801 | CopySource := Module^.Module_SamplePointer^[I3]^.Sample_LoopBegin; |
| 802 | CopyDestination := Module^.Module_SamplePointer^[I3]^.Sample_LoopEnd; |
| 803 | CopyLength := CopyDestination - CopySource; |
| 804 | ASM |
| 805 | { *** load sample-selector } |
| 806 | MOV ES,WORD PTR TempP[00002h] |
| 807 | DB 066h; MOV DI,WORD PTR CopyDestination |
| 808 | { *** calculate number of full sample-loops to copy } |
| 809 | XOR DX,DX |
| 810 | MOV AX,c_LoopExtensionSize |
| 811 | MOV BX,WORD PTR CopyLength |
| 812 | DIV BX |
| 813 | OR AX,AX |
| 814 | JE @@NoFullLoop |
| 815 | { *** copy some full-loops (size=bx) } |
| 816 | MOV CX,AX |
| 817 | @@InnerLoop: |
| 818 | PUSH CX |
| 819 | DB 066h; MOV SI,WORD PTR CopySource |
| 820 | MOV CX,BX |
| 821 | DB 0F3h; DB 026h,067h,0A4h { REP MOVS BYTE PTR ES:[EDI],ES:[ESI] } |
| 822 | POP CX |
| 823 | LOOP @@InnerLoop |
| 824 | @@NoFullLoop: |
| 825 | { *** calculate number of rest-bytes to copy } |
| 826 | DB 066h; MOV SI,WORD PTR CopySource |
| 827 | MOV CX,DX |
| 828 | DB 0F3h; DB 026h,067h,0A4h { REP MOVS BYTE PTR ES:[EDI],ES:[ESI] } |
| 829 | END; |
| 830 | END |
| 831 | ELSE |
| 832 | BEGIN |
| 833 | CopyDestination := Module^.Module_SamplePointer^[I3]^.Sample_Length; |
| 834 | ASM |
| 835 | { *** load sample-selector } |
| 836 | MOV ES,WORD PTR TempP[00002h] |
| 837 | DB 066h; MOV DI,WORD PTR CopyDestination |
| 838 | { *** clear extension } |
| 839 | MOV CX,c_LoopExtensionSize |
| 840 | MOV AL,080h |
| 841 | DB 0F3h; DB 067h,0AAh { REP STOS BYTE PTR ES:[EDI] } |
| 842 | END; |
| 843 | END; |
| 844 | END; |
| 845 | { *** next sample } |
| 846 | END; |
| 847 | { *** init period-ranges } |
| 848 | NTMIK_MaximumPeriod := $0000D600 SHR 1; |
| 849 | NTMIK_MinimumPeriod := $0000D600 SHR 8; |
| 850 | { *** close file } |
| 851 | CLOSE(InFile); |
| 852 | { *** dispose all dynamic variables } |
| 853 | DISPOSE(Header); |
| 854 | DISPOSE(Sample); |
| 855 | DISPOSE(Order); |
| 856 | DISPOSE(ChannelSettings); |
| 857 | DISPOSE(MultiPurposeBuffer); |
| 858 | DISPOSE(PatternBuffer); |
| 859 | { *** set errorcode to noerror } |
| 860 | ErrorCode := c_NoError; |
| 861 | END; |
| 862 | |
| 863 | */ |