1 // soundmgr.hxx -- Sound effect management class
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
6 // C++-ified by Curtis Olson, started March 2001.
7 // Modified for the new SoundSystem by Erik Hofman, October 2009
9 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
10 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software Foundation,
24 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * Provides a sound manager class to keep track of
31 * multiple sounds and manage playing them with different effects and
35 #ifndef _SG_SOUNDMGR_OPENAL_HXX
36 #define _SG_SOUNDMGR_OPENAL_HXX 1
39 # error This library requires C++
46 #if defined(__APPLE__)
47 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
48 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
49 # include <OpenAL/al.h>
50 # include <OpenAL/alc.h>
51 # include <OpenAL/alut.h>
52 #elif defined(OPENALSDK)
62 #include <simgear/compiler.h>
63 #include <simgear/structure/subsystem_mgr.hxx>
64 #include <simgear/math/SGMathFwd.hxx>
66 #include "sample_group.hxx"
67 #include "sample_openal.hxx"
75 refUint() { refctr = 0; id = (ALuint)-1; };
76 refUint(ALuint i) { refctr = 1; id = i; };
80 typedef std::map < string, refUint > buffer_map;
81 typedef buffer_map::iterator buffer_map_iterator;
82 typedef buffer_map::const_iterator const_buffer_map_iterator;
84 typedef std::map < string, SGSharedPtr<SGSampleGroup> > sample_group_map;
85 typedef sample_group_map::iterator sample_group_map_iterator;
86 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
89 * Manage a collection of SGSampleGroup instances
91 class SGSoundMgr : public SGSubsystem
101 void update(double dt) {};
102 void update_late(double dt);
108 inline void reinit() { stop(); init(); }
111 * Test is the sound manager is in a working condition.
112 * @return true is the sound manager is working
114 inline bool is_working() const { return _working; }
117 * Set the sound manager to a working condition.
122 * Test is the sound manager is in an active and working condition.
123 * @return true is the sound manager is active
125 inline bool is_active() const { return _active; }
128 * Register a sample group to the sound manager.
129 * @para sgrp Pointer to a sample group to add
130 * @param refname Reference name of the sample group
131 * @return true if successful, false otherwise
133 bool add( SGSampleGroup *sgrp, const string& refname );
136 * Remove a sample group from the sound manager.
137 * @param refname Reference name of the sample group to remove
138 * @return true if successful, false otherwise
140 bool remove( const string& refname );
143 * Test if a specified sample group is registered at the sound manager
144 * @param refname Reference name of the sample group test for
145 * @return true if the specified sample group exists
147 bool exists( const string& refname );
150 * Find a specified sample group in the sound manager
151 * @param refname Reference name of the sample group to find
152 * @return A pointer to the SGSampleGroup
154 SGSampleGroup *find( const string& refname, bool create = false );
157 * Set the position of the sound manager.
158 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
159 * @param pos OpenAL listener position
161 void set_position( const SGVec3d& pos ) {
167 * Get the position of the sound manager.
168 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
169 * @return OpenAL listener position
171 SGVec3f get_position() { return toVec3f(_position); }
174 * Set the velocity vector of the sound manager
175 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
176 * @param vel Velocity vector of the OpenAL listener
178 void set_velocity( SGVec3d& dir ) {
184 * Get the velocity vector of the sound manager
185 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
186 * @return Velocity vector of the OpenAL listener
188 inline SGVec3f get_velocity() { return toVec3f(_velocity); }
191 * Set the orientation of the sound manager
192 * @param ori Quaternation containing the orientation information
194 void set_orientation( SGQuatd ori );
197 * Get the orientation of the sound manager
198 * @return Quaternation containing the orientation information
200 inline const SGQuatd& get_orientation() { return _orientation; }
203 * Get the direction vector of the sound manager
204 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
205 * @return Look-at direction of the OpenAL listener
207 SGVec3f get_direction() {
208 return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
212 NO_SOURCE = (unsigned int)-1,
213 NO_BUFFER = (unsigned int)-1
217 * Set the master volume.
218 * @param vol Volume (must be between 0.0 and 1.0)
220 void set_volume( float vol );
223 * Get the master volume.
224 * @return Volume (must be between 0.0 and 1.0)
226 inline float get_volume() { return _volume; }
229 * Get a free OpenAL source-id
230 * @return NO_SOURCE if no source is available
232 unsigned int request_source();
235 * Free an OpenAL source-id for future use
236 * @param source OpenAL source-id to free
238 void release_source( unsigned int source );
241 * Get a free OpenAL buffer-id
242 * The buffer-id will be asigned to the sample by calling this function.
243 * @param sample Pointer to an audio sample to assign the buffer-id to
244 * @return NO_BUFFER if loading of the buffer failed.
246 unsigned int request_buffer(SGSoundSample *sample);
249 * Free an OpenAL buffer-id for this sample
250 * @param sample Pointer to an audio sample for which to free the buffer
252 void release_buffer( SGSoundSample *sample );
255 * Test if the position of the sound manager has changed.
256 * The value will be set to false upon the next call to update_late()
257 * @return true if the position has changed
259 inline bool has_changed() { return _changed; }
262 * Load a sample file and return it's configuration and data.
263 * @param samplepath Path to the file to load
264 * @param data Pointer to a variable that points to the allocated data
265 * @param format Pointer to a vairable that gets the OpenAL format
266 * @param size Pointer to a vairable that gets the sample size in bytes
267 * @param freq Pointer to a vairable that gets the sample frequency in Herz
268 * @return true if succesful, false on error
270 bool load(string &samplepath, void **data, int *format,
271 size_t *size, int *freq );
274 static int _alut_init;
282 ALCcontext *_context;
284 // Position of the listener.
287 // Velocity of the listener.
290 // Orientation of the listener.
291 // first 3 elements are "at" vector, second 3 are "up" vector
292 SGQuatd _orientation;
293 ALfloat _at_up_vec[6];
295 sample_group_map _sample_groups;
298 vector<ALuint> _free_sources;
299 vector<ALuint> _sources_in_use;
303 bool testForALError(string s);
304 bool testForALCError(string s);
305 bool testForALUTError(string s);
306 bool testForError(void *p, string s);
308 void update_sample_config( SGSampleGroup *sound );
312 #endif // _SG_SOUNDMGR_OPENAL_HXX