INT PAL_MKFGetDecompressedSize( UINT uiChunkNum, FILE *fp ) /*++ Purpose: Get the decompressed size of a compressed chunk in an MKF archive. Parameters: [IN] uiChunkNum - the number of the chunk in the MKF archive. [IN] fp - pointer to the fopen'ed MKF file. Return value: Integer value which indicates the size of the chunk. -1 if the chunk does not exist. --*/ { DWORD buf[2]; UINT uiOffset; UINT uiChunkCount; if (fp == NULL) { return -1; } // // Get the total number of chunks. // uiChunkCount = PAL_MKFGetChunkCount(fp); if (uiChunkNum >= uiChunkCount) { return -1; } // // Get the offset of the chunk. // fseek(fp, 4 * uiChunkNum, SEEK_SET); fread(&uiOffset, 4, 1, fp); uiOffset = SWAP32(uiOffset); // // Read the header. // fseek(fp, uiOffset, SEEK_SET); fread(buf, sizeof(DWORD), 2, fp); buf[0] = SWAP32(buf[0]); buf[1] = SWAP32(buf[1]); return (buf[0] != 0x315f4a59) ? -1 : (INT)buf[1]; }
INT PAL_MKFGetChunkSize( UINT uiChunkNum, FILE *fp ) /*++ Purpose: Get the size of a chunk in an MKF archive. Parameters: [IN] uiChunkNum - the number of the chunk in the MKF archive. [IN] fp - pointer to the fopen'ed MKF file. Return value: Integer value which indicates the size of the chunk. -1 if the chunk does not exist. --*/ { UINT uiOffset = 0; UINT uiNextOffset = 0; UINT uiChunkCount = 0; // // Get the total number of chunks. // uiChunkCount = PAL_MKFGetChunkCount(fp); if (uiChunkNum >= uiChunkCount) { return -1; } // // Get the offset of the specified chunk and the next chunk. // fseek(fp, 4 * uiChunkNum, SEEK_SET); fread(&uiOffset, sizeof(UINT), 1, fp); fread(&uiNextOffset, sizeof(UINT), 1, fp); uiOffset = SWAP32(uiOffset); uiNextOffset = SWAP32(uiNextOffset); // // Return the length of the chunk. // return uiNextOffset - uiOffset; }
INT PAL_MKFReadChunk( LPBYTE lpBuffer, UINT uiBufferSize, UINT uiChunkNum, FILE *fp ) /*++ Purpose: Read a chunk from an MKF archive into lpBuffer. Parameters: [OUT] lpBuffer - pointer to the destination buffer. [IN] uiBufferSize - size of the destination buffer. [IN] uiChunkNum - the number of the chunk in the MKF archive to read. [IN] fp - pointer to the fopen'ed MKF file. Return value: Integer value which indicates the size of the chunk. -1 if there are error in parameters. -2 if buffer size is not enough. --*/ { UINT uiOffset = 0; UINT uiNextOffset = 0; UINT uiChunkCount; UINT uiChunkLen; if (lpBuffer == NULL || fp == NULL || uiBufferSize == 0) { return -1; } // // Get the total number of chunks. // uiChunkCount = PAL_MKFGetChunkCount(fp); if (uiChunkNum >= uiChunkCount) { return -1; } // // Get the offset of the chunk. // fseek(fp, 4 * uiChunkNum, SEEK_SET); fread(&uiOffset, 4, 1, fp); fread(&uiNextOffset, 4, 1, fp); uiOffset = SWAP32(uiOffset); uiNextOffset = SWAP32(uiNextOffset); // // Get the length of the chunk. // uiChunkLen = uiNextOffset - uiOffset; if (uiChunkLen > uiBufferSize) { return -2; } if (uiChunkLen != 0) { fseek(fp, uiOffset, SEEK_SET); fread(lpBuffer, uiChunkLen, 1, fp); } else { return -1; } return (INT)uiChunkLen; }
VOID MIDI_Play( INT iNumRIX, BOOL fLoop ) /*++ Purpose: Start playing the specified music in MIDI format. Parameters: [IN] iNumRIX - number of the music. 0 to stop playing current music. [IN] fLoop - Whether the music should be looped or not. Return value: None. --*/ { FILE *fp; unsigned char *buf; int size; SDL_RWops *rw; #ifdef PAL_WIN95 char filename[1024]; #endif if (g_pMid != NULL && iNumRIX == iMidCurrent && native_midi_active()) { return; } SOUND_PlayCDA(-1); native_midi_freesong(g_pMid); g_pMid = NULL; iMidCurrent = -1; if (g_fNoMusic || iNumRIX <= 0) { return; } #ifdef PAL_WIN95 sprintf(filename, "%s/musics/%.3d.mid", PAL_PREFIX, iNumRIX); g_pMid = native_midi_loadsong(filename); if (g_pMid != NULL) { native_midi_start(g_pMid); iMidCurrent = iNumRIX; fMidLoop = fLoop; } #else fp = UTIL_OpenFile("midi.mkf"); if (fp == NULL) { return; } if (iNumRIX > PAL_MKFGetChunkCount(fp)) { fclose(fp); return; } size = PAL_MKFGetChunkSize(iNumRIX, fp); if (size <= 0) { fclose(fp); return; } buf = (unsigned char *)UTIL_malloc(size); PAL_MKFReadChunk((LPBYTE)buf, size, iNumRIX, fp); fclose(fp); rw = SDL_RWFromConstMem((const void *)buf, size); g_pMid = native_midi_loadsong_RW(rw); if (g_pMid != NULL) { native_midi_start(g_pMid); iMidCurrent = iNumRIX; fMidLoop = fLoop; } SDL_RWclose(rw); free(buf); #endif }
VOID MIDI_Play( INT iNumRIX, BOOL fLoop ) /*++ Purpose: Start playing the specified music in MIDI format. Parameters: [IN] iNumRIX - number of the music. 0 to stop playing current music. [IN] fLoop - Whether the music should be looped or not. Return value: None. --*/ { FILE *fp; unsigned char *buf; int size; SDL_RWops *rw; #ifdef TIMIDITY if (g_pMid != NULL && iNumRIX == iMidCurrent && Timidity_Active()) #else if (g_pMid != NULL && iNumRIX == iMidCurrent && native_midi_active()) #endif { return; } SOUND_PlayCDA(-1); #ifdef TIMIDITY Timidity_FreeSong(g_pMid); #else native_midi_freesong(g_pMid); #endif g_pMid = NULL; iMidCurrent = -1; if (g_fNoMusic || iNumRIX <= 0) { return; } fp = fopen(PAL_PREFIX "midi.mkf", "rb"); if (fp == NULL) { return; } if (iNumRIX > PAL_MKFGetChunkCount(fp)) { fclose(fp); return; } size = PAL_MKFGetChunkSize(iNumRIX, fp); if (size <= 0) { fclose(fp); return; } buf = (unsigned char *)UTIL_malloc(size); PAL_MKFReadChunk((LPBYTE)buf, size, iNumRIX, fp); fclose(fp); rw = SDL_RWFromConstMem((const void *)buf, size); #ifdef TIMIDITY g_pMid = Timidity_LoadSong_RW(rw); #else g_pMid = native_midi_loadsong_RW(rw); #endif if (g_pMid != NULL) { #ifdef TIMIDITY Timidity_Start(g_pMid); #else native_midi_start(g_pMid); #endif iMidCurrent = iNumRIX; fMidLoop = fLoop; } SDL_RWclose(rw); free(buf); }
static INT PAL_RNGReadFrame( LPBYTE lpBuffer, UINT uiBufferSize, UINT uiRngNum, UINT uiFrameNum, FILE *fpRngMKF ) /*++ Purpose: Read a frame from a RNG animation. Parameters: [OUT] lpBuffer - pointer to the destination buffer. [IN] uiBufferSize - size of the destination buffer. [IN] uiRngNum - the number of the RNG animation in the MKF archive. [IN] uiFrameNum - frame number in the RNG animation. [IN] fpRngMKF - pointer to the fopen'ed MKF file. Return value: Integer value which indicates the size of the chunk. -1 if there are error in parameters. -2 if buffer size is not enough. --*/ { UINT uiOffset = 0; UINT uiSubOffset = 0; UINT uiNextOffset = 0; UINT uiChunkCount = 0; INT iChunkLen = 0; if (lpBuffer == NULL || fpRngMKF == NULL || uiBufferSize == 0) { return -1; } // // Get the total number of chunks. // uiChunkCount = PAL_MKFGetChunkCount(fpRngMKF); if (uiRngNum >= uiChunkCount) { return -1; } // // Get the offset of the chunk. // fseek(fpRngMKF, 4 * uiRngNum, SEEK_SET); fread(&uiOffset, sizeof(UINT), 1, fpRngMKF); fread(&uiNextOffset, sizeof(UINT), 1, fpRngMKF); uiOffset = SWAP32(uiOffset); uiNextOffset = SWAP32(uiNextOffset); // // Get the length of the chunk. // iChunkLen = uiNextOffset - uiOffset; if (iChunkLen != 0) { fseek(fpRngMKF, uiOffset, SEEK_SET); } else { return -1; } // // Get the number of sub chunks. // fread(&uiChunkCount, sizeof(UINT), 1, fpRngMKF); uiChunkCount = (SWAP32(uiChunkCount) - 4) / 4; if (uiFrameNum >= uiChunkCount) { return -1; } // // Get the offset of the sub chunk. // fseek(fpRngMKF, uiOffset + 4 * uiFrameNum, SEEK_SET); fread(&uiSubOffset, sizeof(UINT), 1, fpRngMKF); fread(&uiNextOffset, sizeof(UINT), 1, fpRngMKF); uiSubOffset = SWAP32(uiSubOffset); uiNextOffset = SWAP32(uiNextOffset); // // Get the length of the sub chunk. // iChunkLen = uiNextOffset - uiSubOffset; if ((UINT)iChunkLen > uiBufferSize) { return -2; } if (iChunkLen != 0) { fseek(fpRngMKF, uiOffset + uiSubOffset, SEEK_SET); fread(lpBuffer, iChunkLen, 1, fpRngMKF); } else { return -1; } return iChunkLen; }