X-Git-Url: http://4ch.mooo.com/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fvgmsnd%2FvgmSnd.c;h=c46b0b76a6e9d561a89ed502135fadb6ea3b5dd2;hb=e62d9fad29bbc049320ec69914aefd43c0f62f78;hp=d5e1cb2f2da100a5aef817e357f9959fc53a1597;hpb=01e6a54bfa71fd6b159146f30c405ca045e5fbdd;p=16.git diff --git a/src/lib/vgmsnd/vgmSnd.c b/src/lib/vgmsnd/vgmSnd.c index d5e1cb2f..c46b0b76 100755 --- a/src/lib/vgmsnd/vgmSnd.c +++ b/src/lib/vgmsnd/vgmSnd.c @@ -21,55 +21,55 @@ typedef struct _vgm_file_header_base { - sdword fccVGM; // 00 - sdword lngEOFOffset; // 04 - sdword lngVersion; // 08 - sdword lngSkip1[2]; // 0C - sdword lngGD3Offset; // 14 - sdword lngTotalSamples; // 18 - sdword lngLoopOffset; // 1C - sdword lngLoopSamples; // 20 - sdword lngRate; // 24 - sdword lngSkip2[3]; // 28 - sdword lngDataOffset; // 34 - sdword lngSkip3[2]; // 38 + dword fccVGM; // 00 + dword/*32*/ lngEOFOffset; // 04 + dword/*32*/ lngVersion; // 08 + dword/*32*/ lngSkip1[2]; // 0C + dword/*32*/ lngGD3Offset; // 14 + dword/*32*/ lngTotalSamples; // 18 + dword/*32*/ lngLoopOffset; // 1C + dword/*32*/ lngLoopSamples; // 20 + dword/*32*/ lngRate; // 24 + dword/*32*/ lngSkip2[3]; // 28 + dword/*32*/ lngDataOffset; // 34 + dword/*32*/ lngSkip3[2]; // 38 } VGM_BASE_HDR; #define PBMODE_MUSIC 0x00 #define PBMODE_SFX 0x01 typedef struct _vgm_playback { - byte pbMode; - byte vgmEnd; // 00 - running, 01 - finished, FF - not loaded - sword/**/ curLoopCnt; - sdword vgmPos; - sdword vgmSmplPos; - sdword pbSmplPos; + UINT8 pbMode; + UINT8 vgmEnd; // 00 - running, 01 - finished, FF - not loaded + UINT16 curLoopCnt; + dword/*32*/ vgmPos; + dword/*32*/ vgmSmplPos; + dword/*32*/ pbSmplPos; VGM_FILE* file; // oplChnMask: // Music: mask of channels used/overridden by SFX // SFX: ID of channel used by SFX (all commands are forces to it) - sword/**/ oplChnMask; - byte* oplRegCache; - byte workRAM[0x04]; + UINT16 oplChnMask; + UINT8* oplRegCache; + UINT8 workRAM[0x04]; } VGM_PBK; -INLINE sword/**/ ReadLE16(const byte* buffer) +INLINE UINT16 ReadLE16(const UINT8* buffer) { #ifdef QUICK_READ - return *(sword/**/*)buffer; + return *(UINT16*)buffer; #else return (buffer[0x00] << 0) | (buffer[0x01] << 8); #endif } -INLINE sdword ReadLE32(const byte* buffer) +INLINE dword/*32*/ ReadLE32(const UINT8* buffer) { #ifdef QUICK_READ - return *(sword*)buffer; + return *(dword/*32*/*)buffer; #else return (buffer[0x00] << 0) | (buffer[0x01] << 8) | (buffer[0x02] << 16) | (buffer[0x03] << 24); @@ -78,35 +78,35 @@ INLINE sdword ReadLE32(const byte* buffer) // Function Prototypes -//byte OpenVGMFile(const char* FileName, VGM_FILE* vgmFile); +//UINT8 OpenVGMFile(const char* FileName, VGM_FILE* vgmFile); //void FreeVGMFile(VGM_FILE* vgmFile); static boolean DoVgmLoop(VGM_PBK* vgmPlay); -static void UpdateVGM(VGM_PBK* vgmPlay, sword/**/ Samples); +static void UpdateVGM(VGM_PBK* vgmPlay, UINT16 Samples); //void InitEngine(void); //void DeinitEngine(void); -//byte PlayMusic(VGM_FILE* vgmFile); -//byte PlaySFX(VGM_FILE* vgmFile, byte sfxChnID); -//byte StopMusic(void); -//byte StopSFX(byte sfxChnID); // Note: sfxChnID == 0xFF -> stop all SFX -//byte PauseMusic(void); -//byte ResumeMusic(void); +//UINT8 PlayMusic(VGM_FILE* vgmFile); +//UINT8 PlaySFX(VGM_FILE* vgmFile, UINT8 sfxChnID); +//UINT8 StopMusic(void); +//UINT8 StopSFX(UINT8 sfxChnID); // Note: sfxChnID == 0xFF -> stop all SFX +//UINT8 PauseMusic(void); +//UINT8 ResumeMusic(void); static void StartPlayback(VGM_PBK* vgmPb); static void StopPlayback(VGM_PBK* vgmPb); -static void ym2413_write(VGM_PBK* vgmPb, byte reg, byte data); -static void ym3812_write(VGM_PBK* vgmPb, byte reg, byte data); -static void ym3512_write(VGM_PBK* vgmPb, byte reg, byte data); -static void ymf262_write(VGM_PBK* vgmPb, byte port, byte reg, byte data); +static void ym2413_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data); +static void ym3812_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data); +static void ym3512_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data); +static void ymf262_write(VGM_PBK* vgmPb, UINT8 port, UINT8 reg, UINT8 data); //void UpdateSoundEngine(void); // Functions that must be supplied by external library -extern void OPL2_Write(byte reg, byte data); -extern byte OPL2_ReadStatus(void); +extern void OPL2_Write(UINT8 reg, UINT8 data); +extern UINT8 OPL2_ReadStatus(void); @@ -119,32 +119,32 @@ extern byte OPL2_ReadStatus(void); static VGM_PBK vgmPbMusic; static VGM_PBK vgmPbSFX[SFX_CHN_COUNT]; -static byte oplRegs_Music[0x100]; -static byte oplRegs_SFX[SFX_CHN_COUNT][0x0D]; // 20 23 40 43 60 63 80 83 E0 E3 C0 A0 B0 +static UINT8 oplRegs_Music[0x100]; +static UINT8 oplRegs_SFX[SFX_CHN_COUNT][0x0D]; // 20 23 40 43 60 63 80 83 E0 E3 C0 A0 B0 -static const byte SFX_REGS[0x0D] = +static const UINT8 SFX_REGS[0x0D] = { 0x20, 0x23, 0x40, 0x43, 0x60, 0x63, 0x80, 0x83, 0xE0, 0xE3, 0xC0, 0xA0, 0xB0}; -static const byte SFX_REGS_REV[0x10] = // 20/30 -> 0, 40/50 -> 2, ... +static const UINT8 SFX_REGS_REV[0x10] = // 20/30 -> 0, 40/50 -> 2, ... { 0xFF, 0xFF, 0x00, 0x00, 0x02, 0x02, 0x04, 0x04, 0x06, 0x06, 0x0B, 0x0C, 0x0A, 0xFF, 0x08, 0x08}; -static const byte CHN_OPMASK[0x09] = +static const UINT8 CHN_OPMASK[0x09] = { 0x00, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x10, 0x11, 0x12}; -static const byte CHN_OPMASK_REV[0x20] = +static const UINT8 CHN_OPMASK_REV[0x20] = { 0x00, 0x01, 0x02, 0x80, 0x81, 0x82, 0xFF, 0xFF, 0x03, 0x04, 0x05, 0x83, 0x84, 0x85, 0xFF, 0xFF, 0x06, 0x07, 0x08, 0x86, 0x87, 0x88, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; -byte OpenVGMFile(const char* FileName, VGM_FILE* vgmFile) +UINT8 OpenVGMFile(const char* FileName, VGM_FILE* vgmFile, global_game_variables_t *gvar) { size_t hdrSize; size_t readEl; // 'elements' read from file size_t bytesToRead; VGM_BASE_HDR vgmBaseHdr; FILE* hFile; - sdword CurPos; + dword/*32*/ CurPos; memset(vgmFile, 0x00, sizeof(VGM_FILE)); @@ -171,7 +171,12 @@ byte OpenVGMFile(const char* FileName, VGM_FILE* vgmFile) } vgmFile->dataLen = vgmBaseHdr.lngEOFOffset + 0x04; - vgmFile->data = (byte*)malloc(vgmFile->dataLen); +#ifndef VGM_USESCAMMPM + vgmFile->data = (UINT8*)malloc(vgmFile->dataLen); +#else + MM_GetPtr(MEMPTRCONV gvar->ca.audiosegs[0], vgmFile->dataLen, gvar); + vgmFile->data = (UINT8*)gvar->ca.audiosegs[0]; +#endif if (vgmFile->data == NULL) { fclose(hFile); @@ -206,15 +211,20 @@ byte OpenVGMFile(const char* FileName, VGM_FILE* vgmFile) CurPos = 0x40; hdrSize = sizeof(VGM_HEADER); if (hdrSize > CurPos) - memset((byte*)&vgmFile->header + CurPos, 0x00, hdrSize - CurPos); + memset((UINT8*)&vgmFile->header + CurPos, 0x00, hdrSize - CurPos); fclose(hFile); return 0x00; } -void FreeVGMFile(VGM_FILE* vgmFile) +void FreeVGMFile(VGM_FILE* vgmFile, global_game_variables_t *gvar) { - free(vgmFile->data); vgmFile->data = NULL; +#ifndef VGM_USESCAMMPM + if(vgmFile->data){ free(vgmFile->data); vgmFile->data = NULL; } +#else + MM_FreePtr(MEMPTRCONV gvar->ca.audiosegs[0], gvar); +#endif +// if(vgmFile->data) free(vgmFile->data); vgmFile->dataLen = 0; return; @@ -237,16 +247,16 @@ static boolean DoVgmLoop(VGM_PBK* vgmPlay) return true; } -static void UpdateVGM(VGM_PBK* vgmPlay, sword/**/ Samples) +static void UpdateVGM(VGM_PBK* vgmPlay, UINT16 Samples) { - const sdword vgmLen = vgmPlay->file->dataLen; - const byte* vgmData = vgmPlay->file->data; - const byte* VGMPnt; - sdword VGMPos; - sdword VGMSmplPos; - byte Command; - byte blockType; - sdword blockLen; + const dword/*32*/ vgmLen = vgmPlay->file->dataLen; + const UINT8* vgmData = vgmPlay->file->data; + const UINT8* VGMPnt; + dword/*32*/ VGMPos; + dword/*32*/ VGMSmplPos; + UINT8 Command; + UINT8 blockType; + dword/*32*/ blockLen; vgmPlay->pbSmplPos += Samples; VGMPos = vgmPlay->vgmPos; @@ -390,8 +400,8 @@ static void UpdateVGM(VGM_PBK* vgmPlay, sword/**/ Samples) void InitEngine(void) { - byte curSFX; - byte curReg; + UINT8 curSFX; + UINT8 curReg; memset(oplRegs_Music, 0x00, 0x100); memset(&vgmPbMusic, 0x00, sizeof(VGM_PBK)); @@ -429,7 +439,7 @@ void InitEngine(void) void DeinitEngine(void) { - byte curSFX; + UINT8 curSFX; StopPlayback(&vgmPbMusic); for (curSFX = 0; curSFX < SFX_CHN_COUNT; curSFX ++) @@ -441,7 +451,7 @@ void DeinitEngine(void) } -byte PlayMusic(VGM_FILE* vgmFile) +UINT8 PlayMusic(VGM_FILE* vgmFile) { VGM_PBK* vgmPb = &vgmPbMusic; @@ -455,7 +465,7 @@ byte PlayMusic(VGM_FILE* vgmFile) return 0x00; } -byte PlaySFX(VGM_FILE* vgmFile, byte sfxChnID) +UINT8 PlaySFX(VGM_FILE* vgmFile, UINT8 sfxChnID) { VGM_PBK* vgmPb; @@ -474,13 +484,13 @@ byte PlaySFX(VGM_FILE* vgmFile, byte sfxChnID) return 0x00; } -byte StopMusic(void) +UINT8 StopMusic(void) { StopPlayback(&vgmPbMusic); return 0x00; } -byte StopSFX(byte sfxChnID) +UINT8 StopSFX(UINT8 sfxChnID) { if (sfxChnID == 0xFF) { @@ -496,7 +506,7 @@ byte StopSFX(byte sfxChnID) return 0x00; } -byte PauseMusic(void) +UINT8 PauseMusic(void) { if (vgmPbMusic.vgmEnd == 0xFF) return 0xFF; // not playing @@ -511,7 +521,7 @@ byte PauseMusic(void) return 0x00; } -byte ResumeMusic(void) +UINT8 ResumeMusic(void) { if (vgmPbMusic.vgmEnd == 0xFF) return 0xFF; // not playing @@ -544,7 +554,7 @@ static void StartPlayback(VGM_PBK* vgmPb) if (vgmPb->pbMode == PBMODE_SFX) { - byte curReg; + UINT8 curReg; curReg = 0xB0 | vgmPb->oplChnMask; if (oplRegs_Music[curReg] & 0x20) @@ -563,8 +573,8 @@ static void StopPlayback(VGM_PBK* vgmPb) if (vgmPb->pbMode == PBMODE_MUSIC) { - byte curReg; - sword/**/ chnMask; + UINT8 curReg; + UINT16 chnMask; chnMask = 0x0001; for (curReg = 0xB0; curReg < 0xB9; curReg ++, chnMask <<= 1) @@ -588,9 +598,9 @@ static void StopPlayback(VGM_PBK* vgmPb) } else //if (vgmPb->pbMode == PBMODE_SFX) { - byte regID; - byte curReg; - byte opMask; + UINT8 regID; + UINT8 curReg; + UINT8 opMask; curReg = 0xB0 | vgmPb->oplChnMask; if (vgmPb->oplRegCache[0x0C] & 0x20) @@ -624,10 +634,10 @@ static void StopPlayback(VGM_PBK* vgmPb) -static void OPL_CachedWrite(VGM_PBK* vgmPb, byte reg, byte data) +static void OPL_CachedWrite(VGM_PBK* vgmPb, UINT8 reg, UINT8 data) { - byte regChn; - byte ramOfs; + UINT8 regChn; + UINT8 ramOfs; if (vgmPb->pbMode == PBMODE_MUSIC) { @@ -689,12 +699,12 @@ static void OPL_CachedWrite(VGM_PBK* vgmPb, byte reg, byte data) } -static void ym2413_write(VGM_PBK* vgmPb, byte reg, byte data) +static void ym2413_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data) { return; // unsupported for now } -static void ym3812_write(VGM_PBK* vgmPb, byte reg, byte data) +static void ym3812_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data) { if (reg == 0x01) { @@ -710,7 +720,7 @@ static void ym3812_write(VGM_PBK* vgmPb, byte reg, byte data) return; } -static void ym3512_write(VGM_PBK* vgmPb, byte reg, byte data) +static void ym3512_write(VGM_PBK* vgmPb, UINT8 reg, UINT8 data) { if ((reg & 0xE0) == 0xE0) { @@ -728,7 +738,7 @@ static void ym3512_write(VGM_PBK* vgmPb, byte reg, byte data) return; } -static void ymf262_write(VGM_PBK* vgmPb, byte port, byte reg, byte data) +static void ymf262_write(VGM_PBK* vgmPb, UINT8 port, UINT8 reg, UINT8 data) { return; // unsupported for now } @@ -737,8 +747,8 @@ static void ymf262_write(VGM_PBK* vgmPb, byte port, byte reg, byte data) void UpdateSoundEngine(void) { - byte tmrMask; - byte curSFX; + UINT8 tmrMask; + UINT8 curSFX; tmrMask = OPL2_ReadStatus(); if (! (tmrMask & 0x40))