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 LURE_SOUND_H
23 #define LURE_SOUND_H
24 
25 #include "lure/luredefs.h"
26 #include "lure/disk.h"
27 #include "lure/memory.h"
28 
29 #include "common/mutex.h"
30 #include "common/singleton.h"
31 #include "common/ptr.h"
32 
33 #include "audio/adlib_ms.h"
34 #include "audio/mididrv.h"
35 #include "audio/mt32gm.h"
36 
37 class MidiParser;
38 class MidiChannel;
39 
40 namespace Lure {
41 
42 #define NUM_CHANNELS 16
43 #define LURE_MAX_SOURCES 10
44 
45 class MidiMusic: public MidiDriver_BASE {
46 private:
47  uint8 _soundNumber;
48  uint8 _numChannels;
49  byte _volume;
50  MemoryBlock *_decompressedSound;
51  uint8 *_soundData;
52  uint8 _soundSize;
53  MidiDriver_Multisource *_driver;
54  MidiDriver_MT32GM *_mt32Driver;
55  int8 _source;
56  MidiParser *_parser;
57  bool _isMusic;
58  bool _loop;
59  bool _isPlaying;
60 
61  void queueUpdatePos();
62  uint8 randomQueuePos();
63  uint32 songOffset(uint16 songNum) const;
64  uint32 songLength(uint16 songNum) const;
65 
66 public:
67  MidiMusic(MidiDriver_Multisource *driver, uint8 soundNum, bool isMus, bool loop,
68  int8 source, uint8 numChannels, void *soundData, uint32 size, uint8 volume);
69  ~MidiMusic() override;
70  void setVolume(int volume);
71  int getVolume() const { return _volume; }
72 
73  void playSong(uint16 songNum);
74  void stopSong() { stopMusic(); }
75  void playMusic();
76  void stopMusic();
77  void pauseMusic();
78  void resumeMusic();
79  void queueTuneList(int16 tuneList);
80  bool queueSong(uint16 songNum);
81  void toggleVChange();
82 
83  // MidiDriver_BASE interface implementation
84  void send(uint32 b) override;
85  void send(int8 source, uint32 b) override;
86  void metaEvent(byte type, byte *data, uint16 length) override;
87  void metaEvent(int8 source, byte type, byte *data, uint16 length) override;
88 
89  void onTimer();
90 
91  uint8 soundNumber() const { return _soundNumber; }
92  int8 getSource() const { return _source; }
93  bool isPlaying() const { return _isPlaying; }
94  bool isMusic() const { return _isMusic; }
95 };
96 
97 class SoundManager : public Common::Singleton<SoundManager> {
98 private:
99  // Outer sound interface properties
100  MemoryBlock *_descs;
101  MemoryBlock *_soundData;
102  uint8 _soundsTotal;
103  int _numDescs;
104  SoundDescResource *soundDescs() { return (SoundDescResource *) _descs->data(); }
105  MidiDriver_Multisource *_driver;
106  MidiDriver_MT32GM *_mt32Driver;
109  SoundList _activeSounds;
112  MusicList _playingSounds;
113  bool _sourcesInUse[LURE_MAX_SOURCES];
114  bool _nativeMT32;
115  bool _isRoland;
116  Common::Mutex _soundMutex;
117  bool _paused;
118 
119  // Internal support methods
120  void bellsBodge();
121  void musicInterface_TidySounds();
122  static void onTimer(void *data);
123  void doTimer();
124 
125 public:
126  SoundManager();
127  ~SoundManager() override;
128 
129  void saveToStream(Common::WriteStream *stream);
130  void loadFromStream(Common::ReadStream *stream);
131 
132  void loadSection(uint16 sectionId);
133  bool initCustomTimbres(bool canAbort = false);
134  void killSounds();
135  void addSound(uint8 soundIndex, bool tidyFlag = true);
136  void addSound2(uint8 soundIndex);
137  void stopSound(uint8 soundIndex);
138  void killSound(uint8 soundNumber);
139  void setVolume(uint8 soundNumber, uint8 volume);
140  void syncSounds();
141  void tidySounds();
142  uint8 descIndexOf(uint8 soundNumber);
143  SoundDescResource *findSound(uint8 soundNumber);
144  void removeSounds();
145  void restoreSounds();
146  bool fadeOut();
147  void pause();
148  void resume();
149  bool getPaused() const { return _paused; }
150  bool hasNativeMT32() const { return _nativeMT32; }
151  bool isRoland() const { return _isRoland; }
152 
153  // The following methods implement the external sound player module
154  //void musicInterface_Initialize();
155  void musicInterface_Play(uint8 soundNumber, bool isMusic = false, uint8 numChannels = 4, uint8 volume = 0x80);
156  void musicInterface_Stop(uint8 soundNumber);
157  bool musicInterface_CheckPlaying(uint8 soundNumber);
158  void musicInterface_SetVolume(uint8 soundNumber, uint8 volume);
159  void musicInterface_KillAll();
160  void musicInterface_ContinuePlaying();
161  void musicInterface_TrashReverb();
162 };
163 
164 // AdLib MidiDriver subclass implementing the behavior specific to Lure of the
165 // Temptress.
167 protected:
168  // Lookup array for OPL frequency (F-num) values.
169  static const uint16 OPL_FREQUENCY_LOOKUP[];
170 
171 public:
173 
174  // Channel aftertouch is used to adjust a note's volume. This is done by
175  // overriding the velocity of the active note and recalculating the volume.
176  void channelAftertouch(uint8 channel, uint8 pressure, uint8 source) override;
177  // The MIDI data uses three custom sequencer meta events; the most important
178  // one sets the instrument definition for a channel.
179  void metaEvent(int8 source, byte type, byte *data, uint16 length) override;
180 
181 protected:
182  InstrumentInfo determineInstrument(uint8 channel, uint8 source, uint8 note) override;
183  uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note) override;
184  // Returns the number of semitones in bits 8+ and an 8 bit fraction of a
185  // semitone.
186  int32 calculatePitchBend(uint8 channel, uint8 source, uint16 oplFrequency) override;
187  uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
188 
189  // Stores the instrument definitions set by sequencer meta events.
190  OplInstrumentDefinition _instrumentDefs[LURE_MAX_SOURCES][MIDI_CHANNEL_COUNT];
191  // Pitch bend sensitivity in semi-tones. This is a global setting;
192  // it cannot be specified for a specific MIDI channel.
193  uint8 _pitchBendSensitivity;
194 };
195 
196 } // End of namespace Lure
197 
198 #define Sound (::Lure::SoundManager::instance())
199 
200 #endif
Definition: mt32gm.h:111
Definition: stream.h:77
Definition: mididrv_ms.h:86
Definition: list.h:44
Definition: adlib_ms.h:250
Definition: sound.h:166
void send(uint32 b) override
Definition: mididrv.h:103
Definition: res_struct.h:240
Definition: mutex.h:67
Definition: mididrv.h:520
Definition: stream.h:385
Definition: list_intern.h:51
Definition: animseq.h:27
Definition: memory.h:31
Definition: adlib_ms.h:79
Definition: sound.h:45
Definition: midiparser.h:289
Definition: sound.h:97
Definition: singleton.h:42