ScummVM API documentation
mame.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  * LGPL licensed version of MAMEs fmopl (V0.37a modified) by
21  * Tatsuyuki Satoh. Included from LGPL'ed AdPlug.
22  *
23  */
24 
25 
26 #ifndef AUDIO_SOFTSYNTH_OPL_MAME_H
27 #define AUDIO_SOFTSYNTH_OPL_MAME_H
28 
29 #include "common/scummsys.h"
30 #include "common/random.h"
31 
32 #include "audio/fmopl.h"
33 
34 namespace OPL {
35 namespace MAME {
36 
37 enum {
38  FMOPL_ENV_BITS_HQ = 16,
39  FMOPL_ENV_BITS_MQ = 8,
40  FMOPL_ENV_BITS_LQ = 8,
41  FMOPL_EG_ENT_HQ = 4096,
42  FMOPL_EG_ENT_MQ = 1024,
43  FMOPL_EG_ENT_LQ = 128
44 };
45 
46 
47 typedef void (*OPL_TIMERHANDLER)(int channel,double interval_Sec);
48 typedef void (*OPL_IRQHANDLER)(int param,int irq);
49 typedef void (*OPL_UPDATEHANDLER)(int param,int min_interval_us);
50 
51 #define OPL_TYPE_WAVESEL 0x01 /* waveform select */
52 
53 /* Saving is necessary for member of the 'R' mark for suspend/resume */
54 /* ---------- OPL one of slot ---------- */
55 typedef struct fm_opl_slot {
56  int TL; /* total level :TL << 8 */
57  int TLL; /* adjusted now TL */
58  uint8 KSR; /* key scale rate :(shift down bit) */
59  int *AR; /* attack rate :&AR_TABLE[AR<<2] */
60  int *DR; /* decay rate :&DR_TABLE[DR<<2] */
61  int SL; /* sustain level :SL_TABLE[SL] */
62  int *RR; /* release rate :&DR_TABLE[RR<<2] */
63  uint8 ksl; /* keyscale level :(shift down bits) */
64  uint8 ksr; /* key scale rate :kcode>>KSR */
65  uint mul; /* multiple :ML_TABLE[ML] */
66  uint Cnt; /* frequency count */
67  uint Incr; /* frequency step */
68 
69  /* envelope generator state */
70  uint8 eg_typ;/* envelope type flag */
71  uint8 evm; /* envelope phase */
72  int evc; /* envelope counter */
73  int eve; /* envelope counter end point */
74  int evs; /* envelope counter step */
75  int evsa; /* envelope step for AR :AR[ksr] */
76  int evsd; /* envelope step for DR :DR[ksr] */
77  int evsr; /* envelope step for RR :RR[ksr] */
78 
79  /* LFO */
80  uint8 ams; /* ams flag */
81  uint8 vib; /* vibrate flag */
82  /* wave selector */
83  int **wavetable;
84 } OPL_SLOT;
85 
86 /* ---------- OPL one of channel ---------- */
87 typedef struct fm_opl_channel {
88  OPL_SLOT SLOT[2];
89  uint8 CON; /* connection type */
90  uint8 FB; /* feed back :(shift down bit)*/
91  int *connect1; /* slot1 output pointer */
92  int *connect2; /* slot2 output pointer */
93  int op1_out[2]; /* slot1 output for selfeedback */
94 
95  /* phase generator state */
96  uint block_fnum; /* block+fnum */
97  uint8 kcode; /* key code : KeyScaleCode */
98  uint fc; /* Freq. Increment base */
99  uint ksl_base; /* KeyScaleLevel Base step */
100  uint8 keyon; /* key on/off flag */
101 } OPL_CH;
102 
103 /* OPL state */
104 typedef struct fm_opl_f {
105  uint8 type; /* chip type */
106  int clock; /* master clock (Hz) */
107  int rate; /* sampling rate (Hz) */
108  double freqbase; /* frequency base */
109  double TimerBase; /* Timer base time (==sampling time) */
110  uint8 address; /* address register */
111  uint8 status; /* status flag */
112  uint8 statusmask; /* status mask */
113  uint mode; /* Reg.08 : CSM , notesel,etc. */
114 
115  /* Timer */
116  int T[2]; /* timer counter */
117  uint8 st[2]; /* timer enable */
118 
119  /* FM channel slots */
120  OPL_CH *P_CH; /* pointer of CH */
121  int max_ch; /* maximum channel */
122 
123  /* Rhythm sention */
124  uint8 rhythm; /* Rhythm mode , key flag */
125 
126  /* time tables */
127  int AR_TABLE[76]; /* atttack rate tables */
128  int DR_TABLE[76]; /* decay rate tables */
129  uint FN_TABLE[1024];/* fnumber -> increment counter */
130 
131  /* LFO */
132  int *ams_table;
133  int *vib_table;
134  int amsCnt;
135  int amsIncr;
136  int vibCnt;
137  int vibIncr;
138 
139  /* wave selector enable flag */
140  uint8 wavesel;
141 
142  /* external event callback handler */
143  OPL_TIMERHANDLER TimerHandler; /* TIMER handler */
144  int TimerParam; /* TIMER parameter */
145  OPL_IRQHANDLER IRQHandler; /* IRQ handler */
146  int IRQParam; /* IRQ parameter */
147  OPL_UPDATEHANDLER UpdateHandler; /* stream update handler */
148  int UpdateParam; /* stream update parameter */
149 
151 } FM_OPL;
152 
153 /* ---------- Generic interface section ---------- */
154 #define OPL_TYPE_YM3526 (0)
155 #define OPL_TYPE_YM3812 (OPL_TYPE_WAVESEL)
156 
157 void OPLBuildTables(int ENV_BITS_PARAM, int EG_ENT_PARAM);
158 
159 FM_OPL *OPLCreate(int type, int clock, int rate);
160 void OPLDestroy(FM_OPL *OPL);
161 void OPLSetTimerHandler(FM_OPL *OPL, OPL_TIMERHANDLER TimerHandler, int channelOffset);
162 void OPLSetIRQHandler(FM_OPL *OPL, OPL_IRQHANDLER IRQHandler, int param);
163 void OPLSetUpdateHandler(FM_OPL *OPL, OPL_UPDATEHANDLER UpdateHandler, int param);
164 
165 void OPLResetChip(FM_OPL *OPL);
166 int OPLWrite(FM_OPL *OPL, int a, int v);
167 unsigned char OPLRead(FM_OPL *OPL, int a);
168 int OPLTimerOver(FM_OPL *OPL, int c);
169 void OPLWriteReg(FM_OPL *OPL, int r, int v);
170 void YM3812UpdateOne(FM_OPL *OPL, int16 *buffer, int length);
171 
172 // Factory method
173 FM_OPL *makeAdLibOPL(int rate);
174 
175 // OPL API implementation
176 class OPL : public ::OPL::OPL, public Audio::EmulatedChip {
177 private:
178  FM_OPL *_opl;
179 public:
180  OPL() : _opl(0) {}
181  ~OPL();
182 
183  bool init();
184  void reset();
185 
186  void write(int a, int v);
187 
188  void writeReg(int r, int v);
189 
190  bool isStereo() const { return false; }
191 
192 protected:
193  void generateSamples(int16 *buffer, int length);
194 };
195 
196 } // End of namespace MAME
197 } // End of namespace OPL
198 
199 #endif
Definition: mame.h:55
bool isStereo() const
Definition: mame.h:190
Definition: random.h:44
Definition: mame.h:87
Definition: chip.h:112
Definition: fmopl.h:35
Definition: mame.h:104
Definition: fmopl.h:115