1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef AGOS_SIMON1_ADLIB_H
24 #define AGOS_SIMON1_ADLIB_H
25 
26 #include "audio/mididrv.h"
27 #include "audio/fmopl.h"
28 
29 namespace AGOS {
30 
31 class MidiDriver_Simon1_AdLib : public MidiDriver {
32 public:
33 	MidiDriver_Simon1_AdLib(const byte *instrumentData);
34 	~MidiDriver_Simon1_AdLib() override;
35 
36 	// MidiDriver API
37 	int open() override;
38 	bool isOpen() const override;
39 	void close() override;
40 
41 	void send(uint32 b) override;
42 
43 	void setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) override;
44 	uint32 getBaseTempo() override;
45 
allocateChannel()46 	MidiChannel *allocateChannel() override { return 0; }
getPercussionChannel()47 	MidiChannel *getPercussionChannel() override { return 0; }
48 private:
49 	bool _isOpen;
50 
51 	OPL::OPL *_opl;
52 
53 	Common::TimerManager::TimerProc _timerProc;
54 	void *_timerParam;
55 	void onTimer();
56 
57 	void reset();
58 	void resetOPLVoices();
59 
60 	void resetRhythm();
61 	int _melodyVoices;
62 	uint8 _amvdrBits;
63 	bool _rhythmEnabled;
64 
65 	enum {
66 		kNumberOfVoices = 11,
67 		kNumberOfMidiChannels = 16
68 	};
69 
70 	struct Voice {
71 		Voice();
72 
73 		uint channel;
74 		uint note;
75 		uint instrTotalLevel;
76 		uint instrScalingLevel;
77 		uint frequency;
78 	};
79 
80 	void resetVoices();
81 	int allocateVoice(uint channel);
82 
83 	Voice _voices[kNumberOfVoices];
84 	uint _midiPrograms[kNumberOfMidiChannels];
85 
86 	void noteOff(uint channel, uint note);
87 	void noteOn(uint channel, uint note, uint velocity);
88 	void noteOnRhythm(uint channel, uint note, uint velocity);
89 	void controlChange(uint channel, uint controller, uint value);
90 	void programChange(uint channel, uint program);
91 
92 	void setupInstrument(uint voice, uint instrument);
93 	const byte *_instruments;
94 
95 	static const int _operatorMap[9];
96 	static const int _operatorDefaults[8];
97 
98 	static const int _rhythmOperatorMap[5];
99 	static const uint _rhythmInstrumentMask[5];
100 	static const int _rhythmVoiceMap[5];
101 
102 	static const int _frequencyIndexAndOctaveTable[128];
103 	static const int _frequencyTable[16];
104 
105 	struct RhythmMap {
106 		int channel;
107 		int program;
108 		int note;
109 	};
110 
111 	static const RhythmMap _rhythmMap[39];
112 };
113 
114 MidiDriver *createMidiDriverSimon1AdLib(const char *instrumentFilename);
115 
116 } // End of namespace AGOS
117 
118 #endif
119