xoreos  0.0.5
sound.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef SOUND_SOUND_H
26 #define SOUND_SOUND_H
27 
28 // Mac OS X has to have this set up separately because of the include
29 // path for the OpenAL framework.
30 #ifdef MACOSX
31  #include <OpenAL/al.h>
32  #include <OpenAL/alc.h>
33 #else
34  #include <AL/al.h>
35  #include <AL/alc.h>
36 #endif
37 
38 #include <list>
39 #include <map>
40 
41 #include "src/common/types.h"
42 #include "src/common/scopedptr.h"
44 #include "src/common/singleton.h"
45 #include "src/common/thread.h"
46 #include "src/common/mutex.h"
47 #include "src/common/ustring.h"
48 
49 #include "src/sound/types.h"
50 
51 namespace Common {
52  class SeekableReadStream;
53 }
54 
55 namespace Sound {
56 
57 class AudioStream;
58 
60 class SoundManager : public Common::Singleton<SoundManager>, public Common::Thread {
61 public:
62  SoundManager();
63  ~SoundManager();
64 
66  void init();
68  void deinit();
69 
71  bool ready() const;
72 
73 
75  void triggerUpdate();
76 
77 
78  // .--- Channel status
80  bool isValidChannel(const ChannelHandle &handle) const;
81 
83  bool isPlaying(const ChannelHandle &handle);
85  bool isPaused(const ChannelHandle &handle);
86 
91  // '---
92 
93  // .--- Playing sounds
105  SoundType type, bool loop = false);
106 
118  SoundType type, bool disposeAfterUse = true);
119  // '---
120 
121  // .--- Starting/Pausing/Stopping channels
123  void startChannel(ChannelHandle &handle);
124 
126  void pauseChannel(ChannelHandle &handle, bool pause);
128  void pauseChannel(ChannelHandle &handle);
129 
131  void stopChannel(ChannelHandle &handle);
132  // '---
133 
134  // .--- Pausing/Stopping all channels
136  void pauseAll(bool pause);
137 
139  void stopAll();
140  // '---
141 
142 
143  // .--- Listener properties
145  void setListenerGain(float gain);
147  void setListenerPosition(float x, float y, float z);
149  void setListenerOrientation(float dirX, float dirY, float dirZ, float upX, float upY, float upZ);
150  // '---
151 
152  // .--- Channel properties
154  void setChannelPosition(const ChannelHandle &handle, float x, float y, float z);
155 
157  void getChannelPosition(const ChannelHandle &handle, float &x, float &y, float &z);
158 
160  void setChannelGain(const ChannelHandle &handle, float gain);
161 
163  void setChannelPitch(const ChannelHandle &handle, float pitch);
164 
166  void setChannelRelative(const ChannelHandle &handle, bool relative);
167 
169  void setChannelDistance(const ChannelHandle &handle, float minDistance, float maxDistance);
170  // '---
171 
172  // .--- Type properties
174  void setTypeGain(SoundType type, float gain);
175  // '---
176 
177  // .--- Utility methods
184 
186  Common::UString formatChannel(const ChannelHandle &handle) const;
187  // '---
188 
189 private:
190  static const size_t kChannelCount = 65535;
191 
192  struct Channel;
193  typedef std::list<Channel *> TypeList;
194 
196  struct Type {
197  float gain;
199  };
200 
202  struct Channel {
204  size_t index;
205 
206  ALint state;
207 
209 
210  ALuint source;
211 
212  std::list<ALuint> buffers;
213  std::list<ALuint> freeBuffers;
214 
215  std::map<ALuint, ALsizei> bufferSize;
216 
218  TypeList::iterator typeIt;
219 
222 
223  float gain;
224 
225  Channel(uint32 i, size_t idx, SoundType t, const TypeList::iterator &ti, AudioStream *s, bool d);
226  };
227 
228  bool _ready;
229 
230  bool _hasSound;
231 
233  ALenum _format51;
234 
237 
239 
241 
244 
245  ALCdevice *_dev;
246  ALCcontext *_ctx;
247 
249  void checkReady();
250 
252  void update();
253 
256 
258  void bufferData(Channel &channel);
260  void bufferData(size_t channel);
261 
263  bool isPlaying(size_t channel) const;
264 
266  void pauseChannel(Channel *channel, bool pause);
268  void pauseChannel(Channel *channe);
269 
271  void freeChannel(ChannelHandle &handle);
273  void freeChannel(size_t channel);
274 
276  const Channel *getChannel(const ChannelHandle &handle) const;
278  Channel *getChannel(const ChannelHandle &handle);
279 
280  void threadMethod();
281 
283  bool fillBuffer(const Channel &channel, ALuint alBuffer,
284  AudioStream *stream, ALsizei &bufferedSize) const;
285 
287  Common::UString formatChannel(const Channel *channel) const;
288 };
289 
290 } // End of namespace Sound
291 
293 #define SoundMan Sound::SoundManager::instance()
294 
295 #endif // SOUND_SOUND_H
TypeList::iterator typeIt
Iterator into the type list.
Definition: sound.h:218
bool _ready
Was the sound subsystem successfully initialized?
Definition: sound.h:228
void getChannelPosition(const ChannelHandle &handle, float &x, float &y, float &z)
Get the position of the channel.
Definition: sound.cpp:551
static AudioStream * makeAudioStream(Common::SeekableReadStream *stream)
Create an audio stream from this data stream.
Definition: sound.cpp:244
void setTypeGain(SoundType type, float gain)
Set the gain/volume of all channels of a specific type.
Definition: sound.cpp:645
Class and macro for implementing singletons.
float gain
The sound type&#39;s current gain.
Definition: sound.h:197
void deinit()
Deinitialize the sound subsystem.
Definition: sound.cpp:136
Definition: 2dafile.h:39
A class holding an UTF-8 string.
Definition: ustring.h:48
void init()
Initialize the sound subsystem.
Definition: sound.cpp:81
void setChannelRelative(const ChannelHandle &handle, bool relative)
Set if the channel is relative.
Definition: sound.cpp:590
ALint state
The sound&#39;s state.
Definition: sound.h:206
TypeList list
The list of channels for that type.
Definition: sound.h:198
uint64_t uint64
Definition: types.h:206
bool isValidChannel(const ChannelHandle &handle) const
Does this channel handle point to an existing channel?
Definition: sound.cpp:164
Common::ScopedPtr< Channel > _channels[kChannelCount]
The sound channels.
Definition: sound.h:235
void startChannel(ChannelHandle &handle)
Start the channel.
Definition: sound.cpp:450
void stopAll()
Stop all channels.
Definition: sound.cpp:503
Basic sound types.
void setChannelPitch(const ChannelHandle &handle, float pitch)
Set the pitch of the channel.
Definition: sound.cpp:579
void setListenerOrientation(float dirX, float dirY, float dirZ, float upX, float upY, float upZ)
Set the orientation of the listener.
Definition: sound.cpp:527
std::list< Channel * > TypeList
Definition: sound.h:192
A sound channel.
Definition: sound.h:202
A simple scoped smart pointer template.
A condition.
Definition: mutex.h:83
std::list< ALuint > buffers
List of buffers for that channel.
Definition: sound.h:212
Channel(uint32 i, size_t idx, SoundType t, const TypeList::iterator &ti, AudioStream *s, bool d)
Definition: sound.cpp:67
Definition: game.h:37
Threading helpers.
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
A mutex.
Definition: mutex.h:40
float gain
The channel&#39;s gain.
Definition: sound.h:223
A class that creates its own thread.
Definition: thread.h:44
bool isPaused(const ChannelHandle &handle)
Is that channel currently paused?
Definition: sound.cpp:232
ALuint source
OpenAL source for this channel.
Definition: sound.h:210
uint64 getChannelDurationPlayed(const ChannelHandle &handle)
Return the time this channel has already played in milliseconds.
Definition: sound.cpp:635
Common::Condition _needUpdate
Condition to signal that an update is needed.
Definition: sound.h:243
void checkReady()
Check that the SoundManager was properly initialized.
Definition: sound.cpp:780
void bufferData(Channel &channel)
Buffer more sound from the channel to the OpenAL buffers.
Definition: sound.cpp:729
ALCdevice * _dev
Definition: sound.h:245
Common::UString formatChannel(const ChannelHandle &handle) const
Return a string representing the channel referenced by this handle.
Definition: sound.cpp:915
void setChannelGain(const ChannelHandle &handle, float gain)
Set the gain/volume of the channel.
Definition: sound.cpp:566
Common::Mutex _mutex
Definition: sound.h:240
void update()
Update the sound information.
Definition: sound.cpp:785
Low-level type definitions to handle fixed width types portably.
void setChannelPosition(const ChannelHandle &handle, float x, float y, float z)
Set the position the channel is being played.
Definition: sound.cpp:536
void setListenerGain(float gain)
Set the gain of the listener (= the global master volume).
Definition: sound.cpp:510
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
SoundType type
The channel&#39;s sound type.
Definition: sound.h:217
std::list< ALuint > freeBuffers
List of free buffers not filled with data.
Definition: sound.h:213
A disposable plain pointer, allowing pointer-y access and normal deletion.
static const size_t kChannelCount
Maximal number of channels.
Definition: sound.h:190
Type _types[kSoundTypeMAX]
The sound types.
Definition: sound.h:236
std::map< ALuint, ALsizei > bufferSize
Size of a buffer in bytes.
Definition: sound.h:215
uint32 _curID
The ID the next sound will get.
Definition: sound.h:238
bool _hasSound
Do we have working sound output?
Definition: sound.h:230
const Channel * getChannel(const ChannelHandle &handle) const
Return the channel the handle refers to.
Definition: sound.cpp:424
void stopChannel(ChannelHandle &handle)
Stop and free the channel.
Definition: sound.cpp:487
Unicode string handling.
A sound type.
Definition: sound.h:196
Thread mutex classes.
Generic audio input stream.
Definition: audiostream.h:70
void setChannelDistance(const ChannelHandle &handle, float minDistance, float maxDistance)
Set the min and max distance for the channel.
Definition: sound.cpp:601
ChannelHandle playAudioStream(AudioStream *audStream, SoundType type, bool disposeAfterUse=true)
Play an audio stream.
Definition: sound.cpp:330
A smart pointer with a deletion flag.
uint32 id
The channel&#39;s ID.
Definition: sound.h:203
bool _hasMultiChannel
Do we have the multi-channel extension?
Definition: sound.h:232
A handle to a sound channel.
Definition: types.h:35
SoundType
The type of a sound.
Definition: types.h:43
uint32_t uint32
Definition: types.h:204
ALCcontext * _ctx
Definition: sound.h:246
void pauseChannel(ChannelHandle &handle, bool pause)
Pause the channel.
Definition: sound.cpp:474
void setListenerPosition(float x, float y, float z)
Set the position of the listener.
Definition: sound.cpp:519
void pauseAll(bool pause)
Pause all channels.
Definition: sound.cpp:496
size_t index
The channel&#39;s index.
Definition: sound.h:204
Common::DisposablePtr< AudioStream > stream
The actual audio stream.
Definition: sound.h:208
void triggerUpdate()
Signal that one of streams currently being played has changed and should be updated immediately...
Definition: sound.cpp:158
bool fillBuffer(const Channel &channel, ALuint alBuffer, AudioStream *stream, ALsizei &bufferedSize) const
Fill the buffer with data from the audio stream.
Definition: sound.cpp:662
bool ready() const
Was the sound subsystem successfully initialized?
Definition: sound.cpp:154
uint64 finishedBuffers
Number of bytes in all buffers that finished playing and were unqueued.
Definition: sound.h:221
ChannelHandle playSoundFile(Common::SeekableReadStream *wavStream, SoundType type, bool loop=false)
Play a sound file.
Definition: sound.cpp:402
uint64 getChannelSamplesPlayed(const ChannelHandle &handle)
Return the number of samples this channel has already played.
Definition: sound.cpp:614
void freeChannel(ChannelHandle &handle)
Stop and free a channel.
Definition: sound.cpp:860
bool isPlaying(const ChannelHandle &handle)
Is that channel currently playing a sound?
Definition: sound.cpp:174
Interface for a seekable & readable data stream.
Definition: readstream.h:265
ALenum _format51
The value for the 5.1 multi-channel format.
Definition: sound.h:233
The sound manager.
Definition: sound.h:60
void threadMethod()
Definition: sound.cpp:901
ChannelHandle newChannel()
Look for a free place in the channel vector.
Definition: sound.cpp:808