aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/music.h
blob: 89d8e2933c1469dfd15dfd87d651b12e5ddce495 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2006 The ScummVM project
 * Original ADL-Player source Copyright (C) 2004 by Patrick Combet aka Dorian Gray
 *
 * 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$
 *
 */

#ifndef GOB_MUSIC_H
#define GOB_MUSIC_H

#include "sound/audiostream.h"
#include "sound/mixer.h"
#include "sound/fmopl.h"
#include "common/mutex.h"

#include "gob/gob.h"
#include "gob/util.h"

namespace Gob {

class GobEngine;

// Emulation of the "Paula" Amiga music chip
class Paula: public Audio::AudioStream {
public:
	Paula(GobEngine *vm, bool stereo = false, int intFreq = 0);
	~Paula();
	
	bool playing() const { return _playing; }
	void setInterruptFreq(int freq) { _intFreq = freq; }
	void clearVoice(int voice);
	void clearVoices() { int i; for (i = 0; i < 4; i++) clearVoice(i); }
	virtual void startPlay(void) {}
	virtual void stopPlay(void) {}
	virtual void pausePlay(bool pause) {}

// AudioStream API
	int readBuffer(int16 *buffer, const int numSamples);
	bool isStereo() const { return _stereo; }
	bool endOfData() const { return _end; }
	int getRate() const { return _rate; }

protected:
	struct Channel {
		int8 *data;
		int8 *dataRepeat;
		uint32 length;
		uint32 lengthRepeat;
		int16 period;
		byte volume;
		double offset;
		byte panning; // For stereo mixing; 0: left, 1: right
	} _voice[4];

	int _rate;
	int _intFreq;
	int _curInt;
	bool _stereo;
	bool _end;
	bool _playing;

	GobEngine *_vm;

	void mix(int16 *&buf, int8 data, int voice) {
		if (_stereo) {
			*buf++ += _voice[voice].panning == 0 ? 2 * _voice[voice].volume * data : 0;
			*buf++ += _voice[voice].panning == 1 ? 2 * _voice[voice].volume * data : 0;
		} else
			*buf++ += _voice[voice].volume * data;
	}
	virtual void interrupt(void) {};
};

class Infogrames : public Paula {
public:
	class Instruments {
	public:
		struct Sample {
			int8 *data;
			int8 *dataRepeat;
			uint32 length;
			uint32 lengthRepeat;
		} _samples[32];

		uint8 _count;
		int8 *_sampleData;

		Instruments();
		~Instruments();

		bool load(Common::SeekableReadStream &ins);
		bool load(const char *ins) {
			Common::File f;

			if (f.open(ins))
				return load(f);
			return false;
		}
	};

	Infogrames(GobEngine *vm, bool stereo = false);
	~Infogrames();

	Instruments *getInstruments(void) const { return _instruments; }
	bool getRepeating(void) const { return _repCount != 0; }
	void setRepeating (int32 repCount) { _repCount = repCount; }
	void restart(void) { if (!_data || !_instruments) return; init(); _end = false; }
	virtual void startPlay(void) { if (_data && _instruments) { restart(); _playing = true; } }
	virtual void stopPlay(void) { _playing = false; }
	virtual void pausePlay(bool pause) { _playing = !pause; }

	bool load(Common::SeekableReadStream &dum);
	bool load(const char *dum) {
		Common::File f;

		if (f.open(dum))
			return load(f);
		return false;
	}
	void unload(bool destroyInstruments = false);
	template<typename T> bool loadInstruments(T ins) {
		unload(true);
		_instruments = new Instruments();
		if (!_instruments->load(ins)) {
			delete _instruments;
			_instruments = 0;
			return false;
		}
		return true;
	}

protected:
	Instruments *_instruments;

	static const uint8 tickCount[];
	static const uint16 periods[];
	byte *_data;
	int32 _repCount;

	uint16 _volume;
	int16 _period;
	byte *_volSlideBlocks;
	byte *_periodSlideBlocks;
	byte *_subSong;
	byte *_cmdBlocks;
	uint8 _sample;
	uint8 _speedCounter;
	uint8 _speed;
	uint8 _newVol;
	uint8 _field_1E;

	struct Slide {
		int16 finetuneNeg;
		int16 finetunePos;
		byte *data;
		int8 amount;
		uint8 dataOffset;
		uint8 flags; // 0: Apply finetune modifier, 2: Don't slide, 7: Continuous
		uint8 curDelay1;
		uint8 curDelay2;
	};
	struct Channel {
		byte *cmdBlockIndices;
		byte *cmds;
		byte *cmdBlocks;
		Slide volSlide;
		Slide periodSlide;
		int16 curPeriod;
		int16 period;
		uint16 curCmdBlock;
		uint16 flags; // 0: Need init, 5: Loop cmdBlocks, 6: Ignore channel
		uint8 ticks;
		uint8 tickCount;
		int8 periodMod;
		uint8 field_2B;
		uint8 field_2C;
		uint8 field_2F;
	} _chn[4];

	void init(void);
	void reset(void);
	void getNextSample(Channel &chn);
	int16 tune(Slide &slide, int16 start) const;
	virtual void interrupt(void);
};

class Adlib : public Audio::AudioStream {
public:
	Adlib(GobEngine *vm);
	virtual ~Adlib();

	void lock() { _mutex.lock(); }
	void unlock() { _mutex.unlock(); }
	bool playing() const { return _playing; }
	bool getRepeating(void) const { return _repCount != 0; }
	void setRepeating (int32 repCount) { _repCount = repCount; }
	int getIndex(void) const { return _index; }
	virtual void startPlay(void);
	virtual void stopPlay(void) { _mutex.lock(); _playing = false; _mutex.unlock(); }
	virtual void playTrack(const char *trackname);
	virtual void playBgMusic(void);
	virtual bool load(const char *filename);
	virtual void load(byte *data, int index=-1);
	virtual void unload(void);

// AudioStream API
	int readBuffer(int16 *buffer, const int numSamples) {
		premixerCall(buffer, numSamples / 2);
		return numSamples;
	}
	bool isStereo() const { return true; }
	bool endOfData() const { return false; }
	int getRate() const { return _rate; }
	
protected:
	static const char *_tracks[][2];
	static const char *_trackFiles[];
	static const unsigned char _operators[];
	static const unsigned char _volRegNums [];
	FM_OPL *_opl;
	int _index;
	byte *_data;
	byte *_playPos;
	uint32 _dataSize;
	uint32 _rate;
	short _freqs[25][12];
	byte _notes[11];
	byte _notCol[11];
	byte _notLin[11];
	bool _notOn[11];
	byte _pollNotes[16];
	uint32 _samplesTillPoll;
	int32 _repCount;
	bool _playing;
	bool _first;
	bool _ended;
	bool _needFree;
	Common::Mutex _mutex;
	GobEngine *_vm;

	void premixerCall(int16 *buf, uint len);
	void writeOPL(byte reg, byte val);
	void setFreqs(void);
	void reset(void);
	void setVoices();
	void setVoice(byte voice, byte instr, bool set);
	void setKey(byte voice, byte note, bool on, bool spec);
	void setVolume(byte voice, byte volume);
	void pollMusic(void);
};

class Adlib_Dummy: public Adlib {
public:
	Adlib_Dummy(GobEngine *vm) : Adlib(vm) {}

	virtual void startPlay(void) {};
	virtual void stopPlay(void) {};
	virtual void playTrack(const char *trackname) {};
	virtual void playBgAdlib(void) {};
	virtual bool load(const char *filename) { return true; }
	virtual void load(byte *data, int index=-1) {}
	virtual void unload(void) {};

	virtual ~Adlib_Dummy() {};
};

} // End of namespace Gob

#endif