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 TSAGE_SOUND_H
23 #define TSAGE_SOUND_H
24 
25 #include "common/scummsys.h"
26 #include "common/mutex.h"
27 #include "common/queue.h"
28 #include "audio/mixer.h"
29 #include "common/list.h"
30 #include "tsage/saveload.h"
31 #include "tsage/core.h"
32 
33 namespace Audio {
34 class QueuingAudioStream;
35 }
36 
37 namespace OPL {
38 class OPL;
39 }
40 
41 namespace TsAGE {
42 
43 class Sound;
44 
45 #define SOUND_ARR_SIZE 16
46 #define ROLAND_DRIVER_NUM 2
47 #define ADLIB_DRIVER_NUM 3
48 #define SBLASTER_DRIVER_NUM 4
49 #define CALLBACKS_PER_SECOND 60
50 
52  int _numTracks;
53  int _chunks[SOUND_ARR_SIZE];
54  int _voiceTypes[SOUND_ARR_SIZE];
55 };
56 
57 enum SoundDriverStatus {SNDSTATUS_FAILED = 0, SNDSTATUS_DETECTED = 1, SNDSTATUS_SKIPPED = 2};
58 enum VoiceType {VOICETYPE_0 = 0, VOICETYPE_1 = 1};
59 
61 public:
62  int _driverNum;
63  SoundDriverStatus _status;
64  int _field2, _field6;
65  Common::String _shortDescription;
66  Common::String _longDescription;
67 };
68 
69 struct GroupData {
70  uint32 _groupMask;
71  const byte *_pData;
72 };
73 
74 struct RegisterValue {
75  uint8 _regNum;
76  uint8 _value;
77 
78  RegisterValue(int regNum, int value) {
79  _regNum = regNum; _value = value;
80  }
81 };
82 
83 class SoundDriver {
84 public:
85  Common::String _shortDescription, _longDescription;
86  int _minVersion, _maxVersion;
87  // The following fields were originally held in separate arrays in the SoundManager class
88  uint32 _groupMask;
89  const GroupData *_groupOffset;
90  int _driverResID;
91 public:
92  SoundDriver();
93  virtual ~SoundDriver() {}
94 
95  const Common::String &getShortDriverDescription() { return _shortDescription; }
96  const Common::String &getLongDriverDescription() { return _longDescription; }
97 
98  virtual bool open() { return true; } // Method #0
99  virtual void close() {} // Method #1
100  virtual bool reset() { return true; } // Method #2
101  virtual const GroupData *getGroupData() { return NULL; } // Method #3
102  virtual void installPatch(const byte *data, int size) {} // Method #4
103  virtual void poll() {} // Method #5
104  virtual void method6() {} // Method #6
105  virtual int setMasterVolume(int volume) { return 0; } // Method #7
106  virtual void proc16() {} // Method #8
107  virtual void proc18(int al, VoiceType voiceType) {} // Method #9
108  virtual void proc20(int al, VoiceType voiceType) {} // Method #10
109  virtual void proc22(int al, VoiceType voiceType, int v3) {} // Method #11
110  virtual void proc24(int channel, int voiceIndex, Sound *sound, int v1, int v2) {}
111  virtual void setProgram(int channel, int program) {} // Method #13
112  virtual void setVolume1(int channel, int v2, int v3, int volume) {}
113  virtual void setPitchBlend(int channel, int pitchBlend) {} // Method #15
114  virtual void playSound(const byte *channelData, int dataOffset, int program, int channel, int v0, int v1) {}// Method #16
115  virtual void updateVoice(int channel) {} // Method #17
116  virtual void proc36() {} // Method #18
117  virtual void proc38(int channel, int cmd, int value) {} // Method #19
118  virtual void setPitch(int channel, int pitchBlend) {} // Method #20
119  virtual void proc42(int channel, int cmd, int value, int *v1, int *v2) {} // Method #21
120 };
121 
123  Sound *_sound;
124  int _channelNum;
125  int _priority;
126  bool _fieldA;
127  Sound *_sound2;
128  int _channelNum2;
129  int _priority2;
130  bool _field12;
131  Sound *_sound3;
132  int _channelNum3;
133  int _priority3;
134 };
135 
137  int _field4;
138  int _field5;
139  int _field6;
140  Sound *_sound;
141  int _channelNum;
142  int _priority;
143  Sound *_sound2;
144  int _channelNum2;
145  int _priority2;
146  Sound *_sound3;
147  int _channelNum3;
148  int _priority3;
149 };
150 
152  int _voiceNum;
153  int _field1;
154  SoundDriver *_driver;
155 
156  VoiceStructEntryType0 _type0;
157  VoiceStructEntryType1 _type1;
158 };
159 
161 public:
162  VoiceType _voiceType;
163  int _total;
164  int _numVoices;
165  int _field3;
166 
168 };
169 
170 class SoundManager : public SaveListener {
171 private:
172  SoundDriver *instantiateDriver(int driverNum);
173 public:
174  bool _sndmgrReady;
175  int _ourSndResVersion, _ourDrvResVersion;
176  SynchronizedList<Sound *> _playList;
177  Common::List<SoundDriver *> _installedDrivers;
178  VoiceTypeStruct *_voiceTypeStructPtrs[SOUND_ARR_SIZE];
179  uint32 _groupsAvail;
180  int _masterVol;
181  int _newVolume;
182  Common::Mutex _serverDisabledMutex;
183  Common::Mutex _serverSuspendedMutex;
184  bool _driversDetected;
185  SynchronizedList<Sound *> _soundList;
186  Common::List<SoundDriverEntry> _availableDrivers;
187  bool _needToRethink;
188  // Misc flags
189  bool _soTimeIndexFlag;
190 public:
191  SoundManager();
192  ~SoundManager() override;
193 
194  void dispatch();
195  void listenerSynchronize(Serializer &s) override;
196  virtual void postInit();
197  void syncSounds();
198 
199  static void saveNotifier(bool postFlag);
200  void saveNotifierProc(bool postFlag);
201  static void loadNotifier(bool postFlag);
202  void loadNotifierProc(bool postFlag);
203 
204  void installConfigDrivers();
205  Common::List<SoundDriverEntry> &buildDriverList(bool detectFlag);
206  Common::List<SoundDriverEntry> &getDriverList(bool detectFlag);
207  void dumpDriverList();
208  void installDriver(int driverNum);
209  bool isInstalled(int driverNum) const;
210  void unInstallDriver(int driverNum);
211  void checkResVersion(const byte *soundData);
212  int determineGroup(const byte *soundData);
213  int extractPriority(const byte *soundData);
214  int extractLoop(const byte *soundData);
215  bool isOnPlayList(Sound *sound);
216  void extractTrackInfo(trackInfoStruct *trackInfo, const byte *soundData, int groupNum);
217  void addToSoundList(Sound *sound);
218  void removeFromSoundList(Sound *sound);
219  void addToPlayList(Sound *sound);
220  void removeFromPlayList(Sound *sound);
221  void rethinkVoiceTypes();
222  void updateSoundVol(Sound *sound);
223  void updateSoundPri(Sound *sound);
224  void updateSoundLoop(Sound *sound);
225  void setMasterVol(int volume);
226  int getMasterVol() const;
227  void loadSound(int soundNum, bool showErrors);
228  void unloadSound(int soundNum);
229  bool isFading();
230 
231  // _sf methods
232  static SoundManager &sfManager();
233  static void sfTerminate();
234  static int sfDetermineGroup(const byte *soundData);
235  static void sfAddToPlayList(Sound *sound);
236  static void sfRemoveFromPlayList(Sound *sound);
237  static bool sfIsOnPlayList(Sound *sound);
238  static void sfRethinkSoundDrivers();
239  static void sfRethinkVoiceTypes();
240  static void sfUpdateVolume(Sound *sound);
241  static void sfDereferenceAll();
242  static void sfUpdatePriority(Sound *sound);
243  static void sfUpdateLoop(Sound *sound);
244  static void sfSetMasterVol(int volume);
245  static void sfExtractTrackInfo(trackInfoStruct *trackInfo, const byte *soundData, int groupNum);
246  static void sfExtractGroupMask();
247  static bool sfInstallDriver(SoundDriver *driver);
248  static void sfUnInstallDriver(SoundDriver *driver);
249  static void sfInstallPatchBank(SoundDriver *driver, const byte *bankData);
250  static void sfDoAddToPlayList(Sound *sound);
251  static bool sfDoRemoveFromPlayList(Sound *sound);
252  static void sfDoUpdateVolume(Sound *sound);
253  static void sfSoundServer(void *);
254  static void sfProcessFading();
255  static void sfUpdateVoiceStructs();
256  static void sfUpdateVoiceStructs2();
257 };
258 
259 class Sound: public EventHandler {
260 private:
261  void _prime(int soundResID, bool dontQueue);
262  void _primeBuffer(const byte *soundData);
263  void _unPrime();
264 public:
265  bool _stoppedAsynchronously;
266  int _soundResID;
267  int _group;
268  int _sndResPriority;
269  int _fixedPriority;
270  int _sndResLoop;
271  int _fixedLoop;
272  int _priority;
273  int _volume;
274  int _loop;
275  int _pausedCount;
276  int _mutedCount;
277  int _hold;
278  int _cueValue;
279  int _fadeDest;
280  int _fadeSteps;
281  int _fadeTicks;
282  int _fadeCounter;
283  bool _stopAfterFadeFlag;
284  uint32 _timer;
285  uint32 _newTimeIndex;
286  int _loopTimer;
287  int _chProgram[SOUND_ARR_SIZE];
288  int _chModulation[SOUND_ARR_SIZE];
289  int _chVolume[SOUND_ARR_SIZE];
290  int _chPan[SOUND_ARR_SIZE];
291  int _chDamper[SOUND_ARR_SIZE];
292  int _chPitchBlend[SOUND_ARR_SIZE];
293  int _chVoiceType[SOUND_ARR_SIZE];
294  int _chNumVoices[SOUND_ARR_SIZE];
295  int _chSubPriority[SOUND_ARR_SIZE];
296  int _chFlags[SOUND_ARR_SIZE];
297  bool _chWork[SOUND_ARR_SIZE];
298  trackInfoStruct _trackInfo;
299  byte *_channelData[SOUND_ARR_SIZE];
300  int _trkChannel[SOUND_ARR_SIZE];
301  int _trkState[SOUND_ARR_SIZE];
302  int _trkLoopState[SOUND_ARR_SIZE];
303  int _trkIndex[SOUND_ARR_SIZE];
304  int _trkLoopIndex[SOUND_ARR_SIZE];
305  int _trkRest[SOUND_ARR_SIZE];
306  int _trkLoopRest[SOUND_ARR_SIZE];
307 
308  bool _primed;
309  bool _isEmpty;
310  byte *_remoteReceiver;
311 public:
312  Sound();
313  ~Sound() override;
314 
315  void synchronize(Serializer &s) override;
316  void orientAfterRestore();
317 
318  void play(int soundResID);
319  void stop();
320  void prime(int soundResID);
321  void unPrime();
322  void go();
323  void halt(void);
324  bool isPlaying();
325  int getSoundNum() const;
326  bool isPrimed() const;
327  bool isPaused() const;
328  bool isMuted() const;
329  void pause(bool flag);
330  void mute(bool flag);
331  void fade(int fadeDest, int fadeSteps, int fadeTicks, bool stopAfterFadeFlag);
332  void setTimeIndex(uint32 timeIndex);
333  uint32 getTimeIndex() const;
334  int getCueValue() const;
335  void setCueValue(int cueValue);
336  void setVol(int volume);
337  int getVol() const;
338  void setPri(int priority);
339  void setLoop(int flag);
340  int getPri() const;
341  int getLoop();
342  void holdAt(int amount);
343  void release();
344  void orientAfterDriverChange();
345 
346  // _so methods
347  void soPrimeSound(bool dontQueue);
348  void soSetTimeIndex(uint timeIndex);
349  bool soServiceTracks();
350  void soPrimeChannelData();
351  void soRemoteReceive();
352  void soServiceTrackType0(int trackIndex, const byte *channelData);
353  void soUpdateDamper(VoiceTypeStruct *voiceType, int channelNum, VoiceType mode, int v0);
354  void soPlaySound(VoiceTypeStruct *vtStruct, const byte *channelData, int channelNum, VoiceType voiceType, int v0, int v1);
355  void soPlaySound2(VoiceTypeStruct *vtStruct, const byte *channelData, int channelNum, VoiceType voiceType, int v0);
356  void soProc38(VoiceTypeStruct *vtStruct, int channelNum, VoiceType voiceType, int cmd, int value);
357  void soProc40(VoiceTypeStruct *vtStruct, int channelNum, int pitchBlend);
358  void soDoTrackCommand(int channelNum, int command, int value);
359  bool soDoUpdateTracks(int command, int value);
360  void soSetTrackPos(int trackIndex, int trackPos, int cueValue);
361 
362  void soServiceTrackType1(int trackIndex, const byte *channelData);
363  int soFindSound(VoiceTypeStruct *vtStruct, int channelNum);
364 };
365 
366 class ASound: public EventHandler {
367 public:
368  Sound _sound;
369  EventHandler *_endAction;
370  int _cueValue;
371 
372  ASound();
373  ~ASound() override;
374  void synchronize(Serializer &s) override;
375  void dispatch() override;
376 
377  void play(int soundNum, EventHandler *endAction = NULL, int volume = 127);
378  void stop();
379  void prime(int soundNum, Action *action = NULL);
380  void unPrime();
381  void go() { _sound.go(); }
382  void hault(void) { _sound.halt(); }
383  bool isPlaying() { return _sound.isPlaying(); }
384  int getSoundNum() const { return _sound.getSoundNum(); }
385  bool isPaused() const { return _sound.isPaused(); }
386  bool isMuted() const { return _sound.isMuted(); }
387  void pause(bool flag) { _sound.pause(flag); }
388  void mute(bool flag) { _sound.mute(flag); }
389  void fade(int fadeDest, int fadeSteps, int fadeTicks, bool stopAfterFadeFlag, EventHandler *endAction);
390  void fadeIn() { fade(127, 5, 10, false, NULL); }
391  void fadeOut(Action *action) { fade(0, 5, 10, true, action); }
392  void setTimeIndex(uint32 timeIndex) { _sound.setTimeIndex(timeIndex); }
393  uint32 getTimeIndex() const { return _sound.getTimeIndex(); }
394  void setPri(int v) { _sound.setPri(v); }
395  void setLoop(int total) { _sound.setLoop(total); }
396  int getPri() const { return _sound.getPri(); }
397  int getLoop() { return _sound.getLoop(); }
398  void setVol(int volume) { _sound.setVol(volume); }
399  int getVol() const { return _sound.getVol(); }
400  void holdAt(int v) { _sound.holdAt(v); }
401  void release() { _sound.release(); }
402  void fadeSound(int soundNum);
403 };
404 
405 class ASoundExt: public ASound {
406 public:
407  int _soundNum;
408 
409  ASoundExt();
410  void fadeOut2(EventHandler *endAction);
411  void changeSound(int soundNum);
412 
413  Common::String getClassName() override { return "ASoundExt"; }
414  void synchronize(Serializer &s) override;
415  void signal() override;
416 };
417 
418 class PlayStream: public EventHandler {
419  class ResFileData {
420  public:
421  int _fileChunkSize;
422  uint _indexSize;
423  uint _chunkSize;
424 
425  void load(Common::SeekableReadStream &stream);
426  };
427 private:
428  Common::File _file;
429  ResFileData _resData;
430  Audio::QueuingAudioStream *_audioStream;
431  Audio::SoundHandle _soundHandle;
432  uint16 *_index;
433  EventHandler *_endAction;
434  int _voiceNum;
435 
436  static uint32 getFileOffset(const uint16 *data, int count, int voiceNum);
437 public:
438  PlayStream();
439  ~PlayStream() override;
440 
441  bool setFile(const Common::Path &filename);
442  bool play(int voiceNum, EventHandler *endAction);
443  void stop();
444  bool isPlaying() const;
445 
446  void remove() override;
447  void dispatch() override;
448 };
449 
450 #define ADLIB_CHANNEL_COUNT 9
451 
453 private:
454  GroupData _groupData;
455  Audio::Mixer *_mixer;
456  OPL::OPL *_opl;
457  byte _portContents[256];
458  const byte *_patchData;
459  int _masterVolume;
460  Common::Mutex _queueMutex;
462 
463  bool _channelVoiced[ADLIB_CHANNEL_COUNT];
464  int _channelVolume[ADLIB_CHANNEL_COUNT];
465  int _v4405E[ADLIB_CHANNEL_COUNT];
466  int _v44067[ADLIB_CHANNEL_COUNT];
467  int _v44070[ADLIB_CHANNEL_COUNT];
468  int _v44079[ADLIB_CHANNEL_COUNT];
469  int _v44082[ADLIB_CHANNEL_COUNT + 1];
470  int _pitchBlend[ADLIB_CHANNEL_COUNT];
471  int _v4409E[ADLIB_CHANNEL_COUNT];
472 
473 
474  void write(byte reg, byte value);
475  void flush();
476  void updateChannelVolume(int channel);
477  void setVoice(int channel);
478  void clearVoice(int channel);
479  void updateChannel(int channel);
480  void setFrequency(int channel);
481 public:
483  ~AdlibSoundDriver() override;
484 
485  bool open() override;
486  void close() override;
487  bool reset() override;
488  const GroupData *getGroupData() override;
489  void installPatch(const byte *data, int size) override;
490  int setMasterVolume(int volume) override;
491  void playSound(const byte *channelData, int dataOffset, int program, int channel, int v0, int v1) override;
492  void updateVoice(int channel) override;
493  void proc38(int channel, int cmd, int value) override;
494  void setPitch(int channel, int pitchBlend) override;
495 
496 private:
497  void onTimer();
498 };
499 
501 private:
502  GroupData _groupData;
503  Audio::Mixer *_mixer;
504  Audio::SoundHandle _soundHandle;
505  Audio::QueuingAudioStream *_audioStream;
506  int _sampleRate;
507 
508  byte _masterVolume;
509  byte _channelVolume;
510  const byte *_channelData;
511 public:
513  ~SoundBlasterDriver() override;
514 
515  bool open() override;
516  void close() override;
517  bool reset() override;
518  const GroupData *getGroupData() override;
519  int setMasterVolume(int volume) override;
520  void playSound(const byte *channelData, int dataOffset, int program, int channel, int v0, int v1) override;
521  void updateVoice(int channel) override;
522  void proc38(int channel, int cmd, int value) override;
523  void proc42(int channel, int cmd, int value, int *v1, int *v2) override;
524 };
525 
526 
527 } // End of namespace TsAGE
528 
529 #endif
Definition: str.h:59
Definition: sound.h:74
Definition: core.h:115
Definition: array.h:52
Definition: sound.h:170
Definition: list.h:44
Definition: core.h:134
Definition: path.h:52
Definition: sound.h:366
Definition: sound.h:60
Definition: stream.h:745
Definition: sound.h:259
Definition: sound.h:500
Definition: sound.h:405
Definition: mixer.h:49
Definition: mixer.h:59
Definition: file.h:47
Definition: mutex.h:67
Definition: sound.h:452
Definition: saveload.h:115
Definition: blueforce_dialogs.h:30
Definition: sound.h:51
Definition: sound.h:136
Definition: sound.h:69
Definition: saveload.h:63
Definition: audiostream.h:370
Definition: fmopl.h:35
Definition: sound.h:83
Definition: sound.h:418
Definition: sound.h:122
Definition: sound.h:151
Definition: saveload.h:90
Definition: system.h:38
Definition: fmopl.h:115
Definition: sound.h:160