]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.hxx
Pass current-dir down through XMLSound
[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(const char *devname = NULL);
87     void bind();
88     void unbind();
89     void update(double dt);
90     
91     void suspend();
92     void resume();
93     void stop();
94
95     inline void reinit() { stop(); init(); }
96
97     /**
98      * Test is the sound manager is in a working condition.
99      * @return true is the sound manager is working
100      */
101     inline bool is_working() const { return _working; }
102
103     /**
104      * Set the sound manager to a  working condition.
105      */
106     void activate();
107
108     /**
109      * Test is the sound manager is in an active and working condition.
110      * @return true is the sound manager is active
111      */
112     inline bool is_active() const { return _active; }
113
114     /**
115      * Register a sample group to the sound manager.
116      * @para sgrp Pointer to a sample group to add
117      * @param refname Reference name of the sample group
118      * @return true if successful, false otherwise
119      */
120     bool add( SGSampleGroup *sgrp, const std::string& refname );
121
122     /** 
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
126      */
127     bool remove( const std::string& refname );
128
129     /**
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
133      */
134     bool exists( const std::string& refname );
135
136     /**
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
140      */
141     SGSampleGroup *find( const string& refname, bool create = false );
142
143     /**
144      * Set the Cartesian position of the sound manager.
145      * @param pos OpenAL listener position
146      */
147     void set_position( const SGVec3d& pos, const SGGeod& pos_geod ) {
148         _base_pos = pos; _geod_pos = pos_geod; _changed = true;
149     }
150
151     void set_position_offset( const SGVec3d& pos ) {
152         _offset_pos = pos; _changed = true;
153     }
154
155     /**
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
159      */
160     SGVec3d& get_position() { return _absolute_pos; }
161
162     /**
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
166      */
167     void set_velocity( const SGVec3d& vel ) {
168         _velocity = vel; _changed = true;
169     }
170
171     /**
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
175      */
176     inline SGVec3f get_velocity() { return toVec3f(_velocity); }
177
178     /**
179      * Set the orientation of the sound manager
180      * @param ori Quaternation containing the orientation information
181      */
182     void set_orientation( const SGQuatd& ori ) {
183         _orientation = ori; _changed = true;
184     }
185
186     /**
187      * Get the orientation of the sound manager
188      * @return Quaternation containing the orientation information
189      */
190     inline const SGQuatd& get_orientation() { return _orientation; }
191
192     /**
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
196      */
197     SGVec3f get_direction() {
198         return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]);
199     }
200
201     enum {
202         NO_SOURCE = (unsigned int)-1,
203         NO_BUFFER = (unsigned int)-1
204     };
205
206     /**
207      * Set the master volume.
208      * @param vol Volume (must be between 0.0 and 1.0)
209      */
210     void set_volume( float vol );
211
212     /**
213      * Get the master volume.
214      * @return Volume (must be between 0.0 and 1.0)
215      */
216     inline float get_volume() { return _volume; }
217
218     /**
219      * Get a free OpenAL source-id
220      * @return NO_SOURCE if no source is available
221      */
222     unsigned int request_source();
223
224     /**
225      * Free an OpenAL source-id for future use
226      * @param source OpenAL source-id to free
227      */
228     void release_source( unsigned int source );
229
230     /**
231      * Get a free OpenAL buffer-id
232      * The buffer-id will be asigned to the sample by calling this function.
233      * @param sample Pointer to an audio sample to assign the buffer-id to
234      * @return NO_BUFFER if loading of the buffer failed.
235      */
236     unsigned int request_buffer(SGSoundSample *sample);
237
238     /**
239      * Free an OpenAL buffer-id for this sample
240      * @param sample Pointer to an audio sample for which to free the buffer
241      */
242     void release_buffer( SGSoundSample *sample );
243
244     /**
245      * Test if the position of the sound manager has changed.
246      * The value will be set to false upon the next call to update_late()
247      * @return true if the position has changed
248      */
249     inline bool has_changed() { return _changed; }
250
251     /**
252      * Some implementations seem to need the velocity miltyplied by a
253      * factor of 100 to make them distinct. I've not found if this is
254      * a problem in the implementation or in out code. Until then
255      * this function is used to detect the problematic implementations.
256      */
257     inline bool bad_doppler_effect() { return _bad_doppler; }
258
259     /**
260      * Load a sample file and return it's configuration and data.
261      * @param samplepath Path to the file to load
262      * @param data Pointer to a variable that points to the allocated data
263      * @param format Pointer to a vairable that gets the OpenAL format
264      * @param size Pointer to a vairable that gets the sample size in bytes
265      * @param freq Pointer to a vairable that gets the sample frequency in Herz
266      * @return true if succesful, false on error
267      */
268     bool load(string &samplepath, void **data, int *format,
269                                          size_t *size, int *freq );
270
271     /**
272      * Get a list of available playback devices.
273      */
274     std::vector<const char*> get_available_devices();
275
276     /**
277      * Get the current OpenAL vendor or rendering backend.
278      */
279     const std::string& get_vendor() { return _vendor; }
280     const std::string& get_renderer() { return _renderer; }
281
282 private:
283     static int _alut_init;
284
285     bool _working;
286     bool _active;
287     bool _changed;
288     float _volume;
289
290     ALCdevice *_device;
291     ALCcontext *_context;
292
293     // Position of the listener.
294     SGVec3d _absolute_pos;
295     SGVec3d _offset_pos;
296     SGVec3d _base_pos;
297     SGGeod _geod_pos;
298
299     // Velocity of the listener.
300     SGVec3d _velocity;
301
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];
306
307     sample_group_map _sample_groups;
308     buffer_map _buffers;
309
310     std::vector<ALuint> _free_sources;
311     std::vector<ALuint> _sources_in_use;
312
313     bool _bad_doppler;
314     std::string _renderer;
315     std::string _vendor;
316
317     bool testForALError(std::string s);
318     bool testForALCError(std::string s);
319     bool testForALUTError(std::string s);
320     bool testForError(void *p, std::string s);
321
322     void update_pos_and_orientation();
323     void update_sample_config( SGSampleGroup *sound );
324 };
325
326
327 #endif // _SG_SOUNDMGR_OPENAL_HXX
328
329