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
42 #if defined(__APPLE__)
43 # include <OpenAL/al.h>
44 # include <OpenAL/alc.h>
45 #elif defined(OPENALSDK)
53 #include <simgear/compiler.h>
54 #include <simgear/structure/subsystem_mgr.hxx>
55 #include <simgear/math/SGMathFwd.hxx>
57 #include "sample_group.hxx"
63 refUint() { refctr = 0; id = (ALuint)-1; };
64 refUint(ALuint i) { refctr = 1; id = i; };
68 typedef std::map < std::string, refUint > buffer_map;
69 typedef buffer_map::iterator buffer_map_iterator;
70 typedef buffer_map::const_iterator const_buffer_map_iterator;
72 typedef std::map < std::string, SGSharedPtr<SGSampleGroup> > sample_group_map;
73 typedef sample_group_map::iterator sample_group_map_iterator;
74 typedef sample_group_map::const_iterator const_sample_group_map_iterator;
77 * Manage a collection of SGSampleGroup instances
79 class SGSoundMgr : public SGSubsystem
87 void update(double dt);
96 * Select a specific sound device.
97 * Requires a init/reinit call before sound is actually switched.
99 inline void select_device(const char* devname) {_device_name = devname;}
102 * Test is the sound manager is in a working condition.
103 * @return true is the sound manager is working
105 inline bool is_working() const { return (_device != NULL); }
108 * Set the sound manager to a working condition.
113 * Test is the sound manager is in an active and working condition.
114 * @return true is the sound manager is active
116 inline bool is_active() const { return _active; }
119 * Register a sample group to the sound manager.
120 * @param sgrp Pointer to a sample group to add
121 * @param refname Reference name of the sample group
122 * @return true if successful, false otherwise
124 bool add( SGSampleGroup *sgrp, const std::string& refname );
127 * Remove a sample group from the sound manager.
128 * @param refname Reference name of the sample group to remove
129 * @return true if successful, false otherwise
131 bool remove( const std::string& refname );
134 * Test if a specified sample group is registered at the sound manager
135 * @param refname Reference name of the sample group test for
136 * @return true if the specified sample group exists
138 bool exists( const std::string& refname );
141 * Find a specified sample group in the sound manager
142 * @param refname Reference name of the sample group to find
143 * @return A pointer to the SGSampleGroup
145 SGSampleGroup *find( const string& refname, bool create = false );
148 * Set the Cartesian position of the sound manager.
149 * @param pos OpenAL listener position
151 void set_position( const SGVec3d& pos, const SGGeod& pos_geod ) {
152 _base_pos = pos; _geod_pos = pos_geod; _changed = true;
155 void set_position_offset( const SGVec3d& pos ) {
156 _offset_pos = pos; _changed = true;
160 * Get the position of the sound manager.
161 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
162 * @return OpenAL listener position
164 SGVec3d& get_position() { return _absolute_pos; }
167 * Set the velocity vector (in meters per second) of the sound manager
168 * This is the horizontal local frame; x=north, y=east, z=down
169 * @param Velocity vector
171 void set_velocity( const SGVec3d& vel ) {
172 _velocity = vel; _changed = true;
176 * Get the velocity vector of the sound manager
177 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
178 * @return Velocity vector of the OpenAL listener
180 inline SGVec3f get_velocity() { return toVec3f(_velocity); }
183 * Set the orientation of the sound manager
184 * @param ori Quaternation containing the orientation information
186 void set_orientation( const SGQuatd& ori ) {
187 _orientation = ori; _changed = true;
191 * Get the orientation of the sound manager
192 * @return Quaternation containing the orientation information
194 inline const SGQuatd& get_orientation() { return _orientation; }
197 * Get the direction vector of the sound manager
198 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
199 * @return Look-at direction of the OpenAL listener
201 SGVec3f get_direction() {
202 return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
206 NO_SOURCE = (unsigned int)-1,
207 NO_BUFFER = (unsigned int)-1,
208 FAILED_BUFFER = (unsigned int)-2
212 * Set the master volume.
213 * @param vol Volume (must be between 0.0 and 1.0)
215 void set_volume( float vol );
218 * Get the master volume.
219 * @return Volume (must be between 0.0 and 1.0)
221 inline float get_volume() { return _volume; }
224 * Get a free OpenAL source-id
225 * @return NO_SOURCE if no source is available
227 unsigned int request_source();
230 * Free an OpenAL source-id for future use
231 * @param source OpenAL source-id to free
233 void release_source( unsigned int source );
236 * Get a free OpenAL buffer-id
237 * The buffer-id will be asigned to the sample by calling this function.
238 * @param sample Pointer to an audio sample to assign the buffer-id to
239 * @return NO_BUFFER if loading of the buffer failed.
241 unsigned int request_buffer(SGSoundSample *sample);
244 * Free an OpenAL buffer-id for this sample
245 * @param sample Pointer to an audio sample for which to free the buffer
247 void release_buffer( SGSoundSample *sample );
250 * Test if the position of the sound manager has changed.
251 * The value will be set to false upon the next call to update_late()
252 * @return true if the position has changed
254 inline bool has_changed() { return _changed; }
257 * Some implementations seem to need the velocity multiplied by a
258 * factor of 100 to make them distinct. I've not found if this is
259 * a problem in the implementation or in out code. Until then
260 * this function is used to detect the problematic implementations.
262 inline bool bad_doppler_effect() { return _bad_doppler; }
265 * Load a sample file and return it's configuration and data.
266 * @param samplepath Path to the file to load
267 * @param data Pointer to a variable that points to the allocated data
268 * @param format Pointer to a vairable that gets the OpenAL format
269 * @param size Pointer to a vairable that gets the sample size in bytes
270 * @param freq Pointer to a vairable that gets the sample frequency in Herz
271 * @return true if succesful, false on error
273 bool load(const string &samplepath, void **data, int *format,
274 size_t *size, int *freq );
277 * Get a list of available playback devices.
279 std::vector<const char*> get_available_devices();
282 * Get the current OpenAL vendor or rendering backend.
284 const std::string& get_vendor() { return _vendor; }
285 const std::string& get_renderer() { return _renderer; }
294 ALCcontext *_context;
296 // Position of the listener.
297 SGVec3d _absolute_pos;
302 // Velocity of the listener.
305 // Orientation of the listener.
306 // first 3 elements are "at" vector, second 3 are "up" vector
307 SGQuatd _orientation;
308 ALfloat _at_up_vec[6];
310 sample_group_map _sample_groups;
313 std::vector<ALuint> _free_sources;
314 std::vector<ALuint> _sources_in_use;
317 std::string _renderer;
319 std::string _device_name;
321 bool testForALError(std::string s);
322 bool testForALCError(std::string s);
323 bool testForError(void *p, std::string s);
325 void update_pos_and_orientation();
326 void update_sample_config( SGSampleGroup *sound );
330 #endif // _SG_SOUNDMGR_OPENAL_HXX