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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001/2002 The ScummVM project
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*
*/
#include <stdafx.h>
#include "channel.h"
#include "chunk.h"
#include "chunk_type.h"
#include <assert.h>
#include <string.h> // for memcpy.h
#ifndef min
#define min(x, y) ((x) > (y) ? (y) : (x))
#endif
ImuseChannel::ImuseChannel(int track, int freq) :
_track(track),
_tbuffer(0),
_tbufferSize(0),
_sbuffer(0),
_sbufferSize(0),
_frequency(freq),
_dataSize(-1),
_inData(false) {
}
ImuseChannel::~ImuseChannel() {
if(_tbuffer) {
delete []_tbuffer;
}
if(_sbuffer) {
warning("_sbuffer should be 0 !!!");
delete []_sbuffer;
}
}
bool ImuseChannel::isTerminated() const {
return (_dataSize <= 0 && _sbuffer == 0);
}
bool ImuseChannel::setParameters(int nbframes, int size, int unk1, int unk2) {
return true;
}
bool ImuseChannel::checkParameters(int index, int nbframes, int size, int unk1, int unk2) {
return true;
}
bool ImuseChannel::appendData(Chunk & b, int size) {
if(_dataSize == -1) { // First call
assert(size > 8);
Chunk::type imus_type = b.getDword(); imus_type = SWAP_BYTES(imus_type);
unsigned int imus_size = b.getDword(); imus_size = SWAP_BYTES(imus_size);
if(imus_type != TYPE_iMUS) error("Invalid Chunk for imuse_channel");
size -= 8;
_tbufferSize = size;
assert(_tbufferSize);
_tbuffer = new unsigned char[_tbufferSize];
if(!_tbuffer) error("imuse_channel failed to allocate memory");
b.read(_tbuffer, size);
_dataSize = -2; // even if _in_data does not get set, this won't be called again
} else {
if(_tbuffer) { // remaining from last call
unsigned char * old = _tbuffer;
int new_size = size + _tbufferSize;
_tbuffer = new unsigned char[new_size];
if(!_tbuffer) error("imuse_channel failed to allocate memory");
memcpy(_tbuffer, old, _tbufferSize);
delete []old;
b.read(_tbuffer + _tbufferSize, size);
_tbufferSize += size;
} else {
_tbufferSize = size;
_tbuffer = new unsigned char[_tbufferSize];
if(!_tbuffer) error("imuse_channel failed to allocate memory");
b.read(_tbuffer, size);
}
}
return processBuffer();
}
bool ImuseChannel::handleFormat(Chunk & src) {
if(src.getSize() != 20) error("invalid size for FRMT Chunk");
unsigned imuse_start = src.getDword();
imuse_start = SWAP_BYTES(imuse_start);
src.seek(4);
_bitsize = src.getDword();
_bitsize = SWAP_BYTES(_bitsize);
_rate = src.getDword();
_rate = SWAP_BYTES(_rate);
_channels = src.getDword();
_channels = SWAP_BYTES(_channels);
assert(_channels == 1 || _channels == 2);
return true;
}
bool ImuseChannel::handleText(Chunk & src) {
return true;
}
bool ImuseChannel::handleRegion(Chunk & src) {
if(src.getSize() != 8) error("invalid size for REGN Chunk");
return true;
}
bool ImuseChannel::handleStop(Chunk & src) {
if(src.getSize() != 4) error("invalid size for STOP Chunk");
return true;
}
bool ImuseChannel::handleMap(Chunk & map) {
while(!map.eof()) {
Chunk * sub = map.subBlock();
switch(sub->getType()) {
case TYPE_FRMT:
handleFormat(*sub);
break;
case TYPE_TEXT:
handleText(*sub);
break;
case TYPE_REGN:
handleRegion(*sub);
break;
case TYPE_STOP:
handleStop(*sub);
break;
default:
error("Unknown iMUS subChunk found : %s, %d", Chunk::ChunkString(sub->getType()), sub->getSize());
}
delete sub;
}
return true;
}
void ImuseChannel::decode() {
int remaining_size = _sbufferSize % 3;
if(remaining_size) {
_srbufferSize -= remaining_size;
assert(_inData);
if(_tbuffer == 0) {
_tbuffer = new unsigned char[remaining_size];
memcpy(_tbuffer, _sbuffer + _sbufferSize - remaining_size, remaining_size);
_tbufferSize = remaining_size;
_sbufferSize -= remaining_size;
} else {
warning("impossible ! : %p, %d, %d, %p(%d), %p(%d, %d)",
this, _dataSize, _inData, _tbuffer, _tbufferSize, _sbuffer, _sbufferSize, _srbufferSize);
unsigned char * old = _tbuffer;
int new_size = remaining_size + _tbufferSize;
_tbuffer = new unsigned char[new_size];
if(!_tbuffer) error("imuse_channel failed to allocate memory");
memcpy(_tbuffer, old, _tbufferSize);
delete []old;
memcpy(_tbuffer + _tbufferSize, _sbuffer + _sbufferSize - remaining_size, remaining_size);
_tbufferSize += remaining_size;
}
}
int loop_size = _sbufferSize / 3;
int new_size = loop_size * 2;
short * keep, * decoded;
keep = decoded = new short[new_size];
assert(keep);
unsigned char * source = _sbuffer;
while(loop_size--) {
int v1 = *source++;
int v2 = *source++;
int v3 = *source++;
int value = (((v2 & 0x0f) << 12) | (v1 << 4)) - 0x8000;
*decoded++ = (short)value;
value = (((v2 & 0xf0) << 8) | (v3 << 4)) - 0x8000;
*decoded++ = (short)value;
}
delete []_sbuffer;
_sbuffer = (unsigned char*)keep;
_sbufferSize = new_size * sizeof(short);
}
bool ImuseChannel::handleSubTags(int & offset) {
if(_tbufferSize - offset >= 8) {
Chunk::type type = READ_BE_UINT32(_tbuffer + offset);
unsigned int size = READ_BE_UINT32(_tbuffer + offset + 4);
unsigned int available_size = _tbufferSize - offset;
switch(type) {
case TYPE_MAP_:
_inData = false;
if(available_size >= (size + 8)) {
ContChunk c((char*)_tbuffer + offset);
handleMap(c);
}
break;
case TYPE_DATA: // Sound data !!!
_inData = true;
_dataSize = size;
offset += 8;
{
int reqsize = 1;
if(_channels == 2) reqsize *= 2;
if(_bitsize == 16) reqsize *= 2;
else if(_bitsize == 12) {
if(reqsize > 1)
reqsize = reqsize * 3 / 2;
else reqsize = 3;
}
if((size % reqsize) != 0) {
warning("Invalid iMUS sound data size : (%d %% %d) != 0, correcting...", size, reqsize);
size += 3 - (size % reqsize);
}
}
return false;
default:
error("unknown Chunk in iMUS track : %s ", Chunk::ChunkString(type));
}
offset += size + 8;
return true;
}
return false;
}
bool ImuseChannel::processBuffer() {
// see comments in saud_channel::processBuffer for an explanation
assert(_tbuffer != 0);
assert(_tbufferSize != 0);
assert(_sbuffer == 0);
assert(_sbufferSize == 0);
if(_inData) {
if(_dataSize < _tbufferSize) {
int offset= _dataSize;
while(handleSubTags(offset));
_sbufferSize = _dataSize;
_sbuffer = _tbuffer;
if(offset < _tbufferSize) { // there is still some unprocessed data
int new_size = _tbufferSize - offset;
_tbuffer = new unsigned char[new_size];
if(!_tbuffer) error("imuse_channel failed to allocate memory");
memcpy(_tbuffer, _sbuffer + offset, new_size);
_tbufferSize = new_size;
} else {
_tbuffer = 0;
_tbufferSize = 0;
}
if(_sbufferSize == 0) {
// this never happened yet, but who knows
delete []_sbuffer;
_sbuffer = 0;
}
} else {
// easy, swap the buffer
_sbufferSize = _tbufferSize;
_sbuffer = _tbuffer;
_tbufferSize = 0;
_tbuffer = 0;
}
} else {
int offset = 0;
while(handleSubTags(offset));
if(_inData) {
//~ unsigned char * old = _tbuffer;
_sbufferSize = _tbufferSize - offset;
assert(_sbufferSize);
_sbuffer = new unsigned char[_sbufferSize];
if(!_sbuffer) error("imuse_channel failed to allocate memory");
memcpy(_sbuffer, _tbuffer + offset, _sbufferSize);
delete []_tbuffer;
_tbuffer = 0;
_tbufferSize = 0;
} else {
if(offset) { // maybe I should assert() this to avoid a lock...
unsigned char * old = _tbuffer;
int new_size = _tbufferSize - offset;
_tbuffer = new unsigned char[new_size];
if(!_tbuffer) error("imuse_channel failed to allocate memory");
memcpy(_tbuffer, old + offset, new_size);
_tbufferSize = new_size;
delete []old;
}
}
}
_srbufferSize = _sbufferSize;
if(_sbuffer && _bitsize == 12) decode();
return true;
}
int ImuseChannel::availableSoundData(void) const {
int ret = _sbufferSize;
if(_channels == 2) ret /= 2;
if(_bitsize > 8) ret /= 2;
return ret;
}
void ImuseChannel::getSoundData(short * snd, int size) {
if(_dataSize <= 0 || _bitsize <= 8) error("invalid call to imuse_channel::read_sound_data()");
if(_channels == 2) size *= 2;
for(int i = 0; i < size; i++)
snd[i] = READ_BE_UINT16(_sbuffer + 2 * i);
delete []_sbuffer;
assert(_sbufferSize == 2 * size);
_sbuffer = 0;
_sbufferSize = 0;
_dataSize -= _srbufferSize;
}
void ImuseChannel::getSoundData(char * snd, int size) {
if(_dataSize <= 0 || _bitsize > 8) error("invalid call to imuse_channel::read_sound_data()");
if(_channels == 2) size *= 2;
for(int i = 0; i < size; i++)
snd[i] = _sbuffer[i];
delete []_sbuffer;
_sbuffer = 0;
_sbufferSize = 0;
_dataSize -= _srbufferSize;
}
|