aboutsummaryrefslogtreecommitdiff
path: root/engines/agos/drivers/accolade/adlib.h
blob: c044d2e05a41d6f14d54bfc62ca92f80983bbe3e (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
/* 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.
 *
 */

#include "agos/drivers/accolade/mididriver.h"

#include "audio/fmopl.h"
#include "audio/mididrv.h"

namespace AGOS {

#define AGOS_ADLIB_VOICES_COUNT 11

struct InstrumentEntry {
	byte reg20op1; // Amplitude Modulation / Vibrato / Envelope Generator Type / Keyboard Scaling Rate / Modulator Frequency Multiple
	byte reg40op1; // Level Key Scaling / Total Level
	byte reg60op1; // Attack Rate / Decay Rate
	byte reg80op1; // Sustain Level / Release Rate
	byte reg20op2; // Amplitude Modulation / Vibrato / Envelope Generator Type / Keyboard Scaling Rate / Modulator Frequency Multiple
	byte reg40op2; // Level Key Scaling / Total Level
	byte reg60op2; // Attack Rate / Decay Rate
	byte reg80op2; // Sustain Level / Release Rate
	byte regC0;    // Feedback / Algorithm, bit 0 - set -> both operators in use
};

class MidiDriver_Accolade_AdLib : public MidiDriver {
public:
	MidiDriver_Accolade_AdLib();
	virtual ~MidiDriver_Accolade_AdLib();

	// MidiDriver
	int open();
	void close();
	void send(uint32 b);
	MidiChannel *allocateChannel() { return NULL; }
	MidiChannel *getPercussionChannel() { return NULL; }

	bool isOpen() const { return _isOpen; }
	uint32 getBaseTempo() { return 1000000 / OPL::OPL::kDefaultCallbackFrequency; }

	void setVolume(byte volume);
	virtual uint32 property(int prop, uint32 param);

	bool setupInstruments(byte *instrumentData, uint16 instrumentDataSize, bool useMusicDrvFile);

	void setTimerCallback(void *timerParam, Common::TimerManager::TimerProc timerProc);

private:
	bool _musicDrvMode;

	// from INSTR.DAT/MUSIC.DRV - simple mapping between MIDI channel and MT32 channel
	byte _channelMapping[AGOS_MIDI_CHANNEL_COUNT];
	// from INSTR.DAT/MUSIC.DRV - simple mapping between MIDI instruments and MT32 instruments
	byte _instrumentMapping[AGOS_MIDI_INSTRUMENT_COUNT];
	// from INSTR.DAT/MUSIC.DRV - volume adjustment per instrument
	signed char _instrumentVolumeAdjust[AGOS_MIDI_INSTRUMENT_COUNT];
	// simple mapping between MIDI key notes and MT32 key notes
	byte _percussionKeyNoteMapping[AGOS_MIDI_KEYNOTE_COUNT];

	// from INSTR.DAT/MUSIC.DRV - adlib instrument data
	InstrumentEntry *_instrumentTable;
	byte            _instrumentCount;

	struct ChannelEntry {
		const  InstrumentEntry *currentInstrumentPtr;
		byte   currentNote;
		byte   currentA0hReg;
		byte   currentB0hReg;
		int16  volumeAdjust;
		byte   velocity;

		ChannelEntry() : currentInstrumentPtr(NULL), currentNote(0),
						currentA0hReg(0), currentB0hReg(0), volumeAdjust(0), velocity(0) { }
	};

	byte _percussionReg;

	OPL::OPL *_opl;
	int _masterVolume;

	Common::TimerManager::TimerProc _adlibTimerProc;
	void *_adlibTimerParam;

	bool _isOpen;

	// stores information about all FM voice channels
	ChannelEntry _channels[AGOS_ADLIB_VOICES_COUNT];

	void onTimer();

	void resetAdLib();
	void resetAdLibOperatorRegisters(byte baseRegister, byte value);
	void resetAdLibFMVoiceChannelRegisters(byte baseRegister, byte value);

	void programChange(byte FMvoiceChannel, byte mappedInstrumentNr, byte MIDIinstrumentNr);
	void programChangeSetInstrument(byte FMvoiceChannel, byte mappedInstrumentNr, byte MIDIinstrumentNr);
	void setRegister(int reg, int value);
	void noteOn(byte FMvoiceChannel, byte note, byte velocity);
	void noteOnSetVolume(byte FMvoiceChannel, byte operatorReg, byte adjustedVelocity);
	void noteOff(byte FMvoiceChannel, byte note, bool dontCheckNote);
};

} // End of namespace AGOS