aboutsummaryrefslogtreecommitdiff
path: root/audio/softsynth/fmtowns_pc98/towns_euphony.h
blob: 76aa153a37b602e32f32ed0fe76be3d380e68950 (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
/* 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.
 *
 */

#ifndef TOWNS_EUP_H
#define TOWNS_EUP_H

#include "audio/softsynth/fmtowns_pc98/towns_audio.h"
#include "common/array.h"

class EuphonyBaseDriver {
public:
	EuphonyBaseDriver() {}
	virtual ~EuphonyBaseDriver() {}

	virtual bool init() { return true; }

	virtual void send(uint8 command) = 0;
};

class EuphonyPlayer;

class EuphonyDriver : public EuphonyBaseDriver {
public:
	EuphonyDriver(Audio::Mixer *mixer, EuphonyPlayer *pl);
	~EuphonyDriver();

	bool init();
	void reset();

	int assignPartToChannel(int chan, int part);

	void send(uint8 command);

	void setTimerA(bool enable, int tempo);
	void setTimerB(bool enable, int tempo);

	void loadInstrument(int chanType, int id, const uint8 *data);
	void setInstrument(int chan, int instrID);
	void loadWaveTable(const uint8 *data);
	void unloadWaveTable(int id);

	void reserveSoundEffectChannels(int num);
	void playSoundEffect(int chan, int note, int velo, const uint8 *data);
	void stopSoundEffect(int chan);
	bool soundEffectIsPlaying(int chan);

	void channelPan(int chan, int mode);
	void channelPitch(int chan, int pitch);
	void channelVolume(int chan, int vol);

	void setOutputVolume(int chanType, int volLeft, int volRight);
	void cdaToggle(int a);

	void setMusicVolume(int volume);
	void setSoundEffectVolume(int volume);

private:
	void noteOff();
	void noteOn();
	void controlChange_volume();
	void controlChange_panPos();
	void controlChange_allNotesOff();
	void programChange();
	void pitchWheel();

	Common::Array<uint8> _currentEvent;
	int8 *_partToChanMapping;
	int8 *_sustainChannels;

	struct Channel {
		int8 part;
		int8 next;
		uint8 note;
		uint8 pri;
	} *_channels;

	TownsAudioInterface *_intf;
};

class Type0Driver : public EuphonyBaseDriver {
public:
	Type0Driver(EuphonyPlayer *pl);
	~Type0Driver();

	bool init();

	void send(uint8 command);
};

class EuphonyPlayer : public TownsAudioInterfacePluginDriver {
public:
	EuphonyPlayer(Audio::Mixer *mixer);
	virtual ~EuphonyPlayer();

	bool init();

	int startTrack(const uint8 *data, int trackSize, int barLen);
	void stop();
	void pause();
	void resume();

	int setTempo(int tempo);
	void setLoopStatus(bool loop);
	
	bool isPlaying() {return _playing; }

	int configPart_enable(int part, int val);
	int configPart_setType(int part, int val);
	int configPart_remap(int part, int val);
	int configPart_adjustVolume(int part, int val);
	int configPart_setTranspose(int part, int val);

	void timerCallback(int timerId);

	EuphonyDriver *driver() { return _eupDriver; }

private:
	void reset();
	void resetPartConfig();
	void resetTempo();

	void updatePulseCounters();
	void updateBeat();
	void updateParser();
	void updateCheckEot();

	bool parseEvent();
	void proceedToNextEvent();

	void updateHangingNotes();
	void clearHangingNotes();
	
	void resetAllControls();
	void allPartsOff();
	
	uint8 appendEvent(uint8 evt, uint8 chan);

	typedef bool(EuphonyPlayer::*EuphonyEvent)();
	bool event_notImpl();
	bool event_noteOn();
	bool event_polyphonicAftertouch();
	bool event_controlChange_pitchWheel();
	bool event_programChange_channelAftertouch();

	bool event_sysex();
	bool event_advanceBar();
	bool event_setTempo();
	bool event_typeOrdrChange();

	uint8 applyTranspose(uint8 in);
	uint8 applyVolumeAdjust(uint8 in);

	void sendEvent(uint8 type, uint8 command);
	void sendNoteEvent(int type, int evt, int note, int velo);
	void sendControllerReset(int type, int part);
	void sendAllNotesOff(int type, int part);
	void sendTempo(int tempo);

	uint8 *_partConfig_enable;
	uint8 *_partConfig_type;
	uint8 *_partConfig_ordr;
	int8 *_partConfig_volume;
	int8 *_partConfig_transpose;

	struct SavedEvent {
		SavedEvent(int ev, int tp, int nt, int vl, int ln, SavedEvent *chain) : evt(ev), type(tp), note(nt), velo(vl), len(ln), next(chain) {}
		uint8 evt;
		uint8 type;
		uint8 note;
		uint8 velo;
		uint16 len;
		SavedEvent *next;
	};
	
	SavedEvent *_savedEventsChain;

	uint8 _defaultBarLength;
	uint8 _barLength;
	int _playerUpdatesLeft;
	int _tempoControlMode;
	int _updatesPerPulseRemainder;
	int _updatesPerPulse;
	int _timerSetting;
	int8 _tempoMode1PulseCounter;
	int _tempoModifier;
	uint32 _bar;
	uint32 _parseToBar;
	int8 _tempoMode1UpdateF8;
	uint8 _deltaTicks;
	uint32 _beat;
	uint8 _defaultTempo;
	int _trackTempo;

	bool _loop;
	bool _playing;
	bool _endOfTrack;
	bool _paused;

	const uint8 *_musicStart;
	const uint8 *_musicPos;
	uint32 _musicTrackSize;

	EuphonyDriver *_eupDriver;
	EuphonyBaseDriver *_drivers[3];
};

#endif