]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
84d93baafa2933fd44cfd59c6c90d1a42b8177cd
[simgear.git] / simgear / sound / soundmgr_openal.hxx
1 // soundmgr.hxx -- Sound effect management class
2 //
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
5 //
6 // C++-ified by Curtis Olson, started March 2001.
7 // Modified for the new SoundSystem by Erik Hofman, October 2009
8 //
9 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
10 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
11 //
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.
16 //
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.
21 //
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.
25 //
26 // $Id$
27
28 /**
29  * \file soundmgr.hxx
30  * Provides a sound manager class to keep track of
31  * multiple sounds and manage playing them with different effects and
32  * timings.
33  */
34
35 #ifndef _SG_SOUNDMGR_OPENAL_HXX
36 #define _SG_SOUNDMGR_OPENAL_HXX 1
37
38 #include <string>
39 #include <vector>
40 #include <map>
41
42 #if defined(__APPLE__)
43 # include <OpenAL/al.h>
44 # include <OpenAL/alc.h>
45 #elif defined(OPENALSDK)
46 # include <al.h>
47 # include <alc.h>
48 #else
49 # include <AL/al.h>
50 # include <AL/alc.h>
51 #endif
52
53 #include <simgear/compiler.h>
54 #include <simgear/structure/subsystem_mgr.hxx>
55 #include <simgear/math/SGMathFwd.hxx>
56
57 #include "sample_group.hxx"
58
59 struct refUint {
60     unsigned int refctr;
61     ALuint id;
62
63     refUint() { refctr = 0; id = (ALuint)-1; };
64     refUint(ALuint i) { refctr = 1; id = i; };
65     ~refUint() {};
66 };
67
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;
71
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;
75
76 /**
77  * Manage a collection of SGSampleGroup instances
78  */
79 class SGSoundMgr : public SGSubsystem
80 {
81 public:
82
83     SGSoundMgr();
84     ~SGSoundMgr();
85
86     void init();
87     void update(double dt);
88     
89     void suspend();
90     void resume();
91     void stop();
92
93     void reinit();
94
95     /**
96      * Select a specific sound device.
97      * Requires a init/reinit call before sound is actually switched.
98      */
99     inline void select_device(const char* devname) {_device_name = devname;}
100
101     /**
102      * Test is the sound manager is in a working condition.
103      * @return true is the sound manager is working
104      */
105     inline bool is_working() const { return (_device != NULL); }
106
107     /**
108      * Set the sound manager to a  working condition.
109      */
110     void activate();
111
112     /**
113      * Test is the sound manager is in an active and working condition.
114      * @return true is the sound manager is active
115      */
116     inline bool is_active() const { return _active; }
117
118     /**
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
123      */
124     bool add( SGSampleGroup *sgrp, const std::string& refname );
125
126     /** 
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
130      */
131     bool remove( const std::string& refname );
132
133     /**
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
137      */
138     bool exists( const std::string& refname );
139
140     /**
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
144      */
145     SGSampleGroup *find( const string& refname, bool create = false );
146
147     /**
148      * Set the Cartesian position of the sound manager.
149      * @param pos OpenAL listener position
150      */
151     void set_position( const SGVec3d& pos, const SGGeod& pos_geod ) {
152         _base_pos = pos; _geod_pos = pos_geod; _changed = true;
153     }
154
155     void set_position_offset( const SGVec3d& pos ) {
156         _offset_pos = pos; _changed = true;
157     }
158
159     /**
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
163      */
164     SGVec3d& get_position() { return _absolute_pos; }
165
166     /**
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
170      */
171     void set_velocity( const SGVec3d& vel ) {
172         _velocity = vel; _changed = true;
173     }
174
175     /**
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
179      */
180     inline SGVec3f get_velocity() { return toVec3f(_velocity); }
181
182     /**
183      * Set the orientation of the sound manager
184      * @param ori Quaternation containing the orientation information
185      */
186     void set_orientation( const SGQuatd& ori ) {
187         _orientation = ori; _changed = true;
188     }
189
190     /**
191      * Get the orientation of the sound manager
192      * @return Quaternation containing the orientation information
193      */
194     inline const SGQuatd& get_orientation() { return _orientation; }
195
196     /**
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
200      */
201     SGVec3f get_direction() {
202         return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
203     }
204
205     enum {
206         NO_SOURCE = (unsigned int)-1,
207         NO_BUFFER = (unsigned int)-1,
208         FAILED_BUFFER = (unsigned int)-2
209     };
210
211     /**
212      * Set the master volume.
213      * @param vol Volume (must be between 0.0 and 1.0)
214      */
215     void set_volume( float vol );
216
217     /**
218      * Get the master volume.
219      * @return Volume (must be between 0.0 and 1.0)
220      */
221     inline float get_volume() { return _volume; }
222
223     /**
224      * Get a free OpenAL source-id
225      * @return NO_SOURCE if no source is available
226      */
227     unsigned int request_source();
228
229     /**
230      * Free an OpenAL source-id for future use
231      * @param source OpenAL source-id to free
232      */
233     void release_source( unsigned int source );
234
235     /**
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.
240      */
241     unsigned int request_buffer(SGSoundSample *sample);
242
243     /**
244      * Free an OpenAL buffer-id for this sample
245      * @param sample Pointer to an audio sample for which to free the buffer
246      */
247     void release_buffer( SGSoundSample *sample );
248
249     /**
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
253      */
254     inline bool has_changed() { return _changed; }
255
256     /**
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.
261      */
262     inline bool bad_doppler_effect() { return _bad_doppler; }
263
264     /**
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
272      */
273     bool load(const string &samplepath, void **data, int *format,
274                                          size_t *size, int *freq );
275
276     /**
277      * Get a list of available playback devices.
278      */
279     std::vector<const char*> get_available_devices();
280
281     /**
282      * Get the current OpenAL vendor or rendering backend.
283      */
284     const std::string& get_vendor() { return _vendor; }
285     const std::string& get_renderer() { return _renderer; }
286
287 private:
288
289     bool _active;
290     bool _changed;
291     float _volume;
292
293     ALCdevice *_device;
294     ALCcontext *_context;
295
296     // Position of the listener.
297     SGVec3d _absolute_pos;
298     SGVec3d _offset_pos;
299     SGVec3d _base_pos;
300     SGGeod _geod_pos;
301
302     // Velocity of the listener.
303     SGVec3d _velocity;
304
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];
309
310     sample_group_map _sample_groups;
311     buffer_map _buffers;
312
313     std::vector<ALuint> _free_sources;
314     std::vector<ALuint> _sources_in_use;
315
316     bool _bad_doppler;
317     std::string _renderer;
318     std::string _vendor;
319     std::string _device_name;
320
321     bool testForALError(std::string s);
322     bool testForALCError(std::string s);
323     bool testForError(void *p, std::string s);
324
325     void update_pos_and_orientation();
326     void update_sample_config( SGSampleGroup *sound );
327 };
328
329
330 #endif // _SG_SOUNDMGR_OPENAL_HXX
331
332