ScummVM API documentation
player_ad.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 SCUMM_PLAYERS_PLAYER_AD_H
23 #define SCUMM_PLAYERS_PLAYER_AD_H
24 
25 #include "scumm/music.h"
26 
27 #include "common/mutex.h"
28 #include "common/serializer.h"
29 
30 namespace OPL {
31 class OPL;
32 }
33 
34 namespace Scumm {
35 
36 class ScummEngine;
37 
41 class Player_AD : public MusicEngine {
42 public:
43  Player_AD(ScummEngine *scumm, Common::Mutex &mutex);
44  ~Player_AD() override;
45 
46  // MusicEngine API
47  void setMusicVolume(int vol) override;
48  void startSound(int sound) override;
49  void stopSound(int sound) override;
50  void stopAllSounds() override;
51  int getMusicTimer() override;
52  int getSoundStatus(int sound) const override;
53 
54  void saveLoadWithSerializer(Common::Serializer &ser) override;
55 
56  // Timer callback
57  void onTimer();
58 
59 private:
60  ScummEngine *const _vm;
61  Common::Mutex &_mutex;
62 
63  void setupVolume();
64  int _musicVolume;
65  int _sfxVolume;
66 
67  OPL::OPL *_opl2;
68 
69  int _musicResource;
70  int32 _engineMusicTimer;
71 
72  struct SfxSlot;
73 
74  struct HardwareChannel {
75  bool allocated;
76  int priority;
77  SfxSlot *sfxOwner;
78  } _hwChannels[9];
79  int _numHWChannels;
80  static const int _operatorOffsetToChannel[22];
81 
82  int allocateHWChannel(int priority, SfxSlot *owner = nullptr);
83  void freeHWChannel(int channel);
84  void limitHWChannels(int newCount);
85 
86  // AdLib register utilities
87  uint8 _registerBackUpTable[256];
88  void writeReg(int r, int v);
89  uint8 readReg(int r) const;
90 
91  // Instrument setup
92  void setupChannel(const uint channel, const byte *instrOffset);
93  void setupOperator(const uint opr, const byte *&instrOffset);
94  static const int _operatorOffsetTable[18];
95 
96  // Music handling
97  void startMusic();
98  void stopMusic();
99  void updateMusic();
100  bool parseCommand();
101  uint parseVLQ();
102  void noteOff(uint channel);
103  void setupFrequency(uint channel, int8 frequency);
104  void setupRhythm(uint rhythmInstr, uint instrOffset);
105 
106  const byte *_musicData;
107  uint _timerLimit;
108  uint _musicTicks;
109  uint32 _musicTimer;
110  uint32 _internalMusicTimer;
111  bool _loopFlag;
112  uint _musicLoopStart;
113  uint _instrumentOffset[16];
114 
115  struct VoiceChannel {
116  uint lastEvent;
117  uint frequency;
118  uint b0Reg;
119  } _voiceChannels[9];
120  void freeVoiceChannel(uint channel);
121 
122  void musicSeekTo(const uint position);
123  bool _isSeeking;
124 
125  uint _mdvdrState;
126 
127  uint32 _curOffset;
128  uint32 _nextEventTimer;
129 
130  static const uint _noteFrequencies[12];
131  static const uint _mdvdrTable[6];
132  static const uint _rhythmOperatorTable[6];
133  static const uint _rhythmChannelTable[6];
134 
135  // SFX handling
136  enum {
137  kNoteStatePreInit = -1,
138  kNoteStateAttack = 0,
139  kNoteStateDecay = 1,
140  kNoteStateSustain = 2,
141  kNoteStateRelease = 3,
142  kNoteStateOff = 4
143  };
144 
145  struct Note {
146  int state;
147  int playTime;
148  int sustainTimer;
149  int instrumentValue;
150  int bias;
151  int preIncrease;
152  int adjust;
153 
154  struct Envelope {
155  int stepIncrease;
156  int step;
157  int stepCounter;
158  int timer;
159  } envelope;
160  };
161 
162  enum {
163  kChannelStateOff = 0,
164  kChannelStateParse = 1,
165  kChannelStatePlay = 2
166  };
167 
168  struct Channel {
169  int state;
170  const byte *currentOffset;
171  const byte *startOffset;
172  uint8 instrumentData[7];
173 
174  Note notes[2];
175 
176  int hardwareChannel;
177  };
178 
179  struct SfxSlot {
180  int resource;
181  int priority;
182 
183  Channel channels[3];
184  } _sfx[3];
185 
186  SfxSlot *allocateSfxSlot(int priority);
187  bool startSfx(SfxSlot *sfx, const byte *resource);
188  void stopSfx(SfxSlot *sfx);
189 
190  void updateSfx();
191  void clearChannel(const Channel &channel);
192  void updateChannel(Channel *channel);
193  void parseSlot(Channel *channel);
194  void updateSlot(Channel *channel);
195  void parseNote(Note *note, const Channel &channel, const byte *offset);
196  bool processNote(Note *note, const Channel &channel, const byte *offset);
197  void noteOffOn(int channel);
198  void writeRegisterSpecial(int channel, uint8 value, int offset);
199  uint8 readRegisterSpecial(int channel, uint8 defaultValue, int offset);
200  void setupNoteEnvelopeState(Note *note, int steps, int adjust);
201  bool processNoteEnvelope(Note *note);
202 
203  int _sfxTimer;
204 
205  uint8 _rndSeed;
206  uint8 getRnd();
207 
208  static const uint _noteBiasTable[7];
209  static const uint _numStepsTable[16];
210  static const uint _noteAdjustScaleTable[7];
211  static const uint _noteAdjustTable[16];
212  static const bool _useOperatorTable[7];
213  static const uint _channelOffsetTable[11];
214  static const uint _channelOperatorOffsetTable[7];
215  static const uint _baseRegisterTable[7];
216  static const uint _registerMaskTable[7];
217  static const uint _registerShiftTable[7];
218 };
219 
220 } // End of namespace Scumm
221 
222 #endif
Definition: player_ad.h:41
Definition: music.h:40
Definition: serializer.h:79
Definition: scumm.h:518
Definition: player_ad.h:154
Definition: mutex.h:67
Definition: fmopl.h:35
Definition: actor.h:30
Definition: fmopl.h:115