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 DRACI_SOUND_H
23 #define DRACI_SOUND_H
24 
25 #include "common/str.h"
26 #include "common/file.h"
27 #include "common/list.h"
28 #include "audio/mixer.h"
29 
30 namespace Common {
31 class Archive;
32 class SeekableReadStream;
33 }
34 
35 namespace Draci {
36 
37 enum SoundFormat { RAW, RAW80, MP3, OGG, FLAC }; // RAW80 means skip the first 80 bytes
38 
42 struct SoundSample {
43  uint _offset; // For internal use of LegacySoundArchive
44  uint _length;
45 
46  uint _frequency; // Only when _format == RAW or RAW80
47  SoundFormat _format;
48 
49  byte *_data; // At most one of these two pointer can be non-NULL
51 
52  SoundSample() : _offset(0), _length(0), _frequency(0), _format(RAW), _data(NULL), _stream(NULL) { }
53  // The standard copy constructor is good enough, since we only store numbers and pointers.
54  // Don't call close() automaticall in the destructor, otherwise copying causes SIGSEGV.
55  void close() {
56  delete[] _data;
57  delete _stream;
58  _data = NULL;
59  _stream = NULL;
60  }
61 };
62 
66 class SoundArchive {
67 public:
68  SoundArchive() { }
69  virtual ~SoundArchive() { }
70 
76  virtual uint size() const = 0;
77 
82  virtual bool isOpen() const = 0;
83 
87  virtual void clearCache() = 0;
88 
96  virtual SoundSample *getSample(int i, uint freq) = 0;
97 };
98 
104 public:
105  LegacySoundArchive(const char *path, uint defaultFreq) :
106  _path(NULL), _samples(NULL), _sampleCount(0), _defaultFreq(defaultFreq), _opened(false), _f(NULL) {
107  openArchive(path);
108  }
109  ~LegacySoundArchive() override { closeArchive(); }
110 
111  void openArchive(const char *path);
112  void closeArchive();
113 
114  uint size() const override { return _sampleCount; }
115  bool isOpen() const override { return _opened; }
116 
117  void clearCache() override;
118  SoundSample *getSample(int i, uint freq) override;
119 
120 private:
121  const char *_path;
122  SoundSample *_samples;
123  uint _sampleCount;
124  uint _defaultFreq;
125  bool _opened;
126  Common::File *_f;
127 };
128 
135 public:
136  ZipSoundArchive() : _archive(NULL), _path(NULL), _extension(NULL), _format(RAW), _sampleCount(0), _defaultFreq(0), _cache() { }
137  ~ZipSoundArchive() override { closeArchive(); }
138 
139  void openArchive(const char *path, const char *extension, SoundFormat format, int raw_frequency = 0);
140  void closeArchive();
141 
142  uint size() const override { return _sampleCount; }
143  bool isOpen() const override { return _archive != NULL; }
144 
145  void clearCache() override;
146  SoundSample *getSample(int i, uint freq) override;
147 
148 private:
149  Common::Archive *_archive;
150  const char *_path;
151  const char *_extension;
152  SoundFormat _format;
153  uint _sampleCount;
154  uint _defaultFreq;
155 
156  // Since we typically play at most 1 dubbing at a time, we could get
157  // away with having just 1 record allocated and reusing it each time.
158  // However, that would be thread-unsafe if two samples were played.
159  // Although the player does not do that, it is nicer to allow for it in
160  // principle. For each dubbed sentence, we allocate a new record in
161  // the following link-list, which is cleared during each location
162  // change. The dubbed samples themselves are manually deallocated
163  // after they end.
165 };
166 
167 #define SOUND_HANDLES 10
168 
169 enum sndHandleType {
170  kFreeHandle,
171  kEffectHandle,
172  kVoiceHandle
173 };
174 
175 struct SndHandle {
176  Audio::SoundHandle handle;
177  sndHandleType type;
178 };
179 
180 // Taken from engines/saga/sound.h and simplified (in particular, removed
181 // decompression until we support compressed files too).
182 class Sound {
183 public:
184 
185  Sound(Audio::Mixer *mixer);
186  ~Sound() {}
187 
188  uint playSound(const SoundSample *buffer, int volume, bool loop);
189  void pauseSound();
190  void resumeSound();
191  void stopSound();
192  bool isMutedSound() const { return _muteSound; }
193 
194  uint playVoice(const SoundSample *buffer);
195  void pauseVoice();
196  void resumeVoice();
197  void stopVoice();
198  bool isMutedVoice() const { return _muteVoice; }
199 
200  void stopAll() { stopVoice(); stopSound(); }
201 
202  void setVolume();
203 
204  bool showSubtitles() const { return _showSubtitles; }
205  int talkSpeed() const { return _talkSpeed; }
206 
207  private:
208  // Returns the length of the sound sample in miliseconds.
209  uint playSoundBuffer(Audio::SoundHandle *handle, const SoundSample &buffer, int volume,
210  sndHandleType handleType, bool loop);
211 
212  SndHandle *getHandle();
213 
214  Audio::Mixer *_mixer;
215 
216  bool _muteSound;
217  bool _muteVoice;
218  bool _showSubtitles;
219  int _talkSpeed;
220 
221  SndHandle _handles[SOUND_HANDLES];
222 };
223 
224 } // End of namespace Draci
225 
226 #endif // DRACI_SOUND_H
Definition: sound.h:103
Definition: list.h:44
bool isOpen() const override
Definition: sound.h:115
Definition: sound.h:42
Definition: sound.h:175
Definition: stream.h:745
bool isOpen() const override
Definition: sound.h:143
Definition: archive.h:141
Definition: sound.h:182
uint size() const override
Definition: sound.h:142
Definition: mixer.h:49
Definition: mixer.h:59
Definition: file.h:47
Definition: algorithm.h:29
Definition: sound.h:134
Definition: animation.h:30
uint size() const override
Definition: sound.h:114
Definition: sound.h:66