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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "common/endian.h"
#include "common/list.h"
#include "common/util.h"
#include "made/sound.h"
namespace Made {
void decompressSound(byte *source, byte *dest, uint16 chunkSize, uint16 chunkCount, SoundEnergyArray *soundEnergyArray) {
int16 prevSample = 0, workSample = 0;
byte soundBuffer[1025];
byte deltaSoundBuffer[1024];
int16 soundBuffer2[16];
byte deltaType, type;
uint16 workChunkSize, byteCount, bitCount;
byte bitMask, bitShift;
uint16 ofs = 0;
uint16 i = 0, l = 0;
byte val;
SoundEnergyItem soundEnergyItem;
const int modeValues[3][4] = {
{ 2, 8, 0x01, 1},
{ 4, 4, 0x03, 2},
{16, 2, 0x0F, 4}
};
soundEnergyItem.position = 0;
if (soundEnergyArray)
soundEnergyArray->clear();
while (chunkCount--) {
deltaType = (*source) >> 6;
workChunkSize = chunkSize;
if (deltaType == 1)
workChunkSize /= 2;
else if (deltaType == 2)
workChunkSize /= 4;
type = (*source++) & 0x0F;
workSample = prevSample;
switch (type) {
case 0:
memset(soundBuffer, 0x80, workChunkSize);
workSample = 0;
soundEnergyItem.energy = 0;
// FIXME: I believe that this should be added in a different manner than the
// rest of the values. 0 means "mouth shut", but it seems to be occuring too
// often. Removing this bit makes the mouth of the lighthouse keeper move
// without going too much off-sync. If I'm wrong here, please remove this.
if (soundEnergyArray)
soundEnergyArray->push_back(soundEnergyItem);
break;
case 1:
break;
case 2:
case 3:
case 4:
byteCount = modeValues[type - 2][0];
bitCount = modeValues[type - 2][1];
bitMask = modeValues[type - 2][2];
bitShift = modeValues[type - 2][3];
ofs = 0;
for (i = 0; i < byteCount; i++)
soundBuffer2[i] = (*source++) * 2 - 128;
while (ofs < workChunkSize) {
val = *source++;
for (i = 0; i < bitCount; i++) {
workSample = CLIP<int16>(workSample + soundBuffer2[val & bitMask], -127, 127);
val >>= bitShift;
soundBuffer[ofs++] = workSample + 128;
}
}
soundEnergyItem.energy = type - 1;
if (soundEnergyArray)
soundEnergyArray->push_back(soundEnergyItem);
break;
case 5:
for (i = 0; i < workChunkSize; i++)
soundBuffer[i] = *source++;
workSample = soundBuffer[workChunkSize - 1] - 128;
soundEnergyItem.energy = type - 1;
if (soundEnergyArray)
soundEnergyArray->push_back(soundEnergyItem);
break;
default:
return;
}
if (deltaType > 0) {
if (deltaType == 1) {
for (i = 0; i < chunkSize - 1; i += 2) {
l = i / 2;
deltaSoundBuffer[i] = soundBuffer[l];
deltaSoundBuffer[i + 1] = (soundBuffer[l] + soundBuffer[l + 1]) / 2;
}
} else if (deltaType == 2) {
for (i = 0; i < chunkSize - 1; i += 4) {
l = i / 4;
deltaSoundBuffer[i] = soundBuffer[l];
deltaSoundBuffer[i + 2] = (soundBuffer[l] + soundBuffer[l + 1]) / 2;
deltaSoundBuffer[i + 1] = (deltaSoundBuffer[i + 2] + soundBuffer[l]) / 2;
deltaSoundBuffer[i + 3] = (deltaSoundBuffer[i + 2] + soundBuffer[l + 1]) / 2;
}
}
for (i = 0; i < chunkSize; i++)
soundBuffer[i] = deltaSoundBuffer[i];
}
prevSample = workSample;
memcpy(dest, soundBuffer, chunkSize);
dest += chunkSize;
soundEnergyItem.position += chunkSize;
}
}
} // End of namespace Made
|