aboutsummaryrefslogtreecommitdiff
path: root/engines/lilliput/sound.cpp
blob: 55624e74b108e3f0dd53174bf7fa096c96df2b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "lilliput/lilliput.h"
#include "lilliput/sound.h"

#include "common/debug.h"

namespace Lilliput {

LilliputSound::LilliputSound(LilliputEngine *vm) : _vm(vm) {
	_unpackedFiles = nullptr;
	_unpackedSizes = nullptr;
}

LilliputSound::~LilliputSound() {
	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 start) {
	uint32 i = start;
	for (; i < len; ++i) {
		*dst++ = readByte(src, i);
	}
	return i;
}

void LilliputSound::loadMusic(Common::String filename) {
	debugC(1, kDebugSound, "loadMusic(%s)", filename.c_str());

	Common::File f;

	if (!f.open(filename))
		error("Missing music file %s", filename.c_str());

	_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, dstBuf, _unpackedSizes[i], shift);
			_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
void LilliputSound::init() {
	debugC(1, kDebugSound, "LilliputSound::init()");

	loadMusic("ROBIN.MUS");
}

void LilliputSound::refresh() {
	debugC(1, kDebugSound, "LilliputSound::refresh()");
}

void LilliputSound::play(int var1, Common::Point var2, Common::Point var3, Common::Point var4) {
	debugC(1, kDebugSound, "LilliputSound::play(%d, %d - %d, %d - %d, %d - %d)", var1, var2.x, var2.y, var3.x, var3.y, var4.x, var4.y);
}

void LilliputSound::stop(Common::Point pos) {
	debugC(1, kDebugSound, "LilliputSound::stop(%d - %d)", pos.x, pos.y);
}

void LilliputSound::toggleOnOff() {
	debugC(1, kDebugSound, "LilliputSound::toggleOnOff()");
}

void LilliputSound::update() {
	debugC(1, kDebugSound, "LilliputSound::update()");
}

void LilliputSound::remove() {
	debugC(1, kDebugSound, "Lilliput::remove()");
}

} // End of namespace