ScummVM API documentation
fmopl.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  */
21 
22 #ifndef AUDIO_FMOPL_H
23 #define AUDIO_FMOPL_H
24 
25 #include "audio/audiostream.h"
26 
27 #include "common/func.h"
28 #include "common/ptr.h"
29 #include "common/scummsys.h"
30 
31 namespace Audio {
32 class SoundHandle;
33 }
34 
35 namespace Common {
36 class String;
37 }
38 
39 namespace OPL {
40 
41 class OPL;
42 
51 class Config {
52 public:
53  enum OplFlags {
54  kFlagOpl2 = (1 << 0),
55  kFlagDualOpl2 = (1 << 1),
56  kFlagOpl3 = (1 << 2)
57  };
58 
62  enum OplType {
63  kOpl2,
64  kDualOpl2,
65  kOpl3
66  };
67 
68  typedef int8 DriverId;
70  const char *name;
71  const char *description;
72 
73  DriverId id; // A unique ID for each driver
74  uint32 flags; // Capabilities of this driver
75  };
76 
81  static const EmulatorDescription *getAvailable() { return _drivers; }
82 
86  static DriverId parse(const Common::String &name);
87 
92  static const EmulatorDescription *findDriver(DriverId id);
93 
99  static DriverId detect(OplType type);
100 
104  static OPL *create(DriverId driver, OplType type);
105 
110  static OPL *create(OplType type = kOpl2);
111 
112 private:
113  static const EmulatorDescription _drivers[];
114 };
115 
120 
124 class OPL {
125 private:
126  static bool _hasInstance;
127 public:
128  OPL();
129  virtual ~OPL() { _hasInstance = false; }
130 
136  virtual bool init() = 0;
137 
141  virtual void reset() = 0;
142 
149  virtual void write(int a, int v) = 0;
150 
157  virtual byte read(int a) = 0;
158 
168  virtual void writeReg(int r, int v) = 0;
169 
173  void start(TimerCallback *callback, int timerFrequency = kDefaultCallbackFrequency);
174 
178  void stop();
179 
184  virtual void setCallbackFrequency(int timerFrequency) = 0;
185 
186  enum {
190  kDefaultCallbackFrequency = 250
191  };
192 
193 protected:
200  void initDualOpl2OnOpl3(Config::OplType oplType);
201 
214  bool emulateDualOpl2OnOpl3(int r, int v, Config::OplType oplType);
215 
219  virtual void startCallbacks(int timerFrequency) = 0;
220 
224  virtual void stopCallbacks() = 0;
225 
230 
231  bool _rhythmMode;
232  int _connectionFeedbackValues[3];
233 };
234 
241 class RealOPL : public OPL {
242 public:
243  RealOPL();
244  virtual ~RealOPL();
245 
246  // OPL API
247  void setCallbackFrequency(int timerFrequency);
248 
249 protected:
250  // OPL API
251  void startCallbacks(int timerFrequency);
252  void stopCallbacks();
253  virtual void onTimer();
254 
255 private:
256  static void timerProc(void *refCon);
257 
258  uint _baseFreq;
259  uint _remainingTicks;
260 
261  enum {
262  kMaxFreq = 100
263  };
264 };
265 
272 class EmulatedOPL : public OPL, protected Audio::AudioStream {
273 public:
274  EmulatedOPL();
275  virtual ~EmulatedOPL();
276 
277  // OPL API
278  void setCallbackFrequency(int timerFrequency);
279 
280  // AudioStream API
281  int readBuffer(int16 *buffer, const int numSamples);
282  int getRate() const;
283  bool endOfData() const { return false; }
284 
285 protected:
286  // OPL API
287  void startCallbacks(int timerFrequency);
288  void stopCallbacks();
289 
300  virtual void generateSamples(int16 *buffer, int numSamples) = 0;
301 
302 private:
303  int _baseFreq;
304 
305  enum {
306  FIXP_SHIFT = 16
307  };
308 
309  int _nextTick;
310  int _samplesPerTick;
311 
312  Audio::SoundHandle *_handle;
313 };
315 } // End of namespace OPL
316 
317 #endif
Definition: str.h:59
Common::Functor0< void > TimerCallback
Definition: fmopl.h:119
static const EmulatorDescription * getAvailable()
Definition: fmopl.h:81
Definition: ptr.h:572
Definition: fmopl.h:51
Definition: mixer.h:49
OplType
Definition: fmopl.h:62
Definition: algorithm.h:29
Definition: audiostream.h:50
Definition: fmopl.h:241
bool endOfData() const
Definition: fmopl.h:283
Definition: fmopl.h:39
Definition: fmopl.h:69
Definition: fmopl.h:272
Definition: system.h:37
Common::ScopedPtr< TimerCallback > _callback
Definition: fmopl.h:229