diff options
author | Strangerke | 2018-04-18 07:17:10 +0200 |
---|---|---|
committer | Strangerke | 2018-04-18 07:18:05 +0200 |
commit | 763cf2f6a498348ae566a0d5709deaab95645eb3 (patch) | |
tree | 29fbd9bbe85688c7b805e8f541dcc41f9e55b773 /engines/lilliput/sound.cpp | |
parent | 9c593c0bff7c50c41dc4e54d0c02b546e624abec (diff) | |
download | scummvm-rg350-763cf2f6a498348ae566a0d5709deaab95645eb3.tar.gz scummvm-rg350-763cf2f6a498348ae566a0d5709deaab95645eb3.tar.bz2 scummvm-rg350-763cf2f6a498348ae566a0d5709deaab95645eb3.zip |
LILLIPUT: Add code to load & decompress MUS file
Diffstat (limited to 'engines/lilliput/sound.cpp')
-rw-r--r-- | engines/lilliput/sound.cpp | 78 |
1 files changed, 66 insertions, 12 deletions
diff --git a/engines/lilliput/sound.cpp b/engines/lilliput/sound.cpp index 78de105784..6ea5ab985a 100644 --- a/engines/lilliput/sound.cpp +++ b/engines/lilliput/sound.cpp @@ -28,10 +28,30 @@ namespace Lilliput { LilliputSound::LilliputSound(LilliputEngine *vm) : _vm(vm) { + _unpackedFiles = nullptr; + _unpackedSizes = nullptr; } LilliputSound::~LilliputSound() { - free(_musicBuff); + if (_unpackedFiles) { + for (int i = 0; i < _fileNumb; i++) + free(_unpackedFiles[i]); + } + free(_unpackedFiles); + free(_unpackedSizes); +} + +byte LilliputSound::readByte(const byte *data, uint32 offset) { + uint16 al = data[0x201 + (offset >> 1)]; + return data[1 + (offset & 1) + (al << 1)]; +} + +uint32 LilliputSound::decode(const byte *src, byte *dst, uint32 len) { + uint32 i = 0; + for (; i < len; ++i) { + *dst++ = readByte(src, i); + } + return i; } void LilliputSound::loadMusic(Common::String filename) { @@ -42,17 +62,51 @@ void LilliputSound::loadMusic(Common::String filename) { if (!f.open(filename)) error("Missing music file %s", filename.c_str()); - byte *res = (byte *)malloc(sizeof(byte) * 50000); - for (int i = 0; i < 50000; ++i) - res[i] = f.readByte(); - -// f.close(); - f.seek(0); - int filenumb = f.readUint16LE(); - - - - free(res); + _fileNumb = f.readUint16LE(); + + int *fileSizes = new int[_fileNumb + 1]; + for (int i = 0; i < _fileNumb; ++i) + fileSizes[i] = f.readUint16LE(); + f.seek(0, SEEK_END); + fileSizes[_fileNumb] = f.pos(); + + _unpackedFiles = new byte *[_fileNumb]; + _unpackedSizes = new uint16[_fileNumb]; + int pos = (_fileNumb + 1) * 2; // file number + file sizes + for (int i = 0; i < _fileNumb; ++i) { + int packedSize = fileSizes[i + 1] - fileSizes[i]; + byte *srcBuf = new byte[packedSize]; + f.seek(pos, SEEK_SET); + f.read(srcBuf, packedSize); + if (srcBuf[0] == 'c' || srcBuf[0] == 'C') { + int shift = (srcBuf[0] == 'c') ? 1 : 0; + _unpackedSizes[i] = (1 + packedSize - 0x201) * 2 - shift; + byte *dstBuf = new byte[_unpackedSizes[i]]; + decode(srcBuf + shift, dstBuf, _unpackedSizes[i]); + _unpackedFiles[i] = dstBuf; + } else { + _unpackedSizes[i] = packedSize; + byte *dstBuf = new byte[packedSize]; + for (int j = 0; j < packedSize; ++j) + dstBuf[j] = srcBuf[j]; + _unpackedFiles[i] = dstBuf; + } + delete srcBuf; + pos += packedSize; + } + + delete fileSizes; + f.close(); + + // Debug code + for (int i = 0; i < _fileNumb; ++i) { + Common::DumpFile dmp; + Common::String name = Common::String::format("dmp%d.mid", i); + dmp.open(name); + dmp.write(_unpackedFiles[i], _unpackedSizes[i]); + dmp.close(); + } + // } // Used during initialization |