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  * Additional copyright for this file:
8  * Copyright (C) 1994-1998 Revolution Software Ltd.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /*****************************************************************************
25  * SOUND.H Sound engine
26  *
27  * SOUND.CPP Contains the sound engine, fx & music functions
28  * Some very 'sound' code in here ;)
29  *
30  * (16Dec96 JEL)
31  *
32  ****************************************************************************/
33 
34 #ifndef SWORD2_SOUND_H
35 #define SWORD2_SOUND_H
36 
37 #include "common/file.h"
38 #include "common/mutex.h"
39 #include "audio/audiostream.h"
40 #include "audio/mixer.h"
41 
42 // Max number of sound fx
43 #define MAXMUS 2
44 
45 // Max number of fx in queue at once
46 #define FXQ_LENGTH 32
47 
48 #define BUFFER_SIZE 4096
49 
50 namespace Sword2 {
51 
52 enum {
53  kCLUMode = 1,
54  kMP3Mode,
55  kVorbisMode,
56  kFLACMode
57 };
58 
59 enum {
60  kLeadInSound,
61  kLeadOutSound
62 };
63 
64 enum {
65  // These three types correspond to types set by the scripts
66  FX_SPOT = 0,
67  FX_LOOP = 1,
68  FX_RANDOM = 2,
69 
70  // These are used for FX queue bookkeeping
71  FX_SPOT2 = 3,
72  FX_LOOPING = 4
73 };
74 
75 // Sound defines
76 
77 enum {
78  RDSE_SAMPLEFINISHED = 0,
79  RDSE_SAMPLEPLAYING = 1,
80  RDSE_FXTOCLEAR = 0, // Unused
81  RDSE_FXCACHED = 1, // Unused
82  RDSE_FXSPOT = 0,
83  RDSE_FXLOOP = 1,
84  RDSE_FXLEADIN = 2,
85  RDSE_FXLEADOUT = 3,
86  RDSE_QUIET = 1,
87  RDSE_SPEAKING = 0
88 };
89 
91 private:
92  Common::File *_file;
93  bool _firstTime;
94  uint32 _file_pos;
95  uint32 _end_pos;
96  int16 _outbuf[BUFFER_SIZE];
97  byte _inbuf[BUFFER_SIZE];
98  const int16 *_bufferEnd;
99  const int16 *_pos;
100 
101  uint16 _prev;
102 
103  void refill();
104 
105  inline bool eosIntern() const {
106  return !_file->isOpen() || _pos >= _bufferEnd;
107  }
108 
109 public:
110  CLUInputStream(Common::File *file, int size);
111  ~CLUInputStream() override;
112 
113  int readBuffer(int16 *buffer, const int numSamples) override;
114 
115  bool endOfData() const override { return eosIntern(); }
116  bool isStereo() const override { return false; }
117  int getRate() const override { return 22050; }
118 };
119 
121  Common::File file;
122  uint32 *idxTab;
123  uint32 idxLen;
124  int32 fileSize;
125  uint32 fileType;
126  volatile bool inUse;
127 };
128 
130 private:
131  int _cd;
132  SoundFileHandle *_fh;
133  uint32 _musicId;
134  Audio::AudioStream *_decoder;
135  int16 _buffer[BUFFER_SIZE];
136  const int16 *_bufferEnd;
137  const int16 *_pos;
138  bool _remove;
139  uint32 _numSamples;
140  uint32 _samplesLeft;
141  bool _looping;
142  int32 _fading;
143  int32 _fadeSamples;
144 
145  void refill();
146 
147  inline bool eosIntern() const {
148  if (_looping)
149  return false;
150  return _remove || _pos >= _bufferEnd;
151  }
152 
153 public:
154  MusicInputStream(int cd, SoundFileHandle *fh, uint32 musicId, bool looping);
155  ~MusicInputStream() override;
156 
157  int readBuffer(int16 *buffer, const int numSamples) override;
158 
159  bool endOfData() const override { return eosIntern(); }
160  bool isStereo() const override { return _decoder->isStereo(); }
161  int getRate() const override { return _decoder->getRate(); }
162 
163  int getCD() { return _cd; }
164 
165  void fadeUp();
166  void fadeDown();
167 
168  bool isReady() { return _decoder != nullptr; }
169  int32 isFading() { return _fading; }
170 
171  bool readyToRemove();
172  int32 getTimeRemaining();
173 };
174 
175 class Sound : public Audio::AudioStream {
176 private:
177  Sword2Engine *_vm;
178 
179  Common::Mutex _mutex;
180 
181  Audio::SoundHandle _mixerSoundHandle;
182  Audio::SoundHandle _leadInHandle;
183  Audio::SoundHandle _leadOutHandle;
184 
185  struct FxQueueEntry {
186  Audio::SoundHandle handle; // sound handle
187  uint32 resource; // resource id of sample
188  byte *data; // pointer to WAV data
189  uint32 len; // WAV data length
190  uint16 delay; // cycles to wait before playing (or 'random chance' if FX_RANDOM)
191  uint8 volume; // sound volume
192  int8 pan; // sound panning
193  uint8 type; // FX_SPOT, FX_RANDOM, FX_LOOP
194  };
195 
196  FxQueueEntry _fxQueue[FXQ_LENGTH];
197 
198  void triggerFx(uint8 i);
199 
200  bool _reverseStereo;
201 
202  bool _speechMuted;
203  bool _fxMuted;
204  bool _musicMuted;
205 
206  bool _speechPaused;
207  bool _fxPaused;
208  bool _musicPaused;
209 
210  int32 _loopingMusicId;
211 
212  Audio::SoundHandle _soundHandleSpeech;
213 
214  MusicInputStream *_music[MAXMUS];
215  SoundFileHandle _musicFile[MAXMUS];
216  SoundFileHandle _speechFile[MAXMUS];
217 
218  int16 *_mixBuffer;
219  int _mixBufferLen;
220 
221 public:
222  Sound(Sword2Engine *vm);
223  ~Sound() override;
224 
225  // AudioStream API
226 
227  int readBuffer(int16 *buffer, const int numSamples) override;
228  bool isStereo() const override { return false; }
229  bool endOfData() const override;
230  int getRate() const override { return Sword2Engine::isPsx() ? 11025 : 22050; }
231 
232  // End of AudioStream API
233 
234  void clearFxQueue(bool killMovieSounds);
235  void processFxQueue();
236 
237  void setReverseStereo(bool reverse);
238  bool isReverseStereo() const { return _reverseStereo; }
239 
240  void muteSpeech(bool mute);
241  bool isSpeechMute() const { return _speechMuted; }
242 
243  void muteFx(bool mute);
244  bool isFxMute() const { return _fxMuted; }
245 
246  void muteMusic(bool mute) { _musicMuted = mute; }
247  bool isMusicMute() const { return _musicMuted; }
248 
249  void setLoopingMusicId(int32 id) { _loopingMusicId = id; }
250  int32 getLoopingMusicId() const { return _loopingMusicId; }
251 
252  void pauseSpeech();
253  void unpauseSpeech();
254 
255  void pauseFx();
256  void unpauseFx();
257 
258  void pauseMusic();
259  void unpauseMusic();
260 
261  void playMovieSound(int32 res, int type);
262  void stopMovieSounds();
263 
264  void queueFx(int32 res, int32 type, int32 delay, int32 volume, int32 pan);
265  int32 playFx(FxQueueEntry *fx);
266  int32 playFx(Audio::SoundHandle *handle, byte *data, uint32 len, uint8 vol, int8 pan, bool loop, Audio::Mixer::SoundType soundType);
267  int32 stopFx(int32 i);
268  int32 setFxIdVolumePan(int32 id, int vol, int pan = 255);
269 
270  int32 getSpeechStatus();
271  int32 amISpeaking();
272  int32 playCompSpeech(uint32 speechId, uint8 vol, int8 pan);
273  int32 stopSpeech();
274 
275  int32 streamCompMusic(uint32 musicId, bool loop);
276  void stopMusic(bool immediately);
277  int32 musicTimeRemaining();
278 
279  void printFxQueue();
280 };
281 
282 } // End of namespace Sword2
283 
284 #endif
bool endOfData() const override
Definition: sound.h:159
Definition: sound.h:129
bool isStereo() const override
Definition: sound.h:116
Definition: animation.h:37
int readBuffer(int16 *buffer, const int numSamples) override
int getRate() const override
Definition: sound.h:161
virtual int getRate() const =0
bool endOfData() const override
Definition: sound.h:115
bool isStereo() const override
Definition: sound.h:160
virtual bool isStereo() const =0
int getRate() const override
Definition: sound.h:230
Definition: sword2.h:99
Definition: mixer.h:49
SoundType
Definition: mixer.h:62
Definition: sound.h:175
Definition: file.h:47
Definition: mutex.h:67
Definition: audiostream.h:50
bool isOpen() const
bool isStereo() const override
Definition: sound.h:228
Definition: sound.h:90
Definition: sound.h:120
int getRate() const override
Definition: sound.h:117