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
86 void init(const char *devname = NULL);
89 void update(double dt);
95 inline void reinit() { stop(); init(); }
98 * Test is the sound manager is in a working condition.
99 * @return true is the sound manager is working
101 inline bool is_working() const { return _working; }
104 * Set the sound manager to a working condition.
109 * Test is the sound manager is in an active and working condition.
110 * @return true is the sound manager is active
112 inline bool is_active() const { return _active; }
115 * Register a sample group to the sound manager.
116 * @param sgrp Pointer to a sample group to add
117 * @param refname Reference name of the sample group
118 * @return true if successful, false otherwise
120 bool add( SGSampleGroup *sgrp, const std::string& refname );
123 * Remove a sample group from the sound manager.
124 * @param refname Reference name of the sample group to remove
125 * @return true if successful, false otherwise
127 bool remove( const std::string& refname );
130 * Test if a specified sample group is registered at the sound manager
131 * @param refname Reference name of the sample group test for
132 * @return true if the specified sample group exists
134 bool exists( const std::string& refname );
137 * Find a specified sample group in the sound manager
138 * @param refname Reference name of the sample group to find
139 * @return A pointer to the SGSampleGroup
141 SGSampleGroup *find( const string& refname, bool create = false );
144 * Set the Cartesian position of the sound manager.
145 * @param pos OpenAL listener position
147 void set_position( const SGVec3d& pos, const SGGeod& pos_geod ) {
148 _base_pos = pos; _geod_pos = pos_geod; _changed = true;
151 void set_position_offset( const SGVec3d& pos ) {
152 _offset_pos = pos; _changed = true;
156 * Get the position of the sound manager.
157 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right
158 * @return OpenAL listener position
160 SGVec3d& get_position() { return _absolute_pos; }
163 * Set the velocity vector (in meters per second) of the sound manager
164 * This is the horizontal local frame; x=north, y=east, z=down
165 * @param Velocity vector
167 void set_velocity( const SGVec3d& vel ) {
168 _velocity = vel; _changed = true;
172 * Get the velocity vector of the sound manager
173 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
174 * @return Velocity vector of the OpenAL listener
176 inline SGVec3f get_velocity() { return toVec3f(_velocity); }
179 * Set the orientation of the sound manager
180 * @param ori Quaternation containing the orientation information
182 void set_orientation( const SGQuatd& ori ) {
183 _orientation = ori; _changed = true;
187 * Get the orientation of the sound manager
188 * @return Quaternation containing the orientation information
190 inline const SGQuatd& get_orientation() { return _orientation; }
193 * Get the direction vector of the sound manager
194 * This is in the same coordinate system as OpenGL; y=up, z=back, x=right.
195 * @return Look-at direction of the OpenAL listener
197 SGVec3f get_direction() {
198 return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
202 NO_SOURCE = (unsigned int)-1,
203 NO_BUFFER = (unsigned int)-1,
204 FAILED_BUFFER = (unsigned int)-2
208 * Set the master volume.
209 * @param vol Volume (must be between 0.0 and 1.0)
211 void set_volume( float vol );
214 * Get the master volume.
215 * @return Volume (must be between 0.0 and 1.0)
217 inline float get_volume() { return _volume; }
220 * Get a free OpenAL source-id
221 * @return NO_SOURCE if no source is available
223 unsigned int request_source();
226 * Free an OpenAL source-id for future use
227 * @param source OpenAL source-id to free
229 void release_source( unsigned int source );
232 * Get a free OpenAL buffer-id
233 * The buffer-id will be asigned to the sample by calling this function.
234 * @param sample Pointer to an audio sample to assign the buffer-id to
235 * @return NO_BUFFER if loading of the buffer failed.
237 unsigned int request_buffer(SGSoundSample *sample);
240 * Free an OpenAL buffer-id for this sample
241 * @param sample Pointer to an audio sample for which to free the buffer
243 void release_buffer( SGSoundSample *sample );
246 * Test if the position of the sound manager has changed.
247 * The value will be set to false upon the next call to update_late()
248 * @return true if the position has changed
250 inline bool has_changed() { return _changed; }
253 * Some implementations seem to need the velocity multiplied by a
254 * factor of 100 to make them distinct. I've not found if this is
255 * a problem in the implementation or in out code. Until then
256 * this function is used to detect the problematic implementations.
258 inline bool bad_doppler_effect() { return _bad_doppler; }
261 * Load a sample file and return it's configuration and data.
262 * @param samplepath Path to the file to load
263 * @param data Pointer to a variable that points to the allocated data
264 * @param format Pointer to a vairable that gets the OpenAL format
265 * @param size Pointer to a vairable that gets the sample size in bytes
266 * @param freq Pointer to a vairable that gets the sample frequency in Herz
267 * @return true if succesful, false on error
269 bool load(const string &samplepath, void **data, int *format,
270 size_t *size, int *freq );
273 * Get a list of available playback devices.
275 std::vector<const char*> get_available_devices();
278 * Get the current OpenAL vendor or rendering backend.
280 const std::string& get_vendor() { return _vendor; }
281 const std::string& get_renderer() { return _renderer; }
291 ALCcontext *_context;
293 // Position of the listener.
294 SGVec3d _absolute_pos;
299 // Velocity of the listener.
302 // Orientation of the listener.
303 // first 3 elements are "at" vector, second 3 are "up" vector
304 SGQuatd _orientation;
305 ALfloat _at_up_vec[6];
307 sample_group_map _sample_groups;
310 std::vector<ALuint> _free_sources;
311 std::vector<ALuint> _sources_in_use;
314 std::string _renderer;
317 bool testForALError(std::string s);
318 bool testForALCError(std::string s);
319 bool testForError(void *p, std::string s);
321 void update_pos_and_orientation();
322 void update_sample_config( SGSampleGroup *sound );
326 #endif // _SG_SOUNDMGR_OPENAL_HXX