aboutsummaryrefslogtreecommitdiff
path: root/engines/cine/sound_driver.cpp
blob: cb8ff0d3d0c16933d30d4d7e7c450178ad18c766 (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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2006 The ScummVM project
 *
 * cinE Engine is (C) 2004-2005 by CinE Team
 *
 * 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 "cine/cine.h"
#include "cine/sound_driver.h"

#include "sound/mixer.h"
#include "sound/fmopl.h"

uint8 snd_useAdlib = 0;
uint16 snd_fadeOutCounter = 0;
uint16 snd_songTicksCounter = 0;
uint8 *snd_adlibInstrumentsTable[4];
sndDriverStruct snd_driver;

static uint8 snd_adlibVibrato = 0;
static int16 snd_adlibChannelVolume[4];

static const uint16 snd_adlibFreqTable[] = {
	0x0157, 0x016C, 0x0181, 0x0198, 0x01B1, 0x01CB, 0x01E6, 0x0203,
	0x0222, 0x0243, 0x0266, 0x028A
};

static const uint8 snd_adlibOpTable[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x08, 0x09, 0x0A,
	0x0B, 0x0C, 0x0D, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15
};

static const uint8 snd_adlibNoteTable[] = {
	0x00, 0x03, 0x01, 0x04, 0x02, 0x05, 0x06, 0x09, 0x07,
	0x0A, 0x08, 0x0B, 0x0C, 0x0F, 0x10, 0x10, 0x0E, 0x0E,
	0x11, 0x11, 0x0D, 0x0D, 0x00, 0x00
};

static const int16 snd_adlibNoteFreqTable[] = {
	0x0EEE, 0x0E17, 0x0D4D, 0x0C8C, 0x0BD9, 0x0B2F, 0x0A8E, 0x09F7,
	0x0967, 0x08E0, 0x0861, 0x07E8, 0x0777, 0x070B, 0x06A6, 0x0647,
	0x05EC, 0x0597, 0x0547, 0x04FB, 0x04B3, 0x0470, 0x0430, 0x03F4,
	0x03BB, 0x0385, 0x0353, 0x0323, 0x02F6, 0x02CB, 0x02A3, 0x027D,
	0x0259, 0x0238, 0x0218, 0x01FA, 0x01DD, 0x01C2, 0x01A9, 0x0191,
	0x017B, 0x0165, 0x0151, 0x013E, 0x012C, 0x011C, 0x010C, 0x00FD,
	0x00EE, 0x00E1, 0x00D4, 0x00C8, 0x00BD, 0x00B2, 0x00A8, 0x009F,
	0x0096, 0x008E, 0x0086, 0x007E, 0x0077, 0x0070, 0x006A, 0x0064,
	0x005E, 0x0059, 0x0054, 0x004F, 0x004B, 0x0047, 0x0043, 0x003F,
	0x003B, 0x0038, 0x0035, 0x0032, 0x002F, 0x002C, 0x002A, 0x0027,
	0x0025, 0x0023, 0x0021, 0x001F, 0x001D, 0x001C, 0x001A, 0x0019,
	0x0017, 0x0016, 0x0015, 0x0013, 0x0012, 0x0011, 0x0010, 0x000F
};

static void snd_adlibWriteData(int port, int value) {
	OPLWriteReg(g_cine_adlib->getOPL(), port, value);
}

static void snd_adlibDriverSetupInstrument(const uint8 *instrumentData, int channelNum) {
	int16 tmp;

	uint8 waveSelect1 = instrumentData[54] & 3;	/* var2 */
	uint8 waveSelect2 = instrumentData[56] & 3;	/* var1 */

	uint8 fl = *instrumentData++;	/* varB */
	uint8 ch = *instrumentData++;	/* var4 */

	uint8 adlibOp1, adlibOp2;	/* _di, varA */

	if (fl != 0) {
		adlibOp1 = snd_adlibOpTable[snd_adlibNoteTable[ch * 2 + 0]];
		adlibOp2 = snd_adlibOpTable[snd_adlibNoteTable[ch * 2 + 1]];
	} else {
		adlibOp1 = snd_adlibOpTable[snd_adlibNoteTable[channelNum * 2 + 0]];
		adlibOp2 = snd_adlibOpTable[snd_adlibNoteTable[channelNum * 2 + 1]];
	}

	if (fl == 0 || ch == 6) {
		// vibrato
		tmp = 0;
		if (READ_LE_UINT16(instrumentData + 18) != 0)
			tmp |= 0x80;
		if (READ_LE_UINT16(instrumentData + 20) != 0)
			tmp |= 0x40;
		if (READ_LE_UINT16(instrumentData + 10) != 0)
			tmp |= 0x20;
		if (READ_LE_UINT16(instrumentData + 22) != 0)
			tmp |= 0x10;
		tmp |= (READ_LE_UINT16(instrumentData + 2) & 0xF);
		snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_EG_KS + adlibOp1, tmp);

		// key scaling
		tmp = 0x3F - (READ_LE_UINT16(instrumentData + 16) & 0x3F);
		tmp = snd_adlibChannelVolume[channelNum] * tmp;
		tmp += tmp + 0x7F;
		tmp = 0x3F - (tmp / 0xFE);
		if (READ_LE_UINT16(instrumentData + 24) != 0)
			tmp = READ_LE_UINT16(instrumentData + 16) & 0x3F;
		tmp |= READ_LE_UINT16(instrumentData) << 6;
		snd_adlibWriteData(ADLIB_REG_KEY_SCALING_OPERATOR_OUTPUT + adlibOp1, tmp);

		// attack/decay rates
		tmp = (READ_LE_UINT16(instrumentData + 6) << 4) | (READ_LE_UINT16(instrumentData + 12) & 0xF);
		snd_adlibWriteData(ADLIB_REG_ATTACK_RATE_DECAY_RATE + adlibOp1, tmp);

		// sustain/release rates
		tmp = (READ_LE_UINT16(instrumentData + 8) << 4) | (READ_LE_UINT16(instrumentData + 14) & 0xF);
		snd_adlibWriteData(ADLIB_REG_SUSTAIN_LEVEL_RELEASE_RATE_0 + adlibOp1, tmp);

		if (fl != 0) {
			tmp = READ_LE_UINT16(instrumentData + 4) * 2;
			if (READ_LE_UINT16(instrumentData + 24) == 0)
				tmp |= 1;

			snd_adlibWriteData(ADLIB_REG_FEEDBACK_STRENGTH_CONNECTION_TYPE + ch, tmp);
		} else {
			tmp = READ_LE_UINT16(instrumentData + 4) * 2;
			if (READ_LE_UINT16(instrumentData + 24) == 0)
				tmp |= 1;

			snd_adlibWriteData(ADLIB_REG_FEEDBACK_STRENGTH_CONNECTION_TYPE + channelNum, tmp);
		}
		snd_adlibWriteData(ADLIB_REG_WAVE_SELECT + adlibOp1, waveSelect1);
		instrumentData += 26;
	}

	// vibrato
	tmp = 0;
	if (READ_LE_UINT16(instrumentData + 18) != 0)
		tmp |= 0x80;
	if (READ_LE_UINT16(instrumentData + 20) != 0)
		tmp |= 0x40;
	if (READ_LE_UINT16(instrumentData + 10) != 0)
		tmp |= 0x20;
	if (READ_LE_UINT16(instrumentData + 22) != 0)
		tmp |= 0x10;
	tmp |= (READ_LE_UINT16(instrumentData + 2) & 0xF);
	snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_EG_KS + adlibOp2, tmp);

	// key scaling
	tmp = 0x3F - (READ_LE_UINT16(instrumentData + 16) & 0x3F);
	tmp = snd_adlibChannelVolume[channelNum] * tmp;
	tmp += tmp + 0x7F;
	tmp = 0x3F - (tmp / 0xFE);
	tmp |= READ_LE_UINT16(instrumentData) << 6;
	snd_adlibWriteData(ADLIB_REG_KEY_SCALING_OPERATOR_OUTPUT + adlibOp2, tmp);

	// attack/decay rates */
	tmp =(READ_LE_UINT16(instrumentData + 6) << 4) | (READ_LE_UINT16(instrumentData + 12) & 0xF);
	snd_adlibWriteData(ADLIB_REG_ATTACK_RATE_DECAY_RATE + adlibOp2, tmp);

	// sustain/release rates */
	tmp = (READ_LE_UINT16(instrumentData + 8) << 4) | (READ_LE_UINT16(instrumentData + 14) & 0xF);
	snd_adlibWriteData(ADLIB_REG_SUSTAIN_LEVEL_RELEASE_RATE_0 + adlibOp2, tmp);
	snd_adlibWriteData(ADLIB_REG_WAVE_SELECT + adlibOp2, waveSelect2);
}

static void snd_adlibInterrupt(void *param, int16 *buf, int len) {
	int16 *origData = buf;
	uint origLen = len;
	static int samplesLeft = 0;

	while (len != 0) {
		int count;
		if (samplesLeft == 0) {
			if (snd_songIsPlaying || (snd_fadeOutCounter != 0 && snd_fadeOutCounter < 100)) {
				++snd_songTicksCounter;
				if (snd_songTicksCounter > snd_eventsDelay) {
					snd_handleEvents();
					snd_songTicksCounter = 0;
				}
			}
			samplesLeft = g_cine_adlib->getRate() / 50;
		}
		count = samplesLeft;
		if (count > len)
			count = len;

		YM3812UpdateOne(g_cine_adlib->getOPL(), buf, count);

		samplesLeft -= count;
		len -= count;
		buf += count;
	}

	// Convert mono data to stereo
	for (int i = (origLen - 1); i >= 0; i--) {
		origData[2 * i] = origData[2 * i + 1] = origData[i];
	}
}

static void snd_adlibDriverSetupChannel(int channelNum, const uint8 *data, int instrumentNum) {
	int16 vol = snd_sfxState.songData[instrumentNum];
	if (vol != 0 && vol < 0x50)
		vol = 0x50;

	vol -= snd_fadeOutCounter;
	if (vol < 0)
		vol = 0;

	vol += vol / 4;
	if (vol > 0x7F)
		vol = 0x7F;

	snd_adlibChannelVolume[channelNum] = vol;
	snd_adlibDriverSetupInstrument(data, channelNum);
}

static void snd_getAdlibFrequency(int frequency, int *adlibFreq) {
	int i;

	*adlibFreq = 95;
	for (i = 0; i < 96; ++i) {
		if (snd_adlibNoteFreqTable[i] <= frequency) {
			*adlibFreq = i;
			break;
		}
	}
}

static void snd_adlibDriverSetChannelFrequency(int channelNum, int frequency) {
	const uint8 *instr = snd_adlibInstrumentsTable[channelNum];
	uint8 fl = *instr++;	// var2
	uint8 ch = *instr++;	// var1

	if (fl != 0 && ch == 6)
		channelNum = 6;

	if (fl == 0 || channelNum == 6) {
		uint16 freqLow, freqHigh;	// var8
		int adlibFreq;

		snd_getAdlibFrequency(frequency, &adlibFreq);
		if (channelNum == 6)
			adlibFreq %= 12;

		freqLow = snd_adlibFreqTable[adlibFreq % 12];
		snd_adlibWriteData(ADLIB_REG_FREQUENCY_0 + channelNum, freqLow);
		freqHigh = ((adlibFreq / 12) << 2) | ((freqLow & 0x300) >> 8);
		if (fl == 0)
			freqHigh |= 0x20;

		snd_adlibWriteData(ADLIB_REG_KEY_ON_OCTAVE_FREQUENCY_0 + channelNum, freqHigh);
	}
	if (fl != 0) {
		snd_adlibVibrato |= 1 << (10 - ch);
		snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_RHYTHM, snd_adlibVibrato);
	}
}

static void snd_adlibDriverStopChannel(int channelNum) {
	const uint8 *instr = snd_adlibInstrumentsTable[channelNum];
	uint8 fl = *instr++;	// var2
	uint8 ch = *instr++;	// var1

	if (fl != 0 && ch == 6)
		channelNum = 6;

	if (fl == 0 || channelNum == 6)
		snd_adlibWriteData(ADLIB_REG_KEY_ON_OCTAVE_FREQUENCY_0 + channelNum, 0);

	if (fl != 0) {
		snd_adlibVibrato &= (1 << (10 - ch)) ^ 0xFF;
		snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_RHYTHM, snd_adlibVibrato);
	}
}

static void snd_adlibDriverPlaySound(uint8 * data, int channelNum, int volume) {
//  if (_snd_mute) return;
	uint8 fl, ch;		// var2, var1

	assert(channelNum < 4);
	data += 257;
	snd_adlibInstrumentsTable[channelNum] = data;
	snd_resetChannel(channelNum);
	snd_adlibChannelVolume[channelNum] = 0x7F;
	snd_adlibDriverSetupInstrument(data, channelNum);
	fl = *data++;
	ch = *data++;

	if (fl != 0 && ch == 6)
		channelNum = 6;

	if (fl == 0 || channelNum == 6) {
		uint16 freqLow, freqHigh;
		freqLow = snd_adlibFreqTable[0];
		snd_adlibWriteData(ADLIB_REG_FREQUENCY_0 + channelNum, freqLow);
		freqHigh = 4 | ((freqLow & 0x300) >> 8);
		if (fl == 0)
			freqHigh |= 0x20;

		snd_adlibWriteData(ADLIB_REG_KEY_ON_OCTAVE_FREQUENCY_0 + channelNum, freqHigh);
	}
	if (fl != 0) {
		snd_adlibVibrato = 1 << (10 - ch);
		snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_RHYTHM, snd_adlibVibrato);
	}
}

static sndDriverStruct snd_adlibDriver = {
	&snd_adlibDriverSetupChannel,
	&snd_adlibDriverSetChannelFrequency,
	&snd_adlibDriverStopChannel,
	&snd_adlibDriverPlaySound
};

void snd_adlibDriverStopSong() {
	int i;

	for (i = 0; i < 18; ++i)
		snd_adlibWriteData(ADLIB_REG_KEY_SCALING_OPERATOR_OUTPUT + snd_adlibOpTable[i], 0x3F);

	for (i = 0; i < 9; ++i)
		snd_adlibWriteData(ADLIB_REG_KEY_ON_OCTAVE_FREQUENCY_0 + i, 0);

	snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_RHYTHM, 0);
}

void snd_resetChannel(int channelNum) {
	(*snd_driver.stopChannel) (channelNum);
	if (snd_useAdlib)
		snd_adlibDriverStopSong();
}

AdlibMusic::AdlibMusic(Audio::Mixer *pMixer) {
	_mixer = pMixer;
	_sampleRate = pMixer->getOutputRate();
	g_cine_adlib = this;
	_opl = makeAdlibOPL(_sampleRate);

	snd_adlibVibrato = 0x20;
	snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_RHYTHM, snd_adlibVibrato);
	snd_adlibWriteData(0x08, 0x40);

	int i;

	for (i = 0; i < 18; ++i)
		snd_adlibWriteData(ADLIB_REG_KEY_SCALING_OPERATOR_OUTPUT + snd_adlibOpTable[i], 0);

	for (i = 0; i < 9; ++i)
		snd_adlibWriteData(ADLIB_REG_KEY_ON_OCTAVE_FREQUENCY_0 + i, 0);

	for (i = 0; i < 9; ++i)
		snd_adlibWriteData(ADLIB_REG_FEEDBACK_STRENGTH_CONNECTION_TYPE + i, 0);

	for (i = 0; i < 18; ++i)
		snd_adlibWriteData(ADLIB_REG_ATTACK_RATE_DECAY_RATE + snd_adlibOpTable[i], 0);

	for (i = 0; i < 18; ++i)
		snd_adlibWriteData(ADLIB_REG_SUSTAIN_LEVEL_RELEASE_RATE_0 + snd_adlibOpTable[i], 0);

	for (i = 0; i < 18; ++i)
		snd_adlibWriteData(ADLIB_REG_AM_VIBRATO_EG_KS + snd_adlibOpTable[i], 0);

	for (i = 0; i < 18; ++i)
		snd_adlibWriteData(ADLIB_REG_WAVE_SELECT + snd_adlibOpTable[i],
		    0);

	snd_adlibWriteData(1, 0x20);
	snd_adlibWriteData(1, 0);

	for (i = 0; i < 4; ++i)
		snd_adlibInstrumentsTable[i] = snd_nullInstrument;

	snd_useAdlib = 1;
	snd_driver = snd_adlibDriver;

	_mixer->setupPremix(this);
}

void AdlibMusic::premixerCall(int16 *data, uint len) {
	snd_adlibInterrupt(NULL, data, len);
}

void AdlibMusic::setVolume(uint8 volume) {
	for (int i = 0; i < 4; ++i)
		snd_adlibChannelVolume[i] = volume | 128;
}

AdlibMusic::~AdlibMusic(void) {
	_mixer->setupPremix(NULL);
}