ScummVM API documentation
sound.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 MADS_SOUND_H
23 #define MADS_SOUND_H
24 
25 #include "common/array.h"
26 #include "common/file.h"
27 #include "common/mutex.h"
28 #include "common/queue.h"
29 
30 namespace Audio {
31 class Mixer;
32 }
33 
34 namespace OPL {
35 class OPL;
36 }
37 
38 namespace MADS {
39 
40 class ASound;
41 
42 class SoundManager {
43 protected:
44  Audio::Mixer *_mixer;
45  bool &_soundFlag;
46  OPL::OPL *_opl = nullptr;
47  ASound *_driver = nullptr;
48  bool _pollSoundEnabled = false;
49  bool _soundPollFlag = false;
50  bool _newSoundsPaused = false;
51  Common::Queue<int> _queuedCommands;
52  int _masterVolume = 255;
53 
54 protected:
59  virtual void loadDriver(int sectionNum) = 0;
60 
61 public:
62  SoundManager(Audio::Mixer *mixer, bool &soundFlag);
63  virtual ~SoundManager();
64 
65  virtual void validate() = 0;
66 
67  bool _preferRoland;
68 
72  void init(int sectionNumber);
73 
77  void closeDriver();
78 
82  void removeDriver();
83 
88  void setEnabled(bool flag);
89 
93  void pauseNewCommands();
94 
98  void startQueuedCommands();
99 
103  void setVolume(int volume);
104 
106 
111  int command(int commandId, int param = 0);
112 
116  void stop();
117 
122  void noise();
123 
125 };
126 
127 
132 public:
133  ASound *_owner;
134 
135  int _activeCount;
136  int _field1;
137  int _field2;
138  int _field3;
139  int _field4;
140  int _sampleIndex;
141  int _volume;
142  int _field7;
143  int _field8;
144  int _field9;
145  int _fieldA;
146  uint8 _fieldB;
147  int _fieldC;
148  int _fieldD;
149  int _fieldE;
150  byte *_ptr1;
151  byte *_pSrc;
152  byte *_ptr3;
153  byte *_ptr4;
154  byte *_ptrEnd;
155  int _field17;
156  int _field19;
157  byte *_soundData;
158  int _field1D;
159  int _volumeOffset;
160  int _field1F;
161 
162  // TODO: Only used by asound.003. Figure out usage
163  byte _field20;
164 
165  // Phantom-specific fields
166  int _field26; // pitch delta (set in case -14, zeroed in case -3)
167  int _field28; // zeroed in case -3
168  int _field2A; // set in case -18
169  int _field2B; // volume-cap flag (suppresses upward volume changes)
170  int _field2C; // frequency counter (used with _field7 in cases -9/-10)
171 public:
172  static bool _channelsEnabled;
173 public:
174  AdlibChannel();
175 
176  void reset();
177  void enable(int flag);
178  void setPtr2(byte *pData);
179  void load(byte *pData);
180  void check(byte *nullPtr);
181 };
182 
184 public:
185  int _field0;
186  int _freqMask;
187  int _freqBase;
188  int _field6;
189 };
190 
191 class AdlibSample {
192 public:
193  int _attackRate;
194  int _decayRate;
195  int _sustainLevel;
196  int _releaseRate;
197  bool _egTyp;
198  bool _ksr;
199  int _totalLevel;
200  int _scalingLevel;
201  int _waveformSelect;
202  int _freqMultiple;
203  int _feedback;
204  bool _ampMod;
205  int _vib;
206  int _alg;
207  int _fieldE;
208  int _freqMask;
209  int _freqBase;
210  int _field14;
211 
212  AdlibSample() {
213  }
215 };
216 
218  uint8 _regNum;
219  uint8 _value;
220 
221  RegisterValue(int regNum, int value) {
222  _regNum = regNum; _value = value;
223  }
224 };
225 
226 #define ADLIB_CHANNEL_COUNT 9
227 #define ADLIB_CHANNEL_MIDWAY 5
228 #define CALLBACKS_PER_SECOND 60
229 
231  int _offset;
232  byte *_data;
233  byte *_dataEnd;
234 };
235 
239 class ASound {
240 private:
242  uint16 _randomSeed;
243  int _masterVolume;
244 
248  void adlibInit();
249 
253  void update();
254 
258  void pollChannels();
259 
263  void checkChannels();
264 
268  void pollActiveChannel();
269 
273  void updateOctave();
274 
275  void updateChannelState();
276  void updateActiveChannel();
277 
281  void processSample();
282 
283  void updateFNumber();
284 
288  void onTimer();
289 
290 protected:
291  int _chanCommandCount;
292  int _commandParam;
293 
294  virtual void channelCommand(byte *&pSrc, bool &updateFlag) = 0;
295 
299  void loadSample(int sampleIndex);
300 
304  void write(int reg, int val);
305 
309  int write2(int state, int reg, int val);
310 
314  void flush();
315 
319  void channelOn(int reg, int volume);
320 
324  void channelOff(int reg);
325 
329  void resultCheck();
330 
335  byte *loadData(int offset, int size);
336 
342  void playSound(int offset, int size);
343 
349  void playSoundData(byte *pData, int startingChannel = ADLIB_CHANNEL_MIDWAY);
350 
354  bool isSoundActive(byte *pData);
355 
359  void setFrequency(int channel, int freq);
360 
364  int getRandomNumber();
365 
370  byte *getDataPtr(int nearPtr);
371 
372  virtual int command0();
373  int command1();
374  int command2();
375  int command3();
376  int command4();
377  int command5();
378  int command6();
379  int command7();
380  int command8();
381 
382  int nullCommand() {
383  return 0;
384  }
385 public:
386  Audio::Mixer *_mixer;
387  OPL::OPL *_opl;
388  AdlibChannel _channels[ADLIB_CHANNEL_COUNT];
389  AdlibChannel *_activeChannelPtr;
390  AdlibChannelData _channelData[11];
392  AdlibSample *_samplePtr;
393  Common::File _soundFile;
395  Common::Mutex _driverMutex;
396  int _dataOffset;
397  int _frameCounter;
398  bool _isDisabled;
399  int _v1;
400  int _v2;
401  int _activeChannelNumber;
402  int _freqMask1;
403  int _freqMask2;
404  int _freqBase1;
405  int _freqBase2;
406  int _channelNum1, _channelNum2;
407  int _v7;
408  int _v8;
409  int _v9;
410  int _v10;
411  int _pollResult;
412  int _resultFlag;
413  byte _nullData[2];
414  int _ports[256];
415  bool _stateFlag;
416  int _activeChannelReg;
417  int _v11;
418  bool _amDep, _vibDep, _splitPoint;
419 public:
427  ASound(Audio::Mixer *mixer, OPL::OPL *opl, const Common::Path &filename, int dataOffset);
428 
432  virtual ~ASound();
433 
437  static void validate();
438 
445  virtual int command(int commandId, int param) = 0;
446 
450  int stop();
451 
455  int poll();
456 
460  void noise();
461 
466  return _frameCounter;
467  }
468 
472  CachedDataEntry &getCachedData(byte *pData);
473 
477  void setVolume(int volume);
478 };
479 
480 } // namespace MADS
481 
482 #endif
Definition: sound.h:191
Definition: sound.h:217
Definition: array.h:52
Definition: list.h:44
Definition: path.h:52
Definition: stream.h:745
Definition: sound.h:183
Definition: mixer.h:70
Definition: file.h:47
Definition: sound.h:230
Definition: sound.h:42
Definition: mutex.h:67
Definition: sound.h:239
Definition: fmopl.h:35
Definition: action.h:28
Definition: system.h:38
Definition: sound.h:131
int getFrameCounter()
Definition: sound.h:465
Definition: fmopl.h:116