ScummVM API documentation
adlib.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  * This file is dual-licensed.
22  * In addition to the GPLv3 license mentioned above, this code is also
23  * licensed under LGPL 2.1. See LICENSES/COPYING.LGPL file for the
24  * full text of the license.
25  *
26  */
27 
28 #ifndef GOB_SOUND_ADLIB_H
29 #define GOB_SOUND_ADLIB_H
30 
31 #include "common/mutex.h"
32 
33 #include "audio/mixer.h"
34 
35 namespace OPL {
36  class OPL;
37 }
38 
39 namespace Gob {
40 
42 class AdLib {
43 public:
44  AdLib(int callbackFrequency);
45  virtual ~AdLib();
46 
47  bool isPlaying() const;
48  int32 getRepeating() const;
49 
56  void setRepeating(int32 repCount);
57 
58  void startPlay();
59  void stopPlay();
60  void syncVolume();
61 
62 protected:
63  enum kVoice {
64  kVoiceMelody0 = 0,
65  kVoiceMelody1 = 1,
66  kVoiceMelody2 = 2,
67  kVoiceMelody3 = 3,
68  kVoiceMelody4 = 4,
69  kVoiceMelody5 = 5,
70  kVoiceMelody6 = 6, // Only available in melody mode.
71  kVoiceMelody7 = 7, // Only available in melody mode.
72  kVoiceMelody8 = 8, // Only available in melody mode.
73  kVoiceBaseDrum = 6, // Only available in percussion mode.
74  kVoiceSnareDrum = 7, // Only available in percussion mode.
75  kVoiceTom = 8, // Only available in percussion mode.
76  kVoiceCymbal = 9, // Only available in percussion mode.
77  kVoiceHihat = 10 // Only available in percussion mode.
78  };
79 
81  enum kParam {
82  kParamKeyScaleLevel = 0,
83  kParamFreqMulti = 1,
84  kParamFeedback = 2,
85  kParamAttack = 3,
86  kParamSustain = 4,
87  kParamSustaining = 5,
88  kParamDecay = 6,
89  kParamRelease = 7,
90  kParamLevel = 8,
91  kParamAM = 9,
92  kParamVib = 10,
93  kParamKeyScaleRate = 11,
94  kParamFM = 12,
95  kParamWaveSelect = 13
96  };
97 
98  static const int kOperatorCount = 18;
99  static const int kParamCount = 14;
100  static const int kPitchStepCount = 25;
101  static const int kOctaveCount = 8;
102  static const int kHalfToneCount = 12;
103 
104  static const int kOperatorsPerVoice = 2;
105 
106  static const int kMelodyVoiceCount = 9;
107  static const int kPercussionVoiceCount = 5;
108  static const int kMaxVoiceCount = 11;
109 
111  static const int kNoteCount = kHalfToneCount * kOctaveCount;
112 
113  static const int kMaxVolume = 0x007F;
114  static const int kMaxPitch = 0x3FFF;
115  static const int kMidPitch = 0x2000;
116 
117  static const int kStandardMidC = 60;
118  static const int kOPLMidC = 48;
119 
120 
121 
123  void writeOPL(byte reg, byte val);
124 
129  void end(bool killRepeat = false);
130 
136  virtual uint32 pollMusic(bool first) = 0;
137 
139  virtual void rewind() = 0;
140 
142  bool isPercussionMode() const;
143 
145  void setPercussionMode(bool percussion);
146 
151  void enableWaveSelect(bool enable);
152 
158  void setPitchRange(uint8 range);
159 
164  void setTremoloDepth(bool tremoloDepth);
165 
170  void setVibratoDepth(bool vibratoDepth);
171 
173  void setKeySplit(bool keySplit);
174 
183  void setVoiceTimbre(uint8 voice, const uint16 *params);
184 
186  void setVoiceVolume(uint8 voice, uint8 volume);
187 
194  void bendVoicePitch(uint8 voice, uint16 pitchBend);
195 
201  void noteOn(uint8 voice, uint8 note);
202 
204  void noteOff(uint8 voice);
205 
209  void setTimerFrequency(int timerFrequency);
210 
211 private:
212  static const uint8 kVolumeTable[Audio::Mixer::kMaxMixerVolume + 1];
213 
214  static const uint8 kOperatorType [kOperatorCount];
215  static const uint8 kOperatorOffset[kOperatorCount];
216  static const uint8 kOperatorVoice [kOperatorCount];
217 
218  static const uint8 kVoiceMelodyOperator [kOperatorsPerVoice][kMelodyVoiceCount];
219  static const uint8 kVoicePercussionOperator[kOperatorsPerVoice][kPercussionVoiceCount];
220 
221  static const byte kPercussionMasks[kPercussionVoiceCount];
222 
223  static const uint16 kPianoParams [kOperatorsPerVoice][kParamCount];
224  static const uint16 kBaseDrumParams [kOperatorsPerVoice][kParamCount];
225 
226  static const uint16 kSnareDrumParams[kParamCount];
227  static const uint16 kTomParams [kParamCount];
228  static const uint16 kCymbalParams [kParamCount];
229  static const uint16 kHihatParams [kParamCount];
230 
231 
232  OPL::OPL *_opl;
233 
234  Common::Mutex _mutex;
235 
236  int _volume;
237 
238  uint32 _toPoll;
239 
240  int32 _repCount;
241 
242  bool _first;
243  bool _playing;
244  bool _ended;
245 
246  bool _tremoloDepth;
247  bool _vibratoDepth;
248  bool _keySplit;
249 
250  bool _enableWaveSelect;
251 
252  bool _percussionMode;
253  byte _percussionBits;
254 
255  uint8 _pitchRange;
256  uint16 _pitchRangeStep;
257 
258  uint8 _voiceNote[kMaxVoiceCount]; // Last note of each voice
259  uint8 _voiceOn [kMaxVoiceCount]; // Whether each voice is currently on
260 
261  uint8 _operatorVolume[kOperatorCount]; // Volume of each operator
262 
263  byte _operatorParams[kOperatorCount][kParamCount]; // All operator parameters
264 
265  uint16 _freqs[kPitchStepCount][kHalfToneCount];
266  uint16 *_freqPtr[kMaxVoiceCount];
267 
268  int _halfToneOffset[kMaxVoiceCount];
269 
270 
271  void createOPL();
272  void initOPL();
273 
274  void reset();
275  void allOff();
276 
277  // Write global parameters into the OPL
278  void writeTremoloVibratoDepthPercMode();
279  void writeKeySplit();
280 
281  // Write operator parameters into the OPL
282  void writeWaveSelect(uint8 oper);
283  void writeTremoloVibratoSustainingKeyScaleRateFreqMulti(uint8 oper);
284  void writeSustainRelease(uint8 oper);
285  void writeAttackDecay(uint8 oper);
286  void writeFeedbackFM(uint8 oper);
287  void writeKeyScaleLevelVolume(uint8 oper);
288  void writeAllParams(uint8 oper);
289 
290  void initOperatorParams();
291  void initOperatorVolumes();
292  void setOperatorParams(uint8 oper, const uint16 *params, uint8 wave);
293 
294  void voiceOff(uint8 voice);
295 
296  void initFreqs();
297  void setFreqs(uint16 *freqs, int32 num, int32 denom);
298  int32 calcFreq(int32 deltaDemiToneNum, int32 deltaDemiToneDenom);
299  void resetFreqs();
300 
301  void changePitch(uint8 voice, uint16 pitchBend);
302 
303  void setFreq(uint8 voice, uint16 note, bool on);
304 
308  void onTimer();
309 };
310 
311 } // End of namespace Gob
312 
313 #endif // GOB_SOUND_ADLIB_H
Definition: mixer.h:72
Definition: anifile.h:40
Definition: mutex.h:67
kParam
Definition: adlib.h:81
Definition: fmopl.h:35
Definition: adlib.h:42
Definition: fmopl.h:115