aboutsummaryrefslogtreecommitdiff
path: root/audio/mods/soundfx.cpp
blob: 06a1e295146657b279b8a8227e99f9417f307f86 (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
272
273
274
275
/* 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 "audio/mods/paula.h"
#include "audio/mods/soundfx.h"
#include "audio/audiostream.h"

namespace Audio {

struct SoundFxInstrument {
	char name[23];
	uint16 len;
	uint8 finetune;
	uint8 volume;
	uint16 repeatPos;
	uint16 repeatLen;
	int8 *data;
};

class SoundFx : public Paula {
public:

	enum {
		NUM_CHANNELS = 4,
		NUM_INSTRUMENTS = 15
	};

	SoundFx(int rate, bool stereo);
	virtual ~SoundFx();

	bool load(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb);
	void play();

protected:

	void handlePattern(int ch, uint32 pat);
	void updateEffects(int ch);
	void handleTick();

	void disablePaulaChannel(uint8 channel);
	void setupPaulaChannel(uint8 channel, const int8 *data, uint16 len, uint16 repeatPos, uint16 repeatLen);

	virtual void interrupt();

	uint8 _ticks;
	uint16 _delay;
	SoundFxInstrument _instruments[NUM_INSTRUMENTS];
	uint8 _numOrders;
	uint8 _curOrder;
	uint16 _curPos;
	uint8 _ordersTable[128];
	uint8 *_patternData;
	uint16 _effects[NUM_CHANNELS];
};

SoundFx::SoundFx(int rate, bool stereo)
	: Paula(stereo, rate) {
	setTimerBaseValue(kPalCiaClock);
	_ticks = 0;
	_delay = 0;
	memset(_instruments, 0, sizeof(_instruments));
	_numOrders = 0;
	_curOrder = 0;
	_curPos = 0;
	memset(_ordersTable, 0, sizeof(_ordersTable));
	_patternData = 0;
	memset(_effects, 0, sizeof(_effects));
}

SoundFx::~SoundFx() {
	free(_patternData);
	for (int i = 0; i < NUM_INSTRUMENTS; ++i) {
		free(_instruments[i].data);
	}
}

bool SoundFx::load(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb) {
	int instrumentsSize[15];
	if (!loadCb) {
		for (int i = 0; i < NUM_INSTRUMENTS; ++i) {
			instrumentsSize[i] = data->readUint32BE();
		}
	}
	uint8 tag[4];
	data->read(tag, 4);
	if (memcmp(tag, "SONG", 4) != 0) {
		return false;
	}
	_delay = data->readUint16BE();
	data->skip(7 * 2);
	for (int i = 0; i < NUM_INSTRUMENTS; ++i) {
		SoundFxInstrument *ins = &_instruments[i];
		data->read(ins->name, 22); ins->name[22] = 0;
		ins->len = data->readUint16BE();
		ins->finetune = data->readByte();
		ins->volume = data->readByte();
		ins->repeatPos = data->readUint16BE();
		ins->repeatLen = data->readUint16BE();
	}
	_numOrders = data->readByte();
	data->skip(1);
	data->read(_ordersTable, 128);
	int maxOrder = 0;
	for (int i = 0; i < _numOrders; ++i) {
		if (_ordersTable[i] > maxOrder) {
			maxOrder = _ordersTable[i];
		}
	}
	int patternSize = (maxOrder + 1) * 4 * 4 * 64;
	_patternData = (uint8 *)malloc(patternSize);
	if (!_patternData) {
		return false;
	}
	data->read(_patternData, patternSize);
	for (int i = 0; i < NUM_INSTRUMENTS; ++i) {
		SoundFxInstrument *ins = &_instruments[i];
		if (!loadCb) {
			if (instrumentsSize[i] != 0) {
				assert(ins->len <= 1 || ins->len * 2 <= instrumentsSize[i]);
				assert(ins->repeatLen <= 1 || (ins->repeatPos + ins->repeatLen) * 2 <= instrumentsSize[i]);
				ins->data = (int8 *)malloc(instrumentsSize[i]);
				if (!ins->data) {
					return false;
				}
				data->read(ins->data, instrumentsSize[i]);
			}
		} else {
			if (ins->name[0]) {
				ins->name[8] = '\0';
				ins->data = (int8 *)(*loadCb)(ins->name, 0);
				if (!ins->data) {
					return false;
				}
			}
		}
	}
	return true;
}

void SoundFx::play() {
	_curPos = 0;
	_curOrder = 0;
	_ticks = 0;
	setInterruptFreqUnscaled(_delay);
	startPaula();
}

void SoundFx::handlePattern(int ch, uint32 pat) {
	uint16 note1 = pat >> 16;
	uint16 note2 = pat & 0xFFFF;
	if (note1 == 0xFFFD) { // PIC
		_effects[ch] = 0;
		return;
	}
	_effects[ch] = note2;
	if (note1 == 0xFFFE) { // STP
		disablePaulaChannel(ch);
		return;
	}
	int ins = (note2 & 0xF000) >> 12;
	if (ins != 0) {
		SoundFxInstrument *i = &_instruments[ins - 1];
		setupPaulaChannel(ch, i->data, i->len, i->repeatPos, i->repeatLen);
		int effect = (note2 & 0xF00) >> 8;
		int volume = i->volume;
		switch (effect) {
		case 5: // volume up
			volume += (note2 & 0xFF);
			if (volume > 63) {
				volume = 63;
			}
			break;
		case 6: // volume down
			volume -= (note2 & 0xFF);
			if (volume < 0) {
				volume = 0;
			}
			break;
		}
		setChannelVolume(ch, volume);
	}
	if (note1 != 0) {
		setChannelPeriod(ch, note1);
	}
}

void SoundFx::updateEffects(int ch) {
	// updateEffects() is a no-op in all Delphine Software games using SoundFx : FW,OS,Cruise,AW
	if (_effects[ch] != 0) {
		switch (_effects[ch]) {
		case 1: // appreggiato
		case 2: // pitchbend
		case 3: // ledon, enable low-pass filter
		case 4: // ledoff, disable low-pass filter
		case 7: // set step up
		case 8: // set step down
			warning("Unhandled effect %d", _effects[ch]);
			break;
		}
	}
}

void SoundFx::handleTick() {
	++_ticks;
	if (_ticks != 6) {
		for (int ch = 0; ch < 4; ++ch) {
			updateEffects(ch);
		}
	} else {
		_ticks = 0;
		const uint8 *patternData = _patternData + _ordersTable[_curOrder] * 1024 + _curPos;
		for (int ch = 0; ch < 4; ++ch) {
			handlePattern(ch, READ_BE_UINT32(patternData));
			patternData += 4;
		}
		_curPos += 4 * 4;
		if (_curPos >= 1024) {
			_curPos = 0;
			++_curOrder;
			if (_curOrder == _numOrders) {
				stopPaula();
			}
		}
	}
}

void SoundFx::disablePaulaChannel(uint8 channel) {
	disableChannel(channel);
}

void SoundFx::setupPaulaChannel(uint8 channel, const int8 *data, uint16 len, uint16 repeatPos, uint16 repeatLen) {
	if (data && len > 1) {
		setChannelData(channel, data, data + repeatPos * 2, len * 2, repeatLen * 2);
	}
}

void SoundFx::interrupt() {
	handleTick();
}

AudioStream *makeSoundFxStream(Common::SeekableReadStream *data, LoadSoundFxInstrumentCallback loadCb, int rate, bool stereo) {
	SoundFx *stream = new SoundFx(rate, stereo);
	if (stream->load(data, loadCb)) {
		stream->play();
		return stream;
	}
	delete stream;
	return 0;
}

} // End of namespace Audio