Skip to content
Permalink
Browse files

LILLIPUT: Add code to load & decompress MUS file

  • Loading branch information
Strangerke committed Apr 18, 2018
1 parent 9c593c0 commit 763cf2f6a498348ae566a0d5709deaab95645eb3
Showing with 74 additions and 14 deletions.
  1. +2 −1 engines/lilliput/lilliput.cpp
  2. +66 −12 engines/lilliput/sound.cpp
  3. +6 −1 engines/lilliput/sound.h
@@ -2727,8 +2727,9 @@ Common::Error LilliputEngine::run() {

// Setup mixer
syncSoundSettings();
//TODO: Init sound/music player
_soundHandler->init();

// Init palette
initPalette();

// Load files. In the original, the size was hardcoded
@@ -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
@@ -43,7 +43,12 @@ class LilliputSound {
private:
LilliputEngine *_vm;

byte *_musicBuff;
int _fileNumb;
byte **_unpackedFiles;
uint16 *_unpackedSizes;

uint32 decode(const byte *src, byte *dst, uint32 len);
byte readByte(const byte *data, uint32 offset);

void loadMusic(Common::String filename);
};

0 comments on commit 763cf2f

Please sign in to comment.
You can’t perform that action at this time.