ScummVM API documentation
sound_intern.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 KYRA_SOUND_INTERN_H
23 #define KYRA_SOUND_INTERN_H
24 
25 
26 #include "kyra/sound/sound.h"
27 #include "kyra/sound/sound_pc_v1.h"
28 
29 #include "audio/midiparser.h"
30 #include "audio/miles.h"
31 #include "audio/softsynth/emumidi.h"
32 #include "audio/softsynth/fmtowns_pc98/towns_audio.h"
33 
34 #include "common/mutex.h"
35 
36 class EuphonyPlayer;
38 
39 namespace Audio {
40 class PCSpeaker;
41 class MaxTrax;
42 } // End of namespace Audio
43 
44 namespace Common {
45 class MacResManager;
46 } // End of namespace Common
47 
48 namespace Kyra {
49 
50 class MidiOutput;
51 
58 class SoundMidiPC : public Sound {
59 public:
60  SoundMidiPC(KyraEngine_v1 *vm, Audio::Mixer *mixer, MidiDriver *driver, kType type);
61  ~SoundMidiPC() override;
62 
63  kType getMusicType() const override { return _type; }
64 
65  bool init() override;
66 
67  void updateVolumeSettings() override;
68 
69  void initAudioResourceInfo(int set, void *info) override;
70  void selectAudioResourceSet(int set) override;
71  bool hasSoundFile(uint file) const override;
72  void loadSoundFile(uint file) override;
73  void loadSoundFile(const Common::Path &file) override;
74  void loadSfxFile(const Common::Path &file) override;
75 
76  void playTrack(uint8 track) override;
77  void haltTrack() override;
78  bool isPlaying() const override;
79 
80  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
81  void stopAllSoundEffects() override;
82 
83  void beginFadeOut() override;
84 
85  void pause(bool paused) override;
86 private:
87  static void onTimer(void *data);
88 
89  // Our channel handling
90  int _musicVolume, _sfxVolume;
91 
92  uint32 _fadeStartTime;
93  bool _fadeMusicOut;
94 
95  // Midi file related
96  Common::Path _mFileName, _sFileName;
97  byte *_musicFile, *_sfxFile;
98 
99  MidiParser *_music;
100  MidiParser *_sfx[3];
101 
102  const SoundResourceInfo_PC *res() const {return _resInfo[_currentResourceSet]; }
103  SoundResourceInfo_PC *_resInfo[3];
104  int _currentResourceSet;
105 
106  // misc
107  kType _type;
108  Common::Path getFileName(const Common::Path &str);
109 
110  bool _nativeMT32;
111  MidiDriver *_driver;
113 
114  Common::Mutex _mutex;
115 };
116 
117 class SoundTowns_LoK : public Sound {
118 public:
120  ~SoundTowns_LoK() override;
121 
122  kType getMusicType() const override { return kTowns; }
123 
124  bool init() override;
125  void process() override;
126 
127  void initAudioResourceInfo(int set, void *info) override;
128  void selectAudioResourceSet(int set) override;
129  bool hasSoundFile(uint file) const override;
130  void loadSoundFile(uint file) override;
131  void loadSoundFile(const Common::Path &) override {}
132 
133  void playTrack(uint8 track) override;
134  void haltTrack() override;
135 
136  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
137  void stopAllSoundEffects() override;
138 
139  void beginFadeOut() override;
140 
141  void updateVolumeSettings() override;
142  void enableMusic(int enable) override;
143 
144 private:
145  bool loadInstruments();
146  void playEuphonyTrack(uint32 offset, int loop);
147 
148  void fadeOutSoundEffects();
149 
150  int _lastTrack;
151  Audio::SoundHandle _sfxHandle;
152 
153  uint8 *_musicTrackData;
154 
155  uint _sfxFileIndex;
156  uint8 *_sfxFileData;
157  uint8 _sfxChannel;
158 
159  EuphonyPlayer *_player;
160 
161  bool _cdaPlaying;
162 
163  const SoundResourceInfo_Towns *res() const {return _resInfo[_currentResourceSet]; }
164  SoundResourceInfo_Towns *_resInfo[3];
165  int _currentResourceSet;
166 
167  const uint8 *_musicFadeTable;
168  const uint8 *_sfxBTTable;
169  const uint8 *_sfxWDTable;
170 };
171 
172 class SoundPC98_LoK : public Sound {
173 public:
175  ~SoundPC98_LoK() override;
176 
177  kType getMusicType() const override { return kPC98; }
178 
179  bool init() override;
180 
181  void initAudioResourceInfo(int set, void *info) override;
182  void selectAudioResourceSet(int set) override;
183  bool hasSoundFile(uint file) const override;
184  void loadSoundFile(uint file) override;
185  void loadSoundFile(const Common::Path &file) override;
186 
187  void playTrack(uint8 track) override;
188  void haltTrack() override;
189  void beginFadeOut() override;
190 
191  int32 voicePlay(const char *file, Audio::SoundHandle *handle, uint8 volume, uint8 priority, bool isSfx) override { return -1; }
192  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
193 
194  void updateVolumeSettings() override;
195 
196 private:
197  int _lastTrack;
198  uint8 *_musicTrackData;
199  uint8 *_sfxTrackData;
200  TownsPC98_AudioDriver *_driver;
201 
202  const char *resPattern() {return _resInfo[_currentResourceSet]->c_str(); }
203  Common::String *_resInfo[3];
204  int _currentResourceSet;
205 };
206 
207 class SoundTownsPC98_v2 : public Sound {
208 public:
210  ~SoundTownsPC98_v2() override;
211 
212  kType getMusicType() const override { return _vm->gameFlags().platform == Common::kPlatformFMTowns ? kTowns : kPC98; }
213 
214  bool init() override;
215  void process() override;
216 
217  void initAudioResourceInfo(int set, void *info) override;
218  void selectAudioResourceSet(int set) override;
219  bool hasSoundFile(uint file) const override;
220  void loadSoundFile(uint file) override {}
221  void loadSoundFile(const Common::Path &file) override;
222 
223  void playTrack(uint8 track) override;
224  void haltTrack() override;
225  void beginFadeOut() override;
226 
227  int32 voicePlay(const char *file, Audio::SoundHandle *handle, uint8 volume = 255, uint8 priority = 255, bool isSfx = true) override;
228  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
229 
230  void updateVolumeSettings() override;
231 
232 private:
233  Audio::AudioStream *_currentSFX;
234  int _lastTrack;
235  bool _useFmSfx;
236 
237  uint8 *_musicTrackData;
238  uint8 *_sfxTrackData;
239  TownsPC98_AudioDriver *_driver;
240 
241  const SoundResourceInfo_TownsPC98V2 *res() const {return _resInfo[_currentResourceSet]; }
242  SoundResourceInfo_TownsPC98V2 *_resInfo[3];
243  int _currentResourceSet;
244 };
245 
246 // PC Speaker MIDI driver
248 public:
250  ~MidiDriver_PCSpeaker() override;
251 
252  // MidiDriver interface
253  void close() override {}
254 
255  void send(uint32 data) override;
256 
257  MidiChannel *allocateChannel() override { return 0; }
258  MidiChannel *getPercussionChannel() override { return 0; }
259 
260  // MidiDriver_Emulated interface
261  void generateSamples(int16 *buffer, int numSamples) override;
262 
263  // AudioStream interface
264  bool isStereo() const override { return false; }
265  int getRate() const override { return _rate; }
266 private:
267  Common::Mutex _mutex;
268  Audio::PCSpeaker *_speaker;
269  int _rate;
270 
271  struct Channel {
272  uint8 pitchBendLow, pitchBendHigh;
273  uint8 hold;
274  uint8 modulation;
275  uint8 voiceProtect;
276  uint8 noteCount;
277  } _channel[2];
278 
279  void resetController(int channel);
280 
281  struct Note {
282  bool enabled;
283  uint8 hardwareChannel;
284  uint8 midiChannel;
285  uint8 note;
286  bool processHold;
287  uint8 flags;
288  uint8 hardwareFlags;
289  uint16 priority;
290  int16 modulation;
291  uint16 precedence;
292  } _note[2];
293 
294  void noteOn(int channel, int note);
295  void noteOff(int channel, int note);
296 
297  void turnNoteOn(int note);
298  void overwriteNote(int note);
299  void turnNoteOff(int note);
300 
301  void setupTone(int note);
302 
303  uint16 _countdown;
304  uint8 _hardwareChannel[1];
305  bool _modulationFlag;
306 
307  uint8 _timerValue;
308  void onTimer() override;
309 
310  static const uint8 _noteTable1[];
311  static const uint8 _noteTable2[];
312 };
313 
314 // for StaticResource (maybe we can find a nicer way to handle it)
316  uint8 note;
317  uint8 patch;
318  uint16 duration;
319  uint8 volume;
320  uint8 pan;
321 };
322 
323 class SoundAmiga_LoK : public Sound {
324 public:
326  ~SoundAmiga_LoK() override;
327 
328  kType getMusicType() const override { return kAmiga; } //FIXME
329 
330  bool init() override;
331 
332  void initAudioResourceInfo(int set, void *info) override;
333  void selectAudioResourceSet(int set) override;
334  bool hasSoundFile(uint file) const override;
335  void loadSoundFile(uint file) override;
336  void loadSoundFile(const Common::Path &) override {}
337 
338  void playTrack(uint8 track) override;
339  void haltTrack() override;
340  void beginFadeOut() override;
341 
342  int32 voicePlay(const char *file, Audio::SoundHandle *handle, uint8 volume, uint8 priority, bool isSfx) override { return -1; }
343  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
344 
345 protected:
346  Audio::MaxTrax *_driver;
347  Audio::SoundHandle _musicHandle;
348  enum FileType { kFileNone = -1, kFileIntro = 0, kFileGame = 1, kFileFinal = 2 } _fileLoaded;
349 
350  const AmigaSfxTable *_tableSfxIntro;
351  int _tableSfxIntro_Size;
352 
353  const AmigaSfxTable *_tableSfxGame;
354  int _tableSfxGame_Size;
355 };
356 
357 class SoundMacRes;
358 class HalestormDriver;
359 class SoundMac : public Sound {
360 public:
361  SoundMac(KyraEngine_v1 *vm, Audio::Mixer *mixer);
362  ~SoundMac() override;
363 
364  kType getMusicType() const override;
365 
366  bool init() override { return init(musicEnabled() == 1); }
367  bool init(bool hiQuality);
368  void initAudioResourceInfo(int, void*) override {}
369  void selectAudioResourceSet(int set) override;
370  bool hasSoundFile(uint) const override { return true; }
371  void loadSoundFile(uint) override {}
372  void loadSoundFile(const Common::Path &) override {}
373  void playTrack(uint8 track) override;
374  void haltTrack() override;
375  void playSoundEffect(uint16 track, uint8) override;
376  bool isPlaying() const override;
377  void beginFadeOut() override;
378  void updateVolumeSettings() override;
379  void enableMusic(int enable) override;
380 
381 private:
382  void setQuality(bool hi);
383 
384  SoundMacRes *_res;
385  HalestormDriver *_driver;
386  const int _talkieFlag;
387  bool _ready;
388 
389  const uint16 *_resIDMusic;
390  int _currentResourceSet;
391 
392  static const uint16 _resIDMusicIntro[4];
393  static const uint16 _resIDMusicIngame[35];
394  static const uint8 _musicLoopTable[35];
395  static const uint16 _resIDSfxIntro[2][39];
396  static const uint16 _resIDSfxIngame[2][39];
397 
398  struct SoundEffectDef {
399  uint8 note;
400  uint8 number;
401  uint16 rate;
402  uint8 unk;
403  };
404 
405  static const SoundEffectDef _soundEffectDefsIntro[16];
406  static const SoundEffectDef _soundEffectDefsIngame[120];
407 };
408 
409 #ifdef ENABLE_EOB
410 
411 class SoundTowns_Darkmoon : public Sound, public TownsAudioInterfacePluginDriver {
412 public:
413  SoundTowns_Darkmoon(KyraEngine_v1 *vm, Audio::Mixer *mixer);
414  ~SoundTowns_Darkmoon() override;
415 
416  kType getMusicType() const override { return kTowns; }
417 
418  bool init() override;
419 
420  void timerCallback(int timerId) override;
421 
422  void initAudioResourceInfo(int set, void *info) override;
423  void selectAudioResourceSet(int set) override;
424  bool hasSoundFile(uint file) const override;
425  void loadSoundFile(uint file) override;
426  void loadSoundFile(const Common::Path &name) override;
427 
428  void playTrack(uint8 track) override;
429  void haltTrack() override;
430  bool isPlaying() const override;
431 
432  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
433  void stopAllSoundEffects() override;
434 
435  void beginFadeOut() override;
436 
437  void updateVolumeSettings() override;
438 
439  int checkTrigger() override;
440 
441  void resetTrigger() override;
442 
443 private:
444  struct SoundTableEntry {
445  int8 type;
446  int32 para1;
447  int16 para2;
448  } _soundTable[120];
449 
450  const char *const *_fileList;
451  uint _fileListLen;
452 
453  uint8 _lastSfxChan;
454  uint8 _lastEnvChan;
455  uint8 *_pcmData;
456  uint32 _pcmDataSize;
457  uint8 _pcmVol;
458 
459  int _timer;
460  int _timerSwitch;
461 
462  SoundResourceInfo_TownsEoB *_resource[3];
463 
464  TownsAudioInterface *_intf;
465 };
466 
467 class AudioMaster2;
468 class SoundAmiga_EoB: public Sound {
469 public:
470  SoundAmiga_EoB(KyraEngine_v1 *vm, Audio::Mixer *mixer);
471  ~SoundAmiga_EoB() override;
472 
473  kType getMusicType() const override;
474 
475  bool init() override;
476  void initAudioResourceInfo(int set, void *info) override;
477  void selectAudioResourceSet(int set) override;
478  bool hasSoundFile(uint file) const override { return false; }
479  void loadSoundFile(uint) override {}
480  void loadSoundFile(const Common::Path &file) override;
481  void unloadSoundFile(const Common::String &file) override;
482  void playTrack(uint8 track) override;
483  void haltTrack() override;
484  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
485  void beginFadeOut() override { beginFadeOut(160); }
486  void beginFadeOut(int delay) override;
487  void updateVolumeSettings() override;
488  int checkTrigger() override;
489 
490 private:
491  uint8 *_fileBuffer;
492 
493  KyraEngine_v1 *_vm;
494  AudioMaster2 *_driver;
495  SoundResourceInfo_AmigaEoB *_resInfo[3];
496  Common::String _lastSound;
497 
498  int _currentResourceSet;
499 
500  bool _ready;
501 };
502 
503 class MLALF98;
504 class SoundPC98_EoB : public Sound {
505 public:
506  SoundPC98_EoB(KyraEngine_v1 *vm, Audio::Mixer *mixer);
507  ~SoundPC98_EoB() override;
508 
509  kType getMusicType() const override;
510 
511  bool init() override;
512  void initAudioResourceInfo(int set, void *info) override;
513  void selectAudioResourceSet(int set) override;
514  bool hasSoundFile(uint file) const override { return false; }
515  void loadSoundFile(uint file) override;
516  void loadSoundFile(const Common::Path &file) override {}
517  void loadSfxFile(const Common::Path &file) override;
518  void playTrack(uint8 track) override;
519  void haltTrack() override;
520  void playSoundEffect(uint16 track, uint8) override;
521  void beginFadeOut() override {}
522  void updateVolumeSettings() override;
523 
524 private:
525  KyraEngine_v1 *_vm;
526  MLALF98 *_driver;
527 
528  SoundResourceInfo_PC *_resInfo[3];
529  int _currentResourceSet;
530 
531  uint32 _sfxDelay;
532 
533  bool _ready;
534 };
535 
536 class CapcomPC98AudioDriver;
537 class SoundPC98_Darkmoon : public Sound {
538 public:
539  SoundPC98_Darkmoon(KyraEngine_v1 *vm, MidiDriver::DeviceHandle dev, Audio::Mixer *mixer);
540  ~SoundPC98_Darkmoon() override;
541 
542  kType getMusicType() const override;
543  kType getSfxType() const override;
544 
545  bool init() override;
546 
547  void initAudioResourceInfo(int set, void *info) override;
548  void selectAudioResourceSet(int set) override;
549  bool hasSoundFile(uint file) const override { return true; }
550  void loadSoundFile(uint file) override;
551  void loadSoundFile(const Common::Path &name) override;
552 
553  void playTrack(uint8 track) override;
554  void haltTrack() override;
555  bool isPlaying() const override;
556 
557  void playSoundEffect(uint16 track, uint8 volume = 0xFF) override;
558  void stopAllSoundEffects() override;
559 
560  void beginFadeOut() override;
561 
562  void pause(bool paused) override;
563 
564  void updateVolumeSettings() override;
565 
566  int checkTrigger() override;
567  void resetTrigger() override {} // This sound class is for EOB II only, this method is not needed there.
568 
569 private:
570  void restartBackgroundMusic();
571  const uint8 *getData(uint16 track) const;
572 
573  KyraEngine_v1 *_vm;
574  CapcomPC98AudioDriver *_driver;
575  uint8 *_soundData, *_fileBuffer;
576 
577  int _lastTrack;
578 
579  const SoundResourceInfo_PC *res() const {return _resInfo[_currentResourceSet]; }
580  SoundResourceInfo_PC *_resInfo[3];
581  int _currentResourceSet;
582 
583  Common::Path _soundFileLoaded;
584 
586  kType _drvType;
587  bool _ready;
588 };
589 
590 class SegaAudioDriver;
591 class SoundSegaCD_EoB : public Sound {
592 public:
593  SoundSegaCD_EoB(KyraEngine_v1 *vm, Audio::Mixer *mixer);
594  ~SoundSegaCD_EoB() override;
595 
596  kType getMusicType() const override;
597 
598  bool init() override;
599  void initAudioResourceInfo(int, void*) override {}
600  void selectAudioResourceSet(int) override {}
601  bool hasSoundFile(uint file) const override { return false; }
602  void loadSoundFile(uint file) override {}
603  void loadSoundFile(const Common::Path &file) override {}
604  void playTrack(uint8 track) override;
605  void haltTrack() override;
606  void playSoundEffect(uint16 track, uint8 volume) override;
607  bool isPlaying() const override;
608  void beginFadeOut() override {}
609  void updateVolumeSettings() override;
610 
611 private:
612  void loadPCMData();
613  void loadFMData();
614 
615  KyraEngine_v1 *_vm;
616  SegaAudioDriver *_driver;
617 
618  uint8 _pcmOffsets[8];
619  uint16 _fmOffsets[140];
620  const uint8 *_fmData;
621  int _lastSoundEffect;
622  bool _ready;
623 
624  static const uint8 _fmTrackMap[140];
625 };
626 
627 #endif
628 
629 } // End of namespace Kyra
630 
631 #endif
Definition: sound_intern.h:207
void loadSoundFile(const Common::Path &) override
Definition: sound_intern.h:131
Definition: str.h:59
Definition: sound.h:40
Definition: sound.h:68
uint32 DeviceHandle
Definition: mididrv.h:308
void loadSoundFile(const Common::Path &) override
Definition: sound_intern.h:336
void loadSoundFile(uint) override
Definition: sound_intern.h:371
Definition: miles.h:123
Definition: kyra_v1.h:126
bool init() override
Definition: sound_intern.h:366
Definition: path.h:52
Definition: sound_intern.h:323
Definition: sound.h:78
Definition: sound.h:91
Definition: mididrv.h:299
bool hasSoundFile(uint) const override
Definition: sound_intern.h:370
bool isStereo() const override
Definition: sound_intern.h:264
Definition: towns_audio.h:37
Definition: mixer.h:49
Definition: mixer.h:59
Definition: sound_intern.h:247
Definition: towns_euphony.h:115
Definition: halestorm.h:36
Definition: algorithm.h:29
Definition: mutex.h:67
int32 voicePlay(const char *file, Audio::SoundHandle *handle, uint8 volume, uint8 priority, bool isSfx) override
Definition: sound_intern.h:342
Definition: audiostream.h:50
void close() override
Definition: sound_intern.h:253
Definition: sound_intern.h:172
Definition: mididrv.h:519
Definition: detection.h:27
Definition: towns_pc98_driver.h:34
void loadSoundFile(uint file) override
Definition: sound_intern.h:220
int getRate() const override
Definition: sound_intern.h:265
Definition: emumidi.h:29
Definition: sound_intern.h:359
Definition: sound_intern.h:117
Definition: sound_mac_res.h:39
int32 voicePlay(const char *file, Audio::SoundHandle *handle, uint8 volume, uint8 priority, bool isSfx) override
Definition: sound_intern.h:191
void initAudioResourceInfo(int, void *) override
Definition: sound_intern.h:368
Definition: sound_intern.h:315
Definition: pcspk.h:31
Definition: system.h:37
Definition: sound_intern.h:58
void loadSoundFile(const Common::Path &) override
Definition: sound_intern.h:372
Definition: midiparser.h:289
Definition: sound.h:46
Definition: towns_audio.h:31