aboutsummaryrefslogtreecommitdiff
path: root/sound/mods/tfmx.h
blob: 3ca1d8a4ba583b3837cb4dabb18e5038d7ebf630 (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
/* 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$
 *
 */

#ifndef SOUND_MODS_TFMX_H
#define SOUND_MODS_TFMX_H

#include "sound/mods/paula.h"

namespace {
#ifndef NDEBUG
inline void boundaryCheck(const void *bufStart, size_t bufLen, const void *address, size_t accessLen = 1) {
	assert(bufStart <= address && (const byte *)address + accessLen <= (const byte *)bufStart + bufLen);
}
#else
inline void boundaryCheck(const void *, size_t, void *, size_t = 1) {}
#endif
}

namespace Audio {

class Tfmx : public Paula {
public:
	Tfmx(int rate, bool stereo);
	virtual ~Tfmx();

	void interrupt();
	void stopSong(bool stopAudio = true);
	void doSong(int songPos, bool stopAudio = false);
	void doSfx(int sfxIndex);
	void doMacro(int note, int macro, int relVol = 0, int finetune = 0, int channelNo = 0);
	bool load(Common::SeekableReadStream &musicData, Common::SeekableReadStream &sampleData);
	int getTicks() {return _playerCtx.tickCount;}
	int getSongIndex() {return _playerCtx.song;}
	uint16 getSignal(int index) {return _playerCtx.signal[index];}

// Note: everythings public so the debug-Routines work.
// private:
	enum {kPalDefaultCiaVal = 11822, kNtscDefaultCiaVal = 14320, kCiaBaseInterval = 0x1B51F8};
	enum {kNumVoices = 4, kNumChannels = 8, kNumSubsongs = 32, kMaxPatternOffsets = 128, kMaxMacroOffsets = 128};

	static const uint16 noteIntervalls[64];

	struct Resource {
		uint32 _trackstepOffset;	//!< Offset in mdat
		uint32 _sfxTableOffset;

		byte *_mdatData;  	//!< Currently the whole mdat-File
		byte *_sampleData;	//!< Currently the whole sample-File
		
		uint32 _mdatLen;  
		uint32 _sampleLen;

		byte header[10];
		uint16 headerFlags;
		uint32 headerUnknown;
		char textField[6 * 40];

		const byte *getSfxPtr(uint8 index = 0) {
			byte *sfxPtr = (byte *)(_mdatData + _sfxTableOffset + index * 8);

			boundaryCheck(_mdatData, _mdatLen, sfxPtr, 8);
			return sfxPtr;
		}

		const uint16 *getTrackPtr(uint16 trackstep = 0) {
			uint16 *trackData = (uint16 *)(_mdatData + _trackstepOffset + 16 * trackstep);

			boundaryCheck(_mdatData, _mdatLen, trackData, 16);
			return trackData;
		}

		const uint32 *getPatternPtr(uint32 offset) {
			uint32 *pattData = (uint32 *)(_mdatData + offset);

			boundaryCheck(_mdatData, _mdatLen, pattData, 4);
			return pattData;
		}

		const uint32 *getMacroPtr(uint32 offset) {
			uint32 *macroData = (uint32 *)(_mdatData + offset);

			boundaryCheck(_mdatData, _mdatLen, macroData, 4);
			return macroData;
		}

		const int8 *getSamplePtr(const uint32 offset) {
			int8 *sampleData = (int8 *)(_sampleData + offset);

			boundaryCheck(_sampleData, _sampleLen, sampleData, 2);
			return sampleData;
		}
		Resource() : _mdatData(), _mdatLen(), _sampleData(), _sampleLen() {}

		~Resource() {
			delete[] _mdatData;
			delete[] _sampleData;
		}
	} _resource;

	uint32 _patternOffset[kMaxPatternOffsets];	//!< Offset in mdat
	uint32 _macroOffset[kMaxMacroOffsets];	//!< Offset in mdat

	struct Subsong {
		uint16 songstart;	//!< Index in Trackstep-Table
		uint16 songend;		//!< Last index in Trackstep-Table
		uint16 tempo;
	} _subsong[kNumSubsongs];

	struct ChannelContext {
		byte	paulaChannel;

		byte	macroIndex;
		uint16	macroWait;
		uint32	macroOffset;
		uint32	macroReturnOffset;
		uint16	macroStep;
		uint16	macroReturnStep;
		uint8	macroLoopCount;
		bool	macroRun;

		uint32	customMacro;
		uint8	customMacroIndex;
		uint8	customMacroPrio;

		bool	sfxLocked;
		int16	sfxLockTime;
		bool	keyUp;

		bool	deferWait;
		uint16	dmaIntCount;

		uint32	sampleStart;
		uint16	sampleLen;
		uint16	refPeriod;
		uint16	period;

		int8	volume;
		uint8	relVol;
		uint8	note;
		uint8	prevNote;
		int16	fineTune; // always a signextended byte

		uint8	portaSkip;
		uint8	portaCount;
		uint16	portaDelta;
		uint16	portaValue;

		uint8	envSkip;
		uint8	envCount;
		uint8	envDelta;
		int8	envEndVolume;

		uint8	vibLength;
		uint8	vibCount;
		int16	vibValue;
		int8	vibDelta;

		uint8	addBeginTime;
		uint8	addBeginReset;
		int16	addBeginDelta;
	} _channelCtx[kNumVoices];

	struct PatternContext {
		uint32	offset; // patternStart, Offset from mdat
		uint32	savedOffset;	// for subroutine calls
		uint16	step;	// distance from patternStart
		uint16	savedStep;

		uint8	command;
		int8	expose; 
		uint8	loopCount;
		uint8	wait;	//!< how many ticks to wait before next Command
	} _patternCtx[kNumChannels];

	struct TrackStepContext {
		uint16	startInd;
		uint16	stopInd;
		uint16	posInd;
		int16	loopCount;
	} _trackCtx;

	struct PlayerContext {
		bool	enabled;
//		bool	end;
		int8	song;	//!< >= 0 if Song is running (means process Patterns)

		uint16	patternCount;
		uint16	patternSkip;	//!< skip that amount of CIA-Interrupts

		int8	volume;	//!< Master Volume

		uint8	fadeSkip;
		uint8	fadeCount;
		int8	fadeEndVolume;
		int8	fadeDelta;

		int		tickCount;

		uint16	signal[4];

		bool	stopWithLastPattern; //!< hack to automatically stop the whole player if no Pattern is running
	} _playerCtx;

	void initMacroProgramm(ChannelContext &channel) {
		channel.macroStep = 0;
		channel.macroWait = 0;
		channel.macroRun = true;
		channel.macroLoopCount = 0xFF;
		channel.dmaIntCount = 0;
	}

	void clearEffects(ChannelContext &channel) {
		channel.envSkip = 0;
		channel.vibLength = 0;
		channel.portaDelta = 0;
	}

	void clearMacroProgramm(ChannelContext &channel) {
		channel.macroRun = false;
		channel.dmaIntCount = 0;
	}

	void unlockMacroChannel(ChannelContext &channel) {
		channel.customMacro = 0;
		channel.customMacroPrio = false;
		channel.sfxLocked = false;
		channel.sfxLockTime = -1;
	}

	void stopPatternChannels() {
		for (int i = 0; i < kNumChannels; ++i) {
			_patternCtx[i].command = 0xFF;
			_patternCtx[i].expose = 0;
		}
	}

	void stopMacroChannels() {
		for (int i = 0; i < kNumVoices; ++i) {
			clearEffects(_channelCtx[i]);
			unlockMacroChannel(_channelCtx[i]);
			clearMacroProgramm(_channelCtx[i]);
			_channelCtx[i].note = 0;
			_channelCtx[i].volume = 0;
		}
	}

	void setNoteMacro(ChannelContext &channel, uint note, int fineTune) {
		const uint16 noteInt = noteIntervalls[note & 0x3F];
		const uint16 finetune = (uint16)(fineTune + channel.fineTune + (1 << 8));
		channel.refPeriod = ((uint32)noteInt * finetune >> 8);
		if (!channel.portaDelta) {
			channel.period = channel.refPeriod;
			Paula::setChannelPeriod(channel.paulaChannel, channel.period);
		}
	}

	void effects(ChannelContext &channel);
	FORCEINLINE bool macroStep(ChannelContext &channel);
	void advancePatterns();
	FORCEINLINE bool patternStep(PatternContext &pattern, bool &pendingTrackstep);
	bool trackStep();
	void noteCommand(uint8 note, uint8 param1, uint8 param2, uint8 param3);
};

}

#endif