aboutsummaryrefslogtreecommitdiff
path: root/scumm/smush/saud_channel.cpp
blob: c101c0d3466e17e585e49f967a5daf878dcd9f7b (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
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
/* 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

void SaudChannel::handleStrk(Chunk & b) {
	int size = b.getSize();
	if(size != 14 && size != 10) {
		error("STRK has a invalid size : %d", size);
	}
}

void SaudChannel::handleSmrk(Chunk & b) {
	_markReached = true;
}

void SaudChannel::handleShdr(Chunk & b) {
	int size = b.getSize();
	if(size != 4) warning("SMRK has a invalid size : %d", size);
}

bool SaudChannel::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_STRK:
				_inData = false;
				if(available_size >= (size + 8)) {
					ContChunk c((char*)_tbuffer + offset);
					handleStrk(c);
				}
				else
					return false;
				break;
			case TYPE_SMRK:
				_inData = false;
				if(available_size >= (size + 8)) {
					ContChunk c((char*)_tbuffer + offset);
					handleSmrk(c);
				}
				else
					return false;
				break;
			case TYPE_SHDR:
				_inData = false;
				if(available_size >= (size + 8)) {
					ContChunk c((char*)_tbuffer + offset);
					handleShdr(c);
				}
				else
					return false;
				break;
			case TYPE_SDAT: 
				_inData = true;
				_dataSize = size;
				offset += 8;
				return false;
			default:
				error("unknown Chunk in SAUD track : %s ", Chunk::ChunkString(type));
		}
		offset += size + 8;
		return true;
	}
	return false;
}

bool SaudChannel::processBuffer() {
	// At the start of this function, we have _tbuffer[0.._tbuffersize] containing possible data...
	// and _sbuffer is 0
	// At the end we have :
	// if(sound data) _sbuffer[0.._sbuffer_size] contains the sound data
	// the unprocessed data is kept in _tbuffer[0.._tbuffersize] (which may have changed)
	// if no unprocessed data, then _tbuffer is 0
	assert(_tbuffer != 0);
	assert(_tbufferSize != 0);
	assert(_sbuffer == 0);
	assert(_sbufferSize == 0);
	
	if(_inData) {
		if(_dataSize < _tbufferSize) {
			// I can't assume that the channel is finished after data is received... (this assumption failed in realride.san)
			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("SaudChannel 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) {
			_sbufferSize = _tbufferSize - offset;
			assert(_sbufferSize);
			_sbuffer = new unsigned char[_sbufferSize];
			if(!_sbuffer)  error("saud_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("SaudChannel failed to allocate memory");
				memcpy(_tbuffer, old + offset, new_size);
				_tbufferSize = new_size;
				delete []old;
			}
		}
	}
	return true;
}

SaudChannel::SaudChannel(int track, int freq) : 
			_track(track), 
			_nbframes(0),
			_dataSize(-1),
			_frequency(freq),
			_inData(false),
			_markReached(false),
			_tbuffer(0),
			_tbufferSize(0),
			_sbuffer(0),
			_sbufferSize(0)
{
}

SaudChannel::~SaudChannel() {
	if(_tbuffer) delete []_tbuffer;
	if(_sbuffer) {
		warning("this should never happen !!!! (_sbuffer not NULL here)");
		delete []_sbuffer;
	}
}

bool SaudChannel::isTerminated() const {
	return (_markReached && _dataSize == 0 && _sbuffer == 0);
}

void SaudChannel::recalcVolumeTable() {
	const int MAX_BALANCE = 100;
	int volume_left, volume_right;
	if(_balance < -MAX_BALANCE || _balance > MAX_BALANCE) {
		error("balance is out of range ! : %d", _balance);
	}
	int left_multiplier = MAX_BALANCE - _balance;
	int right_multiplier = MAX_BALANCE + _balance;
	volume_left = _volume * left_multiplier / (MAX_BALANCE * 2);
	volume_right = _volume * right_multiplier / (MAX_BALANCE * 2);
	if(volume_left < 0) volume_left = 0;
	if(volume_left > 128) volume_left = 128;
	if(volume_right < 0) volume_right = 0;
	if(volume_right > 128) volume_right = 128;
	for(int i = 0; i < 256; i++) {
		int value = volume_left * (signed char)i;
		_voltable[0][i] = TO_BE_16(value);
		value = volume_right * (signed char)i;
		_voltable[1][i] = TO_BE_16(value);
	}
}

bool SaudChannel::setParameters(int nb, int flags, int volume, int balance) {
	_nbframes = nb;
	_flags = flags; // bit 7 == IS_VOICE, bit 6 == IS_BACKGROUND_MUSIC, other ??
	_volume = volume;
	_balance = balance;
	_index = 0;
	recalcVolumeTable();
	return true;
}

bool SaudChannel::checkParameters(int index, int nb, int flags, int volume, int balance) {
	if(++_index != index) error("invalid index in SaudChannel::checkParameters()");
	if(_nbframes != nb) error("invalid duration in SaudChannel::checkParameters()");
	if(_flags != flags) error("invalid flags in SaudChannel::checkParameters()");
	if(_volume != volume || _balance != balance) {
		_volume = volume;
		_balance = balance;
		recalcVolumeTable();
	}
	return true;
}

bool SaudChannel::appendData(Chunk & b, int size) {
	if(_dataSize == -1) { // First call
		assert(size > 8);
		Chunk::type saud_type = b.getDword(); saud_type = TO_BE_32(saud_type);
		unsigned int saud_size = b.getDword(); saud_size = TO_BE_32(saud_size);
		if(saud_type != TYPE_SAUD) error("Invalid Chunk for SaudChannel : %X", saud_type);
		size -= 8;
		_dataSize = -2; // We don't get here again...
	}
	if(_tbuffer) {
		unsigned char * old = _tbuffer;
		_tbuffer = new unsigned char[_tbufferSize + size];
		if(!_tbuffer)  error("saud_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("saud_channel failed to allocate memory");
		b.read(_tbuffer, _tbufferSize);
	}
	return processBuffer();
}

int SaudChannel::availableSoundData(void) const {
	return _sbufferSize;
}

void SaudChannel::getSoundData(short * snd, int size) {
	for(int i = 0; i < size; i++) {
		snd[2 * i] = _voltable[0][_sbuffer[i] ^ 0x80];
		snd[2 * i + 1] = _voltable[1][_sbuffer[i] ^ 0x80];
	}
	_dataSize -= size;
	delete []_sbuffer;
	_sbuffer = 0;
	_sbufferSize = 0;
}