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